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

filePreviewGallery.test.ts

151 lines4.9 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import { ComposerContextId, EnvironmentId, ThreadId } from "@t3tools/contracts";
3import type { FilePreviewItem } from "../components/ui/FilePreviewModal.types";
4import type { DraftComposerAttachment } from "./composerImages";
5import { composerImageGallery } from "./composerImageGallery";
6import { contextImageGallery } from "./contextImageGallery";
7import { messageImageGallery } from "../features/threads/messageImageGallery";
8import { filePreviewGallery, filePreviewKey } from "./filePreviewGallery";
9​
10vi.mock("./uuid", () => ({}));
11​
12describe("image preview groups", () => {
13 it.each([false, true])(
14 "keeps one entry per image through upload and delivery (file: %s)",
15 (fileBacked) => {
16 const environmentId = EnvironmentId.make("test");
17 const context = { environmentId, threadId: ThreadId.make("thread"), workspaceRoot: null };
18 const attachments: DraftComposerAttachment[] = ["a", "b"].map((id) => ({
19 id,
20 type: "image",
21 name: `${id}.png`,
22 mimeType: "image/png",
23 sizeBytes: 3,
24 previewUri: `data:image/png;base64,${id}`,
25 ...(fileBacked ? { fileUri: `file:///documents/${id}.png` } : {}),
26 }));
27 const delivered = (id: string) =>
28 messageImageGallery(
29 {
30 text: "",
31 attachments: [
32 {
33 type: "image",
34 id: `uploaded-${id}`,
35 name: `${id}.png`,
36 mimeType: "image/png",
37 sizeBytes: 3,
38 },
39 ],
40 },
41 context,
42 )[0]!;
43 const local = composerImageGallery(attachments, environmentId);
44 const uploading = composerImageGallery(
45 [{ ...attachments[0]!, uploadedAttachmentId: "uploaded-a" }, attachments[1]!],
46 environmentId,
47 );
48 const uploaded = composerImageGallery(
49 attachments.map((attachment) => ({
50 ...attachment,
51 uploadedAttachmentId: `uploaded-${attachment.id}`,
52 })),
53 environmentId,
54 );
55 for (const [selected, gallery] of [
56 [local[0]!, local],
57 [delivered("a"), uploading],
58 [uploading[1]!, uploading],
59 [delivered("a"), uploaded],
60 [delivered("a"), [...uploaded, delivered("a"), delivered("b")]],
61 [delivered("a"), [delivered("a"), delivered("b")]],
62 ] as const) {
63 const result = filePreviewGallery(selected, gallery);
64 expect(result).toHaveLength(2);
65 expect(result.map(filePreviewKey)).toEqual([...new Set(gallery.map(filePreviewKey))]);
66 }
67 expect(composerImageGallery(attachments)).toEqual(local);
68 },
69 );
70​
71 it("gives every remote image page its own media actions", () => {
72 const environmentId = EnvironmentId.make("test");
73 const ids = ["a", "b"];
74 const galleries = [
75 messageImageGallery(
76 {
77 text: "",
78 attachments: ids.map((id) => ({
79 type: "image" as const,
80 id: `uploaded-${id}`,
81 name: `${id}.png`,
82 mimeType: "image/png",
83 sizeBytes: 3,
84 })),
85 },
86 { environmentId, threadId: ThreadId.make("thread"), workspaceRoot: null },
87 ),
88 composerImageGallery(
89 ids.map((id) => ({
90 id,
91 type: "image",
92 name: `${id}.png`,
93 mimeType: "image/png",
94 sizeBytes: 3,
95 previewUri: `data:image/png;base64,${id}`,
96 uploadedAttachmentId: `uploaded-${id}`,
97 })),
98 environmentId,
99 ),
100 contextImageGallery(
101 ids.map((id) => ({
102 version: 1,
103 kind: "image",
104 contextId: ComposerContextId.make(`context-${id}`),
105 label: `Screenshot ${id}`,
106 attachmentId: `uploaded-${id}`,
107 name: `${id}.png`,
108 mimeType: "image/png",
109 sizeBytes: 3,
110 })),
111 [],
112 environmentId,
113 ),
114 ];
115​
116 for (const gallery of galleries) {
117 expect(filePreviewGallery(gallery[0]!, gallery)[1]?.actionsSource).toEqual({
118 name: "b.png",
119 mimeType: "image/png",
120 environmentId,
121 resource: {
122 _tag: "attachment",
123 attachmentId: "uploaded-b",
124 fileName: "b.png",
125 mimeType: "image/png",
126 },
127 });
128 }
129 });
130​
131 it("matches remote originals despite metadata and property order differences", () => {
132 const environmentId = EnvironmentId.make("test");
133 const a: FilePreviewItem = {
134 kind: "image",
135 environmentId,
136 resource: { _tag: "attachment", attachmentId: "a", fileName: "old.png" },
137 };
138 const b: FilePreviewItem = {
139 kind: "image",
140 environmentId,
141 resource: {
142 attachmentId: "a",
143 _tag: "attachment",
144 fileName: "new.png",
145 mimeType: "image/png",
146 },
147 };
148 expect(filePreviewKey(a)).toBe(filePreviewKey(b));
149 expect(filePreviewGallery(b, [a])).toEqual([b]);
150 });
151});