Open History to see the last commit
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("builds a video preview for a local video attachment", () => {
46 const file = new File([new Uint8Array([1, 2, 3])], "demo.mp4", { type: "video/mp4" });
47 const attachment: ComposerFileAttachment = {
48 type: "file",
49 id: "video-1",
50 name: file.name,
51 mimeType: file.type,
52 sizeBytes: file.size,
53 file,
54 };
55
56 const preview = buildExpandedImagePreview([attachment], attachment.id);
57
58 expect(preview).toMatchObject({
59 images: [{ name: "demo.mp4", type: "video" }],
60 index: 0,
61 });
62 expect(preview?.images[0]?.src).toMatch(/^blob:/);
63 URL.revokeObjectURL(preview?.images[0]?.src ?? "");
64 });
65
66 it("releases a video thumbnail URL when detached", async () => {
67 const video = { src: "" } as HTMLVideoElement;
68 const file = new File([new Uint8Array([1, 2, 3])], "demo.mp4", { type: "video/mp4" });
69
70 const detach = attachVideoThumbnail(video, file);
71 const url = video.src;
72
73 expect((await fetch(url)).ok).toBe(true);
74 detach();
75 await expect(fetch(url)).rejects.toThrow();
76 });
77});