Files
reverse-engineering-anything/tests/process-global/hopperDownloadDeadline.test.ts
T

139 lines
3.9 KiB
TypeScript

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<typeof globalThis.fetch>(async (_input, options) => {
const signal = options?.signal ?? undefined;
let sent = 0;
return new Response(
new ReadableStream<Uint8Array>({
start(controller) {
signal?.addEventListener(
"abort",
() => {
controller.error(signal.reason);
},
{ once: true },
);
},
pull: (controller) =>
new Promise<void>((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;
};
it("completes a slow download that keeps delivering bytes past the former 120 s deadline", async () => {
stubPackage(20_000, 9);
const pending = downloadHopperPackage(url);
await vi.advanceTimersByTimeAsync(9 * 20_000);
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<typeof globalThis.fetch>(async (_input, options) => {
const signal = options?.signal ?? undefined;
return new Response(
new ReadableStream<Uint8Array>({
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<typeof globalThis.fetch>(
(_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,
}),
);
});