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

@@ -1,4 +1,5 @@
import mongoose from 'mongoose';
import { ServiceUnavailableException } from '@nestjs/common';
jest.mock('../auth/jwt.service', () => ({
JwtService: class JwtService {},
@@ -7,6 +8,76 @@ jest.mock('../auth/jwt.service', () => ({
import { AuctionGateway } from './auction.gateway';
describe('AuctionGateway', () => {
it('signals pending initial state so the client can retry privately after recovery', async () => {
const matchId = new mongoose.Types.ObjectId().toString();
const gateway = new AuctionGateway(
{
getByMatchId: async () => ({}),
getStateByMatchId: async () => {
throw new ServiceUnavailableException({
code: 'AUCTION_STATE_RECOVERING',
message: 'recovering',
});
},
} as never,
{} as never,
{} as never,
{} as never,
{} as never,
{} as never,
{} as never,
);
const socket = { data: { matchId }, join: jest.fn(), emit: jest.fn() };
await gateway.handleJoin(socket as never, { matchId });
expect(socket.emit).toHaveBeenCalledTimes(1);
expect(socket.emit).toHaveBeenCalledWith('state-pending', {
retryAfterMS: 500,
});
});
it('sends join and resync snapshots only to the requesting socket and coalesces in-flight resyncs', async () => {
const matchId = new mongoose.Types.ObjectId().toString();
const getStateByMatchId = jest.fn().mockResolvedValue({ revision: 3 });
const gateway = new AuctionGateway(
{
getByMatchId: async () => ({ chatMessages: [] }),
getStateByMatchId,
} as never,
{} as never,
{} as never,
{} as never,
{} as never,
{} as never,
{} as never,
);
const broadcast = jest.fn();
(gateway as unknown as { server: unknown }).server = {
to: () => ({ emit: broadcast }),
};
const socket = { data: { matchId }, join: jest.fn(), emit: jest.fn() };
await gateway.handleJoin(socket as never, { matchId });
expect(socket.emit).toHaveBeenCalledWith('state', { revision: 3 });
expect(broadcast).not.toHaveBeenCalled();
let finish!: (value: unknown) => void;
getStateByMatchId.mockImplementationOnce(
() =>
new Promise((resolve) => {
finish = resolve;
}),
);
const ack = jest.fn(),
busyAck = jest.fn();
const pending = gateway.handleResync(socket as never, {}, ack);
await gateway.handleResync(socket as never, {}, busyAck);
expect(busyAck).toHaveBeenCalledWith(
expect.objectContaining({ code: 'RESYNC_BUSY' }),
);
finish({ revision: 4 });
await pending;
expect(ack).toHaveBeenCalledWith({ ok: true, state: { revision: 4 } });
expect(getStateByMatchId).toHaveBeenCalledTimes(2);
expect(broadcast).not.toHaveBeenCalled();
});
function createGateway(params?: {
getCurrentRedisTimestampMS?: jest.Mock;
placeBidDelta?: jest.Mock;