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

ExpandedImagePreview.test.ts

103 lines3.0 KB

1import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2​
3import type { ComposerFileAttachment } from "../../composerDraftStore";
4import {
5 attachVideoThumbnail,
6 buildExpandedImagePreview,
7 resolveMarkdownMediaPreview,
8} from "./ExpandedImagePreview";
9​
10describe("resolveMarkdownMediaPreview", () => {
11 afterEach(() => {
12 vi.unstubAllGlobals();
13 });
14​
15 it.each([
16 ["t3code:", "https:"],
17 ["t3code-dev:", "https:"],
18 ["http:", "http:"],
19 ["https:", "https:"],
20 ])(
21 "resolves protocol-relative media on %s without changing its source",
22 async (pageProtocol, mediaProtocol) => {
23 vi.stubGlobal("window", { location: { protocol: pageProtocol } });
24 const source = "//cdn.example.com/recording.mp4?token=a%2FB&v=1#t=2";
25 const preview = await resolveMarkdownMediaPreview({
26 source,
27 createAssetUrl: async () => {
28 throw new Error("Remote media must not request a local asset URL.");
29 },
30 });
31​
32 expect(preview?.images[0]).toMatchObject({
33 src: `${mediaProtocol}${source}`,
34 originalUrl: source,
35 actionsSource: {
36 src: `${mediaProtocol}${source}`,
37 reference: { kind: "url", url: source },
38 },
39 });
40 },
41 );
42});
43​
44describe("buildExpandedImagePreview", () => {
45 it("keeps window capture details with the expanded image", () => {
46 const source = {
47 kind: "snap-shot" as const,
48 capturedAt: "2026-09-01T00:00:00.000Z",
49 appName: "Editor",
50 windowTitle: "main.ts",
51 accessibleText: "const answer = 42;",
52 };
53 const preview = buildExpandedImagePreview(
54 [
55 {
56 type: "image",
57 id: "capture-1",
58 name: "window.png",
59 mimeType: "image/png",
60 sizeBytes: 3,
61 previewUrl: "data:image/png;base64,AQID",
62 source,
63 },
64 ],
65 "capture-1",
66 );
67​
68 expect(preview?.images[0]?.source).toEqual(source);
69 });
70​
71 it("builds a video preview for a local video attachment", () => {
72 const file = new File([new Uint8Array([1, 2, 3])], "demo.mp4", { type: "video/mp4" });
73 const attachment: ComposerFileAttachment = {
74 type: "file",
75 id: "video-1",
76 name: file.name,
77 mimeType: file.type,
78 sizeBytes: file.size,
79 file,
80 };
81​
82 const preview = buildExpandedImagePreview([attachment], attachment.id);
83​
84 expect(preview).toMatchObject({
85 images: [{ name: "demo.mp4", type: "video" }],
86 index: 0,
87 });
88 expect(preview?.images[0]?.src).toMatch(/^blob:/);
89 URL.revokeObjectURL(preview?.images[0]?.src ?? "");
90 });
91​
92 it("releases a video thumbnail URL when detached", async () => {
93 const video = { src: "" } as HTMLVideoElement;
94 const file = new File([new Uint8Array([1, 2, 3])], "demo.mp4", { type: "video/mp4" });
95​
96 const detach = attachVideoThumbnail(video, file);
97 const url = video.src;
98​
99 expect((await fetch(url)).ok).toBe(true);
100 detach();
101 await expect(fetch(url)).rejects.toThrow();
102 });
103});