Open History to see the last commit
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.each(["notes.constructor", "notes.__proto__"])(
108 "keeps %s with no MIME type on the file path",
109 (name) => {
110 const file = new File(["plain text"], name, { type: "" });
111 expect(classifyComposerAttachmentFile(file)).toBe("file");
112 expect(normalizeComposerImageFileMimeType(file)).toBe(file);
113 },
114 );
115
116 it("preserves text paste when an application adds a synthetic generic file", () => {
117 const file = new File(["clipboard"], "clipboard.rtf", { type: "application/rtf" });
118
119 expect(
120 shouldHandleComposerAttachmentPaste({
121 files: [file],
122 plainText: "Copied text",
123 }),
124 ).toBe(false);
125 });
126
127 it("attaches a pasted PDF when its filename is the only text", () => {
128 const file = new File(["%PDF-1.4"], "report.pdf", { type: "application/pdf" });
129 const input = { files: [file], plainText: file.name };
130
131 expect(shouldHandleComposerAttachmentPaste(input)).toBe(true);
132 expect(shouldHandleComposerAttachmentPaste({ ...input, bypassAutoAttachment: true })).toBe(
133 false,
134 );
135 expect(shouldHandleComposerAttachmentPaste({ ...input, plainText: "\r\n" })).toBe(false);
136 });
137
138 it("claims SVG pastes even when text is present", () => {
139 const image = new File(["svg"], "diagram.svg", { type: "image/svg+xml" });
140 expect(
141 shouldHandleComposerAttachmentPaste({ files: [image], plainText: "Image caption" }),
142 ).toBe(true);
143 });
144
145 it("attaches images with only blank clipboard lines unless pasting as text", () => {
146 const image = new File(["png"], "image.png", { type: "image/png" });
147 const input = { files: [image], plainText: "\r\n\n" };
148
149 expect(shouldHandleComposerAttachmentPaste(input)).toBe(true);
150 expect(shouldHandleComposerAttachmentPaste({ ...input, bypassAutoAttachment: true })).toBe(
151 false,
152 );
153 });
154
155 it("leaves a Word image-and-text clipboard to the text path, with or without Paste as Text", () => {
156 const image = new File(["png"], "image.png", { type: "image/png" });
157 const input = { files: [image], plainText: "Hello from Word", bypassAutoAttachment: true };
158 expect(shouldHandleComposerAttachmentPaste(input)).toBe(false);
159 // A plain paste of the same Word clipboard keeps the text too.
160 expect(shouldHandleComposerAttachmentPaste({ ...input, bypassAutoAttachment: false })).toBe(
161 false,
162 );
163 // Firefox's Copy Image adds the image URL as text and only an <img> as HTML; the image attaches.
164 expect(
165 shouldHandleComposerAttachmentPaste({
166 files: [image],
167 plainText: "https://example.com/image.png",
168 htmlIsOnlyImage: true,
169 }),
170 ).toBe(true);
171 // Finder adds a copied file's name as text.
172 expect(shouldHandleComposerAttachmentPaste({ files: [image], plainText: "image.png" })).toBe(
173 true,
174 );
175 // A QQ or WeChat message has its text beside an <img> in the HTML: the image attaches too.
176 const message = { files: [image], plainText: "Look at this", htmlHasImageAndText: true };
177 expect(shouldHandleComposerAttachmentPaste(message)).toBe(false);
178 expect(imagesToAttachBesidePastedText(message)).toEqual([image]);
179 expect(imagesToAttachBesidePastedText({ ...message, htmlHasImageAndText: false })).toEqual([]);
180 expect(imagesToAttachBesidePastedText({ ...message, bypassAutoAttachment: true })).toEqual([]);
181 });
182
183 it("preserves text when the clipboard also includes generic image files", () => {
184 const image = new File(["tiff"], "photo.tiff", { type: "image/tiff" });
185 expect(
186 shouldHandleComposerAttachmentPaste({ files: [image], plainText: "Image caption" }),
187 ).toBe(false);
188 });
189
190 it("uses the hard local limit while server config is unknown", () => {
191 expect(
192 fileAttachmentStagingLimit({
193 attachmentUploadsCapabilityKnown: false,
194 supportsAttachmentUploads: false,
195 maxFileAttachmentBytes: null,
196 }),
197 ).toBe(PROVIDER_SEND_TURN_MAX_FILE_BYTES);
198 expect(
199 fileAttachmentCapabilityBlockReason({
200 files: [{ name: "pending.zip", sizeBytes: PROVIDER_SEND_TURN_MAX_FILE_BYTES }],
201 attachmentUploadsCapabilityKnown: false,
202 supportsAttachmentUploads: false,
203 maxFileAttachmentBytes: null,
204 }),
205 ).toBe("Waiting for the server before file attachments can send");
206 });
207
208 it("keeps draft-persisted file uploads when the upload capability flips off", () => {
209 const environmentId = EnvironmentId.make("environment-1");
210 const image: ComposerImageAttachment = {
211 type: "image",
212 id: "image-1",
213 name: "photo.png",
214 mimeType: "image/png",
215 sizeBytes: 3,
216 previewUrl: "blob:photo",
217 file: new File([new Uint8Array([1, 2, 3])], "photo.png", { type: "image/png" }),
218 };
219 const uploadingFile: ComposerFileAttachment = {
220 type: "file",
221 id: "file-uploading",
222 name: "fresh.pdf",
223 mimeType: "application/pdf",
224 sizeBytes: 3,
225 file: new File([new Uint8Array([1, 2, 3])], "fresh.pdf", { type: "application/pdf" }),
226 };
227 const hydratedFile: ComposerFileAttachment = {
228 type: "file",
229 id: "file-hydrated",
230 name: "report.pdf",
231 mimeType: "application/pdf",
232 sizeBytes: 3,
233 file: null,
234 uploadedAttachmentId: "pending-report-pdf",
235 uploadEnvironmentId: environmentId,
236 };
237 const uploadedLocalFile: ComposerFileAttachment = {
238 ...uploadingFile,
239 id: "file-uploaded-local",
240 uploadedAttachmentId: "pending-fresh-pdf",
241 uploadEnvironmentId: environmentId,
242 };
243
244 const released = attachmentsToReleaseOnUploadCapabilityLoss([
245 image,
246 uploadingFile,
247 hydratedFile,
248 uploadedLocalFile,
249 ]);
250
251 expect(released.map((attachment) => attachment.id)).toEqual(["image-1", "file-uploading"]);
252 });
253});