diff --git a/tests/unit/http-execution-v3.test.ts b/tests/unit/http-execution-v3.test.ts index d0e80d37..aca57445 100644 --- a/tests/unit/http-execution-v3.test.ts +++ b/tests/unit/http-execution-v3.test.ts @@ -992,6 +992,71 @@ describe("terminal owner classification at every wait", () => { ); }); +/** + * 10-03. The executor, not only the pure rule, must read the contract's retry + * semantics: a KEYED command with budget left retries a send that never left, + * and never replays one that was dispatched. + */ +describe("KEYED retry budget through the executor", () => { + const keyedWithBudget: InstalledHttpContract = { + ...createInstalled, + frontend: { ...createInstalled.frontend, retryBudget: 2 as const }, + }; + + function keyedExecutor(fetcher: typeof fetch) { + return createContractHttpExecutor({ + baseUrl: "https://api.example/", + maxRetryAttempts: 2, + attachCredentials: () => ({ kind: "READY", headers: {} }), + fetcher, + sleep: async () => {}, + random: () => 0, + }); + } + + it("retries a KEYED send that failed before dispatch, with the same key", async () => { + const keys: (string | null)[] = []; + const fetcher = vi.fn((_input, init) => { + keys.push(new Headers(init?.headers).get("idempotency-key")); + if (keys.length === 1) throw new TypeError("fetch refused to start"); + return Promise.resolve( + Response.json({ id: "created", name: "created" }, { status: 201 }), + ); + }); + + const outcome = await keyedExecutor(fetcher).execute( + keyedWithBudget, + { name: "created" }, + { routeId: ROUTE_ID, scope, intent: mutationIntent() }, + ); + + expect(fetcher).toHaveBeenCalledTimes(2); + expect(keys).toEqual(["key-1", "key-1"]); + expect(outcome).toMatchObject({ + kind: "SUCCESS", + effect: "APPLIED_CONFIRMED", + }); + }); + + it("does not replay a dispatched KEYED send even with budget left", async () => { + const fetcher = vi.fn(async () => { + throw new TypeError("connection reset"); + }); + + const outcome = await keyedExecutor(fetcher).execute( + keyedWithBudget, + { name: "created" }, + { routeId: ROUTE_ID, scope, intent: mutationIntent() }, + ); + + expect(fetcher).toHaveBeenCalledOnce(); + expect(outcome).toMatchObject({ + kind: "TRANSPORT_FAILURE", + effect: "MAYBE_APPLIED", + }); + }); +}); + /** * 03-03. A support reference must reach the server log: every logical * execution sends one request id, and a failure carries the server's echoed