import { afterEach, beforeEach, expect, it, vi } from "vitest"; import { downloadHopperPackage } from "../../src/application/HopperDownload.js"; import { err, ok } from "../../src/domain/result.js"; const HOPPER_DOWNLOAD_STALL_MS = 30_000; const HOPPER_DOWNLOAD_TIMEOUT_MS = 15 * 60_000; const url = "https://www.hopperapp.com/downloader/public/Hopper-Linux-demo.rpm"; beforeEach(() => { vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] }); }); afterEach(() => { vi.useRealTimers(); vi.unstubAllGlobals(); }); // Like Fetch, error the body with the request signal's reason on abort. const stubPackage = (chunkEveryMs: number, chunks: number) => { const fetch = vi.fn(async (_input, options) => { const signal = options?.signal ?? undefined; let sent = 0; return new Response( new ReadableStream({ start(controller) { signal?.addEventListener( "abort", () => { controller.error(signal.reason); }, { once: true }, ); }, pull: (controller) => new Promise((resolve) => { if (sent === chunks) { controller.close(); resolve(); return; } setTimeout(() => { if (signal?.aborted !== true) { sent += 1; controller.enqueue(new Uint8Array(1024)); } resolve(); }, chunkEveryMs); }), }), ); }); vi.stubGlobal("fetch", fetch); return fetch; }; // Each chunk arrives just inside the stall bound; the download completes // well before the total bound (the trickle case below exceeds it). it("completes a slow download whose bytes keep arriving within the stall bound", async () => { const chunkEveryMs = HOPPER_DOWNLOAD_STALL_MS - 1; stubPackage(chunkEveryMs, 9); const pending = downloadHopperPackage(url); await vi.advanceTimersByTimeAsync(9 * chunkEveryMs); expect(await pending).toEqual(ok(Buffer.alloc(9 * 1024))); }); it("fails a download whose next bytes stall, naming the URL and the bytes received", async () => { const fetch = vi.fn(async (_input, options) => { const signal = options?.signal ?? undefined; return new Response( new ReadableStream({ start(controller) { controller.enqueue(new Uint8Array(1024)); signal?.addEventListener( "abort", () => { controller.error(signal.reason); }, { once: true }, ); }, }), ); }); vi.stubGlobal("fetch", fetch); const pending = downloadHopperPackage(url); await vi.advanceTimersByTimeAsync(HOPPER_DOWNLOAD_STALL_MS); expect(await pending).toEqual( err({ url, kind: "timeout", limit: "stalled", timeoutMs: HOPPER_DOWNLOAD_STALL_MS, receivedBytes: 1024, }), ); expect(fetch.mock.calls[0]?.[1]?.signal?.aborted).toBe(true); }); it("treats a response that never starts as stalled", async () => { vi.stubGlobal( "fetch", vi.fn( (_input, options) => new Promise((_resolve, reject) => { options?.signal?.addEventListener( "abort", () => { reject(options.signal?.reason); }, { once: true }, ); }), ), ); const pending = downloadHopperPackage(url); await vi.advanceTimersByTimeAsync(HOPPER_DOWNLOAD_STALL_MS); expect(await pending).toEqual( err({ url, kind: "timeout", limit: "stalled", timeoutMs: HOPPER_DOWNLOAD_STALL_MS, receivedBytes: 0, }), ); }); it("bounds a download that keeps trickling within the stall bound", async () => { stubPackage(20_000, Number.POSITIVE_INFINITY); const pending = downloadHopperPackage(url); await vi.advanceTimersByTimeAsync(HOPPER_DOWNLOAD_TIMEOUT_MS); expect(await pending).toEqual( err({ url, kind: "timeout", limit: "deadline", timeoutMs: HOPPER_DOWNLOAD_TIMEOUT_MS, receivedBytes: 44 * 1024, }), ); });