192 lines
5.2 KiB
TypeScript
192 lines
5.2 KiB
TypeScript
import type { MatchDocument } from '../global/database/schema';
|
|
import { TournamentCodeService } from './tournament-code.service';
|
|
|
|
type TournamentCodeServiceInternals = TournamentCodeService & {
|
|
resolveRequiredSlots(match: MatchDocument): number;
|
|
resolveSeriesRefForGameNumber(
|
|
match: MatchDocument,
|
|
gameNumber: number,
|
|
): { teamAIndex: number; teamBIndex: number };
|
|
ensureGameSchedule(match: MatchDocument): void;
|
|
};
|
|
|
|
function createService() {
|
|
return new TournamentCodeService(
|
|
{} as never,
|
|
) as unknown as TournamentCodeServiceInternals;
|
|
}
|
|
|
|
interface MatchOverrides {
|
|
gameCount?: number;
|
|
gameMode?: 'pair' | 'bracket' | 'round-robin';
|
|
participants?: Array<{ excluded?: boolean }>;
|
|
teams?: Array<{ teamIndex: number }>;
|
|
gameSchedule?: Array<{
|
|
gameNumber: number;
|
|
teamAIndex: number;
|
|
teamBIndex: number;
|
|
}>;
|
|
}
|
|
|
|
function createMatch(overrides: MatchOverrides): MatchDocument {
|
|
return {
|
|
gameCount: 1,
|
|
gameMode: 'pair',
|
|
participants: [],
|
|
teams: [],
|
|
gameSchedule: [],
|
|
...overrides,
|
|
} as unknown as MatchDocument;
|
|
}
|
|
|
|
describe('TournamentCodeService round-robin scheduling', () => {
|
|
it('keeps pair slot calculation unchanged', () => {
|
|
const service = createService();
|
|
const match = createMatch({
|
|
gameCount: 3,
|
|
teams: Array.from({ length: 6 }, (_, index) => ({
|
|
teamIndex: index + 1,
|
|
})),
|
|
participants: Array.from({ length: 30 }, () => ({})),
|
|
});
|
|
|
|
expect(service.resolveRequiredSlots(match)).toBe(9);
|
|
expect(service.resolveSeriesRefForGameNumber(match, 1)).toEqual({
|
|
teamAIndex: 1,
|
|
teamBIndex: 2,
|
|
});
|
|
expect(service.resolveSeriesRefForGameNumber(match, 4)).toEqual({
|
|
teamAIndex: 3,
|
|
teamBIndex: 4,
|
|
});
|
|
expect(service.resolveSeriesRefForGameNumber(match, 7)).toEqual({
|
|
teamAIndex: 5,
|
|
teamBIndex: 6,
|
|
});
|
|
});
|
|
|
|
it('calculates round-robin slots from team pairs and per-pair game count', () => {
|
|
const service = createService();
|
|
|
|
expect(
|
|
service.resolveRequiredSlots(
|
|
createMatch({
|
|
gameMode: 'round-robin',
|
|
gameCount: 1,
|
|
teams: Array.from({ length: 6 }, (_, index) => ({
|
|
teamIndex: index + 1,
|
|
})),
|
|
}),
|
|
),
|
|
).toBe(15);
|
|
expect(
|
|
service.resolveRequiredSlots(
|
|
createMatch({
|
|
gameMode: 'round-robin',
|
|
gameCount: 2,
|
|
teams: Array.from({ length: 6 }, (_, index) => ({
|
|
teamIndex: index + 1,
|
|
})),
|
|
}),
|
|
),
|
|
).toBe(30);
|
|
expect(
|
|
service.resolveRequiredSlots(
|
|
createMatch({
|
|
gameMode: 'round-robin',
|
|
gameCount: 1,
|
|
teams: [],
|
|
participants: Array.from({ length: 30 }, () => ({})),
|
|
}),
|
|
),
|
|
).toBe(15);
|
|
});
|
|
|
|
it('uses persisted round-robin game schedule for game refs', () => {
|
|
const service = createService();
|
|
const match = createMatch({
|
|
gameMode: 'round-robin',
|
|
gameCount: 2,
|
|
gameSchedule: [
|
|
{ gameNumber: 1, teamAIndex: 2, teamBIndex: 5 },
|
|
{ gameNumber: 2, teamAIndex: 2, teamBIndex: 5 },
|
|
{ gameNumber: 3, teamAIndex: 1, teamBIndex: 4 },
|
|
],
|
|
});
|
|
|
|
expect(service.resolveSeriesRefForGameNumber(match, 1)).toEqual({
|
|
teamAIndex: 2,
|
|
teamBIndex: 5,
|
|
});
|
|
expect(service.resolveSeriesRefForGameNumber(match, 3)).toEqual({
|
|
teamAIndex: 1,
|
|
teamBIndex: 4,
|
|
});
|
|
});
|
|
|
|
it('falls back to lexicographic round-robin pairs before schedule is persisted', () => {
|
|
const service = createService();
|
|
const match = createMatch({
|
|
gameMode: 'round-robin',
|
|
gameCount: 1,
|
|
teams: Array.from({ length: 4 }, (_, index) => ({
|
|
teamIndex: index + 1,
|
|
})),
|
|
gameSchedule: [],
|
|
});
|
|
|
|
expect(service.resolveSeriesRefForGameNumber(match, 1)).toEqual({
|
|
teamAIndex: 1,
|
|
teamBIndex: 2,
|
|
});
|
|
expect(service.resolveSeriesRefForGameNumber(match, 2)).toEqual({
|
|
teamAIndex: 1,
|
|
teamBIndex: 3,
|
|
});
|
|
});
|
|
|
|
it('creates a round-robin schedule once and leaves pair matches empty', () => {
|
|
const service = createService();
|
|
const roundRobin = createMatch({
|
|
gameMode: 'round-robin',
|
|
gameCount: 2,
|
|
teams: Array.from({ length: 4 }, (_, index) => ({
|
|
teamIndex: index + 1,
|
|
})),
|
|
gameSchedule: [],
|
|
});
|
|
|
|
service.ensureGameSchedule(roundRobin);
|
|
expect(roundRobin.gameSchedule).toHaveLength(12);
|
|
const persisted = [...roundRobin.gameSchedule];
|
|
service.ensureGameSchedule(roundRobin);
|
|
expect(roundRobin.gameSchedule).toEqual(persisted);
|
|
|
|
const pair = createMatch({
|
|
gameMode: 'pair',
|
|
teams: Array.from({ length: 4 }, (_, index) => ({
|
|
teamIndex: index + 1,
|
|
})),
|
|
gameSchedule: [],
|
|
});
|
|
service.ensureGameSchedule(pair);
|
|
expect(pair.gameSchedule).toEqual([]);
|
|
});
|
|
|
|
it('does not expose fallback round-robin slots before the schedule is persisted', async () => {
|
|
const service = createService();
|
|
const slots = await service.buildSlots(
|
|
createMatch({
|
|
gameMode: 'round-robin',
|
|
gameCount: 1,
|
|
teams: Array.from({ length: 6 }, (_, index) => ({
|
|
teamIndex: index + 1,
|
|
})),
|
|
gameSchedule: [],
|
|
}),
|
|
);
|
|
|
|
expect(slots).toEqual([]);
|
|
});
|
|
});
|