test(ocr): make the retry-abort deadline test deterministic (#542)

Defer the overall deadline until the first fetch is dispatched (the deferred-deadline pattern the sibling 20ms test already uses), so a loaded CI runner cannot abort before fetch and randomly fail Unit Tests.
This commit is contained in:
SnapOtter
2026-07-16 23:31:53 +08:00
committed by GitHub
parent 4ac89fe650
commit 4448da9027
@@ -1866,17 +1866,33 @@ describe("downloadVerifiedRuntimeRelease", () => {
temporaryDirectories.push(directory);
const nativeSetTimeout = globalThis.setTimeout;
const retryTimers: ReturnType<typeof setTimeout>[] = [];
let startDeadline: (() => void) | undefined;
vi.spyOn(globalThis, "setTimeout").mockImplementation(((handler, delay, ...args) => {
// Defer the overall 100ms deadline until the first fetch is dispatched, so a
// loaded CI runner cannot fire it before fetch is even called (the source of
// this test's flakiness). Mirrors the deferred-deadline trick used above.
if (delay === 100 && !startDeadline) {
const placeholder = nativeSetTimeout(() => {}, 60_000);
startDeadline = () => {
clearTimeout(placeholder);
nativeSetTimeout(handler, delay, ...args);
};
return placeholder;
}
const timer = nativeSetTimeout(handler, delay, ...args);
if (delay === 500) retryTimers.push(timer);
return timer;
}) as typeof setTimeout);
const clearTimeoutSpy = vi.spyOn(globalThis, "clearTimeout");
const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue(
new Response("temporary outage", {
status: 503,
}),
);
let armed = false;
const fetchImpl = vi.fn<typeof fetch>().mockImplementation(async () => {
if (!armed) {
if (!startDeadline) throw new Error("overall deadline was not registered before fetch");
startDeadline();
armed = true;
}
return new Response("temporary outage", { status: 503 });
});
await expect(
downloadVerifiedRuntimeRelease({