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<string, { base64: string; deleted: boolean; text?: string }>();
8
9const manipulator = vi.hoisted(() => {
10 const state = { sourceSize: { width: 0, height: 0 } };
11 const manipulate = vi.fn((source: string | { width: number; height: number }) => {
12 let size = typeof source === "string" ? state.sourceSize : source;
13 const context = {
14 resize: vi.fn((next: { width: number; height: number }) => {
15 size = next;
16 return context;
17 }),
18 renderAsync: vi.fn(async () => ({
19 ...size,
20 release: vi.fn(),
21 saveAsync: vi.fn(async (options: { format: string }) => ({
22 uri: `file:///cache/rendered.${options.format}`,
23 base64: `cmVuZGVyZWQ=`,
24 })),
25 })),
26 };
27 return context;
28 });
29 return { state, manipulate };
30});
31
32vi.mock("expo-image-manipulator", () => ({
33 ImageManipulator: { manipulate: manipulator.manipulate },
34 SaveFormat: { JPEG: "jpeg", PNG: "png" },
35}));
36
37vi.mock("expo-file-system", () => ({
38 File: class {
39 readonly uri: string;
40 readonly name: string;
41 readonly parentDirectory: { readonly uri: string };
42
43 constructor(parent: string | { readonly uri: string }, name?: string) {
44 const parentUri = typeof parent === "string" ? parent : parent.uri;
45 this.uri = name ? `${parentUri}/${name}` : parentUri;
46 this.name = name ?? this.uri.split("/").at(-1) ?? "file";
47 this.parentDirectory = { uri: this.uri.slice(0, -(this.name.length + 1)) };
48 }
49
50 get exists(): boolean {
51 return files.has(this.uri) && files.get(this.uri)?.deleted === false;
52 }
53
54 async base64(): Promise<string> {
55 const entry = files.get(this.uri);
56 if (!entry || entry.deleted) {
57 throw new Error("missing file");
58 }
59 return entry.base64;
60 }
61
62 delete(): void {
63 const entry = files.get(this.uri);
64 if (entry) {
65 entry.deleted = true;
66 }
67 }
68
69 create(): void {
70 files.set(this.uri, { base64: "", deleted: false });
71 }
72
73 write(text: string): void {
74 files.set(this.uri, { base64: "", deleted: false, text });
75 }
76
77 moveSync(destination: { readonly uri: string }): void {
78 const entry = files.get(this.uri);
79 if (!entry) throw new Error("missing staged file");
80 files.set(destination.uri, entry);
81 files.delete(this.uri);
82 }
83 },
84 Directory: class {
85 readonly uri: string;
86
87 constructor(parent: string, name: string) {
88 this.uri = `${parent}/${name}`;
89 }
90
91 create(): void {}
92 },
93 Paths: { document: "file:///documents" },
94}));
95
96vi.mock("./uuid", () => ({
97 uuidv4: () => "attachment-id",
98}));
99
100import { convertPastedImagesToAttachments } from "./composerImages";
101
102describe("native pasted image cleanup", () => {
103 beforeEach(() => {
104 files.clear();
105 });
106
107 it("converts owned files to data-backed previews and deletes the source", async () => {
108 const uri =
109 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/id.png";
110 files.set(uri, { base64: "aGVsbG8=", deleted: false });
111
112 const attachments = await convertPastedImagesToAttachments({
113 uris: [uri],
114 existingCount: 0,
115 });
116
117 expect(attachments).toEqual([
118 expect.objectContaining({
119 dataUrl: "data:image/png;base64,aGVsbG8=",
120 previewUri: "data:image/png;base64,aGVsbG8=",
121 }),
122 ]);
123 expect(files.get(uri)?.deleted).toBe(true);
124 });
125
126 it("deletes rejected and overflow owned files without deleting user-owned files", async () => {
127 const rejected =
128 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/bad.png";
129 const overflow =
130 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/overflow.png";
131 const userOwned = "file:///private/var/mobile/photos/library.png";
132 files.set(rejected, { base64: "", deleted: false });
133 files.set(overflow, { base64: "aGVsbG8=", deleted: false });
134 files.set(userOwned, { base64: "aGVsbG8=", deleted: false });
135
136 await convertPastedImagesToAttachments({
137 uris: [rejected, overflow, userOwned],
138 existingCount: PROVIDER_SEND_TURN_MAX_ATTACHMENTS - 1,
139 });
140
141 expect(files.get(rejected)?.deleted).toBe(true);
142 expect(files.get(overflow)?.deleted).toBe(true);
143 expect(files.get(userOwned)?.deleted).toBe(false);
144 });
145});
146
147describe("composerStripAttachments", () => {
148 const image = {
149 id: "img-1",
150 type: "image" as const,
151 name: "shot.jpg",
152 mimeType: "image/jpeg",
153 sizeBytes: 10,
154 dataUrl: "",
155 };
156 const video = {
157 id: "vid-1",
158 type: "file" as const,
159 name: "clip.mp4",
160 mimeType: "video/mp4",
161 sizeBytes: 20,
162 fileUri: "file:///clip.mp4",
163 };
164 const doc = {
165 id: "doc-1",
166 type: "file" as const,
167 name: "notes.txt",
168 mimeType: "text/plain",
169 sizeBytes: 5,
170 fileUri: "file:///notes.txt",
171 };
172
173 it("keeps media beside a document rather than dropping the whole strip", async () => {
174 const { composerStripAttachments } = await import("./composerImages");
175 expect(composerStripAttachments([doc, image, video] as never).map((a) => a.id)).toEqual([
176 "img-1",
177 "vid-1",
178 ]);
179 });
180
181 it("keeps oversized document-picked images visible while sending them as files", async () => {
182 const { composerStripAttachments, isComposerImageAttachment } =
183 await import("./composerImages");
184 const oversizedImage = {
185 ...video,
186 id: "large-image",
187 name: "large.png",
188 mimeType: "image/png",
189 fileUri: "file:///large.png",
190 sizeBytes: PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1,
191 };
192
193 expect(composerStripAttachments([oversizedImage])).toEqual([oversizedImage]);
194 expect(isComposerImageAttachment(oversizedImage)).toBe(false);
195 });
196});