Open History to see the last commit
1import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
2import { PROVIDER_SEND_TURN_MAX_ATTACHMENTS } from "@t3tools/contracts";
3
4const files = new Map<string, { base64: string; deleted: boolean }>();
5
6vi.mock("expo-file-system", () => ({
7 File: class {
8 readonly uri: string;
9
10 constructor(uri: string) {
11 this.uri = uri;
12 }
13
14 get exists(): boolean {
15 return files.has(this.uri) && files.get(this.uri)?.deleted === false;
16 }
17
18 async base64(): Promise<string> {
19 const entry = files.get(this.uri);
20 if (!entry || entry.deleted) {
21 throw new Error("missing file");
22 }
23 return entry.base64;
24 }
25
26 delete(): void {
27 const entry = files.get(this.uri);
28 if (entry) {
29 entry.deleted = true;
30 }
31 }
32 },
33}));
34
35vi.mock("./uuid", () => ({
36 uuidv4: () => "attachment-id",
37}));
38
39import { convertPastedImagesToAttachments, isOwnedPastedImageUri } from "./composerImages";
40
41describe("native pasted image cleanup", () => {
42 beforeEach(() => {
43 files.clear();
44 });
45
46 it("recognizes only files created in the native composer paste directory", () => {
47 expect(
48 isOwnedPastedImageUri(
49 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/id.png",
50 ),
51 ).toBe(true);
52 expect(isOwnedPastedImageUri("file:///private/var/mobile/photos/id.png")).toBe(false);
53 expect(isOwnedPastedImageUri("https://example.com/t3-composer-paste/id.png")).toBe(false);
54 });
55
56 it("converts owned files to data-backed previews and deletes the source", async () => {
57 const uri =
58 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/id.png";
59 files.set(uri, { base64: "aGVsbG8=", deleted: false });
60
61 const attachments = await convertPastedImagesToAttachments({
62 uris: [uri],
63 existingCount: 0,
64 });
65
66 expect(attachments).toEqual([
67 expect.objectContaining({
68 dataUrl: "data:image/png;base64,aGVsbG8=",
69 previewUri: "data:image/png;base64,aGVsbG8=",
70 }),
71 ]);
72 expect(files.get(uri)?.deleted).toBe(true);
73 });
74
75 it("deletes rejected and overflow owned files without deleting user-owned files", async () => {
76 const rejected =
77 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/bad.png";
78 const overflow =
79 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/overflow.png";
80 const userOwned = "file:///private/var/mobile/photos/library.png";
81 files.set(rejected, { base64: "", deleted: false });
82 files.set(overflow, { base64: "aGVsbG8=", deleted: false });
83 files.set(userOwned, { base64: "aGVsbG8=", deleted: false });
84
85 await convertPastedImagesToAttachments({
86 uris: [rejected, overflow, userOwned],
87 existingCount: PROVIDER_SEND_TURN_MAX_ATTACHMENTS - 1,
88 });
89
90 expect(files.get(rejected)?.deleted).toBe(true);
91 expect(files.get(overflow)?.deleted).toBe(true);
92 expect(files.get(userOwned)?.deleted).toBe(false);
93 });
94});