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

ExpandedImagePreview.test.ts

148 lines4.3 KB

1import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2import { EnvironmentId } from "@t3tools/contracts";
3​
4import type { ComposerFileAttachment } from "../../composerDraftStore";
5import {
6 wrapExpandedImageIndex,
7 attachVideoThumbnail,
8 buildAttachmentVideoPreview,
9 buildExpandedImagePreview,
10 resolveMarkdownMediaPreview,
11} from "./ExpandedImagePreview";
12​
13describe("resolveMarkdownMediaPreview", () => {
14 afterEach(() => {
15 vi.unstubAllGlobals();
16 });
17​
18 it.each([
19 ["t3code:", "https:"],
20 ["t3code-dev:", "https:"],
21 ["http:", "http:"],
22 ["https:", "https:"],
23 ])(
24 "resolves protocol-relative media on %s without changing its source",
25 async (pageProtocol, mediaProtocol) => {
26 vi.stubGlobal("window", { location: { protocol: pageProtocol } });
27 const source = "//cdn.example.com/recording.mp4?token=a%2FB&v=1#t=2";
28 const preview = await resolveMarkdownMediaPreview({
29 source,
30 createAssetUrl: async () => {
31 throw new Error("Remote media must not request a local asset URL.");
32 },
33 });
34​
35 expect(preview?.images[0]).toMatchObject({
36 src: `${mediaProtocol}${source}`,
37 originalUrl: source,
38 actionsSource: {
39 src: `${mediaProtocol}${source}`,
40 reference: { kind: "url", url: source },
41 },
42 });
43 },
44 );
45});
46​
47describe("buildExpandedImagePreview", () => {
48 it("keeps MIME type for stored images without a filename extension", () => {
49 const preview = buildExpandedImagePreview(
50 [
51 {
52 type: "image",
53 id: "heic-1",
54 name: "photo",
55 mimeType: "image/heic",
56 sizeBytes: 3,
57 previewUrl: "blob:heic",
58 },
59 ],
60 "heic-1",
61 );
62 expect(preview?.images[0]).toMatchObject({
63 name: "photo",
64 mimeType: "image/heic",
65 src: "blob:heic",
66 });
67 });
68​
69 it("builds a signed-asset preview for a persisted video attachment", () => {
70 const preview = buildAttachmentVideoPreview(EnvironmentId.make("environment-1"), {
71 type: "file",
72 id: "attachment-video-1",
73 name: "demo.mp4",
74 mimeType: "video/mp4",
75 sizeBytes: 42,
76 });
77​
78 expect(preview).toEqual({
79 images: [
80 {
81 src: null,
82 name: "demo.mp4",
83 type: "video",
84 actionsSource: {
85 kind: "video",
86 name: "demo.mp4",
87 src: null,
88 asset: {
89 environmentId: "environment-1",
90 resource: {
91 _tag: "attachment",
92 attachmentId: "attachment-video-1",
93 fileName: "demo.mp4",
94 mimeType: "video/mp4",
95 },
96 },
97 },
98 },
99 ],
100 index: 0,
101 });
102 });
103​
104 it("builds a video preview for a local video attachment", () => {
105 const file = new File([new Uint8Array([1, 2, 3])], "demo.mp4", { type: "video/mp4" });
106 const attachment: ComposerFileAttachment = {
107 type: "file",
108 id: "video-1",
109 name: file.name,
110 mimeType: file.type,
111 sizeBytes: file.size,
112 file,
113 };
114​
115 const preview = buildExpandedImagePreview([attachment], attachment.id);
116​
117 expect(preview).toMatchObject({
118 images: [{ name: "demo.mp4", type: "video" }],
119 index: 0,
120 });
121 expect(preview?.images[0]?.src).toMatch(/^blob:/);
122 URL.revokeObjectURL(preview?.images[0]?.src ?? "");
123 });
124​
125 it("releases a video thumbnail URL when detached", async () => {
126 const video = { src: "" } as HTMLVideoElement;
127 const file = new File([new Uint8Array([1, 2, 3])], "demo.mp4", { type: "video/mp4" });
128​
129 const detach = attachVideoThumbnail(video, file);
130 const url = video.src;
131​
132 expect((await fetch(url)).ok).toBe(true);
133 detach();
134 await expect(fetch(url)).rejects.toThrow();
135 });
136});
137​
138it("keeps backward media navigation visible beyond a complete cycle", () => {
139 const images = ["first", "second"];
140 expect(
141 Array.from({ length: 7 }, (_, step) => images[wrapExpandedImageIndex(-step, images.length)]),
142 ).toEqual(["first", "second", "first", "second", "first", "second", "first"]);
143 let index = 0;
144 for (let step = 1; step <= 7; step++) {
145 index = wrapExpandedImageIndex(index - 1, images.length);
146 expect(images[index]).toBe(step % 2 === 1 ? "second" : "first");
147 }
148});