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

composerAttachmentFiles.test.ts

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