fix(auction): harden lifecycle and durable bid storage

Serialize lifecycle changes and make cancellation, deadlines, and match side effects recoverable and idempotent.

Archive accepted bids durably, quarantine invalid payloads, and preserve compatibility with empty round IDs. Synchronize HTTP polling and socket state with scoped resync events.

Add cancellation cleanup index migration and regression coverage. Validated with 744 API tests, 65 Redis integration tests, and 28 Mongo integration tests.
This commit is contained in:
dahyeon committed 2026-11-09 23:24:11 +09:00
1 parent f2eec479d9
commit f649490cd2
55 files changed
+5374 -522

No files matched your search

@@ -7,6 +7,26 @@ import {
const rosterMessage = "팀 배정 상태를 확인해야 해 경매를 완료하지 못했습니다.";
test("asks an outdated client to reload instead of claiming the player advanced", () => {
const message =
"경매 화면이 업데이트되었습니다. 새로고침한 뒤 다시 입찰해 주세요.";
assert.equal(
resolveAuctionRejectionMessage({
auctionRejectionReason: "BID_REJECTED_BECAUSE_CLIENT_UPDATE_REQUIRED",
message,
}),
message,
);
// An older bundle without the new reason mapping still displays the server message.
assert.equal(
resolveAuctionRejectionMessage({
auctionRejectionReason: "UNRECOGNIZED_NEW_CLIENT_REASON",
message,
}),
message,
);
});
test("shows the common error message when roster validation blocks completion", () => {
assert.equal(
resolveAuctionRejectionMessage({
+95
View File
@@ -0,0 +1,95 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
createAuctionResyncRequester,
type AuctionResyncReply,
} from '../src/api/auction-resync.ts';
import type { ApiAuction } from '../src/api/types.ts';
const state = (revision: number) =>
({ revision, auctionLifecycleId: 'life', phase: 'bidding' }) as ApiAuction;
const noWait = async () => {};
test('coalesces simultaneous gaps into one snapshot request', async () => {
let finish!: (reply: AuctionResyncReply) => void;
let calls = 0;
const received: number[] = [];
const sync = createAuctionResyncRequester(
() => {
calls++;
return new Promise((resolve) => {
finish = resolve;
});
},
(value) => received.push(value.revision!),
noWait,
);
const requests = Array.from({ length: 100 }, () => sync.request(12, 'life'));
assert.equal(calls, 1);
finish({ ok: true, state: state(12) });
await Promise.all(requests);
assert.deepEqual(received, [12]);
});
test('fetches again when a newer gap arrived while the snapshot was in flight', async () => {
let finish!: (reply: AuctionResyncReply) => void;
let calls = 0;
const received: number[] = [];
const sync = createAuctionResyncRequester(
() =>
++calls === 1
? new Promise((resolve) => {
finish = resolve;
})
: Promise.resolve({ ok: true, state: state(14) }),
(value) => received.push(value.revision!),
noWait,
);
const pending = sync.request(12, 'life');
void sync.request(14, 'life');
void sync.request(); // A generic refresh cannot lower the required revision.
finish({ ok: true, state: state(12) });
await pending;
assert.deepEqual(received, [12, 14]);
assert.equal(calls, 2);
});
test('bounds retries and honors retry delay without rejoining the room', async () => {
let calls = 0;
const waits: number[] = [];
const sync = createAuctionResyncRequester(
async () => {
calls++;
return { ok: false, retryAfterMS: 1500 };
},
() => assert.fail('must not apply'),
async (ms) => {
waits.push(ms);
},
);
await sync.request();
assert.equal(calls, 3);
assert.deepEqual(waits, [1500, 1500]);
});
test('drops an old connection reply after cancellation and permits a new request', async () => {
let finish!: (reply: AuctionResyncReply) => void;
let calls = 0;
const received: number[] = [];
const sync = createAuctionResyncRequester(
() =>
++calls === 1
? new Promise((resolve) => {
finish = resolve;
})
: Promise.resolve({ ok: true, state: state(20) }),
(value) => received.push(value.revision!),
noWait,
);
const stale = sync.request();
sync.cancel();
await sync.request();
finish({ ok: true, state: state(10) });
await stale;
assert.deepEqual(received, [20]);
});
+188
View File
@@ -0,0 +1,188 @@
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");
});