Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composerImages.test.ts

124 lines3.7 KB

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 {
40 convertPastedImagesToAttachments,
41 isOwnedPastedImageUri,
42 toUploadChatImageAttachments,
43} from "./composerImages";
44​
45describe("toUploadChatImageAttachments", () => {
46 it("strips client draft id and previewUri for the startTurn wire shape", () => {
47 expect(
48 toUploadChatImageAttachments([
49 {
50 id: "client-draft-id",
51 type: "image",
52 name: "pasted-image.png",
53 mimeType: "image/png",
54 sizeBytes: 12,
55 dataUrl: "data:image/png;base64,AA==",
56 previewUri: "file:///tmp/preview.png",
57 },
58 ]),
59 ).toEqual([
60 {
61 type: "image",
62 name: "pasted-image.png",
63 mimeType: "image/png",
64 sizeBytes: 12,
65 dataUrl: "data:image/png;base64,AA==",
66 },
67 ]);
68 });
69});
70​
71describe("native pasted image cleanup", () => {
72 beforeEach(() => {
73 files.clear();
74 });
75​
76 it("recognizes only files created in the native composer paste directory", () => {
77 expect(
78 isOwnedPastedImageUri(
79 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/id.png",
80 ),
81 ).toBe(true);
82 expect(isOwnedPastedImageUri("file:///private/var/mobile/photos/id.png")).toBe(false);
83 expect(isOwnedPastedImageUri("https://example.com/t3-composer-paste/id.png")).toBe(false);
84 });
85​
86 it("converts owned files to data-backed previews and deletes the source", async () => {
87 const uri =
88 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/id.png";
89 files.set(uri, { base64: "aGVsbG8=", deleted: false });
90​
91 const attachments = await convertPastedImagesToAttachments({
92 uris: [uri],
93 existingCount: 0,
94 });
95​
96 expect(attachments).toEqual([
97 expect.objectContaining({
98 dataUrl: "data:image/png;base64,aGVsbG8=",
99 previewUri: "data:image/png;base64,aGVsbG8=",
100 }),
101 ]);
102 expect(files.get(uri)?.deleted).toBe(true);
103 });
104​
105 it("deletes rejected and overflow owned files without deleting user-owned files", async () => {
106 const rejected =
107 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/bad.png";
108 const overflow =
109 "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/overflow.png";
110 const userOwned = "file:///private/var/mobile/photos/library.png";
111 files.set(rejected, { base64: "", deleted: false });
112 files.set(overflow, { base64: "aGVsbG8=", deleted: false });
113 files.set(userOwned, { base64: "aGVsbG8=", deleted: false });
114​
115 await convertPastedImagesToAttachments({
116 uris: [rejected, overflow, userOwned],
117 existingCount: PROVIDER_SEND_TURN_MAX_ATTACHMENTS - 1,
118 });
119​
120 expect(files.get(rejected)?.deleted).toBe(true);
121 expect(files.get(overflow)?.deleted).toBe(true);
122 expect(files.get(userOwned)?.deleted).toBe(false);
123 });
124});