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});
95
96describe("composerStripAttachments", () => {
97 const image = {
98 id: "img-1",
99 type: "image" as const,
100 name: "shot.jpg",
101 mimeType: "image/jpeg",
102 sizeBytes: 10,
103 dataUrl: "",
104 };
105 const video = {
106 id: "vid-1",
107 type: "file" as const,
108 name: "clip.mp4",
109 mimeType: "video/mp4",
110 sizeBytes: 20,
111 fileUri: "file:///clip.mp4",
112 };
113 const doc = {
114 id: "doc-1",
115 type: "file" as const,
116 name: "notes.txt",
117 mimeType: "text/plain",
118 sizeBytes: 5,
119 fileUri: "file:///notes.txt",
120 };
121
122 it("keeps media even when it already has an inline chip", async () => {
123 const { composerStripAttachments } = await import("./composerImages");
124 const kept = composerStripAttachments([image, video] as never, new Set(["img-1", "vid-1"]));
125 // A thumbnail is the only way to see media, so it stays regardless of the chip.
126 expect(kept.map((a) => a.id)).toEqual(["img-1", "vid-1"]);
127 });
128
129 it("drops a plain file once its inline chip represents it", async () => {
130 const { composerStripAttachments } = await import("./composerImages");
131 expect(composerStripAttachments([doc] as never, new Set(["doc-1"]))).toEqual([]);
132 });
133
134 it("keeps a plain file that has no inline chip", async () => {
135 const { composerStripAttachments } = await import("./composerImages");
136 expect(composerStripAttachments([doc] as never, new Set()).map((a) => a.id)).toEqual(["doc-1"]);
137 });
138});
139
140describe("composerAttachmentInlineUri", () => {
141 it("offers the inline bytes of a picture that owns no file", async () => {
142 const { composerAttachmentInlineUri } = await import("./composerImages");
143 // The photo library and the clipboard both produce this shape. Its `attachmentId` is a
144 // local draft id, so a remote asset lookup for it can only fail.
145 expect(
146 composerAttachmentInlineUri({
147 id: "img-1",
148 type: "image",
149 name: "IMG_0111.jpg",
150 mimeType: "image/jpeg",
151 sizeBytes: 9_100_000,
152 dataUrl: "data:image/jpeg;base64,AAAA",
153 previewUri: "ph://asset",
154 } as never),
155 ).toBe("data:image/jpeg;base64,AAAA");
156 });
157
158 it("falls back to the preview when a picture kept only its asset uri", async () => {
159 const { composerAttachmentInlineUri } = await import("./composerImages");
160 expect(
161 composerAttachmentInlineUri({
162 id: "img-2",
163 type: "image",
164 name: "IMG_0112.jpg",
165 mimeType: "image/jpeg",
166 sizeBytes: 10,
167 previewUri: "ph://asset",
168 } as never),
169 ).toBe("ph://asset");
170 });
171
172 it("leaves a file-backed attachment to the retain-lease path", async () => {
173 const { composerAttachmentInlineUri } = await import("./composerImages");
174 expect(
175 composerAttachmentInlineUri({
176 id: "img-3",
177 type: "image",
178 name: "IMG_0113.jpg",
179 mimeType: "image/jpeg",
180 sizeBytes: 10,
181 fileUri: "file:///owned.jpg",
182 previewUri: "file:///owned.jpg",
183 } as never),
184 ).toBeUndefined();
185 });
186
187 it("has nothing to offer for a plain file or a missing attachment", async () => {
188 const { composerAttachmentInlineUri } = await import("./composerImages");
189 expect(composerAttachmentInlineUri(undefined)).toBeUndefined();
190 expect(
191 composerAttachmentInlineUri({
192 id: "doc-1",
193 type: "file",
194 name: "notes.txt",
195 mimeType: "text/plain",
196 sizeBytes: 4,
197 fileUri: "file:///notes.txt",
198 } as never),
199 ).toBeUndefined();
200 });
201});