Files
lolympus-tournament-support/apps/api/src/modules/global/database/migrations/20260502-team-index.spec.ts
T
dahyeon 4b8ccf0077 fix: harden result polling and Redis 8.10 operations
Preserve tournament result jobs through lock contention and transient failures,
atomically schedule retries before acknowledgement, and publish initial polls
after releasing the preparation lock.

Fix Redis connection blocking, durability Hash cleanup, duplicate auction
request ordering, and scheduled-job promotion. Use native Redis 8.10 stream,
lock, counter, and field-expiring cache operations with invalidation fencing.

Repair 90 test type errors and add a repeatable API typecheck command.

Validation: 704 API tests, 129 web tests, 31 real Redis integration tests,
3 dependency checks, workspace type checks and builds, and changed-file lint.
2026-11-09 17:33:13 +09:00

176 lines
4.6 KiB
TypeScript

import { migrateTeamIndex } from './20260502-team-index';
type MigrationDocument = Record<string, unknown> & { _id: unknown };
function createDb(params: {
auctions?: MigrationDocument[];
matches?: MigrationDocument[];
}) {
const collections = {
auction: params.auctions ?? [],
match: params.matches ?? [],
};
return {
collections,
db: {
collection(name: string) {
const key = name === 'auction' ? 'auction' : 'match';
const docs = collections[key];
return {
find() {
return {
limit() {
return {
async *[Symbol.asyncIterator]() {
for (const doc of docs) yield doc;
},
};
},
};
},
async bulkWrite(
writes: Array<{
updateOne: {
filter: { _id: unknown };
update: {
$set: Record<string, unknown>;
$unset: Record<string, ''>;
};
};
}>,
) {
for (const write of writes) {
const doc = docs.find(
(item) => item._id === write.updateOne.filter._id,
);
if (!doc) continue;
Object.assign(doc, write.updateOne.update.$set);
for (const key of Object.keys(write.updateOne.update.$unset)) {
delete doc[key];
}
}
},
};
},
},
};
}
describe('migrateTeamIndex', () => {
it('migrates auction and match team identity fields', async () => {
const { db, collections } = createDb({
auctions: [
{
_id: 'auction-1',
teams: [
{ side: 'blue', groupIndex: 1 },
{ side: 'red', groupIndex: 1 },
],
bids: [{ side: 'red', groupIndex: 1, amount: 100 }],
currentBidder: 'red',
currentBidderGroupIndex: 1,
},
],
matches: [
{
_id: 'match-1',
teams: [
{ side: 'blue', groupIndex: 1 },
{ side: 'red', groupIndex: 1 },
],
result: {
winnerSide: 'blue',
blueScore: 1,
redScore: 0,
games: [
{
gameNumber: 1,
riotMatchId: 'KR_1',
winnerSide: 'blue',
blueGroupIndex: 1,
redGroupIndex: 1,
participants: [
{ userId: 'u1', side: 'blue' },
{ userId: 'u2', side: 'red' },
],
},
],
seriesResults: [
{
blueGroupIndex: 1,
redGroupIndex: 1,
blueScore: 1,
redScore: 0,
winnerSide: 'blue',
},
],
},
},
],
});
const stats = await migrateTeamIndex(db, { dryRun: false });
expect(stats).toEqual({
scannedAuctions: 1,
changedAuctions: 1,
scannedMatches: 1,
changedMatches: 1,
});
expect(collections.auction[0]).toMatchObject({
teams: [{ teamIndex: 1 }, { teamIndex: 2 }],
bids: [{ teamIndex: 2, amount: 100 }],
currentBidderTeamIndex: 2,
});
expect(collections.match[0]).toMatchObject({
teams: [
{ teamIndex: 1, name: '1팀' },
{ teamIndex: 2, name: '2팀' },
],
result: {
winnerTeamIndex: 1,
games: [
{
blueTeamIndex: 1,
redTeamIndex: 2,
winnerTeamIndex: 1,
participants: [
{ teamIndex: 1, side: 'blue' },
{ teamIndex: 2, side: 'red' },
],
},
],
seriesResults: [
{
teamAIndex: 1,
teamBIndex: 2,
teamAWins: 1,
teamBWins: 0,
winnerTeamIndex: 1,
},
],
},
});
expect(collections.auction[0]).not.toHaveProperty('currentBidder');
expect(collections.match[0].result).not.toHaveProperty('blueScore');
});
it('is idempotent after applying updates', async () => {
const { db } = createDb({
matches: [
{
_id: 'match-1',
teams: [{ side: 'blue', groupIndex: 1 }],
},
],
});
await migrateTeamIndex(db, { dryRun: false });
const stats = await migrateTeamIndex(db, { dryRun: false });
expect(stats.changedMatches).toBe(0);
expect(stats.changedAuctions).toBe(0);
});
});