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

composerAttachmentFiles.test.ts

223 lines8.4 KB

1import {
2 EnvironmentId,
3 getProviderAttachmentLimitError,
4 PROVIDER_SEND_TURN_MAX_FILE_BYTES,
5} from "@t3tools/contracts";
6import { describe, expect, it } from "vite-plus/test";
7​
8import { isFileAttachment, isImageAttachment } from "../../types";
9import type { ComposerFileAttachment, ComposerImageAttachment } from "../../composerDraftStore";
10import {
11 attachmentsToReleaseOnUploadCapabilityLoss,
12 classifyComposerAttachmentFile,
13 composerFileAttachmentMimeType,
14 fileAttachmentCapabilityBlockReason,
15 fileAttachmentStagingLimit,
16 imagesToAttachBesidePastedText,
17 normalizeComposerImageFileMimeType,
18 shouldHandleComposerAttachmentPaste,
19} from "./composerAttachmentFiles";
20​
21describe("composer attachment files", () => {
22 it("keeps previewable images providers cannot accept on the file path", () => {
23 expect(classifyComposerAttachmentFile({ name: "diagram.svg", type: "image/svg+xml" })).toBe(
24 "file",
25 );
26 expect(classifyComposerAttachmentFile({ name: "diagram.SVG", type: "" })).toBe("file");
27 expect(
28 classifyComposerAttachmentFile({ name: "diagram.svg", type: "application/octet-stream" }),
29 ).toBe("file");
30 expect(classifyComposerAttachmentFile({ name: "photo.tiff", type: "image/tiff" })).toBe("file");
31 expect(classifyComposerAttachmentFile({ name: "report.pdf", type: "application/pdf" })).toBe(
32 "file",
33 );
34 expect(classifyComposerAttachmentFile({ name: "picture.bmp", type: "image/bmp" })).toBe("file");
35 expect(
36 classifyComposerAttachmentFile({ name: "layers.psd", type: "image/vnd.adobe.photoshop" }),
37 ).toBe("unsupported-image");
38 });
39​
40 it("keeps DNG originals on the file path without changing their bytes", async () => {
41 const bytes = new Uint8Array([0x49, 0x49, 0x2a, 0x00, 0x08, 0, 0, 0, 0xfe, 0xff, 0x00, 0x80]);
42 const files = [
43 new File([bytes], "photo.DNG", { type: "image/tiff" }),
44 new File([bytes], "photo.dng", { type: "image/png" }),
45 new File([bytes], "photo.dng", { type: "image/jpeg" }),
46 new File([bytes], "clipboard", { type: "image/dng" }),
47 new File([bytes], "clipboard", { type: "image/x-dng" }),
48 new File([bytes], "clipboard", { type: "image/x-adobe-dng" }),
49 ];
50​
51 const attachments = await Promise.all(
52 files.map(async (file) => ({
53 kind: classifyComposerAttachmentFile(file),
54 bytes: new Uint8Array(await normalizeComposerImageFileMimeType(file).arrayBuffer()),
55 })),
56 );
57​
58 expect(attachments).toEqual(files.map(() => ({ kind: "file", bytes })));
59 });
60​
61 it("keeps misleading DNG MIME labels out of downstream image detection and budgets", () => {
62 for (const type of ["image/png", "image/jpeg", "image/tiff", "image/dng", ""]) {
63 const attachment = {
64 type: "file" as const,
65 id: "raw",
66 name: "photo.DNG",
67 mimeType: composerFileAttachmentMimeType({ name: "photo.DNG", type }),
68 sizeBytes: 81 * 1024 * 1024,
69 };
70​
71 expect(isImageAttachment(attachment)).toBe(false);
72 expect(isFileAttachment(attachment)).toBe(true);
73 expect(getProviderAttachmentLimitError([attachment])).toBeUndefined();
74 }
75 });
76​
77 it("claims a captioned DNG paste even when its MIME is generic", () => {
78 const file = new File(["raw"], "photo.dng", { type: "application/octet-stream" });
79​
80 expect(
81 shouldHandleComposerAttachmentPaste({ files: [file], plainText: "Inspect this original" }),
82 ).toBe(true);
83 });
84​
85 it("keeps ordinary images on their existing paths", () => {
86 const files = [
87 { name: "photo.jpg", type: "image/jpeg" },
88 { name: "export.dng.png", type: "image/png" },
89 { name: "photo.webp", type: "image/webp" },
90 { name: "photo.gif", type: "image/gif" },
91 { name: "photo.heic", type: "" },
92 { name: "scan.tif", type: "image/tiff" },
93 { name: "photo.cr2", type: "image/x-canon-cr2" },
94 ];
95​
96 expect(files.map(classifyComposerAttachmentFile)).toEqual([
97 "image",
98 "image",
99 "image",
100 "image",
101 "image",
102 "file",
103 "unsupported-image",
104 ]);
105 });
106​
107 it("preserves text paste when an application adds a synthetic generic file", () => {
108 const file = new File(["clipboard"], "clipboard.rtf", { type: "application/rtf" });
109​
110 expect(
111 shouldHandleComposerAttachmentPaste({
112 files: [file],
113 plainText: "Copied text",
114 }),
115 ).toBe(false);
116 });
117​
118 it("claims SVG pastes even when text is present", () => {
119 const image = new File(["svg"], "diagram.svg", { type: "image/svg+xml" });
120 expect(
121 shouldHandleComposerAttachmentPaste({ files: [image], plainText: "Image caption" }),
122 ).toBe(true);
123 });
124​
125 it("leaves a Word image-and-text clipboard to the text path, with or without Paste as Text", () => {
126 const image = new File(["png"], "image.png", { type: "image/png" });
127 const input = { files: [image], plainText: "Hello from Word", bypassAutoAttachment: true };
128 expect(shouldHandleComposerAttachmentPaste(input)).toBe(false);
129 // A plain paste of the same Word clipboard keeps the text too.
130 expect(shouldHandleComposerAttachmentPaste({ ...input, bypassAutoAttachment: false })).toBe(
131 false,
132 );
133 // Firefox's Copy Image adds the image URL as text and only an <img> as HTML; the image attaches.
134 expect(
135 shouldHandleComposerAttachmentPaste({
136 files: [image],
137 plainText: "https://example.com/image.png",
138 htmlIsOnlyImage: true,
139 }),
140 ).toBe(true);
141 // Finder adds a copied file's name as text.
142 expect(shouldHandleComposerAttachmentPaste({ files: [image], plainText: "image.png" })).toBe(
143 true,
144 );
145 // A QQ or WeChat message has its text beside an <img> in the HTML: the image attaches too.
146 const message = { files: [image], plainText: "Look at this", htmlHasImageAndText: true };
147 expect(shouldHandleComposerAttachmentPaste(message)).toBe(false);
148 expect(imagesToAttachBesidePastedText(message)).toEqual([image]);
149 expect(imagesToAttachBesidePastedText({ ...message, htmlHasImageAndText: false })).toEqual([]);
150 expect(imagesToAttachBesidePastedText({ ...message, bypassAutoAttachment: true })).toEqual([]);
151 });
152​
153 it("preserves text when the clipboard also includes generic image files", () => {
154 const image = new File(["tiff"], "photo.tiff", { type: "image/tiff" });
155 expect(
156 shouldHandleComposerAttachmentPaste({ files: [image], plainText: "Image caption" }),
157 ).toBe(false);
158 });
159​
160 it("uses the hard local limit while server config is unknown", () => {
161 expect(
162 fileAttachmentStagingLimit({
163 attachmentUploadsCapabilityKnown: false,
164 supportsAttachmentUploads: false,
165 maxFileAttachmentBytes: null,
166 }),
167 ).toBe(PROVIDER_SEND_TURN_MAX_FILE_BYTES);
168 expect(
169 fileAttachmentCapabilityBlockReason({
170 files: [{ name: "pending.zip", sizeBytes: PROVIDER_SEND_TURN_MAX_FILE_BYTES }],
171 attachmentUploadsCapabilityKnown: false,
172 supportsAttachmentUploads: false,
173 maxFileAttachmentBytes: null,
174 }),
175 ).toBe("Waiting for the server before file attachments can send");
176 });
177​
178 it("keeps draft-persisted file uploads when the upload capability flips off", () => {
179 const environmentId = EnvironmentId.make("environment-1");
180 const image: ComposerImageAttachment = {
181 type: "image",
182 id: "image-1",
183 name: "photo.png",
184 mimeType: "image/png",
185 sizeBytes: 3,
186 previewUrl: "blob:photo",
187 file: new File([new Uint8Array([1, 2, 3])], "photo.png", { type: "image/png" }),
188 };
189 const uploadingFile: ComposerFileAttachment = {
190 type: "file",
191 id: "file-uploading",
192 name: "fresh.pdf",
193 mimeType: "application/pdf",
194 sizeBytes: 3,
195 file: new File([new Uint8Array([1, 2, 3])], "fresh.pdf", { type: "application/pdf" }),
196 };
197 const hydratedFile: ComposerFileAttachment = {
198 type: "file",
199 id: "file-hydrated",
200 name: "report.pdf",
201 mimeType: "application/pdf",
202 sizeBytes: 3,
203 file: null,
204 uploadedAttachmentId: "pending-report-pdf",
205 uploadEnvironmentId: environmentId,
206 };
207 const uploadedLocalFile: ComposerFileAttachment = {
208 ...uploadingFile,
209 id: "file-uploaded-local",
210 uploadedAttachmentId: "pending-fresh-pdf",
211 uploadEnvironmentId: environmentId,
212 };
213​
214 const released = attachmentsToReleaseOnUploadCapabilityLoss([
215 image,
216 uploadingFile,
217 hydratedFile,
218 uploadedLocalFile,
219 ]);
220​
221 expect(released.map((attachment) => attachment.id)).toEqual(["image-1", "file-uploading"]);
222 });
223});