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

composerAttachmentFiles.test.ts

194 lines6.6 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 normalizeComposerImageFileMimeType,
17 shouldHandleComposerAttachmentPaste,
18} from "./composerAttachmentFiles";
19​
20describe("composer attachment files", () => {
21 it("attaches SVG as a file and rejects other unsupported images", () => {
22 expect(classifyComposerAttachmentFile({ name: "diagram.svg", type: "image/svg+xml" })).toBe(
23 "file",
24 );
25 expect(classifyComposerAttachmentFile({ name: "diagram.SVG", type: "" })).toBe("file");
26 expect(
27 classifyComposerAttachmentFile({ name: "diagram.svg", type: "application/octet-stream" }),
28 ).toBe("file");
29 expect(classifyComposerAttachmentFile({ name: "photo.tiff", type: "image/tiff" })).toBe(
30 "unsupported-image",
31 );
32 expect(classifyComposerAttachmentFile({ name: "report.pdf", type: "application/pdf" })).toBe(
33 "file",
34 );
35 });
36​
37 it("keeps DNG originals on the file path without changing their bytes", async () => {
38 const bytes = new Uint8Array([0x49, 0x49, 0x2a, 0x00, 0x08, 0, 0, 0, 0xfe, 0xff, 0x00, 0x80]);
39 const files = [
40 new File([bytes], "photo.DNG", { type: "image/tiff" }),
41 new File([bytes], "photo.dng", { type: "image/png" }),
42 new File([bytes], "photo.dng", { type: "image/jpeg" }),
43 new File([bytes], "clipboard", { type: "image/dng" }),
44 new File([bytes], "clipboard", { type: "image/x-dng" }),
45 new File([bytes], "clipboard", { type: "image/x-adobe-dng" }),
46 ];
47​
48 const attachments = await Promise.all(
49 files.map(async (file) => ({
50 kind: classifyComposerAttachmentFile(file),
51 bytes: new Uint8Array(await normalizeComposerImageFileMimeType(file).arrayBuffer()),
52 })),
53 );
54​
55 expect(attachments).toEqual(files.map(() => ({ kind: "file", bytes })));
56 });
57​
58 it("keeps misleading DNG MIME labels out of downstream image detection and budgets", () => {
59 for (const type of ["image/png", "image/jpeg", "image/tiff", "image/dng", ""]) {
60 const attachment = {
61 type: "file" as const,
62 id: "raw",
63 name: "photo.DNG",
64 mimeType: composerFileAttachmentMimeType({ name: "photo.DNG", type }),
65 sizeBytes: 81 * 1024 * 1024,
66 };
67​
68 expect(isImageAttachment(attachment)).toBe(false);
69 expect(isFileAttachment(attachment)).toBe(true);
70 expect(getProviderAttachmentLimitError([attachment])).toBeUndefined();
71 }
72 });
73​
74 it("claims a captioned DNG paste even when its MIME is generic", () => {
75 const file = new File(["raw"], "photo.dng", { type: "application/octet-stream" });
76​
77 expect(
78 shouldHandleComposerAttachmentPaste({ files: [file], plainText: "Inspect this original" }),
79 ).toBe(true);
80 });
81​
82 it("keeps ordinary images on their existing paths", () => {
83 const files = [
84 { name: "photo.jpg", type: "image/jpeg" },
85 { name: "export.dng.png", type: "image/png" },
86 { name: "photo.webp", type: "image/webp" },
87 { name: "photo.gif", type: "image/gif" },
88 { name: "photo.heic", type: "" },
89 { name: "scan.tif", type: "image/tiff" },
90 { name: "photo.cr2", type: "image/x-canon-cr2" },
91 ];
92​
93 expect(files.map(classifyComposerAttachmentFile)).toEqual([
94 "image",
95 "image",
96 "image",
97 "image",
98 "image",
99 "unsupported-image",
100 "unsupported-image",
101 ]);
102 });
103​
104 it("preserves text paste when an application adds a synthetic generic file", () => {
105 const file = new File(["clipboard"], "clipboard.rtf", { type: "application/rtf" });
106​
107 expect(
108 shouldHandleComposerAttachmentPaste({
109 files: [file],
110 plainText: "Copied text",
111 }),
112 ).toBe(false);
113 });
114​
115 it("claims SVG and unsupported image pastes even when text is present", () => {
116 const images = [
117 new File(["svg"], "diagram.svg", { type: "image/svg+xml" }),
118 new File(["tiff"], "photo.tiff", { type: "image/tiff" }),
119 ];
120​
121 for (const image of images) {
122 expect(
123 shouldHandleComposerAttachmentPaste({
124 files: [image],
125 plainText: "Image caption",
126 }),
127 ).toBe(true);
128 }
129 });
130​
131 it("uses the hard local limit while server config is unknown", () => {
132 expect(
133 fileAttachmentStagingLimit({
134 attachmentUploadsCapabilityKnown: false,
135 supportsAttachmentUploads: false,
136 maxFileAttachmentBytes: null,
137 }),
138 ).toBe(PROVIDER_SEND_TURN_MAX_FILE_BYTES);
139 expect(
140 fileAttachmentCapabilityBlockReason({
141 files: [{ name: "pending.zip", sizeBytes: PROVIDER_SEND_TURN_MAX_FILE_BYTES }],
142 attachmentUploadsCapabilityKnown: false,
143 supportsAttachmentUploads: false,
144 maxFileAttachmentBytes: null,
145 }),
146 ).toBe("Waiting for the server before file attachments can send");
147 });
148​
149 it("keeps draft-persisted file uploads when the upload capability flips off", () => {
150 const environmentId = EnvironmentId.make("environment-1");
151 const image: ComposerImageAttachment = {
152 type: "image",
153 id: "image-1",
154 name: "photo.png",
155 mimeType: "image/png",
156 sizeBytes: 3,
157 previewUrl: "blob:photo",
158 file: new File([new Uint8Array([1, 2, 3])], "photo.png", { type: "image/png" }),
159 };
160 const uploadingFile: ComposerFileAttachment = {
161 type: "file",
162 id: "file-uploading",
163 name: "fresh.pdf",
164 mimeType: "application/pdf",
165 sizeBytes: 3,
166 file: new File([new Uint8Array([1, 2, 3])], "fresh.pdf", { type: "application/pdf" }),
167 };
168 const hydratedFile: ComposerFileAttachment = {
169 type: "file",
170 id: "file-hydrated",
171 name: "report.pdf",
172 mimeType: "application/pdf",
173 sizeBytes: 3,
174 file: null,
175 uploadedAttachmentId: "pending-report-pdf",
176 uploadEnvironmentId: environmentId,
177 };
178 const uploadedLocalFile: ComposerFileAttachment = {
179 ...uploadingFile,
180 id: "file-uploaded-local",
181 uploadedAttachmentId: "pending-fresh-pdf",
182 uploadEnvironmentId: environmentId,
183 };
184​
185 const released = attachmentsToReleaseOnUploadCapabilityLoss([
186 image,
187 uploadingFile,
188 hydratedFile,
189 uploadedLocalFile,
190 ]);
191​
192 expect(released.map((attachment) => attachment.id)).toEqual(["image-1", "file-uploading"]);
193 });
194});