diff --git a/tests/unit/http-execution-v3.test.ts b/tests/unit/http-execution-v3.test.ts index e9b1ec8b..ee085b00 100644 --- a/tests/unit/http-execution-v3.test.ts +++ b/tests/unit/http-execution-v3.test.ts @@ -571,6 +571,54 @@ describe("descriptor-driven HTTP execution lifetime", () => { }); }); + // 0929/10-04. ยง10.8's last check: a valid success body for a scope that went + // stale while the request was out is discarded. The scope here only stops + // being current; its signal never aborts, so no earlier cancellation path + // answers first (the test above aborts it and never reaches this check). + it.each([ + ["read", "NOT_APPLICABLE"], + ["command", "MAYBE_APPLIED"], + ] as const)( + "discards a valid %s success that arrives after its scope stopped being current", + async (kind, effect) => { + let current = true; + const staleScope = Object.freeze({ + ...scope, + signal: new AbortController().signal, + isCurrent: () => current, + }); + const executor = createContractHttpExecutor({ + baseUrl: "https://api.example/", + maxRetryAttempts: 0, + attachCredentials: () => ({ kind: "READY", headers: {} }), + fetcher: vi.fn(async () => { + current = false; + return kind === "read" + ? Response.json([{ id: "a", name: "A" }]) + : Response.json({ id: "created", name: "Created" }, { status: 201 }); + }), + }); + + const outcome = + kind === "read" + ? await executor.execute(installed, { limit: 20 }, { + routeId: ROUTE_ID, + scope: staleScope, + }) + : await executor.execute(createInstalled, { name: "created" }, { + routeId: ROUTE_ID, + scope: staleScope, + intent: mutationIntent({ intentId: "intent-3", idempotencyKey: "key-3" }), + }); + + expect(outcome).toMatchObject({ + kind: "CONTRACT_VIOLATION", + violation: { kind: "SCOPE_FENCED" }, + effect, + }); + }, + ); + it("keeps a dispatched command MAYBE_APPLIED when a retry-time fence lands between scope checks", async () => { // Attempt 1 dispatches an idempotent command and receives 429. The retry // sleep resolves, the loop-entry scope check is still current, and only the