Files
lolympus-tournament-support/apps/web/test/auction-resync.test.ts
T
dahyeon f649490cd2 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.
2026-11-09 23:24:11 +09:00

96 lines
2.8 KiB
TypeScript

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]);
});