import { afterEach, beforeAll, describe, expect, it, vi } from "vitest"; import { MediaFetchError } from "./mediaFetch"; import { decodeVideoThumbnail } from "./thumbnailVideoDecoder"; import { resolveTimelineViewportBudgets } from "./timelineViewportBudgets"; const IDLE_KEPT = resolveTimelineViewportBudgets({ sharedFrameCacheBytes: 0, thumbnailSourceIdleMs: 15_000, }); beforeAll(async () => { await import("mediabunny"); }); afterEach(() => { vi.useRealTimers(); vi.unstubAllGlobals(); vi.restoreAllMocks(); }); describe("decodeVideoThumbnail terminal failure delivery (real mediabunny)", () => { it("logs no terminal error when the sole job cancels as the final fetch failure settles", async () => { vi.spyOn(console, "warn").mockImplementation(() => {}); const errorLog = vi.spyOn(console, "error").mockImplementation(() => {}); const controller = new AbortController(); let attempts = 0; let rejectFinal!: (error: unknown) => void; vi.stubGlobal( "fetch", vi.fn(() => { attempts += 1; if (attempts < 3) return Promise.reject(new TypeError("Failed to fetch")); return new Promise((_, reject) => { rejectFinal = reject; }); }), ); vi.useFakeTimers(); const pending = decodeVideoThumbnail( { source: "http://127.0.0.1:5190/race-sole.mp4", frameCount: 1 }, controller.signal, IDLE_KEPT, ).catch((error: unknown) => error); await vi.advanceTimersByTimeAsync(5_000); expect(attempts).toBe(3); // Final fetch rejects and the sole job cancels as the rejection settles, before async release decrements users. rejectFinal(new TypeError("Failed to fetch")); await Promise.resolve(); await Promise.resolve(); controller.abort(new DOMException("Cancelled", "AbortError")); const result = await pending; expect(result).toMatchObject({ name: "AbortError" }); expect(errorLog).not.toHaveBeenCalled(); }); it("logs one terminal error when one consumer cancels on the final failure while another stays live", async () => { vi.spyOn(console, "warn").mockImplementation(() => {}); const errorLog = vi.spyOn(console, "error").mockImplementation(() => {}); const cancelled = new AbortController(); const live = new AbortController(); let attempts = 0; let rejectFinal!: (error: unknown) => void; vi.stubGlobal( "fetch", vi.fn(() => { attempts += 1; if (attempts < 3) return Promise.reject(new TypeError("Failed to fetch")); return new Promise((_, reject) => { rejectFinal = reject; }); }), ); vi.useFakeTimers(); const first = decodeVideoThumbnail( { source: "http://127.0.0.1:5190/race-shared.mp4", frameCount: 1 }, cancelled.signal, IDLE_KEPT, ).catch((error: unknown) => error); const second = decodeVideoThumbnail( { source: "http://127.0.0.1:5190/race-shared.mp4", frameCount: 1 }, live.signal, IDLE_KEPT, ).catch((error: unknown) => error); await vi.advanceTimersByTimeAsync(5_000); expect(attempts).toBe(3); rejectFinal(new TypeError("Failed to fetch")); await Promise.resolve(); await Promise.resolve(); cancelled.abort(new DOMException("Cancelled", "AbortError")); const [firstResult, secondResult] = await Promise.all([first, second]); expect(firstResult).toMatchObject({ name: "AbortError" }); expect(secondResult).toBeInstanceOf(MediaFetchError); expect(errorLog.mock.calls).toEqual([[secondResult]]); }); it("logs one terminal error when two live jobs share an exhausted open", async () => { vi.spyOn(console, "warn").mockImplementation(() => {}); const errorLog = vi.spyOn(console, "error").mockImplementation(() => {}); vi.stubGlobal("fetch", vi.fn().mockRejectedValue(new TypeError("Failed to fetch"))); vi.useFakeTimers(); const first = decodeVideoThumbnail( { source: "http://127.0.0.1:5190/two-live.mp4", frameCount: 1 }, new AbortController().signal, IDLE_KEPT, ).catch((error: unknown) => error); const second = decodeVideoThumbnail( { source: "http://127.0.0.1:5190/two-live.mp4", frameCount: 1 }, new AbortController().signal, IDLE_KEPT, ).catch((error: unknown) => error); await vi.advanceTimersByTimeAsync(5_000); const [firstResult, secondResult] = await Promise.all([first, second]); expect(firstResult).toBeInstanceOf(MediaFetchError); expect(secondResult).toBe(firstResult); expect(errorLog.mock.calls).toEqual([[firstResult]]); }); it("delivers an exhausted empty 206 as one closed MediaFetchError when live, and stays silent when cancelled", async () => { vi.spyOn(console, "warn").mockImplementation(() => {}); const errorLog = vi.spyOn(console, "error").mockImplementation(() => {}); const empty206 = () => new Response("", { status: 206, headers: { "Content-Range": "bytes 0-1023/1024", "Content-Length": "1024" }, }); vi.useFakeTimers(); vi.stubGlobal( "fetch", vi.fn(async () => empty206()), ); const liveRun = decodeVideoThumbnail( { source: "http://127.0.0.1:5190/empty-live.mp4", frameCount: 1 }, new AbortController().signal, IDLE_KEPT, ).catch((error: unknown) => error); await vi.advanceTimersByTimeAsync(5_000); await liveRun; expect(errorLog).toHaveBeenCalledTimes(1); expect(errorLog.mock.calls[0]?.[0]).toMatchObject({ name: "MediaFetchError", reader: "thumbnail", code: "ERR_EMPTY_RESPONSE", retries: 2, }); errorLog.mockClear(); const controller = new AbortController(); let attempts = 0; vi.stubGlobal( "fetch", vi.fn(async () => { attempts += 1; if (attempts === 3) controller.abort(new DOMException("Cancelled", "AbortError")); return empty206(); }), ); const cancelledRun = decodeVideoThumbnail( { source: "http://127.0.0.1:5190/empty-cancel.mp4", frameCount: 1 }, controller.signal, IDLE_KEPT, ).catch((error: unknown) => error); await vi.advanceTimersByTimeAsync(5_000); const cancelledResult = await cancelledRun; expect(cancelledResult).toMatchObject({ name: "AbortError" }); expect(errorLog).not.toHaveBeenCalled(); }); });