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

incoming-share-model.test.ts

196 lines6.3 KB

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 { buildIncomingShareDraft, hasIncomingShareContent } from "./incoming-share-model";
9​
10describe("incoming native shares", () => {
11 it("converts shared text, URLs, and images into a durable composer draft", async () => {
12 const image: SharePayload = {
13 shareType: "image",
14 value: "file:///shared/Screenshot.png",
15 mimeType: "image/png",
16 };
17 const payloads: SharePayload[] = [
18 { shareType: "text", value: "Please explain this error" },
19 { shareType: "url", value: "https://example.com/issue/1" },
20 { shareType: "text", value: "Please explain this error" },
21 image,
22 ];
23 const resolvedImage: ResolvedSharePayload = {
24 ...image,
25 contentUri: image.value,
26 contentType: "image",
27 contentMimeType: "image/png",
28 contentSize: 3,
29 originalName: "Screenshot.png",
30 };
31 const removeOwnedFile = vi.fn(() => Promise.resolve());
32​
33 const result = await buildIncomingShareDraft({
34 id: "share-1",
35 createdAt: "2026-07-15T10:00:00.000Z",
36 payloads,
37 resolvedPayloads: [resolvedImage],
38 fileReader: {
39 readBase64: async () => "YWJj",
40 removeOwnedFile,
41 },
42 });
43​
44 expect(result).toEqual({
45 schemaVersion: 1,
46 id: "share-1",
47 createdAt: "2026-07-15T10:00:00.000Z",
48 text: "Please explain this error\n\nhttps://example.com/issue/1",
49 attachments: [
50 {
51 id: "share-1:image:3",
52 type: "image",
53 name: "Screenshot.png",
54 mimeType: "image/png",
55 sizeBytes: 3,
56 dataUrl: "data:image/png;base64,YWJj",
57 previewUri: "data:image/png;base64,YWJj",
58 },
59 ],
60 warnings: [],
61 });
62 expect(removeOwnedFile).toHaveBeenCalledWith(image.value);
63 expect(hasIncomingShareContent(result)).toBe(true);
64 });
65​
66 it("skips oversized images and releases the temporary native file", async () => {
67 const image: SharePayload = {
68 shareType: "image",
69 value: "file:///shared/huge.png",
70 mimeType: "image/png",
71 };
72 const readBase64 = vi.fn(async () => "unused");
73 const removeOwnedFile = vi.fn(() => Promise.resolve());
74​
75 const result = await buildIncomingShareDraft({
76 id: "share-2",
77 createdAt: "2026-07-15T10:00:00.000Z",
78 payloads: [image],
79 resolvedPayloads: [
80 {
81 ...image,
82 contentUri: image.value,
83 contentType: "image",
84 contentMimeType: "image/png",
85 contentSize: PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1,
86 originalName: "huge.png",
87 },
88 ],
89 fileReader: { readBase64, removeOwnedFile },
90 });
91​
92 expect(result.attachments).toEqual([]);
93 expect(result.warnings).toEqual(["'huge.png' exceeds the 10 MB attachment limit."]);
94 expect(readBase64).not.toHaveBeenCalled();
95 expect(removeOwnedFile).toHaveBeenCalledWith(image.value);
96 expect(hasIncomingShareContent(result)).toBe(false);
97 });
98​
99 it("releases every temporary file when a share exceeds the attachment limit", async () => {
100 const payloads = Array.from({ length: PROVIDER_SEND_TURN_MAX_ATTACHMENTS + 1 }, (_, index) => ({
101 shareType: "image" as const,
102 value: `file:///shared/${index}.png`,
103 mimeType: "image/png",
104 }));
105 const removeOwnedFile = vi.fn(() => Promise.resolve());
106 const readBase64 = vi.fn(async () => "YWJj");
107​
108 const result = await buildIncomingShareDraft({
109 id: "share-3",
110 createdAt: "2026-07-15T10:00:00.000Z",
111 payloads,
112 resolvedPayloads: [],
113 fileReader: { readBase64, removeOwnedFile },
114 });
115​
116 expect(result.attachments).toHaveLength(PROVIDER_SEND_TURN_MAX_ATTACHMENTS);
117 expect(result.warnings).toEqual([
118 `Only the first ${PROVIDER_SEND_TURN_MAX_ATTACHMENTS} shared images were attached.`,
119 ]);
120 expect(readBase64).toHaveBeenCalledTimes(PROVIDER_SEND_TURN_MAX_ATTACHMENTS);
121 expect(removeOwnedFile).toHaveBeenCalledTimes(payloads.length);
122 });
123​
124 it("maps duplicate image payloads to distinct resolved files", async () => {
125 const duplicate: SharePayload = {
126 shareType: "image",
127 value: "content://shared/screenshot",
128 mimeType: "image/png",
129 };
130 const resolvedPayloads: ResolvedSharePayload[] = [
131 {
132 ...duplicate,
133 contentUri: "file:///cache/first.png",
134 contentType: "image",
135 contentMimeType: "image/png",
136 contentSize: 3,
137 originalName: "first.png",
138 },
139 {
140 ...duplicate,
141 contentUri: "file:///cache/second.png",
142 contentType: "image",
143 contentMimeType: "image/png",
144 contentSize: 3,
145 originalName: "second.png",
146 },
147 ];
148 const readBase64 = vi.fn(async (uri: string) =>
149 uri.includes("first") ? "Zmlyc3Q=" : "c2Vjb25k",
150 );
151 const removeOwnedFile = vi.fn(async () => undefined);
152​
153 const result = await buildIncomingShareDraft({
154 id: "share-duplicates",
155 createdAt: "2026-07-16T08:00:00.000Z",
156 payloads: [duplicate, duplicate],
157 resolvedPayloads,
158 fileReader: { readBase64, removeOwnedFile },
159 });
160​
161 expect(readBase64.mock.calls.map(([uri]) => uri)).toEqual([
162 "file:///cache/first.png",
163 "file:///cache/second.png",
164 ]);
165 expect(result.attachments.map((attachment) => attachment.name)).toEqual([
166 "first.png",
167 "second.png",
168 ]);
169 expect(removeOwnedFile).toHaveBeenCalledWith("file:///cache/first.png");
170 expect(removeOwnedFile).toHaveBeenCalledWith("file:///cache/second.png");
171 });
172​
173 it("keeps imported content when temporary-file cleanup fails", async () => {
174 const image: SharePayload = {
175 shareType: "image",
176 value: "file:///shared/screenshot.png",
177 mimeType: "image/png",
178 };
179​
180 const result = await buildIncomingShareDraft({
181 id: "share-cleanup-failure",
182 createdAt: "2026-07-16T08:00:00.000Z",
183 payloads: [image],
184 resolvedPayloads: [],
185 fileReader: {
186 readBase64: async () => "YWJj",
187 removeOwnedFile: async () => {
188 throw new Error("file is busy");
189 },
190 },
191 });
192​
193 expect(result.attachments).toHaveLength(1);
194 expect(result.warnings).toEqual([]);
195 });
196});