|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { readFileSync } from "node:fs"; |
| 3 | +import { join } from "node:path"; |
| 4 | + |
| 5 | +const source = (relative: string): string => |
| 6 | + readFileSync(join(import.meta.dir, "..", "src", ...relative.split("/")), "utf8"); |
| 7 | + |
| 8 | +/** |
| 9 | + * `transientRetryOn5xx.attempts` is ONE request-wide total-send budget, not a per-leg |
| 10 | + * allowance. A Responses request can reach upstream on several legs — the initial send, a |
| 11 | + * 429/account-rotation refetch, and the terminal-guard continuation — and each leg calls |
| 12 | + * `fetchWithTransientRetry` separately. The budget only holds if every leg draws from the |
| 13 | + * shared request-scoped counter. |
| 14 | + * |
| 15 | + * The continuation leg shipped on the raw policy value instead, so a request that reached it |
| 16 | + * received a fresh full `attempts` allowance: with `attempts: 3` an initial send that had |
| 17 | + * already spent its budget could still emit three more upstream sends. Runtime coverage in |
| 18 | + * `tests/upstream-transient-retry.test.ts` proves the helper reports and honors a remainder; |
| 19 | + * it cannot prove that every call site asks for one, because a site that forgets simply |
| 20 | + * passes a larger number. This asserts the wiring at the source, which is the only place the |
| 21 | + * omission is visible. |
| 22 | + */ |
| 23 | +describe("transient send budget stays request-scoped", () => { |
| 24 | + test("every transient-retry call site draws from the shared counter", () => { |
| 25 | + const core = source("server/responses/core.ts"); |
| 26 | + |
| 27 | + // One owner per request, declared before any leg can send. |
| 28 | + expect(core.match(/let transientSendsUsed = 0;/g)).toHaveLength(1); |
| 29 | + expect(core.match(/const remainingTransientSendBudget = \(budget: number\): number =>/g)).toHaveLength(1); |
| 30 | + |
| 31 | + // Initial send, 429/rotation refetch, and terminal-guard continuation: three legs, three |
| 32 | + // reports into the same counter. |
| 33 | + expect(core.match(/onSendsConsumed: noteTransientSends/g)).toHaveLength(3); |
| 34 | + |
| 35 | + // The refetch and continuation legs must ask for the REMAINDER. Only the initial send may |
| 36 | + // pass a policy value directly, because nothing has been spent yet. |
| 37 | + expect(core.match(/attempts: remainingTransientSendBudget\(/g)).toHaveLength(2); |
| 38 | + expect(core).toContain("attempts: remainingTransientSendBudget(refetchTransientPolicy.attempts)"); |
| 39 | + expect(core).toContain("attempts: remainingTransientSendBudget(continuationTransientPolicy.attempts)"); |
| 40 | + |
| 41 | + // The regressed shape: a leg handing itself a fresh full budget. |
| 42 | + expect(core).not.toContain("attempts: continuationTransientPolicy.attempts }"); |
| 43 | + expect(core).not.toContain("attempts: refetchTransientPolicy.attempts }"); |
| 44 | + }); |
| 45 | + |
| 46 | + test("the helper still exposes the seam those call sites depend on", () => { |
| 47 | + const retry = source("lib/upstream-retry.ts"); |
| 48 | + expect(retry).toContain("onSendsConsumed?: (sends: number) => void;"); |
| 49 | + // Reported in `finally` so every exit path — return, throw, abort — feeds the counter. |
| 50 | + expect(retry).toMatch(/} finally \{\n\s*opts\.onSendsConsumed\?\.\(sent\);/); |
| 51 | + }); |
| 52 | +}); |
0 commit comments