Open History to see the last commit
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 { loadLocalVideoPreview } from "./localVideoPreview";
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("loadLocalVideoPreview", () => {
49 it("resolves the current iOS container and releases its playback lease once", async () => {
50 const preview = await loadLocalVideoPreview(attachment, new AbortController().signal);
51 expect(preview?.uri).toContain("/22222222-2222-4222-8222-222222222222/Documents/");
52 expect(mocks.retain).toHaveBeenCalledWith(attachment);
53 const release = mocks.retain.mock.results[0]!.value;
54 expect(release).not.toHaveBeenCalled();
55 preview?.dispose();
56 preview?.dispose();
57 expect(release).toHaveBeenCalledTimes(1);
58 });
59
60 it.each([undefined, "share-button"])(
61 "keeps a separate share lease after playback closes (source: %s)",
62 async (sourceIdentifier) => {
63 const shared = Promise.withResolvers<void>();
64 mocks.share.mockReturnValue(shared.promise);
65 const preview = await loadLocalVideoPreview(attachment, new AbortController().signal);
66 const share = preview!.share(new AbortController().signal, sourceIdentifier);
67 expect(mocks.retain).toHaveBeenCalledTimes(2);
68 const releasePlayback = mocks.retain.mock.results[0]!.value;
69 const releaseShare = mocks.retain.mock.results[1]!.value;
70 preview!.dispose();
71 expect(releasePlayback).toHaveBeenCalledTimes(1);
72 expect(releaseShare).not.toHaveBeenCalled();
73 shared.resolve();
74 await share;
75 expect(releaseShare).toHaveBeenCalledTimes(1);
76 },
77 );
78
79 it("releases a failed share while keeping playback retained", async () => {
80 mocks.share.mockRejectedValue(new Error("Sharing unavailable"));
81 const preview = await loadLocalVideoPreview(attachment, new AbortController().signal);
82 await expect(preview!.share(new AbortController().signal)).rejects.toThrow(
83 "Sharing unavailable",
84 );
85 expect(mocks.retain.mock.results[1]!.value).toHaveBeenCalledTimes(1);
86 expect(mocks.retain.mock.results[0]!.value).not.toHaveBeenCalled();
87 preview!.dispose();
88 });
89
90 it("releases a load canceled during native module loading", async () => {
91 const controller = new AbortController();
92 const loading = loadLocalVideoPreview(attachment, controller.signal);
93 controller.abort();
94 await expect(loading).resolves.toBeNull();
95 expect(mocks.retain.mock.results[0]!.value).toHaveBeenCalledTimes(1);
96 expect(mocks.exists).not.toHaveBeenCalled();
97 });
98
99 it("reports missing files and releases their lease", async () => {
100 mocks.exists.mockReturnValue(false);
101 await expect(loadLocalVideoPreview(attachment, new AbortController().signal)).rejects.toThrow(
102 "This video is no longer available. Attach the file again.",
103 );
104 expect(mocks.retain.mock.results[0]!.value).toHaveBeenCalledTimes(1);
105 });
106
107 it("does not start sharing a disposed preview", async () => {
108 const preview = await loadLocalVideoPreview(attachment, new AbortController().signal);
109 preview!.dispose();
110 await preview!.share(new AbortController().signal);
111 expect(mocks.share).not.toHaveBeenCalled();
112 expect(mocks.retain).toHaveBeenCalledTimes(1);
113 });
114});