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

localAttachmentPreview.test.ts

127 lines5.0 KB

1import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
2​
3const mocks = vi.hoisted(() => ({
4 retain: vi.fn(),
5 share: vi.fn(),
6 exists: vi.fn(),
7}));
8​
9vi.mock("../state/use-composer-drafts", () => ({
10 retainComposerAttachmentFileForPreview: mocks.retain,
11}));
12vi.mock("./attachmentDownload", () => ({ shareLocalAttachment: mocks.share }));
13vi.mock("expo-file-system", () => ({
14 File: class {
15 constructor(readonly uri: string) {}
16 get exists(): boolean {
17 return mocks.exists(this.uri);
18 }
19 },
20 Paths: {
21 document: {
22 uri: "file:///var/mobile/Containers/Data/Application/22222222-2222-4222-8222-222222222222/Documents/",
23 },
24 },
25}));
26​
27import { loadLocalAttachmentPreview } from "./localAttachmentPreview";
28​
29const attachment = {
30 type: "file" as const,
31 id: "draft-video",
32 name: "clip.mov",
33 mimeType: "video/quicktime",
34 sizeBytes: 12,
35 fileUri:
36 "file:///var/mobile/Containers/Data/Application/11111111-1111-4111-8111-111111111111/Documents/t3-composer-attachments/33333333-3333-4333-8333-333333333333-clip.mov",
37};
38​
39beforeEach(() => {
40 mocks.retain.mockReset();
41 mocks.share.mockReset();
42 mocks.exists.mockReset();
43 mocks.retain.mockImplementation(() => vi.fn());
44 mocks.exists.mockReturnValue(true);
45 mocks.share.mockResolvedValue(undefined);
46});
47​
48describe("loadLocalAttachmentPreview", () => {
49 it("retains and shares a PDF with its original filename and type", async () => {
50 const pdf = { ...attachment, name: "report.pdf", mimeType: "application/pdf" };
51 const preview = await loadLocalAttachmentPreview(pdf, new AbortController().signal);
52 await preview!.share(new AbortController().signal);
53 expect(mocks.share).toHaveBeenCalledWith(
54 expect.objectContaining({
55 attachment: { name: "report.pdf", mimeType: "application/pdf" },
56 }),
57 );
58 expect(mocks.retain.mock.results[0]!.value).not.toHaveBeenCalled();
59 preview!.dispose();
60 expect(mocks.retain.mock.results[0]!.value).toHaveBeenCalledTimes(1);
61 });
62 it("resolves the current iOS container and releases its playback lease once", async () => {
63 const preview = await loadLocalAttachmentPreview(attachment, new AbortController().signal);
64 expect(preview?.uri).toContain("/22222222-2222-4222-8222-222222222222/Documents/");
65 expect(mocks.retain).toHaveBeenCalledWith(attachment);
66 const release = mocks.retain.mock.results[0]!.value;
67 expect(release).not.toHaveBeenCalled();
68 preview?.dispose();
69 preview?.dispose();
70 expect(release).toHaveBeenCalledTimes(1);
71 });
72​
73 it.each([undefined, "share-button"])(
74 "keeps a separate share lease after playback closes (source: %s)",
75 async (sourceIdentifier) => {
76 const shared = Promise.withResolvers<void>();
77 mocks.share.mockReturnValue(shared.promise);
78 const preview = await loadLocalAttachmentPreview(attachment, new AbortController().signal);
79 const share = preview!.share(new AbortController().signal, sourceIdentifier);
80 expect(mocks.retain).toHaveBeenCalledTimes(2);
81 const releasePlayback = mocks.retain.mock.results[0]!.value;
82 const releaseShare = mocks.retain.mock.results[1]!.value;
83 preview!.dispose();
84 expect(releasePlayback).toHaveBeenCalledTimes(1);
85 expect(releaseShare).not.toHaveBeenCalled();
86 shared.resolve();
87 await share;
88 expect(releaseShare).toHaveBeenCalledTimes(1);
89 },
90 );
91​
92 it("releases a failed share while keeping playback retained", async () => {
93 mocks.share.mockRejectedValue(new Error("Sharing unavailable"));
94 const preview = await loadLocalAttachmentPreview(attachment, new AbortController().signal);
95 await expect(preview!.share(new AbortController().signal)).rejects.toThrow(
96 "Sharing unavailable",
97 );
98 expect(mocks.retain.mock.results[1]!.value).toHaveBeenCalledTimes(1);
99 expect(mocks.retain.mock.results[0]!.value).not.toHaveBeenCalled();
100 preview!.dispose();
101 });
102​
103 it("releases a load canceled during native module loading", async () => {
104 const controller = new AbortController();
105 const loading = loadLocalAttachmentPreview(attachment, controller.signal);
106 controller.abort();
107 await expect(loading).resolves.toBeNull();
108 expect(mocks.retain.mock.results[0]!.value).toHaveBeenCalledTimes(1);
109 expect(mocks.exists).not.toHaveBeenCalled();
110 });
111​
112 it("reports missing files and releases their lease", async () => {
113 mocks.exists.mockReturnValue(false);
114 await expect(
115 loadLocalAttachmentPreview(attachment, new AbortController().signal),
116 ).rejects.toThrow("This attachment is no longer available. Attach the file again.");
117 expect(mocks.retain.mock.results[0]!.value).toHaveBeenCalledTimes(1);
118 });
119​
120 it("does not start sharing a disposed preview", async () => {
121 const preview = await loadLocalAttachmentPreview(attachment, new AbortController().signal);
122 preview!.dispose();
123 await preview!.share(new AbortController().signal);
124 expect(mocks.share).not.toHaveBeenCalled();
125 expect(mocks.retain).toHaveBeenCalledTimes(1);
126 });
127});