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

@@ -0,0 +1,180 @@
import { createClient } from 'redis';
import mongoose from 'mongoose';
import { AuctionBidArchiveService } from '../src/modules/auction/auction-bid-archive.service';
import { AuctionRedisService } from '../src/modules/auction/redis/auction-redis.service';
import {
AUCTION_BID_ARCHIVE_QUEUE_KEY as queue,
AUCTION_BID_ARCHIVE_PAYLOADS_KEY as payloads,
AUCTION_BID_ARCHIVE_DEAD_LETTER_QUEUE_KEY as deadQueue,
AUCTION_BID_ARCHIVE_DEAD_LETTER_PAYLOADS_KEY as deadPayloads,
} from '../src/modules/auction/redis/auction-redis.keys';
const client = createClient({
url: process.env.REDIS_TEST_URL,
socket: { reconnectStrategy: false },
});
const redis = new AuctionRedisService(client);
const matchId = new mongoose.Types.ObjectId().toString();
function event(revision: number) {
return {
_id: `${matchId}:life:${String(revision).padStart(16, '0')}`,
matchId,
auctionLifecycleId: 'life',
revision,
bidRequestId: `bid-${revision}`,
bidderUserId: new mongoose.Types.ObjectId().toString(),
bidderTeamIndex: 1,
bidPointAmount: revision,
bidPlacedAtTimestampMS: Date.now(),
};
}
function fixture() {
const bulkWrite = jest.fn(async (operations: unknown[]) => ({
matchedCount: 0,
upsertedCount: operations.length,
}));
const service = new AuctionBidArchiveService(
{ getModel: async () => ({ bulkWrite }) } as never,
redis,
);
return { service, bulkWrite };
}
async function enqueue(id: string, raw: string | null) {
await client.zAdd(queue, { score: 1, value: id });
if (raw !== null) await client.hSet(payloads, id, raw);
}
beforeAll(async () => {
if (process.env.REDIS_TEST_ISOLATED !== '1' || !process.env.REDIS_TEST_URL)
throw new Error('Use the isolated Redis test runner.');
client.on('error', () => {});
await client.connect();
});
beforeEach(async () => {
await client.flushDb();
});
afterEach(() => jest.restoreAllMocks());
afterAll(async () => {
if (client.isOpen) await client.quit();
});
it('isolates malformed, missing and invalid payloads while saving valid bids and retaining the originals', async () => {
const { service, bulkWrite } = fixture();
const good = event(10);
const invalid = [
['broken', '{broken'],
['empty', ''],
['null', 'null'],
['array', '[]'],
['missing', null],
[event(1)._id, JSON.stringify({ ...event(1), revision: -1 })],
[event(2)._id, JSON.stringify({ ...event(2), bidderUserId: 'bad' })],
[event(3)._id, JSON.stringify({ ...event(3), bidderUserId: undefined })],
[event(4)._id, JSON.stringify({ ...event(4), bidPointAmount: '4' })],
] as Array<[string, string | null]>;
for (const [id, raw] of invalid) await enqueue(id, raw);
await enqueue(good._id, JSON.stringify(good));
await expect(service.persistPendingBids()).resolves.toBe(1);
expect(bulkWrite.mock.calls[0][0]).toHaveLength(1);
expect(await client.zCard(queue)).toBe(0);
expect(await client.hLen(payloads)).toBe(0);
expect(await client.zCard(deadQueue)).toBe(invalid.length);
for (const [id, raw] of invalid) {
expect(JSON.parse((await client.hGet(deadPayloads, id))!)).toMatchObject({
id,
rawPayload: raw,
error: expect.any(String),
quarantinedAtTimestampMS: expect.any(Number),
});
}
expect(await client.ttl(deadPayloads)).toBe(-1);
await expect(service.persistPendingBids()).resolves.toBe(0);
});
it('retains valid bids during a Mongo outage, after moving malformed entries out of the pending queue', async () => {
const { service, bulkWrite } = fixture();
const good = event(1);
await enqueue('broken', '{broken');
await enqueue(good._id, JSON.stringify(good));
bulkWrite.mockRejectedValueOnce(new Error('Mongo offline'));
await expect(service.persistPendingBids()).rejects.toThrow('Mongo offline');
expect(await client.zRange(queue, 0, -1)).toEqual([good._id]);
expect(await client.hGet(payloads, good._id)).toBe(JSON.stringify(good));
expect(await client.zCard(deadQueue)).toBe(1);
await expect(service.persistPendingBids()).resolves.toBe(1);
expect(await client.zCard(queue)).toBe(0);
});
it('does not quarantine a payload repaired concurrently after HMGET', async () => {
const { service } = fixture();
const good = event(1);
await enqueue(good._id, '{broken');
const send = client.sendCommand.bind(client);
jest.spyOn(client, 'sendCommand').mockImplementation(async (command) => {
const result = await send(command);
if (command[0] === 'HMGET')
await client.hSet(payloads, good._id, JSON.stringify(good));
return result;
});
await expect(service.persistPendingBids()).resolves.toBe(0);
expect(await client.zCard(deadQueue)).toBe(0);
expect(await client.zRange(queue, 0, -1)).toEqual([good._id]);
await expect(service.persistPendingBids()).resolves.toBe(1);
});
it('preserves pending payloads if quarantine cannot be written', async () => {
const { service, bulkWrite } = fixture();
const good = event(1);
await enqueue('broken', '{broken');
await enqueue(good._id, JSON.stringify(good));
await client.set(deadPayloads, 'wrong-type');
await expect(service.persistPendingBids()).rejects.toThrow('WRONGTYPE');
expect(await client.zCard(queue)).toBe(2);
expect(await client.hGet(payloads, 'broken')).toBe('{broken');
expect(bulkWrite).not.toHaveBeenCalled();
await client.del(deadPayloads);
await expect(service.persistPendingBids()).resolves.toBe(1);
});
it('does not acknowledge a payload changed while Mongo was saving', async () => {
const { service, bulkWrite } = fixture();
const good = event(1);
await enqueue(good._id, JSON.stringify(good));
bulkWrite.mockImplementationOnce(async () => {
await client.hSet(payloads, good._id, '{replaced');
return { matchedCount: 0, upsertedCount: 1 };
});
await expect(service.persistPendingBids()).resolves.toBe(1);
expect(await client.hGet(payloads, good._id)).toBe('{replaced');
expect(await client.zCard(queue)).toBe(1);
});
it.each([
['bidRoundId', null],
['bidRoundId', 1],
['bidRoundId', {}],
['bidRoundId', []],
['bidRoundId', ' '],
['playerUserId', ''],
])('continues quarantining invalid %s=%j', async (field, value) => {
const { service, bulkWrite } = fixture();
const bad = { ...event(1), [field]: value };
await enqueue(bad._id, JSON.stringify(bad));
await expect(service.persistPendingBids()).resolves.toBe(0);
expect(bulkWrite).not.toHaveBeenCalled();
expect(await client.zCard(deadQueue)).toBe(1);
});
it('archives a pre-upgrade bid with an empty round ID instead of quarantining it', async () => {
const { service, bulkWrite } = fixture();
const legacy = { ...event(1), bidRoundId: '' };
await enqueue(legacy._id, JSON.stringify(legacy));
await expect(service.persistPendingBids()).resolves.toBe(1);
expect(await client.zCard(deadQueue)).toBe(0);
expect(await client.zCard(queue)).toBe(0);
const [[operations]] = bulkWrite.mock.calls as unknown as [
[Array<{ updateOne: { update: { $setOnInsert: object } } }>],
];
expect(operations[0].updateOne.update.$setOnInsert).not.toHaveProperty(
'bidRoundId',
);
});