fix: repair legacy round-robin schedules

This commit is contained in:
dahyeon committed 2026-06-19 01:31:27 -07:00
1 parent 658592973e
commit 7399968fe8
3 files changed
+116 -14

No files matched your search

@@ -10,9 +10,9 @@ type TournamentCodeServiceInternals = TournamentCodeService & {
ensureGameSchedule(match: MatchDocument): void;
};
function createService() {
function createService(connectionService = {} as never) {
return new TournamentCodeService(
{} as never,
connectionService,
) as unknown as TournamentCodeServiceInternals;
}
@@ -28,6 +28,7 @@ interface MatchOverrides {
teamAIndex: number;
teamBIndex: number;
}>;
tournamentCodeIds?: unknown[];
}
function createMatch(overrides: MatchOverrides): MatchDocument {
@@ -127,9 +128,9 @@ describe('TournamentCodeService round-robin scheduling', () => {
gameMode: 'round-robin',
gameCount: 2,
gameSchedule: [
{ gameNumber: 1, teamAIndex: 2, teamBIndex: 5 },
{ gameNumber: 2, teamAIndex: 2, teamBIndex: 5 },
{ gameNumber: 3, teamAIndex: 1, teamBIndex: 4 },
{ gameNumber: 1, roundNumber: 1, teamAIndex: 2, teamBIndex: 5 },
{ gameNumber: 2, roundNumber: 1, teamAIndex: 2, teamBIndex: 5 },
{ gameNumber: 3, roundNumber: 2, teamAIndex: 1, teamBIndex: 4 },
],
});
@@ -217,6 +218,82 @@ describe('TournamentCodeService round-robin scheduling', () => {
}
});
it('rebuilds legacy round-robin schedules without round numbers before reservation', () => {
const service = createService();
const roundRobin = createMatch({
gameMode: 'round-robin',
gameCount: 1,
teams: Array.from({ length: 6 }, (_, index) => ({
teamIndex: index + 1,
})),
tournamentCodeIds: [],
gameSchedule: [
{ gameNumber: 1, teamAIndex: 2, teamBIndex: 5 },
{ gameNumber: 2, teamAIndex: 1, teamBIndex: 2 },
{ gameNumber: 3, teamAIndex: 4, teamBIndex: 5 },
{ gameNumber: 4, teamAIndex: 1, teamBIndex: 6 },
{ gameNumber: 5, teamAIndex: 5, teamBIndex: 6 },
{ gameNumber: 6, teamAIndex: 4, teamBIndex: 6 },
],
});
service.ensureGameSchedule(roundRobin);
for (const roundNumber of [1, 2, 3, 4, 5]) {
const round = roundRobin.gameSchedule.filter(
(game) => game.roundNumber === roundNumber,
);
expect(round).toHaveLength(3);
expect(new Set(round.flatMap((game) => [game.teamAIndex, game.teamBIndex])).size)
.toBe(6);
}
});
it('uses regenerated round refs for pending legacy round-robin slots', async () => {
const codeFindExec = jest.fn().mockResolvedValue([]);
const connectionService = {
getModel: jest.fn().mockResolvedValue({
find: jest.fn().mockReturnValue({ exec: codeFindExec }),
}),
};
const service = createService(connectionService as never);
const slots = await service.buildSlots(
createMatch({
gameMode: 'round-robin',
gameCount: 1,
teams: Array.from({ length: 6 }, (_, index) => ({
teamIndex: index + 1,
})),
tournamentCodeIds: [],
gameSchedule: [
{ gameNumber: 1, teamAIndex: 2, teamBIndex: 5 },
{ gameNumber: 2, teamAIndex: 1, teamBIndex: 2 },
{ gameNumber: 3, teamAIndex: 4, teamBIndex: 5 },
{ gameNumber: 4, teamAIndex: 1, teamBIndex: 6 },
{ gameNumber: 5, teamAIndex: 5, teamBIndex: 6 },
{ gameNumber: 6, teamAIndex: 4, teamBIndex: 6 },
{ gameNumber: 7, teamAIndex: 2, teamBIndex: 6 },
{ gameNumber: 8, teamAIndex: 3, teamBIndex: 5 },
{ gameNumber: 9, teamAIndex: 2, teamBIndex: 3 },
{ gameNumber: 10, teamAIndex: 1, teamBIndex: 4 },
{ gameNumber: 11, teamAIndex: 3, teamBIndex: 6 },
{ gameNumber: 12, teamAIndex: 1, teamBIndex: 3 },
{ gameNumber: 13, teamAIndex: 3, teamBIndex: 4 },
{ gameNumber: 14, teamAIndex: 1, teamBIndex: 5 },
{ gameNumber: 15, teamAIndex: 2, teamBIndex: 4 },
],
}),
);
for (const roundNumber of [1, 2, 3, 4, 5]) {
const round = slots.filter((slot) => slot.roundNumber === roundNumber);
expect(round).toHaveLength(3);
expect(new Set(round.flatMap((slot) => [slot.teamAIndex, slot.teamBIndex])).size)
.toBe(6);
}
});
it('does not expose fallback round-robin slots before the schedule is persisted', async () => {
const service = createService();
const slots = await service.buildSlots(
@@ -395,7 +395,7 @@ export class TournamentCodeService {
const gameMode = match.gameMode ?? 'pair';
if (gameMode === 'round-robin') {
if ((match.gameSchedule?.length ?? 0) > 0) {
if (this.shouldUsePersistedRoundRobinSchedule(match)) {
return match.gameSchedule.length;
}
return this.buildRoundRobinGameSchedule(match).length;
@@ -422,10 +422,12 @@ export class TournamentCodeService {
(entry) => entry.gameNumber === gameNumber,
);
if (persisted) {
return {
teamAIndex: persisted.teamAIndex,
teamBIndex: persisted.teamBIndex,
};
if (this.shouldUsePersistedRoundRobinSchedule(match)) {
return {
teamAIndex: persisted.teamAIndex,
teamBIndex: persisted.teamBIndex,
};
}
}
const fallback = this.buildRoundRobinGameSchedule(match).find(
@@ -452,12 +454,12 @@ export class TournamentCodeService {
private ensureGameSchedule(match: MatchDocument): void {
if ((match.gameMode ?? 'pair') !== 'round-robin') return;
if ((match.gameSchedule?.length ?? 0) > 0) return;
const teamCount = resolveRoundRobinTeamCount(match);
if (teamCount < 2) return;
if (this.shouldUsePersistedRoundRobinSchedule(match)) return;
match.gameSchedule = this.buildRoundRobinGameSchedule(match, true);
match.gameSchedule = this.buildRoundRobinGameSchedule(match);
}
private resolveRoundNumberForGameNumber(
@@ -469,13 +471,33 @@ export class TournamentCodeService {
const persisted = match.gameSchedule?.find(
(entry) => entry.gameNumber === gameNumber,
);
if (persisted?.roundNumber) return persisted.roundNumber;
if (persisted) {
if (persisted.roundNumber) return persisted.roundNumber;
if (this.shouldUsePersistedRoundRobinSchedule(match)) return undefined;
}
return this.buildRoundRobinGameSchedule(match).find(
(entry) => entry.gameNumber === gameNumber,
)?.roundNumber;
}
private shouldUsePersistedRoundRobinSchedule(match: MatchDocument): boolean {
const schedule = match.gameSchedule ?? [];
if (schedule.length === 0) return false;
if (schedule.every((entry) => typeof entry.roundNumber === 'number')) {
return true;
}
return !this.canRegenerateRoundRobinSchedule(match);
}
private canRegenerateRoundRobinSchedule(match: MatchDocument): boolean {
return (
(match.tournamentCodeIds?.length ?? 0) === 0 &&
(match.result?.games?.length ?? 0) === 0
);
}
private buildRoundRobinGameSchedule(
match: MatchDocument,
shuffleTeams = false,