import assert from "node:assert/strict"; import test from "node:test"; import { resolveAuctionActionRejection, shouldRetryAuctionAction, } from "../src/api/auction-action-retry.ts"; const request = { clientObservedAuctionLifecycleId: "auction-1", clientObservedCurrentAuctionPlayerOrderIndex: 11, clientObservedCurrentBidRoundPlayerUserId: "player-11", clientObservedPhase: "sold", clientObservedRevision: 130, }; const state = { auctionLifecycleId: "auction-1", currentAuctionPlayerOrderIndex: 11, currentBidRoundPlayerUserId: "player-11", phase: "sold" as const, revision: 131, }; const input = { action: "next", request, serverState: state, currentState: state, resyncRecommended: true, retryAttempt: 0, }; test("a delayed old-lifecycle rejection cannot roll back the screen and retry", () => { const currentState = { ...state, auctionLifecycleId: "auction-2", revision: 1, }; const decision = resolveAuctionActionRejection({ ...input, currentState }); assert.deepEqual(decision, { applyServerState: false, retry: false }); }); test("an older forced-resync rejection cannot roll back an already advanced player", () => { const serverState = { ...state, shouldForceAuctionStateResync: true }; const currentState = { ...state, revision: 132, currentAuctionPlayerOrderIndex: 12, }; assert.deepEqual( resolveAuctionActionRejection({ ...input, serverState, currentState }), { applyServerState: false, retry: false, }, ); }); test("a fresh rejection updates the screen to the new player without retrying", () => { const serverState = { ...state, currentAuctionPlayerOrderIndex: 12, phase: "presenting" as const, }; assert.deepEqual( resolveAuctionActionRejection({ ...input, serverState, currentState: { ...state, revision: 130 }, }), { applyServerState: true, retry: false }, ); }); test("a same-target revision update is applied and permits one retry", () => { assert.deepEqual( resolveAuctionActionRejection({ ...input, currentState: { ...state, revision: 130 }, }), { applyServerState: true, retry: true }, ); }); test("retries a revision conflict only while the original action target is unchanged", () => { assert.equal(shouldRetryAuctionAction(input), true); }); test("does not retry next for the following player after a conflict", () => { const nextPlayer = { ...state, currentAuctionPlayerOrderIndex: 12, currentBidRoundPlayerUserId: "player-12", phase: "presenting" as const, }; assert.equal( shouldRetryAuctionAction({ ...input, serverState: nextPlayer, currentState: nextPlayer, }), false, ); }); for (const patch of [ { auctionLifecycleId: "auction-2" }, { currentAuctionPlayerOrderIndex: 12 }, { currentBidRoundPlayerUserId: "player-12" }, { phase: "passed" as const }, ]) { test(`does not retarget a retry when ${Object.keys(patch)[0]} changes`, () => { assert.equal( shouldRetryAuctionAction({ ...input, serverState: { ...state, ...patch }, }), false, ); }); } test("does not retry from an older acknowledgement after the live screen has advanced", () => { assert.equal( shouldRetryAuctionAction({ ...input, currentState: { ...state, currentAuctionPlayerOrderIndex: 12 }, }), false, ); }); test("does not retry with missing original target identity", () => { for (const field of Object.keys(request)) { assert.equal( shouldRetryAuctionAction({ ...input, request: { ...request, [field]: undefined }, }), false, ); } }); test("does not retry without a current state, a server state, or a retry recommendation", () => { assert.equal( shouldRetryAuctionAction({ ...input, currentState: null }), false, ); assert.equal( shouldRetryAuctionAction({ ...input, serverState: undefined }), false, ); assert.equal( shouldRetryAuctionAction({ ...input, resyncRecommended: false }), false, ); assert.equal(shouldRetryAuctionAction({ ...input, retryAttempt: 1 }), false); }); test("does not retry next in presenting even if the target is unchanged", () => { assert.equal( shouldRetryAuctionAction({ ...input, request: { ...request, clientObservedPhase: "presenting" }, serverState: { ...state, phase: "presenting" }, currentState: { ...state, phase: "presenting" }, }), false, ); }); test("does not retry using a revision older than the original request", () => { assert.equal( shouldRetryAuctionAction({ ...input, serverState: { ...state, revision: 129 }, }), false, ); }); for (const [action, phase] of [ ["open-bidding", "presenting"], ["sold", "bidding"], ["passed", "bidding"], ["next", "passed"], ] as const) { test(`allows a same-target ${action} retry in ${phase}`, () => { assert.equal( shouldRetryAuctionAction({ ...input, action, request: { ...request, clientObservedPhase: phase }, serverState: { ...state, phase }, currentState: { ...state, phase }, }), true, ); }); }