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 ["/tmp/tone.MP3", "audio"],
29 ["./tone.wav", "audio"],
30 ["tone.ogg", "audio"],
31 ["tone.m4a", "audio"],
32 ["tone.flac", "audio"],
33 ["tone.aac", "audio"],
34 ["tone.opus", "audio"],
35 ["tone.webm-audio", "audio"],
36 ["tone.webm", "video"],
37 ["https://example.com/tone%2Emp3?download=1", "audio"],
38 ["data.csv", null],
39 ["report.pdf", null],
40 ["report.docx", null],
41 ["archive.zip", null],
42 ])("classifies the decoded pathname of %s", (source, kind) => {
43 expect(mediaKindFromPath(source)).toBe(kind);
44 });
45});
46
47describe("attachment preview classification", () => {
48 it.each([
49 ["example.json", "application/octet-stream", "text"],
50 ["report.pdf", "application/pdf", "pdf"],
51 ["recording.mp3", "", "audio"],
52 ["recording.webm-audio", "", "audio"],
53 ["recording.webm", "audio/webm", "audio"],
54 ["payload", "application/problem+json", "text"],
55 ["misleading.json", "application/pdf", "pdf"],
56 ["misleading.pdf", "application/zip", "unsupported"],
57 ])("classifies %s (%s) as %s", (name, mimeType, expected) => {
58 expect(filePreviewKind({ name, mimeType })).toBe(expected);
59 });
60 it("rejects binary and invalid UTF-8 data", () => {
61 expect(() => decodeFilePreviewText(new Uint8Array([65, 0, 66]))).toThrow("binary");
62 expect(() => decodeFilePreviewText(new Uint8Array([255]))).toThrow("UTF-8");
63 });
64 it("does not corrupt a multi-byte character at the preview boundary", () => {
65 const bytes = new Uint8Array(FILE_TEXT_PREVIEW_MAX_BYTES + 1).fill(97);
66 bytes[FILE_TEXT_PREVIEW_MAX_BYTES - 1] = 0xe2;
67 bytes[FILE_TEXT_PREVIEW_MAX_BYTES] = 0x82;
68 const preview = decodeFilePreviewText(bytes);
69 expect(preview.truncated).toBe(true);
70 expect(preview.text.endsWith("�")).toBe(false);
71 expect(preview.text.length).toBe(FILE_TEXT_PREVIEW_MAX_BYTES - 1);
72 });
73});