import { describe, expect, it } from "vitest"; import { BOUNDED_POLLING_CEILINGS, assertBoundedPollOperation, definePollLeasePolicy, type BoundedPollOperation, } from "../../../src/application/policies/bounded-polling.ts"; import { TEST_LIST_HTTP_CONTRACT } from "../../helpers/external-contract-fixture.ts"; const input = { operationId: "GET_JOB_STATUS", owner: "reference-job", minimumIntervalMs: 5_000, successIntervalMs: 10_000, maxIntervalMs: 60_000, maxAttempts: 5, maxElapsedMs: 120_000, maxResponseBytes: 16_384, visibility: "VISIBLE_ONLY", fallbackReason: "CONVERGENCE", terminalStates: ["COMPLETED", "FAILED"], } as const; describe("bounded polling policy", () => { it("copies and freezes a finite immutable lease", () => { const terminalStates = ["COMPLETED", "FAILED"]; const policy = definePollLeasePolicy({ ...input, terminalStates, }); terminalStates.push("CANCELLED"); expect(policy.terminalStates).toEqual(["COMPLETED", "FAILED"]); expect(Object.isFrozen(policy)).toBe(true); expect(Object.isFrozen(policy.terminalStates)).toBe(true); }); it("rejects push-like cadence and unbounded convergence", () => { expect(() => definePollLeasePolicy({ ...input, minimumIntervalMs: 4_999, }), ).toThrow(TypeError); expect(() => definePollLeasePolicy({ ...input, terminalStates: [], }), ).toThrow(TypeError); expect(() => definePollLeasePolicy({ ...input, maxAttempts: 121, }), ).toThrow(TypeError); }); // 1103/04-01. The V3 executor retries a SAFE or IDEMPOTENT read // `min(retryBudget, maxRetryAttempts, 2)` times, reading the budget from the // descriptor it executes. A poll binds that descriptor, not a declaration. function pollOperation( contract: Partial = {}, frontend: Partial = {}, ): BoundedPollOperation { return { contract: { ...TEST_LIST_HTTP_CONTRACT.contract, operationId: "GET_JOB_STATUS", ...contract, }, frontend: { ...TEST_LIST_HTTP_CONTRACT.frontend, retryBudget: 0, responseByteLimit: 16_384, ...frontend, }, } as BoundedPollOperation; } it("admits a replay-safe read whose descriptor turns transport retry off", () => { const policy = definePollLeasePolicy(input); expect(() => assertBoundedPollOperation(policy, pollOperation()), ).not.toThrow(); expect(() => assertBoundedPollOperation( policy, pollOperation({ method: "HEAD", retrySemantics: "IDEMPOTENT" }), ), ).not.toThrow(); expect(() => assertBoundedPollOperation( policy, pollOperation({}, { responseByteLimit: 8_192 }), ), ).not.toThrow(); }); it.each([ ["another operation", { operationId: "GET_OTHER_STATUS" }, {}], ["one transport retry", {}, { retryBudget: 1 as const }], ["two transport retries", {}, { retryBudget: 2 as const }], ["a write method", { method: "POST" as const }, {}], ["keyed retry semantics", { retrySemantics: "KEYED" as const }, {}], ["never retry semantics", { retrySemantics: "NEVER" as const }, {}], ["an empty response limit", {}, { responseByteLimit: 0 }], [ "a response limit over the poll ceiling", {}, { responseByteLimit: BOUNDED_POLLING_CEILINGS.maxResponseBytes + 1 }, ], ])("refuses a descriptor with %s", (_case, contract, frontend) => { const policy = definePollLeasePolicy(input); expect(() => assertBoundedPollOperation(policy, pollOperation(contract, frontend)), ).toThrow(TypeError); }); });