fix: harden competition migration recovery

This commit is contained in:
dahyeon committed 2026-07-13 04:33:35 +00:00
1 parent bbcb2f43dc
commit 6b1c450752
7 files changed
+1394 -129

No files matched your search

@@ -199,6 +199,387 @@ describe('planCompetitionMigration', () => {
).toHaveLength(12);
});
it('maps a result-less code through an interleaved persisted round-robin schedule', () => {
const legacy = pairedMatch({
_id: 'match-persisted-round-robin-code',
gameMode: 'round-robin',
gameCount: 2,
roundRobinMaxRounds: 1,
teams: [
legacyTeam(1, 'One'),
legacyTeam(2, 'Two'),
legacyTeam(3, 'Three'),
legacyTeam(4, 'Four'),
],
gameSchedule: [
{ gameNumber: 1, roundNumber: 1, teamAIndex: 1, teamBIndex: 3 },
{ gameNumber: 2, roundNumber: 1, teamAIndex: 2, teamBIndex: 4 },
{ gameNumber: 3, roundNumber: 1, teamAIndex: 1, teamBIndex: 3 },
{ gameNumber: 4, roundNumber: 1, teamAIndex: 2, teamBIndex: 4 },
],
result: { games: [], seriesResults: [] },
tournamentCodes: [
{
_id: 'persisted-round-robin-code-game-3',
state: 'reserved',
gameNumber: 3,
},
],
});
const plan = requireReady(planCompetitionMigration(legacy));
const expectedSeries = plan.competition.seriesGraph.find(
(series) => series.code === 'RR_R1_M1',
);
expect(expectedSeries).toBeDefined();
expect(plan.assignments).toContainEqual(
expect.objectContaining({
seriesId: expectedSeries!.id,
gameSlotId: expectedSeries!.gameSlots[1].id,
}),
);
});
it('quarantines an incomplete persisted round-robin schedule', () => {
const legacy = pairedMatch({
_id: 'match-incomplete-round-robin-schedule',
gameMode: 'round-robin',
gameCount: 1,
roundRobinMaxRounds: 2,
teams: [
legacyTeam(1, 'One'),
legacyTeam(2, 'Two'),
legacyTeam(3, 'Three'),
legacyTeam(4, 'Four'),
],
gameSchedule: [
{ gameNumber: 1, roundNumber: 1, teamAIndex: 1, teamBIndex: 2 },
{ gameNumber: 2, roundNumber: 1, teamAIndex: 3, teamBIndex: 4 },
{ gameNumber: 3, roundNumber: 2, teamAIndex: 1, teamBIndex: 3 },
],
result: { games: [], seriesResults: [] },
});
const plan = planCompetitionMigration(legacy);
expectQuarantineReason(plan, 'SERIES_GRAPH_GENERATION_FAILED');
if (plan.status === 'quarantined') {
expect(plan.reasons).toContainEqual(
expect.objectContaining({
code: 'SERIES_GRAPH_GENERATION_FAILED',
details: {
projectorCode: 'INCOMPLETE_ROUND_ROBIN_PAIRINGS',
},
}),
);
}
});
it('reconstructs playable rounds for a complete legacy schedule without round numbers', () => {
const schedule = [
[2, 5],
[1, 2],
[4, 5],
[1, 6],
[5, 6],
[4, 6],
[2, 6],
[3, 5],
[2, 3],
[1, 4],
[3, 6],
[1, 3],
[3, 4],
[1, 5],
[2, 4],
] as const;
const legacy = pairedMatch({
_id: 'match-legacy-round-robin-without-rounds',
gameMode: 'round-robin',
gameCount: 1,
roundRobinMaxRounds: undefined,
teams: Array.from({ length: 6 }, (_, index) =>
legacyTeam(index + 1, `${index + 1}팀`),
),
gameSchedule: schedule.map(([teamAIndex, teamBIndex], index) => ({
gameNumber: index + 1,
teamAIndex,
teamBIndex,
})),
result: { games: [], seriesResults: [] },
});
const plan = requireReady(planCompetitionMigration(legacy));
expect(plan.competition.seriesGraph).toHaveLength(15);
expect(
new Set(
plan.competition.seriesGraph.map((series) => series.stage.roundNumber),
),
).toHaveProperty('size', 5);
expect(plan.warnings).toContainEqual(
expect.objectContaining({
code: 'LEGACY_ROUND_ROBIN_ROUNDS_RECONSTRUCTED',
}),
);
});
it('reconstructs a large complete opponent-only schedule without combinatorial search', () => {
const teamCount = 14;
const pairs = Array.from({ length: teamCount }, (_, left) =>
Array.from(
{ length: teamCount - left - 1 },
(_, offset) => [left + 1, left + offset + 2] as const,
),
).flat();
const legacy = pairedMatch({
_id: 'match-large-complete-round-robin-without-rounds',
gameMode: 'round-robin',
gameCount: 1,
teams: Array.from({ length: teamCount }, (_, index) =>
legacyTeam(index + 1, `${index + 1}팀`),
),
gameSchedule: pairs.map(([teamAIndex, teamBIndex], index) => ({
gameNumber: index + 1,
teamAIndex,
teamBIndex,
})),
result: { games: [], seriesResults: [] },
});
const plan = requireReady(planCompetitionMigration(legacy));
expect(plan.competition.seriesGraph).toHaveLength(91);
});
it('rejects a large infeasible partial opponent schedule during degree preflight', () => {
const teamCount = 14;
const pairs = Array.from({ length: teamCount }, (_, left) =>
Array.from(
{ length: teamCount - left - 1 },
(_, offset) => [left + 1, left + offset + 2] as const,
),
)
.flat()
.slice(0, 49);
const legacy = pairedMatch({
_id: 'match-large-infeasible-round-robin-without-rounds',
gameMode: 'round-robin',
gameCount: 1,
roundRobinMaxRounds: 7,
teams: Array.from({ length: teamCount }, (_, index) =>
legacyTeam(index + 1, `${index + 1}팀`),
),
gameSchedule: pairs.map(([teamAIndex, teamBIndex], index) => ({
gameNumber: index + 1,
teamAIndex,
teamBIndex,
})),
result: { games: [], seriesResults: [] },
});
const plan = planCompetitionMigration(legacy);
expectQuarantineReason(plan, 'INVALID_ROUND_ROBIN_SCHEDULE');
});
it('uses a dedicated quarantine code for unsupported large partial round reconstruction', () => {
const teamCount = 20;
const rotating = Array.from({ length: teamCount }, (_, index) => index + 1);
const pairs: Array<readonly [number, number]> = [];
for (let round = 0; round < 10; round += 1) {
for (let index = 0; index < teamCount / 2; index += 1) {
pairs.push([rotating[index], rotating[teamCount - 1 - index]]);
}
const fixed = rotating[0];
const tail = rotating.slice(1);
tail.unshift(tail.pop()!);
rotating.splice(0, rotating.length, fixed, ...tail);
}
const legacy = pairedMatch({
_id: 'match-unsupported-large-partial-round-reconstruction',
gameMode: 'round-robin',
gameCount: 1,
roundRobinMaxRounds: 10,
teams: Array.from({ length: teamCount }, (_, index) =>
legacyTeam(index + 1, `${index + 1}팀`),
),
gameSchedule: pairs.reverse().map(([teamAIndex, teamBIndex], index) => ({
gameNumber: index + 1,
teamAIndex,
teamBIndex,
})),
result: { games: [], seriesResults: [] },
});
const plan = planCompetitionMigration(legacy);
expectQuarantineReason(
plan,
'ROUND_ROBIN_ROUND_RECONSTRUCTION_UNSUPPORTED',
);
});
it('preserves a sparse global game number as the exact paired-Series slot', () => {
const legacy = pairedMatch({
_id: 'match-sparse-paired-game-number',
gameCount: 3,
teams: [legacyTeam(1, 'One'), legacyTeam(2, 'Two')],
gameSchedule: undefined,
result: {
games: [
legacyGame({
gameNumber: 2,
riotMatchId: 'KR_SPARSE_PAIR_GAME_2',
riotArchivePresent: true,
blueTeamIndex: 1,
redTeamIndex: 2,
winnerTeamIndex: 1,
}),
],
seriesResults: [],
},
tournamentCodes: [
{
_id: 'sparse-pair-code-game-2',
state: 'used',
gameNumber: 2,
riotMatchId: 'KR_SPARSE_PAIR_GAME_2',
},
],
});
const plan = requireReady(planCompetitionMigration(legacy));
const series = plan.competition.seriesGraph[0];
const gameFact = plan.facts.find((fact) => fact.type === 'game-accepted');
expect(gameFact).toMatchObject({
type: 'game-accepted',
seriesId: series.id,
gameSlotId: series.gameSlots[1].id,
ordinal: 2,
});
expect(plan.assignments).toContainEqual(
expect.objectContaining({
seriesId: series.id,
gameSlotId: series.gameSlots[1].id,
}),
);
});
it('preserves a sparse double-elimination global game number and binds its used code to the same slot', () => {
const legacy = sparseDoubleEliminationWbFinalMatch();
const plan = requireReady(planCompetitionMigration(legacy));
const wbFinal = plan.competition.seriesGraph.find(
(series) => series.code === 'WB_FINAL',
);
expect(wbFinal).toBeDefined();
const wbFinalFact = plan.facts.find(
(fact) => fact.type === 'game-accepted' && fact.seriesId === wbFinal!.id,
);
expect(wbFinalFact).toMatchObject({
type: 'game-accepted',
seriesId: wbFinal!.id,
gameSlotId: wbFinal!.gameSlots[1].id,
ordinal: 2,
evidence: {
type: 'riot',
riotMatchId: 'KR_SPARSE_WB_FINAL_GAME_2',
},
});
expect(plan.assignments).toContainEqual(
expect.objectContaining({
tournamentCodeId: 'sparse-wb-final-code-game-2',
state: 'used',
seriesId: wbFinal!.id,
gameSlotId: wbFinal!.gameSlots[1].id,
riotMatchId: 'KR_SPARSE_WB_FINAL_GAME_2',
}),
);
});
it('quarantines a used TournamentCode when its slot has no accepted game', () => {
const legacy = pairedMatch({
_id: 'match-used-code-without-game',
gameCount: 1,
gameSchedule: undefined,
result: { games: [], seriesResults: [] },
tournamentCodes: [
{
_id: 'used-code-without-game',
state: 'used',
gameNumber: 1,
usedAt: '2026-07-02T00:00:00.000Z',
},
],
});
const plan = planCompetitionMigration(legacy);
expectQuarantineReason(plan, 'USED_CODE_WITHOUT_ACCEPTED_GAME');
});
it('binds a used code without Riot identity to the accepted game in its exact slot', () => {
const legacy = pairedMatch({
_id: 'match-used-code-by-slot',
gameCount: 1,
gameSchedule: undefined,
result: {
games: [
legacyGame({
gameNumber: 1,
riotMatchId: 'KR_USED_CODE_BY_SLOT',
riotArchivePresent: true,
}),
],
seriesResults: [],
},
tournamentCodes: [
{
_id: 'used-code-by-slot',
state: 'used',
gameNumber: 1,
usedAt: '2026-07-02T00:00:00.000Z',
},
],
});
const plan = requireReady(planCompetitionMigration(legacy));
expect(plan.assignments).toHaveLength(1);
expect(plan.warnings).toEqual([]);
});
it('preserves the actual voided lifecycle timestamp', () => {
const legacy = pairedMatch({
_id: 'match-voided-code-timestamp',
gameCount: 1,
gameSchedule: undefined,
tournamentCodes: [
{
_id: 'voided-code-with-timestamp',
state: 'voided',
gameNumber: 1,
reservedAt: '2026-07-01T00:00:00.000Z',
usedAt: '2026-07-02T00:00:00.000Z',
voidedAt: '2026-07-03T00:00:00.000Z',
},
],
});
const plan = requireReady(planCompetitionMigration(legacy));
expect(plan.assignments).toContainEqual(
expect.objectContaining({
state: 'voided',
voidedAt: '2026-07-03T00:00:00.000Z',
}),
);
});
it('maps a result-less reserved TournamentCode by its global game number', () => {
const legacy = pairedMatch({
_id: 'match-future-reserved-code',
@@ -558,3 +939,91 @@ function dataTxtUpperBracketTopology() {
bracketSide: 'winner' as const,
}));
}
function sparseDoubleEliminationWbFinalMatch(): LegacyMatchInput {
const completedSeries = [
['WB_R1_M1', 1, 2, 1, 8, 1],
['WB_R1_M2', 4, 5, 4, 5, 4],
['WB_R1_M3', 7, 8, 2, 7, 2],
['WB_R1_M4', 10, 11, 3, 6, 3],
['WB_R2_M1', 13, 14, 1, 4, 1],
['WB_R2_M2', 16, 17, 2, 3, 2],
] as const;
const games = completedSeries.flatMap(
([
bracketMatchId,
firstGameNumber,
secondGameNumber,
teamA,
teamB,
winner,
]) => [
legacyGame({
bracketMatchId,
gameNumber: firstGameNumber,
riotMatchId: `KR_${bracketMatchId}_1`,
riotArchivePresent: true,
blueTeamIndex: teamA,
redTeamIndex: teamB,
winnerTeamIndex: winner,
gameCreation: 1782864000000 + firstGameNumber * 60_000,
}),
legacyGame({
bracketMatchId,
gameNumber: secondGameNumber,
riotMatchId: `KR_${bracketMatchId}_2`,
riotArchivePresent: true,
blueTeamIndex: teamB,
redTeamIndex: teamA,
winnerTeamIndex: winner,
gameCreation: 1782864000000 + secondGameNumber * 60_000,
}),
],
);
games.push(
legacyGame({
bracketMatchId: 'WB_FINAL',
gameNumber: 20,
riotMatchId: 'KR_SPARSE_WB_FINAL_GAME_2',
riotArchivePresent: true,
blueTeamIndex: 1,
redTeamIndex: 2,
winnerTeamIndex: 1,
gameCreation: 1782864000000 + 20 * 60_000,
}),
);
return {
_id: 'match-sparse-double-elimination-game-number',
communityId: 'community-1',
gameMode: 'double-elimination',
gameCount: 3,
status: 'in-progress',
teams: Array.from({ length: 8 }, (_, index) =>
legacyTeam(index + 1, `${index + 1}팀`),
),
doubleElimination: {
teamCount: 8,
resetFinalEnabled: true,
seeds: Array.from({ length: 8 }, (_, index) => ({
seedNumber: index + 1,
teamIndex: index + 1,
})),
bracketMatches: dataTxtUpperBracketTopology(),
},
result: { games, seriesResults: [] },
tournamentCodes: [
{
_id: 'sparse-wb-final-code-game-2',
state: 'used',
bracketMatchId: 'WB_FINAL',
gameNumber: 20,
riotMatchId: 'KR_SPARSE_WB_FINAL_GAME_2',
reservedAt: '2026-07-01T00:00:00.000Z',
usedAt: '2026-07-02T00:00:00.000Z',
},
],
createdAt: '2026-07-01T00:00:00.000Z',
updatedAt: '2026-07-03T00:00:00.000Z',
};
}