Open History to see the last commit
1import { EnvironmentId, PROVIDER_SEND_TURN_MAX_FILE_BYTES } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import type { ComposerFileAttachment, ComposerImageAttachment } from "../../composerDraftStore";
5import {
6 attachmentsToReleaseOnUploadCapabilityLoss,
7 classifyComposerAttachmentFile,
8 fileAttachmentCapabilityBlockReason,
9 fileAttachmentStagingLimit,
10 inferImageMimeTypeFromName,
11 normalizeComposerImageFileMimeType,
12 shouldHandleComposerAttachmentPaste,
13} from "./composerAttachmentFiles";
14
15describe("composer attachment files", () => {
16 it("keeps supported images and HEIC photos on the image path", () => {
17 expect(classifyComposerAttachmentFile({ name: "photo.png", type: "image/png" })).toBe("image");
18 expect(classifyComposerAttachmentFile({ name: "photo.heic", type: "" })).toBe("image");
19 });
20
21 it("rejects unsupported image types instead of attaching them as generic files", () => {
22 expect(classifyComposerAttachmentFile({ name: "diagram.svg", type: "image/svg+xml" })).toBe(
23 "unsupported-image",
24 );
25 expect(classifyComposerAttachmentFile({ name: "photo.tiff", type: "image/tiff" })).toBe(
26 "unsupported-image",
27 );
28 expect(classifyComposerAttachmentFile({ name: "report.pdf", type: "application/pdf" })).toBe(
29 "file",
30 );
31 });
32
33 it("preserves text paste when an application adds a synthetic generic file", () => {
34 const file = new File(["clipboard"], "clipboard.rtf", { type: "application/rtf" });
35
36 expect(
37 shouldHandleComposerAttachmentPaste({
38 files: [file],
39 plainText: "Copied text",
40 }),
41 ).toBe(false);
42 });
43
44 it("claims unsupported image pastes so the composer can report them", () => {
45 const images = [
46 new File(["svg"], "diagram.svg", { type: "image/svg+xml" }),
47 new File(["tiff"], "photo.tiff", { type: "image/tiff" }),
48 ];
49
50 for (const image of images) {
51 expect(
52 shouldHandleComposerAttachmentPaste({
53 files: [image],
54 plainText: "Image caption",
55 }),
56 ).toBe(true);
57 }
58 });
59
60 it("claims generic file-only pastes so the composer can report validation errors", () => {
61 const file = new File(["report"], "report.pdf", { type: "application/pdf" });
62
63 expect(shouldHandleComposerAttachmentPaste({ files: [file], plainText: "" })).toBe(true);
64 });
65
66 it("routes empty and oversized generic files to composer feedback", () => {
67 const empty = new File([], "empty.txt", { type: "text/plain" });
68 const oversized = new File([new Uint8Array(1024)], "large.zip", {
69 type: "application/zip",
70 });
71
72 expect(shouldHandleComposerAttachmentPaste({ files: [empty], plainText: "" })).toBe(true);
73 expect(shouldHandleComposerAttachmentPaste({ files: [oversized], plainText: "" })).toBe(true);
74 });
75
76 it("ignores an empty clipboard", () => {
77 expect(shouldHandleComposerAttachmentPaste({ files: [], plainText: "" })).toBe(false);
78 });
79
80 it("falls back to the extension when an image arrives without a MIME type", () => {
81 expect(classifyComposerAttachmentFile({ name: "photo.jpg", type: "" })).toBe("image");
82 expect(classifyComposerAttachmentFile({ name: "shot.PNG", type: "" })).toBe("image");
83 expect(classifyComposerAttachmentFile({ name: "archive.zip", type: "" })).toBe("file");
84 expect(classifyComposerAttachmentFile({ name: "no-extension", type: "" })).toBe("file");
85 expect(inferImageMimeTypeFromName("photo.jpg")).toBe("image/jpeg");
86 expect(inferImageMimeTypeFromName("archive.zip")).toBeNull();
87 });
88
89 it("infers supported image types from octet-stream files", () => {
90 const jpeg = new File(["jpeg"], "photo.jpg", { type: "application/octet-stream" });
91 const png = new File(["png"], "shot.PNG", { type: "application/octet-stream" });
92
93 expect(classifyComposerAttachmentFile(jpeg)).toBe("image");
94 expect(classifyComposerAttachmentFile(png)).toBe("image");
95 expect(normalizeComposerImageFileMimeType(jpeg).type).toBe("image/jpeg");
96 expect(normalizeComposerImageFileMimeType(png).type).toBe("image/png");
97 });
98
99 it("does not infer images for unknown extensions or specific conflicting MIME types", () => {
100 const binary = new File(["binary"], "archive.bin", { type: "application/octet-stream" });
101 const unknownDocument = new File(["pdf"], "report.pdf", {
102 type: "application/octet-stream",
103 });
104 const document = new File(["pdf"], "photo.jpg", { type: "application/pdf" });
105 const explicitImage = new File(["png"], "photo.jpg", { type: "image/png" });
106
107 expect(classifyComposerAttachmentFile(binary)).toBe("file");
108 expect(classifyComposerAttachmentFile(unknownDocument)).toBe("file");
109 expect(classifyComposerAttachmentFile(document)).toBe("file");
110 expect(classifyComposerAttachmentFile(explicitImage)).toBe("image");
111 expect(normalizeComposerImageFileMimeType(binary)).toBe(binary);
112 expect(normalizeComposerImageFileMimeType(document)).toBe(document);
113 expect(normalizeComposerImageFileMimeType(explicitImage)).toBe(explicitImage);
114 });
115
116 it("uses the hard local limit while server config is unknown", () => {
117 expect(
118 fileAttachmentStagingLimit({
119 attachmentUploadsCapabilityKnown: false,
120 supportsAttachmentUploads: false,
121 maxFileAttachmentBytes: null,
122 }),
123 ).toBe(PROVIDER_SEND_TURN_MAX_FILE_BYTES);
124 expect(
125 fileAttachmentCapabilityBlockReason({
126 files: [{ name: "pending.zip", sizeBytes: PROVIDER_SEND_TURN_MAX_FILE_BYTES }],
127 attachmentUploadsCapabilityKnown: false,
128 supportsAttachmentUploads: false,
129 maxFileAttachmentBytes: null,
130 }),
131 ).toBe("Waiting for the server before file attachments can send");
132 });
133
134 it("rejects local staging and send when known config has no file support", () => {
135 const unsupportedReason =
136 "This server does not accept file attachments right now. Remove the files to send.";
137 expect(
138 fileAttachmentStagingLimit({
139 attachmentUploadsCapabilityKnown: true,
140 supportsAttachmentUploads: true,
141 maxFileAttachmentBytes: null,
142 }),
143 ).toBeNull();
144 expect(
145 fileAttachmentStagingLimit({
146 attachmentUploadsCapabilityKnown: true,
147 supportsAttachmentUploads: false,
148 maxFileAttachmentBytes: 50 * 1024 * 1024,
149 }),
150 ).toBeNull();
151 expect(
152 fileAttachmentCapabilityBlockReason({
153 files: [{ name: "report.pdf", sizeBytes: 1024 }],
154 attachmentUploadsCapabilityKnown: true,
155 supportsAttachmentUploads: true,
156 maxFileAttachmentBytes: null,
157 }),
158 ).toBe(unsupportedReason);
159 expect(
160 fileAttachmentCapabilityBlockReason({
161 files: [{ name: "report.pdf", sizeBytes: 1024 }],
162 attachmentUploadsCapabilityKnown: true,
163 supportsAttachmentUploads: false,
164 maxFileAttachmentBytes: 50 * 1024 * 1024,
165 }),
166 ).toBe(unsupportedReason);
167 });
168
169 it("blocks retained files that exceed a newly lower server limit", () => {
170 expect(
171 fileAttachmentCapabilityBlockReason({
172 files: [{ name: "large.zip", sizeBytes: 2 * 1024 * 1024 }],
173 attachmentUploadsCapabilityKnown: true,
174 supportsAttachmentUploads: true,
175 maxFileAttachmentBytes: 1024 * 1024,
176 }),
177 ).toBe("'large.zip' exceeds the 1 MB attachment limit.");
178 });
179
180 it("uses the confirmed server limit without exceeding the hard cap", () => {
181 expect(
182 fileAttachmentStagingLimit({
183 attachmentUploadsCapabilityKnown: true,
184 supportsAttachmentUploads: true,
185 maxFileAttachmentBytes: 1024 * 1024,
186 }),
187 ).toBe(1024 * 1024);
188 expect(
189 fileAttachmentStagingLimit({
190 attachmentUploadsCapabilityKnown: true,
191 supportsAttachmentUploads: true,
192 maxFileAttachmentBytes: PROVIDER_SEND_TURN_MAX_FILE_BYTES * 2,
193 }),
194 ).toBe(PROVIDER_SEND_TURN_MAX_FILE_BYTES);
195 expect(
196 fileAttachmentCapabilityBlockReason({
197 files: [{ name: "report.pdf", sizeBytes: 1024 }],
198 attachmentUploadsCapabilityKnown: true,
199 supportsAttachmentUploads: true,
200 maxFileAttachmentBytes: 50 * 1024 * 1024,
201 }),
202 ).toBeNull();
203 });
204
205 it("does not block empty or image-only composers on legacy servers", () => {
206 expect(
207 fileAttachmentCapabilityBlockReason({
208 files: [],
209 attachmentUploadsCapabilityKnown: true,
210 supportsAttachmentUploads: false,
211 maxFileAttachmentBytes: null,
212 }),
213 ).toBeNull();
214 expect(
215 fileAttachmentCapabilityBlockReason({
216 files: [],
217 attachmentUploadsCapabilityKnown: false,
218 supportsAttachmentUploads: false,
219 maxFileAttachmentBytes: null,
220 }),
221 ).toBeNull();
222 });
223
224 it("keeps draft-persisted file uploads when the upload capability flips off", () => {
225 const environmentId = EnvironmentId.make("environment-1");
226 const image: ComposerImageAttachment = {
227 type: "image",
228 id: "image-1",
229 name: "photo.png",
230 mimeType: "image/png",
231 sizeBytes: 3,
232 previewUrl: "blob:photo",
233 file: new File([new Uint8Array([1, 2, 3])], "photo.png", { type: "image/png" }),
234 };
235 const uploadingFile: ComposerFileAttachment = {
236 type: "file",
237 id: "file-uploading",
238 name: "fresh.pdf",
239 mimeType: "application/pdf",
240 sizeBytes: 3,
241 file: new File([new Uint8Array([1, 2, 3])], "fresh.pdf", { type: "application/pdf" }),
242 };
243 const hydratedFile: ComposerFileAttachment = {
244 type: "file",
245 id: "file-hydrated",
246 name: "report.pdf",
247 mimeType: "application/pdf",
248 sizeBytes: 3,
249 file: null,
250 uploadedAttachmentId: "pending-report-pdf",
251 uploadEnvironmentId: environmentId,
252 };
253 const uploadedLocalFile: ComposerFileAttachment = {
254 ...uploadingFile,
255 id: "file-uploaded-local",
256 uploadedAttachmentId: "pending-fresh-pdf",
257 uploadEnvironmentId: environmentId,
258 };
259
260 const released = attachmentsToReleaseOnUploadCapabilityLoss([
261 image,
262 uploadingFile,
263 hydratedFile,
264 uploadedLocalFile,
265 ]);
266
267 expect(released.map((attachment) => attachment.id)).toEqual(["image-1", "file-uploading"]);
268 });
269
270 it("claims image pastes even when clipboard text is present", () => {
271 const image = new File(["image"], "photo.heic", { type: "image/heic" });
272
273 expect(
274 shouldHandleComposerAttachmentPaste({
275 files: [image],
276 plainText: "Image caption",
277 }),
278 ).toBe(true);
279 });
280});