mirror of
https://github.com/MadsLorentzen/ai-job-search.git
synced 2026-09-17 16:46:24 +00:00
`detail` called fetch() directly rather than going through the CLI's own request wrappers, so it had none of the three things apiFetch/apiPost guarantee: no 429/5xx retry loop, a hand-inlined User-Agent that would drift from the exported USER_AGENT, and a timeout no wrapper test covered. A rate-limited detail page wrote API_ERROR and exited after ONE attempt; jobnet, jobbank, jobindex, linkedin, and freehire all retry up to six times on the same response. /scrape calls detail once per shortlisted posting, so a burst that tripped jobdanmark's limiter dropped those postings (no description, no deadline) while any other portal rode it out. Demonstrated by driving the real command handler with a stubbed 429 and instant timers: 1 fetch attempt and exit 1 before, 7 after (initial try plus six retries, the contract's schedule). Add htmlFetch to helpers.ts with the same backoff, timeout, and shared User-Agent as the JSON wrappers - 404 returns null so detail keeps its NOT_FOUND contract - and route detail through it. The retry-backoff, user-agent, and request-timeout suites now cover all three wrappers, and the new detail-backoff.test.ts exercises the handler path itself; its two retry cases fail against the bare fetch().
46 lines
1.7 KiB
TypeScript
46 lines
1.7 KiB
TypeScript
import { afterEach, describe, expect, test } from "bun:test";
|
|
import { apiFetch, apiPost, htmlFetch } from "../src/helpers";
|
|
|
|
// A stalled upstream connection (accepted socket, no response) would otherwise
|
|
// hang the CLI forever - fetch has no default timeout. Assert both request
|
|
// wrappers carry an AbortSignal timeout. Fails on the pre-fix code (no signal).
|
|
const originalFetch = globalThis.fetch;
|
|
afterEach(() => {
|
|
globalThis.fetch = originalFetch;
|
|
});
|
|
|
|
describe("request timeout", () => {
|
|
test("apiFetch passes an AbortSignal timeout to fetch", async () => {
|
|
let init: RequestInit | undefined;
|
|
globalThis.fetch = (async (_url: string | URL | Request, i?: RequestInit) => {
|
|
init = i;
|
|
return new Response("{}", { status: 200 });
|
|
}) as unknown as typeof fetch;
|
|
|
|
await apiFetch("/search");
|
|
expect(init?.signal).toBeInstanceOf(AbortSignal);
|
|
});
|
|
|
|
test("htmlFetch passes an AbortSignal timeout to fetch", async () => {
|
|
let init: RequestInit | undefined;
|
|
globalThis.fetch = (async (_url: string | URL | Request, i?: RequestInit) => {
|
|
init = i;
|
|
return new Response("<html></html>", { status: 200 });
|
|
}) as unknown as typeof fetch;
|
|
|
|
await htmlFetch("https://jobdanmark.dk/job/x");
|
|
expect(init?.signal).toBeInstanceOf(AbortSignal);
|
|
});
|
|
|
|
test("apiPost passes an AbortSignal timeout to fetch", async () => {
|
|
let init: RequestInit | undefined;
|
|
globalThis.fetch = (async (_url: string | URL | Request, i?: RequestInit) => {
|
|
init = i;
|
|
return new Response("{}", { status: 200 });
|
|
}) as unknown as typeof fetch;
|
|
|
|
await apiPost("/search", { q: "x" });
|
|
expect(init?.signal).toBeInstanceOf(AbortSignal);
|
|
});
|
|
});
|