Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 filePreviewKind,
5 decodeFilePreviewText,
6 FILE_TEXT_PREVIEW_MAX_BYTES,
7 isWorkspacePreviewEntryPath,
8 mediaKindFromPath,
9} from "./filePreview.ts";
10
11describe("workspace file previews", () => {
12 // regression: #10311 previews classify by the literal filename, not URL-like suffixes
13 it.each(["image.png.ts", "image.png#notes.txt", "image%2Epng"])(
14 "rejects non-preview path %s",
15 (path) => {
16 expect(isWorkspacePreviewEntryPath(path)).toBe(false);
17 },
18 );
19});
20
21describe("media path parsing", () => {
22 // regression: #9023 media kind comes from the decoded pathname, ignoring query and hash
23 it.each([
24 ["https://cdn.example/clip.webm?download=1#t=2", "video"],
25 ["https://example.com/download?name=recording.mp4", null],
26 ["images%2Fresult%2Epng", "image"],
27 ["images/result.png%23secret.txt", null],
28 ])("classifies the decoded pathname of %s", (source, kind) => {
29 expect(mediaKindFromPath(source)).toBe(kind);
30 });
31});
32
33describe("attachment preview classification", () => {
34 it.each([
35 ["example.json", "application/octet-stream", "text"],
36 ["payload", "application/problem+json", "text"],
37 ["misleading.json", "application/pdf", "pdf"],
38 ["misleading.pdf", "application/zip", "unsupported"],
39 ])("classifies %s (%s) as %s", (name, mimeType, expected) => {
40 expect(filePreviewKind({ name, mimeType })).toBe(expected);
41 });
42 it("rejects binary and invalid UTF-8 data", () => {
43 expect(() => decodeFilePreviewText(new Uint8Array([65, 0, 66]))).toThrow("binary");
44 expect(() => decodeFilePreviewText(new Uint8Array([255]))).toThrow("UTF-8");
45 });
46 it("does not corrupt a multi-byte character at the preview boundary", () => {
47 const bytes = new Uint8Array(FILE_TEXT_PREVIEW_MAX_BYTES + 1).fill(97);
48 bytes[FILE_TEXT_PREVIEW_MAX_BYTES - 1] = 0xe2;
49 bytes[FILE_TEXT_PREVIEW_MAX_BYTES] = 0x82;
50 const preview = decodeFilePreviewText(bytes);
51 expect(preview.truncated).toBe(true);
52 expect(preview.text.endsWith("�")).toBe(false);
53 expect(preview.text.length).toBe(FILE_TEXT_PREVIEW_MAX_BYTES - 1);
54 });
55});