@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 141 lines 5.31 KB
Raw
1 import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2
3 const mocks = vi.hoisted(() => ({ createPlayer: vi.fn() }));
4 vi.mock("expo-video", () => ({ createVideoPlayer: mocks.createPlayer }));
5
6 let thumbnails: typeof import("./videoThumbnails");
7 const frame = { width: 480, height: 270 };
8 const player = () => {
9 const listeners = new Set<(event: { source: unknown }) => void>();
10 const attach = () => listeners.forEach((listener) => listener({ source: {} }));
11 return {
12 attach,
13 addListener: vi.fn((_event: string, listener: (event: { source: unknown }) => void) => {
14 listeners.add(listener);
15 return { remove: () => listeners.delete(listener) };
16 }),
17 replaceAsync: vi.fn(async (): Promise<void> => queueMicrotask(attach)),
18 generateThumbnailsAsync: vi.fn(async () => [frame]),
19 release: vi.fn(),
20 };
21 };
22 const source = () => ({ uri: "file:///clip.mp4", dispose: vi.fn() });
23
24 beforeEach(async () => {
25 vi.resetModules();
26 mocks.createPlayer.mockReset().mockImplementation(player);
27 thumbnails = await import("./videoThumbnails");
28 });
29
30 afterEach(() => vi.useRealTimers());
31
32 describe("video thumbnails", () => {
33 it("serializes decoding and skips queued requests that scroll out of view", async () => {
34 const started = Promise.withResolvers<void>();
35 const generated = Promise.withResolvers<(typeof frame)[]>();
36 const first = player();
37 first.generateThumbnailsAsync.mockImplementation(() => {
38 started.resolve();
39 return generated.promise;
40 });
41 mocks.createPlayer.mockReturnValueOnce(first);
42 const firstRequest = thumbnails.loadVideoThumbnail(
43 "first",
44 async () => source(),
45 new AbortController().signal,
46 );
47 await started.promise;
48 const removed = new AbortController();
49 const skipped = vi.fn(async () => source());
50 const queued = thumbnails.loadVideoThumbnail("removed", skipped, removed.signal);
51 const next = vi.fn(async () => source());
52 const nextRequest = thumbnails.loadVideoThumbnail("next", next, new AbortController().signal);
53 expect(next).not.toHaveBeenCalled();
54 removed.abort();
55 generated.resolve([frame]);
56 expect(await firstRequest).toBe(frame);
57 expect(await queued).toBeNull();
58 expect(await nextRequest).toBe(frame);
59 expect(skipped).not.toHaveBeenCalled();
60 expect(first.release).toHaveBeenCalledTimes(1);
61 });
62
63 it("finishes a started extraction after its reader leaves so a re-signed URL reuses it", async () => {
64 const started = Promise.withResolvers<void>();
65 const replaced = Promise.withResolvers<void>();
66 const first = player();
67 first.replaceAsync.mockImplementation(() => {
68 started.resolve();
69 return replaced.promise;
70 });
71 mocks.createPlayer.mockReturnValueOnce(first);
72 const file = source();
73 const controller = new AbortController();
74 const request = thumbnails.loadVideoThumbnail("video", async () => file, controller.signal);
75 await started.promise;
76 controller.abort();
77 const resigned = thumbnails.loadVideoThumbnail(
78 "video",
79 async () => source(),
80 new AbortController().signal,
81 );
82 replaced.resolve();
83 first.attach();
84 expect(await request).toBe(frame);
85 expect(await resigned).toBe(frame);
86 expect(mocks.createPlayer).toHaveBeenCalledTimes(1);
87 expect(first.release).toHaveBeenCalledTimes(1);
88 expect(file.dispose).toHaveBeenCalledTimes(1);
89 });
90
91 it("asks for a frame only once the player has attached the item", async () => {
92 const first = player();
93 let attached = false;
94 // Like iOS: replaceAsync resolves before the item attaches, and until then
95 // generateThumbnailsAsync finds no asset.
96 first.replaceAsync.mockImplementation(async () => {});
97 first.generateThumbnailsAsync.mockImplementation(async () => (attached ? [frame] : []));
98 mocks.createPlayer.mockReturnValueOnce(first);
99 const request = thumbnails.loadVideoThumbnail(
100 "attach",
101 async () => source(),
102 new AbortController().signal,
103 );
104 await vi.waitFor(() => expect(first.replaceAsync).toHaveBeenCalled());
105 await first.replaceAsync.mock.results[0]!.value;
106 attached = true;
107 first.attach();
108 expect(await request).toBe(frame);
109 });
110
111 it("gives up on an item that never attaches without holding up the next request", async () => {
112 vi.useFakeTimers();
113 const first = player();
114 const subscriptions: Array<{ remove: ReturnType<typeof vi.fn> }> = [];
115 first.addListener.mockImplementation(() => {
116 const subscription = { remove: vi.fn() };
117 subscriptions.push(subscription);
118 return subscription;
119 });
120 first.replaceAsync.mockImplementation(async () => {});
121 mocks.createPlayer.mockReturnValueOnce(first);
122 const file = source();
123 const stuck = thumbnails.loadVideoThumbnail(
124 "never-attached",
125 async () => file,
126 new AbortController().signal,
127 );
128 const next = thumbnails.loadVideoThumbnail(
129 "next",
130 async () => source(),
131 new AbortController().signal,
132 );
133 await vi.advanceTimersByTimeAsync(15_000);
134 expect(await stuck).toBeNull();
135 expect(first.generateThumbnailsAsync).not.toHaveBeenCalled();
136 expect(subscriptions[0]!.remove).toHaveBeenCalledTimes(1);
137 expect(first.release).toHaveBeenCalledTimes(1);
138 expect(file.dispose).toHaveBeenCalledTimes(1);
139 expect(await next).toBe(frame);
140 });
141 });