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

composerAttachmentFiles.test.ts

324 lines11.7 KB

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