Files
reverse-engineering-anything/tests/process-global/hopperDownloadDeadline.test.ts
T
morluto e3eb4c1749 test: state the current contract in tests named after removed limits (#11588)
Eleven tests were named "beyond the former ..." and pinned a value just above
a limit that no longer exists. Each now states and checks the current
contract:

- jsonShape: the caller's byte allowance is the only bound; a shape that fits
  exactly is complete and one byte less is a capacity refusal.
- Hopper download: chunks arriving just inside the stall bound complete (the
  trickle case already covers the total bound).
- Firmware: a selected analysis_timeout_ms above the default governs the owned
  command, expressed from the default constant.
- Provider process output, CDP resources/timeline/WebMCP frames, Playwright
  steps/artifacts/Electron waits, scenario diff and Bun ELF names: renamed to
  the unbounded-count or exact-selection contract they check, with a note on
  what bound (if any) still applies and where it is covered.

The six JavaScript analyzer tests with the same naming are left to their
workstream.

Refs #10515
2026-11-16 07:56:52 +08:00

142 lines
4.1 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;
};
// 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<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,
}),
);
});