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

@@ -159,7 +159,13 @@ describe('generateSeriesGraph', () => {
};
const first = generateSeriesGraph(input);
const second = generateSeriesGraph(input);
const second = generateSeriesGraph({
...input,
format: {
...input.format,
pairings: [...input.format.pairings].reverse(),
},
});
expect(second).toEqual(first);
expect(
@@ -234,6 +240,68 @@ describe('generateSeriesGraph', () => {
])('rejects a duplicate explicit $name', ({ pairings, code }) => {
expectGraphError(explicitRoundRobinInput(pairings), code);
});
it('keeps distinct opponent pairs when opaque TeamIds contain delimiters', () => {
const input = explicitRoundRobinInput([
pairing(1, 1, 'a', 'b:c'),
pairing(1, 2, 'a:b', 'c'),
]);
input.format.maxRounds = 1;
input.teams = ['a', 'b:c', 'a:b', 'c'].map((teamId, index) => ({
teamId,
displayOrder: index + 1,
seedNumber: index + 1,
}));
expect(generateSeriesGraph(input)).toHaveLength(2);
});
it('validates complete odd-team rounds and clamps maxRounds to the full schedule', () => {
const pairings = [
pairing(1, 1, 'team-2', 'team-5'),
pairing(1, 2, 'team-3', 'team-4'),
pairing(2, 1, 'team-1', 'team-5'),
pairing(2, 2, 'team-2', 'team-3'),
pairing(3, 1, 'team-1', 'team-4'),
pairing(3, 2, 'team-3', 'team-5'),
pairing(4, 1, 'team-1', 'team-3'),
pairing(4, 2, 'team-2', 'team-4'),
pairing(5, 1, 'team-1', 'team-2'),
pairing(5, 2, 'team-4', 'team-5'),
];
const input = explicitRoundRobinInput(pairings);
input.teams = Array.from({ length: 5 }, (_, index) => ({
teamId: `team-${index + 1}`,
displayOrder: index + 1,
seedNumber: index + 1,
}));
input.format.maxRounds = 10;
expect(generateSeriesGraph(input)).toHaveLength(10);
});
it.each([
{
name: 'an expected round',
pairings: [
pairing(1, 1, 'team-1', 'team-2'),
pairing(1, 2, 'team-3', 'team-4'),
],
},
{
name: 'a match from an expected round',
pairings: [
pairing(1, 1, 'team-1', 'team-2'),
pairing(1, 2, 'team-3', 'team-4'),
pairing(2, 1, 'team-1', 'team-3'),
],
},
])('rejects explicit pairings missing $name', ({ pairings }) => {
expectGraphError(
explicitRoundRobinInput(pairings),
'INCOMPLETE_ROUND_ROBIN_PAIRINGS',
);
});
});
function firstTo(winsRequired: number, maxGames: number) {
@@ -304,7 +304,7 @@ function validateExplicitRoundRobinPairings(
}
positions.add(position);
const opponents = [pairing.teamAId, pairing.teamBId].sort().join(':');
const opponents = JSON.stringify([pairing.teamAId, pairing.teamBId].sort());
if (opponentPairs.has(opponents)) {
throw graphError(
'DUPLICATE_ROUND_ROBIN_OPPONENTS',
@@ -338,6 +338,28 @@ function validateExplicitRoundRobinPairings(
);
}
}
const expectedRoundCount = roundRobinRoundCount(teams.length, maxRounds);
const expectedMatchesPerRound = Math.floor(teams.length / 2);
for (
let roundNumber = 1;
roundNumber <= expectedRoundCount;
roundNumber += 1
) {
const matchCount = (teamsByRound.get(roundNumber)?.size ?? 0) / 2;
if (matchCount !== expectedMatchesPerRound) {
throw graphError(
'INCOMPLETE_ROUND_ROBIN_PAIRINGS',
`Round ${roundNumber} must contain ${expectedMatchesPerRound} Series.`,
);
}
}
if (teamsByRound.size !== expectedRoundCount) {
throw graphError(
'INCOMPLETE_ROUND_ROBIN_PAIRINGS',
`Explicit round-robin pairings must contain rounds 1 through ${expectedRoundCount}.`,
);
}
return sorted;
}
@@ -549,14 +571,7 @@ function roundRobinPairings(
const rotating = Array.from({ length: teamCount }, (_, index) => index);
if (rotating.length % 2 === 1) rotating.push(bye);
const fullRoundCount = rotating.length - 1;
const requestedRounds =
maxRounds === undefined
? fullRoundCount
: Number.isFinite(maxRounds)
? Math.floor(maxRounds)
: fullRoundCount;
const roundCount = Math.max(0, Math.min(fullRoundCount, requestedRounds));
const roundCount = roundRobinRoundCount(teamCount, maxRounds);
const pairings: CanonicalRoundRobinPairing[] = [];
for (let roundIndex = 0; roundIndex < roundCount; roundIndex += 1) {
@@ -580,6 +595,17 @@ function roundRobinPairings(
return pairings;
}
function roundRobinRoundCount(teamCount: number, maxRounds?: number): number {
const fullRoundCount = teamCount % 2 === 0 ? teamCount - 1 : teamCount;
const requestedRounds =
maxRounds === undefined
? fullRoundCount
: Number.isFinite(maxRounds)
? Math.floor(maxRounds)
: fullRoundCount;
return Math.max(0, Math.min(fullRoundCount, requestedRounds));
}
function normalizeTeams(
teams: readonly CompetitionGraphTeam[],
): CompetitionGraphTeam[] {