From e62b020ad619087a3abc5bc0209403703da81fdb Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Fri, 25 Sep 2026 22:41:36 +0900 Subject: [PATCH] test(http): cover KEYED retries with budget left through the V3 executor (10-03) Every KEYED fixture had retryBudget 0, so passing "SAFE" instead of the contract's retry semantics (M1b) survived. Two executor tests now pin that a KEYED send retries only before dispatch, with the same key. Co-Authored-By: Claude Opus 5.5 --- tests/unit/http-execution-v3.test.ts | 65 ++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) 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