Open History to see the last commit
1import { describe, expect, it, vi } from "@effect/vitest";
2import {
3 PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
4 PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
5} from "@t3tools/contracts";
6import type { ResolvedSharePayload, SharePayload } from "expo-sharing";
7
8import {
9 buildIncomingShareDraft,
10 hasIncomingShareContent,
11 isShareFileUriUnderOwnedRoots,
12 selectIncomingShareAttachments,
13 selectIncomingShareAttachmentsForServer,
14} from "./incoming-share-model";
15
16describe("incoming native shares", () => {
17 it("converts shared text, URLs, and images into a durable composer draft", async () => {
18 const image: SharePayload = {
19 shareType: "image",
20 value: "file:///shared/Screenshot.png",
21 mimeType: "image/png",
22 };
23 const payloads: SharePayload[] = [
24 { shareType: "text", value: "Please explain this error" },
25 { shareType: "url", value: "https://example.com/issue/1" },
26 { shareType: "text", value: "Please explain this error" },
27 image,
28 ];
29 const resolvedImage: ResolvedSharePayload = {
30 ...image,
31 contentUri: image.value,
32 contentType: "image",
33 contentMimeType: "image/png",
34 contentSize: 3,
35 originalName: "Screenshot.png",
36 };
37 const removeOwnedFile = vi.fn(() => Promise.resolve());
38
39 const result = await buildIncomingShareDraft({
40 id: "share-1",
41 createdAt: "2026-07-15T10:00:00.000Z",
42 payloads,
43 resolvedPayloads: [resolvedImage],
44 fileReader: {
45 readBase64: async () => "YWJj",
46 removeOwnedFile,
47 },
48 });
49
50 expect(result).toEqual({
51 schemaVersion: 1,
52 id: "share-1",
53 createdAt: "2026-07-15T10:00:00.000Z",
54 text: "Please explain this error\n\nhttps://example.com/issue/1",
55 attachments: [
56 {
57 id: "share-1:image:3",
58 type: "image",
59 name: "Screenshot.png",
60 mimeType: "image/png",
61 sizeBytes: 3,
62 dataUrl: "data:image/png;base64,YWJj",
63 previewUri: "data:image/png;base64,YWJj",
64 },
65 ],
66 warnings: [],
67 });
68 expect(removeOwnedFile).toHaveBeenCalledWith(image.value);
69 expect(hasIncomingShareContent(result)).toBe(true);
70 });
71
72 it("skips oversized images and releases the temporary native file", async () => {
73 const image: SharePayload = {
74 shareType: "image",
75 value: "file:///shared/huge.png",
76 mimeType: "image/png",
77 };
78 const readBase64 = vi.fn(async () => "unused");
79 const removeOwnedFile = vi.fn(() => Promise.resolve());
80
81 const result = await buildIncomingShareDraft({
82 id: "share-2",
83 createdAt: "2026-07-15T10:00:00.000Z",
84 payloads: [image],
85 resolvedPayloads: [
86 {
87 ...image,
88 contentUri: image.value,
89 contentType: "image",
90 contentMimeType: "image/png",
91 contentSize: PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1,
92 originalName: "huge.png",
93 },
94 ],
95 fileReader: { readBase64, removeOwnedFile },
96 });
97
98 expect(result.attachments).toEqual([]);
99 expect(result.warnings).toEqual(["'huge.png' exceeds the 10 MB attachment limit."]);
100 expect(readBase64).not.toHaveBeenCalled();
101 expect(removeOwnedFile).toHaveBeenCalledWith(image.value);
102 expect(hasIncomingShareContent(result)).toBe(false);
103 });
104
105 it("keeps a shared PDF on disk without converting its contents to base64", async () => {
106 const file: SharePayload = {
107 shareType: "file",
108 value: "file:///shared/report.pdf",
109 mimeType: "application/pdf",
110 };
111 const readBase64 = vi.fn(async () => "unused");
112 const persistFile = vi.fn(async () => "file:///documents/report.pdf");
113 const removeOwnedFile = vi.fn(async (_uri: string) => undefined);
114
115 const result = await buildIncomingShareDraft({
116 id: "share-report",
117 createdAt: "2026-07-15T10:00:00.000Z",
118 payloads: [file],
119 resolvedPayloads: [
120 {
121 ...file,
122 contentUri: file.value,
123 contentType: "file",
124 contentMimeType: "application/pdf",
125 contentSize: 42,
126 originalName: "report.pdf",
127 },
128 ],
129 fileReader: { readBase64, persistFile, removeOwnedFile },
130 });
131
132 expect(result.attachments).toEqual([
133 {
134 id: "share-report:file:0",
135 type: "file",
136 name: "report.pdf",
137 mimeType: "application/pdf",
138 sizeBytes: 42,
139 fileUri: "file:///documents/report.pdf",
140 },
141 ]);
142 expect(readBase64).not.toHaveBeenCalled();
143 expect(persistFile).toHaveBeenCalledWith(file.value, "report.pdf");
144 expect(removeOwnedFile).toHaveBeenCalledWith(file.value);
145 });
146
147 it.each([
148 { value: "file:///shared/clip.MOV", mimeType: "video/quicktime", originalName: "clip.MOV" },
149 { value: "content://media/videos/12", mimeType: "video/mp4", originalName: "clip.mp4" },
150 ])("imports a shared video from $value without reading it as an image", async (video) => {
151 const sizeBytes = 20 * 1024 * 1024;
152 const fileUri = `file:///documents/${video.originalName}`;
153 const readBase64 = vi.fn(async () => "unused");
154 const persistFile = vi.fn(async () => fileUri);
155 const removeOwnedFile = vi.fn(async () => undefined);
156
157 const result = await buildIncomingShareDraft({
158 id: "share-video",
159 createdAt: "2026-08-30T10:00:00.000Z",
160 payloads: [{ ...video, shareType: "video" }],
161 resolvedPayloads: [],
162 fileReader: { readBase64, persistFile, readSize: async () => sizeBytes, removeOwnedFile },
163 });
164
165 expect(result.warnings).toEqual([]);
166 expect(result.attachments).toEqual([
167 {
168 id: "share-video:file:0",
169 type: "file",
170 name: video.originalName,
171 mimeType: video.mimeType,
172 sizeBytes,
173 fileUri,
174 },
175 ]);
176 expect(readBase64).not.toHaveBeenCalled();
177 expect(removeOwnedFile).toHaveBeenCalledWith(video.value);
178 expect(
179 selectIncomingShareAttachments({
180 attachments: result.attachments,
181 maxFileAttachmentBytes: 50 * 1024 * 1024,
182 }),
183 ).toEqual({ attachments: result.attachments, warnings: [] });
184 expect(
185 selectIncomingShareAttachments({
186 attachments: result.attachments,
187 maxFileAttachmentBytes: 10 * 1024 * 1024,
188 }),
189 ).toEqual({
190 attachments: [],
191 warnings: [`'${video.originalName}' exceeds the 10 MB attachment limit.`],
192 });
193 });
194
195 it("reports an unreadable shared file without calling it oversized", async () => {
196 const file: SharePayload = {
197 shareType: "file",
198 value: "file:///shared/empty.txt",
199 mimeType: "text/plain",
200 };
201
202 const result = await buildIncomingShareDraft({
203 id: "share-empty",
204 createdAt: "2026-07-15T10:00:00.000Z",
205 payloads: [file],
206 resolvedPayloads: [],
207 fileReader: {
208 readBase64: async () => "unused",
209 readSize: async () => 0,
210 removeOwnedFile: async () => undefined,
211 },
212 });
213
214 expect(result.attachments).toEqual([]);
215 expect(result.warnings).toEqual(["'empty.txt' is empty or could not be read."]);
216 });
217
218 it("reads an Android content URI's size after copying it into app-owned storage", async () => {
219 const file: SharePayload = {
220 shareType: "file",
221 value: "content://shared/report",
222 mimeType: "application/pdf",
223 };
224 const persistFile = vi.fn(async () => "file:///documents/report.pdf");
225 const readSize = vi.fn(async (uri: string) => (uri.startsWith("content:") ? null : 42));
226
227 const result = await buildIncomingShareDraft({
228 id: "share-android-report",
229 createdAt: "2026-07-15T10:00:00.000Z",
230 payloads: [file],
231 resolvedPayloads: [],
232 fileReader: {
233 readBase64: async () => "unused",
234 persistFile,
235 readSize,
236 removeOwnedFile: async () => undefined,
237 },
238 });
239
240 expect(result.attachments).toEqual([
241 {
242 id: "share-android-report:file:0",
243 type: "file",
244 name: "report",
245 mimeType: "application/pdf",
246 sizeBytes: 42,
247 fileUri: "file:///documents/report.pdf",
248 },
249 ]);
250 expect(readSize.mock.calls).toEqual([
251 ["content://shared/report"],
252 ["file:///documents/report.pdf"],
253 ]);
254 });
255
256 it("rejects a shared file whose persisted copy measures empty and releases the copy", async () => {
257 const file: SharePayload = {
258 shareType: "file",
259 value: "content://shared/report",
260 mimeType: "application/pdf",
261 };
262 const persistedUri = "file:///documents/report.pdf";
263 const removeOwnedFile = vi.fn(async (_uri: string) => undefined);
264
265 const result = await buildIncomingShareDraft({
266 id: "share-empty-copy",
267 createdAt: "2026-07-15T10:00:00.000Z",
268 payloads: [file],
269 resolvedPayloads: [],
270 fileReader: {
271 readBase64: async () => "unused",
272 persistFile: async () => persistedUri,
273 readSize: async (uri) => (uri.startsWith("content:") ? 42 : 0),
274 removeOwnedFile,
275 },
276 });
277
278 expect(result.attachments).toEqual([]);
279 expect(result.warnings).toEqual(["'report' is empty or could not be read."]);
280 expect(removeOwnedFile.mock.calls.map(([uri]) => uri)).toContain(persistedUri);
281 });
282
283 it("keeps a no-copy file source that the returned attachment still owns", async () => {
284 const sourceUri = "file:///documents/report.pdf";
285 const removeOwnedFile = vi.fn(async () => undefined);
286
287 const result = await buildIncomingShareDraft({
288 id: "share-no-copy",
289 createdAt: "2026-07-15T10:00:00.000Z",
290 payloads: [{ shareType: "file", value: sourceUri, mimeType: "application/pdf" }],
291 resolvedPayloads: [],
292 fileReader: {
293 readBase64: async () => "unused",
294 readSize: async () => 42,
295 removeOwnedFile,
296 },
297 });
298
299 expect(result.attachments[0]).toMatchObject({ type: "file", fileUri: sourceUri });
300 expect(removeOwnedFile).not.toHaveBeenCalled();
301 });
302
303 it("keeps a persisted copy and releases distinct temporary source URIs", async () => {
304 const payloadUri = "content://shared/report";
305 const resolvedUri = "file:///cache/report.pdf";
306 const persistedUri = "file:///documents/report.pdf";
307 const removeOwnedFile = vi.fn(async (_uri: string) => undefined);
308
309 const result = await buildIncomingShareDraft({
310 id: "share-copy",
311 createdAt: "2026-07-15T10:00:00.000Z",
312 payloads: [{ shareType: "file", value: payloadUri, mimeType: "application/pdf" }],
313 resolvedPayloads: [
314 {
315 shareType: "file",
316 value: payloadUri,
317 mimeType: "application/pdf",
318 contentUri: resolvedUri,
319 contentType: "file",
320 contentMimeType: "application/pdf",
321 contentSize: 42,
322 originalName: "report.pdf",
323 },
324 ],
325 fileReader: {
326 readBase64: async () => "unused",
327 readSize: async () => 42,
328 persistFile: async () => persistedUri,
329 removeOwnedFile,
330 },
331 });
332
333 expect(result.attachments[0]).toMatchObject({ type: "file", fileUri: persistedUri });
334 expect(removeOwnedFile.mock.calls.map(([uri]) => uri)).toEqual([resolvedUri, payloadUri]);
335 });
336
337 it("uses current server support and limits when selecting a reserved share", () => {
338 const file = {
339 id: "file-1",
340 type: "file" as const,
341 name: "report.pdf",
342 mimeType: "application/pdf",
343 sizeBytes: 6 * 1024 * 1024,
344 fileUri: "file:///documents/report.pdf",
345 };
346
347 expect(
348 selectIncomingShareAttachmentsForServer({ attachments: [file], serverConfig: null }),
349 ).toEqual({ status: "pending" });
350 expect(
351 selectIncomingShareAttachmentsForServer({
352 attachments: [file],
353 serverConfig: { environment: { capabilities: { attachmentUploads: true } } },
354 }),
355 ).toMatchObject({ status: "ready", attachments: [] });
356 expect(
357 selectIncomingShareAttachmentsForServer({
358 attachments: [file],
359 serverConfig: {
360 environment: {
361 capabilities: {
362 attachmentUploads: true,
363 fileAttachments: { maxUploadBytes: 5 * 1024 * 1024 },
364 },
365 },
366 },
367 }),
368 ).toMatchObject({ status: "ready", attachments: [] });
369 expect(
370 selectIncomingShareAttachmentsForServer({
371 attachments: [file],
372 serverConfig: {
373 environment: {
374 capabilities: {
375 attachmentUploads: true,
376 fileAttachments: { maxUploadBytes: 10 * 1024 * 1024 },
377 },
378 },
379 },
380 }),
381 ).toMatchObject({ status: "ready", attachments: [file] });
382 });
383
384 it("releases every temporary file when a share exceeds the attachment limit", async () => {
385 const payloads = Array.from({ length: PROVIDER_SEND_TURN_MAX_ATTACHMENTS + 1 }, (_, index) => ({
386 shareType: "image" as const,
387 value: `file:///shared/${index}.png`,
388 mimeType: "image/png",
389 }));
390 const removeOwnedFile = vi.fn(() => Promise.resolve());
391 const readBase64 = vi.fn(async () => "YWJj");
392
393 const result = await buildIncomingShareDraft({
394 id: "share-3",
395 createdAt: "2026-07-15T10:00:00.000Z",
396 payloads,
397 resolvedPayloads: [],
398 fileReader: { readBase64, removeOwnedFile },
399 });
400
401 expect(result.attachments).toHaveLength(PROVIDER_SEND_TURN_MAX_ATTACHMENTS);
402 expect(result.warnings).toEqual([
403 `Only the first ${PROVIDER_SEND_TURN_MAX_ATTACHMENTS} shared images were attached.`,
404 ]);
405 expect(readBase64).toHaveBeenCalledTimes(PROVIDER_SEND_TURN_MAX_ATTACHMENTS);
406 expect(removeOwnedFile).toHaveBeenCalledTimes(payloads.length);
407 });
408
409 it("maps duplicate image payloads to distinct resolved files", async () => {
410 const duplicate: SharePayload = {
411 shareType: "image",
412 value: "content://shared/screenshot",
413 mimeType: "image/png",
414 };
415 const resolvedPayloads: ResolvedSharePayload[] = [
416 {
417 ...duplicate,
418 contentUri: "file:///cache/first.png",
419 contentType: "image",
420 contentMimeType: "image/png",
421 contentSize: 3,
422 originalName: "first.png",
423 },
424 {
425 ...duplicate,
426 contentUri: "file:///cache/second.png",
427 contentType: "image",
428 contentMimeType: "image/png",
429 contentSize: 3,
430 originalName: "second.png",
431 },
432 ];
433 const readBase64 = vi.fn(async (uri: string) =>
434 uri.includes("first") ? "Zmlyc3Q=" : "c2Vjb25k",
435 );
436 const removeOwnedFile = vi.fn(async () => undefined);
437
438 const result = await buildIncomingShareDraft({
439 id: "share-duplicates",
440 createdAt: "2026-07-16T08:00:00.000Z",
441 payloads: [duplicate, duplicate],
442 resolvedPayloads,
443 fileReader: { readBase64, removeOwnedFile },
444 });
445
446 expect(readBase64.mock.calls.map(([uri]) => uri)).toEqual([
447 "file:///cache/first.png",
448 "file:///cache/second.png",
449 ]);
450 expect(result.attachments.map((attachment) => attachment.name)).toEqual([
451 "first.png",
452 "second.png",
453 ]);
454 expect(removeOwnedFile).toHaveBeenCalledWith("file:///cache/first.png");
455 expect(removeOwnedFile).toHaveBeenCalledWith("file:///cache/second.png");
456 });
457
458 it("keeps imported content when temporary-file cleanup fails", async () => {
459 const image: SharePayload = {
460 shareType: "image",
461 value: "file:///shared/screenshot.png",
462 mimeType: "image/png",
463 };
464
465 const result = await buildIncomingShareDraft({
466 id: "share-cleanup-failure",
467 createdAt: "2026-07-16T08:00:00.000Z",
468 payloads: [image],
469 resolvedPayloads: [],
470 fileReader: {
471 readBase64: async () => "YWJj",
472 removeOwnedFile: async () => {
473 throw new Error("file is busy");
474 },
475 },
476 });
477
478 expect(result.attachments).toHaveLength(1);
479 expect(result.warnings).toEqual([]);
480 });
481
482 it("releases a persisted copy when a later step fails to read it", async () => {
483 const file: SharePayload = {
484 shareType: "file",
485 value: "content://shared/report",
486 mimeType: "application/pdf",
487 };
488 const persistedUri = "file:///documents/t3-composer-attachments/report.pdf";
489 const removeOwnedFile = vi.fn(async (_uri: string) => undefined);
490
491 const result = await buildIncomingShareDraft({
492 id: "share-persist-leak",
493 createdAt: "2026-07-16T08:00:00.000Z",
494 payloads: [file],
495 resolvedPayloads: [],
496 fileReader: {
497 readBase64: async () => "unused",
498 persistFile: async () => persistedUri,
499 readSize: async (uri) => {
500 if (uri === persistedUri) {
501 throw new Error("read failed");
502 }
503 return null;
504 },
505 removeOwnedFile,
506 },
507 });
508
509 expect(result.attachments).toEqual([]);
510 expect(result.warnings).toEqual(["read failed"]);
511 expect(removeOwnedFile.mock.calls.map(([uri]) => uri)).toContain(persistedUri);
512 });
513});
514
515describe("share cleanup ownership", () => {
516 const ownedRoots = [
517 "file:///var/mobile/Containers/Data/Application/APP/Documents/",
518 "file:///var/mobile/Containers/Shared/AppGroup/GROUP",
519 ];
520
521 it("treats /private/var and /var as the same iOS location", () => {
522 expect(
523 isShareFileUriUnderOwnedRoots(
524 "file:///private/var/mobile/Containers/Shared/AppGroup/GROUP/shared.pdf",
525 ownedRoots,
526 ),
527 ).toBe(true);
528 expect(
529 isShareFileUriUnderOwnedRoots(
530 "file:///var/mobile/Containers/Data/Application/APP/Documents/t3-composer-attachments/a.pdf",
531 ["file:///private/var/mobile/Containers/Data/Application/APP/Documents/"],
532 ),
533 ).toBe(true);
534 });
535
536 it("refuses to delete a sender-owned open-in-place document", () => {
537 expect(
538 isShareFileUriUnderOwnedRoots(
539 "file:///private/var/mobile/Containers/Shared/FileProvider/OTHER/File%20Provider%20Storage/taxes.pdf",
540 ownedRoots,
541 ),
542 ).toBe(false);
543 });
544
545 it("refuses traversal segments that escape an owned root", () => {
546 // An encoded separator survives URL normalization: "..%2F.." decodes to
547 // "../..", so the lexical check must reject it before containment.
548 expect(
549 isShareFileUriUnderOwnedRoots(
550 "file:///var/mobile/Containers/Shared/AppGroup/GROUP/..%2F..%2FsenderDoc.pdf",
551 ownedRoots,
552 ),
553 ).toBe(false);
554 expect(
555 isShareFileUriUnderOwnedRoots(
556 "file:///var/mobile/Containers/Shared/AppGroup/GROUP/../senderDoc.pdf",
557 ownedRoots,
558 ),
559 ).toBe(false);
560 expect(
561 isShareFileUriUnderOwnedRoots(
562 "file:///var/mobile/Containers/Shared/AppGroup/GROUP/%2e%2e/senderDoc.pdf",
563 ownedRoots,
564 ),
565 ).toBe(false);
566 });
567
568 it("refuses non-file URIs and the owned root itself", () => {
569 expect(isShareFileUriUnderOwnedRoots("content://shared/report", ownedRoots)).toBe(false);
570 expect(
571 isShareFileUriUnderOwnedRoots(
572 "file:///var/mobile/Containers/Shared/AppGroup/GROUP",
573 ownedRoots,
574 ),
575 ).toBe(false);
576 });
577});