Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

filePreview.test.ts

106 lines3.9 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 filePreviewKind,
5 decodeFilePreviewText,
6 FILE_TEXT_PREVIEW_MAX_BYTES,
7 hostPreviewMimeTypeFromExtension,
8 isWorkspaceAudioPreviewPath,
9 isWorkspaceBrowserPreviewPath,
10 isWorkspaceImagePreviewPath,
11 isWorkspacePreviewEntryPath,
12 isWorkspaceVideoPreviewPath,
13 mediaKindFromPath,
14} from "./filePreview.ts";
15​
16describe("workspace file previews", () => {
17 it.each(["report.html", "report.HTM", "document.pdf?download=1"])(
18 "recognizes browser preview path %s",
19 (path) => {
20 expect(isWorkspaceBrowserPreviewPath(path)).toBe(true);
21 expect(isWorkspacePreviewEntryPath(path)).toBe(true);
22 },
23 );
24​
25 it.each([
26 "icon.png",
27 "photo.JPEG",
28 "animation.gif",
29 "vector.svg#mark",
30 "texture.webp",
31 "image.avif",
32 ])("recognizes image preview path %s", (path) => {
33 expect(isWorkspaceImagePreviewPath(path)).toBe(true);
34 expect(isWorkspacePreviewEntryPath(path)).toBe(true);
35 });
36​
37 it.each(["README.md", "src/index.ts", "image.png.ts", "png"])(
38 "rejects non-preview path %s",
39 (path) => {
40 expect(isWorkspacePreviewEntryPath(path)).toBe(false);
41 },
42 );
43​
44 it("serves audio in place from the host like video and browser documents", () => {
45 expect(isWorkspaceAudioPreviewPath("notes/recording.WAV")).toBe(true);
46 expect(isWorkspaceAudioPreviewPath("recording.wav.ts")).toBe(false);
47 expect(hostPreviewMimeTypeFromExtension(".m4a")).toBe("audio/mp4");
48 expect(hostPreviewMimeTypeFromExtension(".mp4")).toBe("video/mp4");
49 expect(hostPreviewMimeTypeFromExtension(".txt")).toBeNull();
50 });
51});
52​
53describe("media path parsing", () => {
54 it.each([
55 ["https://cdn.example/clip.webm?download=1#t=2", "video"],
56 ["https://example.com/download?name=recording.mp4", null],
57 ["https://example.png", null],
58 ["images%2Fresult%2Epng", "image"],
59 ["images/result%23v2.png", "image"],
60 ["images/result.png%23secret.txt", null],
61 ["images/result.png%3Fsecret.txt", null],
62 ["/tmp/100%.png", "image"],
63 ])("classifies the decoded pathname of %s", (source, kind) => {
64 expect(mediaKindFromPath(source)).toBe(kind);
65 });
66​
67 it.each([
68 ["recording.mp4#t=2", "video", false],
69 ["recording%2Emp4", "video", false],
70 ["recording#take2.mp4", null, true],
71 ["recording?take2.mp4", null, true],
72 ])("distinguishes authored URLs from literal filenames in %s", (source, kind, literalVideo) => {
73 expect(mediaKindFromPath(source)).toBe(kind);
74 expect(isWorkspaceVideoPreviewPath(source)).toBe(literalVideo);
75 });
76});
77​
78describe("attachment preview classification", () => {
79 it.each([
80 ["example.json", "application/octet-stream", "text"],
81 ["README.md", "text/plain", "markdown"],
82 ["component.tsx", "", "text"],
83 ["report.pdf", "application/pdf", "pdf"],
84 ["page.HTML", "", "html"],
85 ["recording.mp3", "", "audio"],
86 ["payload", "application/problem+json", "text"],
87 ["archive.zip", "application/zip", "unsupported"],
88 ["misleading.json", "application/pdf", "pdf"],
89 ["misleading.pdf", "application/zip", "unsupported"],
90 ])("classifies %s (%s) as %s", (name, mimeType, expected) => {
91 expect(filePreviewKind({ name, mimeType })).toBe(expected);
92 });
93 it("rejects binary and invalid UTF-8 data", () => {
94 expect(() => decodeFilePreviewText(new Uint8Array([65, 0, 66]))).toThrow("binary");
95 expect(() => decodeFilePreviewText(new Uint8Array([255]))).toThrow("UTF-8");
96 });
97 it("does not corrupt a multi-byte character at the preview boundary", () => {
98 const bytes = new Uint8Array(FILE_TEXT_PREVIEW_MAX_BYTES + 1).fill(97);
99 bytes[FILE_TEXT_PREVIEW_MAX_BYTES - 1] = 0xe2;
100 bytes[FILE_TEXT_PREVIEW_MAX_BYTES] = 0x82;
101 const preview = decodeFilePreviewText(bytes);
102 expect(preview.truncated).toBe(true);
103 expect(preview.text.endsWith("�")).toBe(false);
104 expect(preview.text.length).toBe(FILE_TEXT_PREVIEW_MAX_BYTES - 1);
105 });
106});