Open History to see the last commit
1import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
2import {
3 PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
4 PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
5} from "@t3tools/contracts";
6
7const files = new Map<
8 string,
9 { base64: string; deleted: boolean; text?: string; mimeType?: string }
10>();
11
12const manipulator = vi.hoisted(() => {
13 const state = { sourceSize: { width: 0, height: 0 }, renderedBase64: "cmVuZGVyZWQ=" };
14 const manipulate = vi.fn((source: string | { width: number; height: number }) => {
15 let size = typeof source === "string" ? state.sourceSize : source;
16 const context = {
17 resize: vi.fn((next: { width: number; height: number }) => {
18 size = next;
19 return context;
20 }),
21 renderAsync: vi.fn(async () => ({
22 ...size,
23 release: vi.fn(),
24 saveAsync: vi.fn(async (options: { format: string }) => {
25 const uri = `file:///cache/rendered.${options.format}`;
26 files.set(uri, { base64: state.renderedBase64, deleted: false });
27 return { uri, base64: state.renderedBase64 };
28 }),
29 })),
30 };
31 return context;
32 });
33 return { state, manipulate };
34});
35
36vi.mock("expo-image-manipulator", () => ({
37 ImageManipulator: { manipulate: manipulator.manipulate },
38 SaveFormat: { JPEG: "jpeg", PNG: "png" },
39}));
40
41vi.mock("expo-file-system", () => ({
42 File: class {
43 readonly uri: string;
44 readonly name: string;
45 readonly parentDirectory: { readonly uri: string };
46
47 constructor(parent: string | { readonly uri: string }, name?: string) {
48 const parentUri = typeof parent === "string" ? parent : parent.uri;
49 this.uri = name ? `${parentUri}/${name}` : parentUri;
50 this.name = name ?? this.uri.split("/").at(-1) ?? "file";
51 this.parentDirectory = { uri: this.uri.slice(0, -(this.name.length + 1)) };
52 }
53
54 get exists(): boolean {
55 return files.has(this.uri) && files.get(this.uri)?.deleted === false;
56 }
57
58 get type(): string {
59 return files.get(this.uri)?.mimeType ?? "";
60 }
61
62 get size(): number | null {
63 const entry = files.get(this.uri);
64 return entry && !entry.deleted
65 ? Math.floor((entry.base64.replace(/=+$/, "").length * 3) / 4)
66 : null;
67 }
68
69 async copy(destination: { readonly uri: string }): Promise<void> {
70 const entry = files.get(this.uri);
71 if (!entry) throw new Error("missing file");
72 files.set(destination.uri, { ...entry });
73 }
74
75 async base64(): Promise<string> {
76 const entry = files.get(this.uri);
77 if (!entry || entry.deleted) {
78 throw new Error("missing file");
79 }
80 return entry.base64;
81 }
82
83 delete(): void {
84 const entry = files.get(this.uri);
85 if (entry) {
86 entry.deleted = true;
87 }
88 }
89
90 create(): void {
91 files.set(this.uri, { base64: "", deleted: false });
92 }
93
94 write(text: string): void {
95 files.set(this.uri, { base64: "", deleted: false, text });
96 }
97
98 moveSync(destination: { readonly uri: string }): void {
99 const entry = files.get(this.uri);
100 if (!entry) throw new Error("missing staged file");
101 files.set(destination.uri, entry);
102 files.delete(this.uri);
103 }
104 },
105 Directory: class {
106 readonly uri: string;
107
108 constructor(parent: string, name: string) {
109 this.uri = `${parent}/${name}`;
110 }
111
112 create(): void {}
113 },
114 FileMode: { ReadOnly: "r", WriteOnly: "w" },
115 Paths: { document: "file:///documents" },
116}));
117
118vi.mock("./uuid", () => ({
119 uuidv4: () => "attachment-id",
120}));
121
122import { convertPastedImagesToAttachments, persistComposerAttachmentFile } from "./composerImages";
123
124function oversizedPng(): string {
125 const png = Buffer.alloc(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1);
126 Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]).copy(png);
127 png.write("IHDR", 12);
128 png.writeUInt32BE(2000, 16);
129 png.writeUInt32BE(2000, 20);
130 return png.toString("base64");
131}
132
133describe("native pasted image cleanup", () => {
134 beforeEach(() => {
135 files.clear();
136 manipulator.state.renderedBase64 = "cmVuZGVyZWQ=";
137 });
138
139 it("preserves the reported image type of extensionless Android content URIs", async () => {
140 const uri = "content://media/external/images/media/42";
141 files.set(uri, { base64: "aGVsbG8=", deleted: false, mimeType: "image/jpeg" });
142
143 const { attachments } = await convertPastedImagesToAttachments({
144 uris: [uri],
145 existingCount: 0,
146 });
147
148 expect(attachments).toEqual([
149 expect.objectContaining({
150 name: "pasted-image.jpeg",
151 mimeType: "image/jpeg",
152 dataUrl: "data:image/jpeg;base64,aGVsbG8=",
153 previewUri: "data:image/jpeg;base64,aGVsbG8=",
154 }),
155 ]);
156 expect(files.get(uri)?.deleted).toBe(false);
157 });
158
159 it("renders unsupported images to JPEG and removes the cache file after attaching", async () => {
160 const uri = "content://media/external/images/media/43";
161 files.set(uri, { base64: "b3JpZ2luYWw=", deleted: false, mimeType: "image/heic" });
162 manipulator.state.sourceSize = { width: 100, height: 100 };
163
164 const { attachments } = await convertPastedImagesToAttachments({
165 uris: [uri],
166 existingCount: 0,
167 });
168
169 expect(attachments).toEqual([
170 expect.objectContaining({
171 name: "pasted-image.jpeg",
172 mimeType: "image/jpeg",
173 dataUrl: "data:image/jpeg;base64,cmVuZGVyZWQ=",
174 previewUri: "data:image/jpeg;base64,cmVuZGVyZWQ=",
175 }),
176 ]);
177 expect(files.get("file:///cache/rendered.jpeg")?.deleted).toBe(true);
178 expect(files.get(uri)?.deleted).toBe(false);
179 });
180
181 it("removes the rendered JPEG when its bytes exceed the image limit", async () => {
182 const uri = "content://media/external/images/media/43";
183 files.set(uri, { base64: "b3JpZ2luYWw=", deleted: false, mimeType: "image/heic" });
184 manipulator.state.sourceSize = { width: 100, height: 100 };
185 manipulator.state.renderedBase64 = "A".repeat(
186 Math.ceil((PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1) / 3) * 4,
187 );
188
189 const { attachments } = await convertPastedImagesToAttachments({
190 uris: [uri],
191 existingCount: 0,
192 });
193
194 expect(attachments).toEqual([]);
195 expect(files.get("file:///cache/rendered.jpeg")?.deleted).toBe(true);
196 expect(files.get(uri)?.deleted).toBe(false);
197 });
198
199 it("converts owned files to data-backed previews and deletes the source", async () => {
200 const uri =
201 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/id.png";
202 const gif =
203 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/anim.gif";
204 files.set(uri, { base64: "aGVsbG8=", deleted: false });
205 files.set(gif, { base64: "R0lGODlh", deleted: false });
206
207 const { attachments } = await convertPastedImagesToAttachments({
208 uris: [uri, gif],
209 existingCount: 0,
210 });
211
212 expect(attachments).toEqual([
213 expect.objectContaining({
214 dataUrl: "data:image/png;base64,aGVsbG8=",
215 previewUri: "data:image/png;base64,aGVsbG8=",
216 }),
217 expect.objectContaining({ mimeType: "image/gif", dataUrl: "data:image/gif;base64,R0lGODlh" }),
218 ]);
219 expect(files.get(uri)?.deleted).toBe(true);
220 expect(files.get(gif)?.deleted).toBe(true);
221 });
222
223 it("deletes rejected and overflow owned files without deleting user-owned files", async () => {
224 const rejected =
225 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/bad.png";
226 const overflow =
227 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/overflow.png";
228 const userOwned = "file:///private/var/mobile/photos/library.png";
229 files.set(rejected, { base64: "", deleted: false });
230 files.set(overflow, { base64: "aGVsbG8=", deleted: false });
231 files.set(userOwned, { base64: "aGVsbG8=", deleted: false });
232
233 await convertPastedImagesToAttachments({
234 uris: [rejected, overflow, userOwned],
235 existingCount: PROVIDER_SEND_TURN_MAX_ATTACHMENTS - 1,
236 });
237
238 expect(files.get(rejected)?.deleted).toBe(true);
239 expect(files.get(overflow)?.deleted).toBe(true);
240 expect(files.get(userOwned)?.deleted).toBe(false);
241 });
242
243 it("re-renders an over-limit paste whose dimensions already fit, like the photo picker", async () => {
244 const uri =
245 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/photo.png";
246 files.set(uri, { base64: oversizedPng(), deleted: false });
247 manipulator.state.sourceSize = { width: 2000, height: 2000 };
248
249 const { attachments } = await convertPastedImagesToAttachments({
250 uris: [uri],
251 existingCount: 0,
252 });
253
254 expect(attachments).toHaveLength(1);
255 expect(attachments[0]?.sizeBytes).toBeLessThanOrEqual(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES);
256 expect(files.get("file:///cache/rendered.jpeg")?.deleted).toBe(true);
257 });
258
259 it("keeps the readable images of a paste and still reports the one it dropped", async () => {
260 const gone =
261 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/gone.png";
262 const kept =
263 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/kept.png";
264 files.set(kept, { base64: "aGVsbG8=", deleted: false });
265
266 const result = await convertPastedImagesToAttachments({ uris: [gone, kept], existingCount: 0 });
267
268 expect(result.attachments).toEqual([
269 expect.objectContaining({ dataUrl: "data:image/png;base64,aGVsbG8=" }),
270 ]);
271 expect(result.error).toBe("Could not read a pasted image.");
272 });
273
274 it("reports the size limit when even the JPEG re-render is too large", async () => {
275 const uri =
276 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/huge.png";
277 files.set(uri, { base64: oversizedPng(), deleted: false });
278 manipulator.state.sourceSize = { width: 2000, height: 2000 };
279 manipulator.state.renderedBase64 = oversizedPng();
280
281 const result = await convertPastedImagesToAttachments({ uris: [uri], existingCount: 0 });
282
283 expect(result).toEqual({
284 attachments: [],
285 error: "A pasted image exceeds the 10 MB attachment limit.",
286 });
287 expect(files.get(uri)?.deleted).toBe(true);
288 });
289});
290
291describe("persistComposerAttachmentFile", () => {
292 beforeEach(() => {
293 files.clear();
294 });
295
296 it("stores picked files under a name Android's java.net.URI accepts", async () => {
297 const source = "file:///cache/DocumentPicker/Report [draft].pdf";
298 files.set(source, { base64: "JVBERg==", deleted: false });
299
300 const fileUri = await persistComposerAttachmentFile(source, "Report [draft].pdf");
301
302 // expo-file-system encodes spaces and `%` but leaves `[`, `]`, `|` and `^` raw.
303 expect(fileUri.split("/").at(-1)).toMatch(/^[\w\-.!~*'();:@&=+$, %]+\.pdf$/);
304 });
305});
306
307describe("composerStripAttachments", () => {
308 const image = {
309 id: "img-1",
310 type: "image" as const,
311 name: "shot.jpg",
312 mimeType: "image/jpeg",
313 sizeBytes: 10,
314 dataUrl: "",
315 };
316 const video = {
317 id: "vid-1",
318 type: "file" as const,
319 name: "clip.mp4",
320 mimeType: "video/mp4",
321 sizeBytes: 20,
322 fileUri: "file:///clip.mp4",
323 };
324 const doc = {
325 id: "doc-1",
326 type: "file" as const,
327 name: "notes.txt",
328 mimeType: "text/plain",
329 sizeBytes: 5,
330 fileUri: "file:///notes.txt",
331 };
332
333 it("keeps media beside a document rather than dropping the whole strip", async () => {
334 const { composerStripAttachments } = await import("./composerImages");
335 expect(composerStripAttachments([doc, image, video] as never).map((a) => a.id)).toEqual([
336 "img-1",
337 "vid-1",
338 ]);
339 });
340
341 it("keeps oversized document-picked images visible while sending them as files", async () => {
342 const { composerStripAttachments, isComposerImageAttachment } =
343 await import("./composerImages");
344 const oversizedImage = {
345 ...video,
346 id: "large-image",
347 name: "large.png",
348 mimeType: "image/png",
349 fileUri: "file:///large.png",
350 sizeBytes: PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1,
351 };
352
353 expect(composerStripAttachments([oversizedImage])).toEqual([oversizedImage]);
354 expect(isComposerImageAttachment(oversizedImage)).toBe(false);
355 });
356});