Files
lolympus-tournament-support/apps/api/src/modules/competition/domain/series-graph.spec.ts
T

311 lines
8.9 KiB
TypeScript

import { generateSeriesGraph } from './series-graph';
type GraphInput = Parameters<typeof generateSeriesGraph>[0];
type ExplicitRoundRobinPairing = {
roundNumber: number;
matchNumber: number;
teamAId: string;
teamBId: string;
};
type ExplicitRoundRobinGraphInput = Omit<GraphInput, 'format'> & {
format: Extract<GraphInput['format'], { type: 'round-robin' }> & {
pairings: readonly ExplicitRoundRobinPairing[];
};
};
describe('generateSeriesGraph', () => {
it('generates a deterministic graph with stable opaque ids', () => {
const input = {
competitionId: 'competition-deterministic',
teams: Array.from({ length: 8 }, (_, index) => ({
teamId: `team-${index + 1}`,
displayOrder: index + 1,
seedNumber: index + 1,
})),
format: {
type: 'double-elimination' as const,
resetFinalEnabled: true,
rules: {
upper: firstTo(2, 3),
lower: firstTo(1, 1),
grandFinal: firstTo(1, 1),
resetFinal: firstTo(1, 1),
},
},
};
const first = generateSeriesGraph(input);
const second = generateSeriesGraph(input);
expect(second).toEqual(first);
expect(first.map((series) => series.id)).toHaveLength(15);
expect(new Set(first.map((series) => series.id))).toHaveProperty(
'size',
15,
);
expect(first.every((series) => series.id.length > 0)).toBe(true);
});
it('predefines a conditional grand-final reset node using only slot sources', () => {
const graph = generateSeriesGraph({
competitionId: 'competition-reset-node',
teams: Array.from({ length: 4 }, (_, index) => ({
teamId: `team-${index + 1}`,
displayOrder: index + 1,
seedNumber: index + 1,
})),
format: {
type: 'double-elimination' as const,
resetFinalEnabled: true,
rules: {
upper: firstTo(2, 3),
lower: firstTo(1, 1),
grandFinal: firstTo(1, 1),
resetFinal: firstTo(1, 1),
},
},
});
const grandFinal = graph.find((series) => series.code === 'GF');
const resetFinal = graph.find((series) => series.code === 'GF_RESET');
expect(grandFinal).toBeDefined();
expect(resetFinal).toMatchObject({
stage: { type: 'reset-final' },
activation: {
type: 'grand-final-reset-required',
grandFinalSeriesId: grandFinal!.id,
},
slots: [
{
position: 'A',
source: {
type: 'series-slot',
seriesId: grandFinal!.id,
slot: 'A',
},
},
{
position: 'B',
source: {
type: 'series-slot',
seriesId: grandFinal!.id,
slot: 'B',
},
},
],
});
for (const series of graph) {
expect(series).not.toHaveProperty('winnerAdvancesTo');
expect(series).not.toHaveProperty('loserAdvancesTo');
}
});
it('rejects a first-to rule that can exhaust every GameSlot without a winner', () => {
try {
generateSeriesGraph({
competitionId: 'competition-unfinishable-first-to',
teams: Array.from({ length: 4 }, (_, index) => ({
teamId: `team-${index + 1}`,
displayOrder: index + 1,
seedNumber: index + 1,
})),
format: {
type: 'double-elimination',
resetFinalEnabled: false,
rules: {
upper: firstTo(2, 2),
lower: firstTo(1, 1),
grandFinal: firstTo(1, 1),
resetFinal: firstTo(1, 1),
},
},
});
throw new Error('Expected INVALID_FIRST_TO_RULE');
} catch (error) {
expect(error).toMatchObject({ code: 'INVALID_FIRST_TO_RULE' });
}
});
it('reproduces an explicit persisted round-robin schedule by TeamId and pairing position', () => {
const input: ExplicitRoundRobinGraphInput = {
competitionId: 'competition-persisted-round-robin',
teams: Array.from({ length: 8 }, (_, index) => ({
teamId: `team-${index + 1}`,
displayOrder: 8 - index,
seedNumber: index + 1,
})),
format: {
type: 'round-robin',
rule: fixedGames(1),
maxRounds: 3,
tieBreakers: ['series-wins', 'game-diff', 'head-to-head'],
// Persisted schedules need not arrive in display order. Their explicit
// round/match positions, rather than array position, define identity.
pairings: [
pairing(3, 4, 'team-6', 'team-7'),
pairing(1, 2, 'team-3', 'team-4'),
pairing(2, 1, 'team-1', 'team-3'),
pairing(1, 4, 'team-7', 'team-8'),
pairing(3, 1, 'team-1', 'team-4'),
pairing(2, 3, 'team-5', 'team-7'),
pairing(1, 1, 'team-1', 'team-2'),
pairing(3, 3, 'team-5', 'team-8'),
pairing(2, 4, 'team-6', 'team-8'),
pairing(1, 3, 'team-5', 'team-6'),
pairing(3, 2, 'team-2', 'team-3'),
pairing(2, 2, 'team-2', 'team-4'),
],
},
};
const first = generateSeriesGraph(input);
const second = generateSeriesGraph(input);
expect(second).toEqual(first);
expect(
first.map((series) => ({
code: series.code,
roundNumber: series.stage.roundNumber,
displayOrder: series.stage.displayOrder,
sources: series.slots.map((slot) => slot.source),
})),
).toEqual([
roundRobinSeries('RR_R1_M1', 1, 1, 'team-1', 'team-2'),
roundRobinSeries('RR_R1_M2', 1, 2, 'team-3', 'team-4'),
roundRobinSeries('RR_R1_M3', 1, 3, 'team-5', 'team-6'),
roundRobinSeries('RR_R1_M4', 1, 4, 'team-7', 'team-8'),
roundRobinSeries('RR_R2_M1', 2, 5, 'team-1', 'team-3'),
roundRobinSeries('RR_R2_M2', 2, 6, 'team-2', 'team-4'),
roundRobinSeries('RR_R2_M3', 2, 7, 'team-5', 'team-7'),
roundRobinSeries('RR_R2_M4', 2, 8, 'team-6', 'team-8'),
roundRobinSeries('RR_R3_M1', 3, 9, 'team-1', 'team-4'),
roundRobinSeries('RR_R3_M2', 3, 10, 'team-2', 'team-3'),
roundRobinSeries('RR_R3_M3', 3, 11, 'team-5', 'team-8'),
roundRobinSeries('RR_R3_M4', 3, 12, 'team-6', 'team-7'),
]);
});
it.each([
{
name: 'a TeamId that is absent from the Competition',
replacement: pairing(1, 1, 'team-1', 'team-9'),
},
{
name: 'the same TeamId in both slots',
replacement: pairing(1, 1, 'team-1', 'team-1'),
},
{
name: 'a non-positive round number',
replacement: pairing(0, 1, 'team-1', 'team-2'),
},
{
name: 'a non-positive match number',
replacement: pairing(1, 0, 'team-1', 'team-2'),
},
])(
'rejects an explicit round-robin pairing with $name',
({ replacement }) => {
const input = explicitRoundRobinInput([
replacement,
pairing(1, 2, 'team-3', 'team-4'),
]);
expectGraphError(input, 'INVALID_ROUND_ROBIN_PAIRING');
},
);
it.each([
{
name: 'round and match position',
pairings: [
pairing(1, 1, 'team-1', 'team-2'),
pairing(1, 1, 'team-3', 'team-4'),
],
code: 'DUPLICATE_ROUND_ROBIN_PAIRING_POSITION',
},
{
name: 'opponents with reversed slots',
pairings: [
pairing(1, 1, 'team-1', 'team-2'),
pairing(2, 1, 'team-2', 'team-1'),
],
code: 'DUPLICATE_ROUND_ROBIN_OPPONENTS',
},
])('rejects a duplicate explicit $name', ({ pairings, code }) => {
expectGraphError(explicitRoundRobinInput(pairings), code);
});
});
function firstTo(winsRequired: number, maxGames: number) {
return { type: 'first-to' as const, winsRequired, maxGames };
}
function expectGraphError(input: GraphInput, code: string): void {
try {
generateSeriesGraph(input);
throw new Error(`Expected graph error ${code}.`);
} catch (error) {
expect(error).toMatchObject({ code });
}
}
function fixedGames(gameCount: number) {
return {
type: 'fixed-games' as const,
gameCount,
drawPolicy: 'allow' as const,
};
}
function pairing(
roundNumber: number,
matchNumber: number,
teamAId: string,
teamBId: string,
): ExplicitRoundRobinPairing {
return {
roundNumber,
matchNumber,
teamAId,
teamBId,
};
}
function explicitRoundRobinInput(
pairings: readonly ExplicitRoundRobinPairing[],
): ExplicitRoundRobinGraphInput {
return {
competitionId: 'competition-explicit-round-robin-validation',
teams: Array.from({ length: 4 }, (_, index) => ({
teamId: `team-${index + 1}`,
displayOrder: index + 1,
seedNumber: index + 1,
})),
format: {
type: 'round-robin',
rule: fixedGames(1),
maxRounds: 2,
tieBreakers: ['series-wins', 'game-diff', 'head-to-head'],
pairings,
},
};
}
function roundRobinSeries(
code: string,
roundNumber: number,
displayOrder: number,
teamAId: string,
teamBId: string,
) {
return {
code,
roundNumber,
displayOrder,
sources: [
{ type: 'team', teamId: teamAId },
{ type: 'team', teamId: teamBId },
],
};
}