import assert from "node:assert/strict"; import test from "node:test"; import { applyBidDelta, mergeAuctionState, shouldApplyIncomingAuctionState, shouldApplyPolledAuctionState, shouldResyncForBidDelta, } from "../src/api/auction-state.ts"; import type { ApiAuction } from "../src/api/types.ts"; const current = { auctionLifecycleId: "life", revision: 10, phase: "bidding", currentBidRoundId: "round-A", currentBidRoundPlayerUserId: "A", currentHighestBidPointAmount: 10, auctionBidHistory: [], bids: [], } as unknown as ApiAuction; const bid = { auctionLifecycleId: "life", revision: 11, currentBidRoundId: "round-A", currentHighestBidPointAmount: 20, currentHighestBidderTeamIndex: 1, auctionBidHistoryEntry: { bidPointAmount: 20, bidderTeamIndex: 1, bidderUserId: "captain", bidPlacedAtTimestampMS: 1000, playerUserId: "A", bidRoundId: "round-A", }, }; test("a contiguous same-round bid updates price and preserves player identity", () => { const next = applyBidDelta(current, bid)!; assert.equal(shouldResyncForBidDelta(current, bid), false); assert.equal(next.currentHighestBidPointAmount, 20); assert.equal(next.currentBidRoundPlayerUserId, "A"); assert.equal(next.auctionBidHistory?.[0].bidRoundId, "round-A"); }); test("a revision gap resyncs instead of attaching a new player bid to the old structure", () => { const missedTransition = { ...bid, revision: 13, currentBidRoundId: "round-B", }; assert.equal(shouldResyncForBidDelta(current, missedTransition), true); assert.equal(applyBidDelta(current, missedTransition), current); }); test("a delta cannot open bidding or change the round by itself", () => { assert.equal( shouldResyncForBidDelta({ ...current, phase: "presenting" }, bid), true, ); assert.equal( shouldResyncForBidDelta(current, { ...bid, currentBidRoundId: "round-B" }), true, ); }); test("a full snapshot can repair the same revision but cannot regress auction revision", () => { assert.equal( shouldApplyIncomingAuctionState(current, { ...current, currentBidRoundPlayerUserId: "B", }), true, ); assert.equal( shouldApplyIncomingAuctionState(current, { ...current, revision: 9 }), false, ); }); test("full state repair preserves a newer independently versioned side effect", () => { const live = { ...current, matchSideEffectRevision: 3, matchSideEffectStatus: "completed" as const, }; const incoming = { ...current, matchSideEffectRevision: 1, matchSideEffectStatus: "pending" as const, }; assert.equal( mergeAuctionState(live, incoming).matchSideEffectStatus, "completed", ); assert.equal( mergeAuctionState(live, { ...incoming, auctionLifecycleId: "new-life" }) .matchSideEffectStatus, "pending", ); }); test("replayed old bids do not resync or change the current state", () => { const replay = { ...bid, revision: 9 }; assert.equal(shouldResyncForBidDelta(current, replay), false); assert.equal(applyBidDelta(current, replay), current); }); test("polled state repairs a socket that missed a transition", () => { const polled = { ...current, matchId: "match", revision: 12, currentBidRoundId: "round-B", currentBidRoundPlayerUserId: "B", currentHighestBidPointAmount: 0, } as unknown as ApiAuction; const socketState = { ...current, matchId: "match" } as ApiAuction; assert.equal( shouldApplyPolledAuctionState(socketState, polled, "match"), true, ); }); test("polled state never regresses or replaces the socket lifecycle", () => { const socketState = { ...current, matchId: "match" } as ApiAuction; const same = { ...socketState } as ApiAuction; const older = { ...socketState, revision: 9 } as ApiAuction; const otherLifecycle = { ...socketState, auctionLifecycleId: "old-life", revision: 99, } as ApiAuction; const otherMatch = { ...socketState, matchId: "other", revision: 11 }; assert.equal(shouldApplyPolledAuctionState(socketState, same, "match"), true); assert.equal( shouldApplyPolledAuctionState(socketState, older, "match"), false, ); assert.equal( shouldApplyPolledAuctionState(socketState, otherLifecycle, "match"), false, ); assert.equal( shouldApplyPolledAuctionState(socketState, otherMatch, "match"), false, ); assert.equal(shouldApplyPolledAuctionState(null, same, "match"), true); }); test("polling initializes a missing socket baseline but rejects another match or missing identity", () => { const incoming = { ...current, matchId: "match", currentBidRoundId: "round-B", currentHighestBidPointAmount: 0, } as ApiAuction; assert.equal(shouldApplyPolledAuctionState(null, incoming, "match"), true); assert.equal( shouldApplyPolledAuctionState(null, incoming, "another-match"), false, ); assert.equal( shouldApplyPolledAuctionState( null, { ...incoming, auctionLifecycleId: undefined }, "match", ), false, ); }); test("a prior side-effect generation cannot replace the status of a new transition", () => { const old = { ...current, matchSideEffectTransitionId: "old", matchSideEffectRevision: 100, matchSideEffectStatus: "failed" as const, }; const next = { ...current, revision: 12, matchSideEffectTransitionId: "new", matchSideEffectRevision: 3, matchSideEffectStatus: "pending" as const, }; assert.equal(mergeAuctionState(old, next).matchSideEffectStatus, "pending"); });