The best way to control Claude Code, Codex and more from any computer or phone

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