|
| 1 | +/** |
| 2 | + * The client timeout must not masquerade as backend unreachability (ACT-1605). |
| 3 | + * |
| 4 | + * The CLI used to abort at 90s — exactly the gateway's own request budget — and |
| 5 | + * report every abort as BACKEND_UNAVAILABLE ("Could not reach the Zenrows |
| 6 | + * API"). Both halves were wrong: the API had been reached, and it was about to |
| 7 | + * return a specific error. These tests pin the two halves of the fix. |
| 8 | + */ |
| 9 | +import { test } from "node:test"; |
| 10 | +import assert from "node:assert/strict"; |
| 11 | +import { |
| 12 | + scrape, |
| 13 | + DEFAULT_TIMEOUT_MS, |
| 14 | + SERVER_BUDGET_MS, |
| 15 | +} from "../src/core/http.ts"; |
| 16 | +import { buildParams, runFetch, type FetchOptions } from "../src/adapters/protected-fetch.ts"; |
| 17 | +import { normalizeTimeout } from "../src/cli/commands/fetch.ts"; |
| 18 | +import { defaultConfig } from "../src/core/config.ts"; |
| 19 | +import { defaultPolicy } from "../src/core/policy.ts"; |
| 20 | +import { ToolkitError } from "../src/core/errors.ts"; |
| 21 | + |
| 22 | +/** |
| 23 | + * A fetch that never answers and rejects only when the caller's own timer |
| 24 | + * aborts it — exactly what undici does when an AbortController fires |
| 25 | + * mid-request. This is the real failure the ticket reproduced, simulated. |
| 26 | + */ |
| 27 | +function hangingFetch(): typeof fetch { |
| 28 | + return ((_input: unknown, init?: { signal?: AbortSignal }) => |
| 29 | + new Promise<Response>((_resolve, reject) => { |
| 30 | + init?.signal?.addEventListener("abort", () => { |
| 31 | + reject(new DOMException("This operation was aborted", "AbortError")); |
| 32 | + }); |
| 33 | + })) as unknown as typeof fetch; |
| 34 | +} |
| 35 | + |
| 36 | +function withFetchImpl(impl: typeof fetch, fn: () => Promise<void>): Promise<void> { |
| 37 | + const orig = globalThis.fetch; |
| 38 | + globalThis.fetch = impl; |
| 39 | + return fn().finally(() => { |
| 40 | + globalThis.fetch = orig; |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +// Encodes: "Client timeout raised above the server budget." |
| 45 | +test("the default client timeout is above the API's own request budget", () => { |
| 46 | + assert.ok( |
| 47 | + DEFAULT_TIMEOUT_MS > SERVER_BUDGET_MS, |
| 48 | + `client default (${DEFAULT_TIMEOUT_MS}ms) must outlive the server budget (${SERVER_BUDGET_MS}ms), ` + |
| 49 | + "otherwise the abort races the API's own error envelope", |
| 50 | + ); |
| 51 | +}); |
| 52 | + |
| 53 | +// Encodes: "Test covering a simulated abort asserting it is not BACKEND_UNAVAILABLE." |
| 54 | +test("a client-side timeout is REQUEST_TIMEOUT, never BACKEND_UNAVAILABLE", async () => { |
| 55 | + await withFetchImpl(hangingFetch(), async () => { |
| 56 | + await assert.rejects( |
| 57 | + () => scrape("https://api.zenrows.com/v1/", "k", { url: "https://x" }, { timeoutMs: 20 }), |
| 58 | + (err: unknown) => { |
| 59 | + assert.ok(err instanceof ToolkitError); |
| 60 | + assert.notEqual(err.code, "BACKEND_UNAVAILABLE", "our own abort is not unreachability"); |
| 61 | + assert.equal(err.code, "REQUEST_TIMEOUT"); |
| 62 | + // The old message claimed the API was never reached. It was. |
| 63 | + assert.doesNotMatch(err.message, /could not reach/i); |
| 64 | + assert.doesNotMatch(err.next_action, /check connectivity/i); |
| 65 | + return true; |
| 66 | + }, |
| 67 | + ); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +// Encodes: "Include the elapsed time in the error so the 90s boundary is visible." |
| 72 | +test("REQUEST_TIMEOUT names the elapsed time and how to raise the timeout", async () => { |
| 73 | + await withFetchImpl(hangingFetch(), async () => { |
| 74 | + await assert.rejects( |
| 75 | + () => scrape("https://api.zenrows.com/v1/", "k", { url: "https://x" }, { timeoutMs: 20 }), |
| 76 | + (err: unknown) => { |
| 77 | + const e = err as ToolkitError; |
| 78 | + assert.match(e.message, /stopped waiting after [\d.]+s/i); |
| 79 | + assert.match(e.likely_cause, /aborted client-side after [\d.]+s/i); |
| 80 | + assert.match(e.next_action, /--timeout \d+/); |
| 81 | + assert.ok( |
| 82 | + e.suggested_commands.some((c) => /--timeout \d+/.test(c)), |
| 83 | + "must hand the operator a runnable retry with a higher timeout", |
| 84 | + ); |
| 85 | + return true; |
| 86 | + }, |
| 87 | + ); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +test("a genuine transport failure is still BACKEND_UNAVAILABLE, with the elapsed time", async () => { |
| 92 | + const failing = (() => Promise.reject(new TypeError("fetch failed"))) as unknown as typeof fetch; |
| 93 | + await withFetchImpl(failing, async () => { |
| 94 | + await assert.rejects( |
| 95 | + () => scrape("https://api.zenrows.com/v1/", "k", { url: "https://x" }), |
| 96 | + (err: unknown) => { |
| 97 | + const e = err as ToolkitError; |
| 98 | + assert.equal(e.code, "BACKEND_UNAVAILABLE"); |
| 99 | + assert.match(e.likely_cause, /fetch failed/); |
| 100 | + assert.match(e.likely_cause, /after [\d.]+s/); |
| 101 | + return true; |
| 102 | + }, |
| 103 | + ); |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +test("runFetch threads --timeout through to the HTTP client", async () => { |
| 108 | + await withFetchImpl(hangingFetch(), async () => { |
| 109 | + await assert.rejects( |
| 110 | + () => |
| 111 | + runFetch( |
| 112 | + { url: "https://x.com", timeoutMs: 20 }, |
| 113 | + defaultConfig(), |
| 114 | + defaultPolicy(), |
| 115 | + "k", |
| 116 | + ), |
| 117 | + (err: unknown) => (err as ToolkitError).code === "REQUEST_TIMEOUT", |
| 118 | + ); |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +test("timeoutMs is a client concern and never leaks into the API query string", () => { |
| 123 | + const opts: FetchOptions = { url: "https://x.com", timeoutMs: 150_000 }; |
| 124 | + const params = buildParams(opts, defaultConfig()); |
| 125 | + assert.equal(params.timeout, undefined); |
| 126 | + assert.equal(params.timeoutMs, undefined); |
| 127 | +}); |
| 128 | + |
| 129 | +test("--timeout accepts milliseconds and defaults when absent", () => { |
| 130 | + assert.equal(normalizeTimeout("180000"), 180_000); |
| 131 | + assert.equal(normalizeTimeout(undefined), undefined); |
| 132 | +}); |
| 133 | + |
| 134 | +test("--timeout rejects a non-numeric or non-positive value instead of silently ignoring it", () => { |
| 135 | + for (const bad of ["fast", "0", "-1", ""]) { |
| 136 | + assert.throws( |
| 137 | + () => normalizeTimeout(bad), |
| 138 | + (e: unknown) => e instanceof ToolkitError && e.code === "INVALID_USAGE", |
| 139 | + `--timeout ${JSON.stringify(bad)} must fail loudly`, |
| 140 | + ); |
| 141 | + } |
| 142 | +}); |
0 commit comments