|
| 1 | +import assert from "node:assert"; |
| 2 | +import requester from "../src/requester.js"; |
| 3 | + |
| 4 | +const identity = (t) => t; |
| 5 | + |
| 6 | +function mockFetch(failCount, { networkError = false } = {}) { |
| 7 | + let calls = 0; |
| 8 | + const originalFetch = globalThis.fetch; |
| 9 | + globalThis.fetch = async (_url) => { |
| 10 | + calls++; |
| 11 | + if (calls <= failCount) { |
| 12 | + if (networkError) throw new Error("Network error"); |
| 13 | + return { |
| 14 | + ok: false, |
| 15 | + status: 500, |
| 16 | + text: async () => "Internal Server Error", |
| 17 | + }; |
| 18 | + } |
| 19 | + return { ok: true, status: 200, text: async () => "success" }; |
| 20 | + }; |
| 21 | + return { |
| 22 | + get calls() { |
| 23 | + return calls; |
| 24 | + }, |
| 25 | + restore() { |
| 26 | + globalThis.fetch = originalFetch; |
| 27 | + }, |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +// Succeeds on first try without retries |
| 32 | +{ |
| 33 | + const mock = mockFetch(0); |
| 34 | + const result = await requester(identity, "http://example.com"); |
| 35 | + assert.strictEqual(result, "success"); |
| 36 | + assert.strictEqual(mock.calls, 1); |
| 37 | + mock.restore(); |
| 38 | + console.log("PASS: succeeds on first try"); |
| 39 | +} |
| 40 | + |
| 41 | +// Fails once, retries and succeeds |
| 42 | +{ |
| 43 | + const mock = mockFetch(1); |
| 44 | + const result = await requester(identity, "http://example.com", { |
| 45 | + retries: 2, |
| 46 | + retryDelay: 10, |
| 47 | + }); |
| 48 | + assert.strictEqual(result, "success"); |
| 49 | + assert.strictEqual(mock.calls, 2); |
| 50 | + mock.restore(); |
| 51 | + console.log("PASS: retries on failure and succeeds"); |
| 52 | +} |
| 53 | + |
| 54 | +// Exhausts all retries then throws |
| 55 | +{ |
| 56 | + const mock = mockFetch(5); |
| 57 | + await assert.rejects( |
| 58 | + () => |
| 59 | + requester(identity, "http://example.com", { retries: 2, retryDelay: 10 }), |
| 60 | + /Request failed/, |
| 61 | + ); |
| 62 | + assert.strictEqual(mock.calls, 3); |
| 63 | + mock.restore(); |
| 64 | + console.log("PASS: exhausts retries and throws"); |
| 65 | +} |
| 66 | + |
| 67 | +// Retries on network error |
| 68 | +{ |
| 69 | + const mock = mockFetch(1, { networkError: true }); |
| 70 | + const result = await requester(identity, "http://example.com", { |
| 71 | + retries: 1, |
| 72 | + retryDelay: 10, |
| 73 | + }); |
| 74 | + assert.strictEqual(result, "success"); |
| 75 | + assert.strictEqual(mock.calls, 2); |
| 76 | + mock.restore(); |
| 77 | + console.log("PASS: retries on network error"); |
| 78 | +} |
| 79 | + |
| 80 | +// Default retries is 0 — no automatic retry |
| 81 | +{ |
| 82 | + const mock = mockFetch(1); |
| 83 | + await assert.rejects( |
| 84 | + () => requester(identity, "http://example.com"), |
| 85 | + /Request failed/, |
| 86 | + ); |
| 87 | + assert.strictEqual(mock.calls, 1); |
| 88 | + mock.restore(); |
| 89 | + console.log("PASS: default retries is 0"); |
| 90 | +} |
| 91 | + |
| 92 | +// Exponential backoff increases delay between attempts |
| 93 | +{ |
| 94 | + const mock = mockFetch(4); |
| 95 | + const delays = []; |
| 96 | + const originalSetTimeout = globalThis.setTimeout; |
| 97 | + globalThis.setTimeout = (fn, ms) => { |
| 98 | + delays.push(ms); |
| 99 | + return originalSetTimeout(fn, 0); |
| 100 | + }; |
| 101 | + await assert.rejects( |
| 102 | + () => |
| 103 | + requester(identity, "http://example.com", { |
| 104 | + retries: 3, |
| 105 | + retryDelay: 100, |
| 106 | + }), |
| 107 | + /Request failed/, |
| 108 | + ); |
| 109 | + globalThis.setTimeout = originalSetTimeout; |
| 110 | + assert.deepStrictEqual(delays, [100, 200, 400]); |
| 111 | + mock.restore(); |
| 112 | + console.log("PASS: exponential backoff delays are correct"); |
| 113 | +} |
| 114 | + |
| 115 | +console.log("\nAll retry tests passed!"); |
0 commit comments