638 lines
18 KiB
TypeScript
638 lines
18 KiB
TypeScript
import { projectCompetition } from './competition-projector';
|
|
import {
|
|
CompetitionDomainError,
|
|
type CompetitionDomainErrorCode,
|
|
} from './domain-error';
|
|
import { generateSeriesGraph } from './series-graph';
|
|
|
|
describe('projectCompetition', () => {
|
|
it('keeps a three-way round-robin cycle on a shared rank', () => {
|
|
const competitionId = 'competition-round-robin-tie';
|
|
const teams = ['team-1', 'team-2', 'team-3'].map((teamId, index) => ({
|
|
teamId,
|
|
displayOrder: index + 1,
|
|
seedNumber: index + 1,
|
|
}));
|
|
const format = {
|
|
type: 'round-robin' as const,
|
|
rule: fixedGames(1),
|
|
tieBreakers: ['series-wins', 'game-diff', 'head-to-head'] as const,
|
|
};
|
|
const seriesGraph = generateSeriesGraph({
|
|
competitionId,
|
|
teams,
|
|
format,
|
|
});
|
|
const accept = acceptedGameFactory(competitionId);
|
|
const oneVersusTwo = requireRoundRobinSeries(
|
|
seriesGraph,
|
|
'team-1',
|
|
'team-2',
|
|
);
|
|
const oneVersusThree = requireRoundRobinSeries(
|
|
seriesGraph,
|
|
'team-1',
|
|
'team-3',
|
|
);
|
|
const twoVersusThree = requireRoundRobinSeries(
|
|
seriesGraph,
|
|
'team-2',
|
|
'team-3',
|
|
);
|
|
const facts = [
|
|
accept(oneVersusTwo, 1, 'team-1', 'team-2', 'team-1'),
|
|
accept(twoVersusThree, 1, 'team-2', 'team-3', 'team-2'),
|
|
accept(oneVersusThree, 1, 'team-3', 'team-1', 'team-3'),
|
|
];
|
|
|
|
const projection = projectCompetition({
|
|
competitionId,
|
|
revision: facts.length,
|
|
projectorVersion: 1,
|
|
teams,
|
|
format,
|
|
seriesGraph,
|
|
facts,
|
|
});
|
|
|
|
expect(projection.outcome).toEqual({
|
|
type: 'round-robin',
|
|
leaderTeamIds: ['team-1', 'team-2', 'team-3'],
|
|
standings: [
|
|
standing('team-1', 1),
|
|
standing('team-2', 1),
|
|
standing('team-3', 1),
|
|
],
|
|
completedAt: '2026-07-12T00:00:03.000Z',
|
|
});
|
|
expect(projection.games).toHaveLength(3);
|
|
expect(projection.gameSlots).toHaveLength(3);
|
|
expect(projection.rounds).toEqual(
|
|
expect.arrayContaining([
|
|
expect.objectContaining({ stage: 'round-robin', roundNumber: 1 }),
|
|
]),
|
|
);
|
|
});
|
|
|
|
it('attributes WB_R2_M2 wins to team ids when series and bracket slot orders differ', () => {
|
|
const competitionId = 'competition-wb-r2-m2-regression';
|
|
const teams = [
|
|
seededTeam('team-1', 1),
|
|
seededTeam('team-8', 2),
|
|
seededTeam('team-3', 3),
|
|
seededTeam('team-4', 4),
|
|
seededTeam('team-5', 5),
|
|
seededTeam('team-6', 6),
|
|
seededTeam('team-7', 7),
|
|
seededTeam('team-2', 8),
|
|
];
|
|
const format = doubleEliminationFormat({ upperWinsRequired: 2 });
|
|
const seriesGraph = generateSeriesGraph({
|
|
competitionId,
|
|
teams,
|
|
format,
|
|
});
|
|
const wbRoundOneMatchThree = requireSeries(seriesGraph, 'WB_R1_M3');
|
|
const wbRoundOneMatchFour = requireSeries(seriesGraph, 'WB_R1_M4');
|
|
const wbRoundTwoMatchTwo = requireSeries(seriesGraph, 'WB_R2_M2');
|
|
const accept = acceptedGameFactory(competitionId);
|
|
const facts = [
|
|
accept(wbRoundOneMatchThree, 1, 'team-8', 'team-7', 'team-8'),
|
|
accept(wbRoundOneMatchThree, 2, 'team-7', 'team-8', 'team-8'),
|
|
accept(wbRoundOneMatchFour, 1, 'team-3', 'team-6', 'team-3'),
|
|
accept(wbRoundOneMatchFour, 2, 'team-6', 'team-3', 'team-3'),
|
|
accept(wbRoundTwoMatchTwo, 1, 'team-3', 'team-8', 'team-8'),
|
|
accept(wbRoundTwoMatchTwo, 2, 'team-8', 'team-3', 'team-3'),
|
|
accept(wbRoundTwoMatchTwo, 3, 'team-3', 'team-8', 'team-8'),
|
|
];
|
|
|
|
const projection = projectCompetition({
|
|
competitionId,
|
|
revision: facts.length,
|
|
projectorVersion: 1,
|
|
teams,
|
|
format,
|
|
seriesGraph,
|
|
facts,
|
|
});
|
|
|
|
expect(projectedSeries(projection, 'WB_R2_M2')).toMatchObject({
|
|
status: 'completed',
|
|
entries: [
|
|
{ slot: 'A', teamId: 'team-8', gameWins: 2 },
|
|
{ slot: 'B', teamId: 'team-3', gameWins: 1 },
|
|
],
|
|
outcome: {
|
|
type: 'played',
|
|
winnerTeamId: 'team-8',
|
|
loserTeamId: 'team-3',
|
|
},
|
|
});
|
|
expect(projectedSeries(projection, 'WB_FINAL').entries).toContainEqual({
|
|
slot: 'B',
|
|
teamId: 'team-8',
|
|
gameWins: 0,
|
|
});
|
|
expect(projectedSeries(projection, 'LB_R2_M1').entries).toContainEqual({
|
|
slot: 'B',
|
|
teamId: 'team-3',
|
|
gameWins: 0,
|
|
});
|
|
expect(projection.games).toHaveLength(7);
|
|
expect(
|
|
projection.games.filter(
|
|
(game) => game.seriesId === wbRoundTwoMatchTwo.id,
|
|
),
|
|
).toEqual([
|
|
expect.objectContaining({ winnerTeamId: 'team-8', ordinal: 1 }),
|
|
expect.objectContaining({ winnerTeamId: 'team-3', ordinal: 2 }),
|
|
expect.objectContaining({ winnerTeamId: 'team-8', ordinal: 3 }),
|
|
]);
|
|
expect(
|
|
projection.connectors.some(
|
|
(connector) =>
|
|
connector.from.seriesId === wbRoundTwoMatchTwo.id &&
|
|
connector.from.select === 'winner' &&
|
|
connector.to.slot === 'B',
|
|
),
|
|
).toBe(true);
|
|
});
|
|
|
|
it('activates the predefined reset final only when the lower-bracket finalist wins GF', () => {
|
|
const competitionId = 'competition-grand-final-reset';
|
|
const teams = Array.from({ length: 4 }, (_, index) =>
|
|
seededTeam(`team-${index + 1}`, index + 1),
|
|
);
|
|
const format = doubleEliminationFormat({ upperWinsRequired: 1 });
|
|
const seriesGraph = generateSeriesGraph({
|
|
competitionId,
|
|
teams,
|
|
format,
|
|
});
|
|
const resetDefinition = requireSeries(seriesGraph, 'GF_RESET');
|
|
|
|
const initial = projectCompetition({
|
|
competitionId,
|
|
revision: 0,
|
|
projectorVersion: 1,
|
|
teams,
|
|
format,
|
|
seriesGraph,
|
|
facts: [],
|
|
});
|
|
expect(projectedSeries(initial, 'GF_RESET')).toMatchObject({
|
|
seriesId: resetDefinition.id,
|
|
status: 'inactive',
|
|
});
|
|
|
|
const accept = acceptedGameFactory(competitionId);
|
|
const facts = [
|
|
accept(
|
|
requireSeries(seriesGraph, 'WB_R1_M1'),
|
|
1,
|
|
'team-1',
|
|
'team-4',
|
|
'team-1',
|
|
),
|
|
accept(
|
|
requireSeries(seriesGraph, 'WB_R1_M2'),
|
|
1,
|
|
'team-2',
|
|
'team-3',
|
|
'team-2',
|
|
),
|
|
accept(
|
|
requireSeries(seriesGraph, 'WB_FINAL'),
|
|
1,
|
|
'team-1',
|
|
'team-2',
|
|
'team-1',
|
|
),
|
|
accept(
|
|
requireSeries(seriesGraph, 'LB_R1_M1'),
|
|
1,
|
|
'team-3',
|
|
'team-4',
|
|
'team-3',
|
|
),
|
|
accept(
|
|
requireSeries(seriesGraph, 'LB_FINAL'),
|
|
1,
|
|
'team-3',
|
|
'team-2',
|
|
'team-2',
|
|
),
|
|
accept(requireSeries(seriesGraph, 'GF'), 1, 'team-2', 'team-1', 'team-2'),
|
|
];
|
|
|
|
const afterGrandFinal = projectCompetition({
|
|
competitionId,
|
|
revision: facts.length,
|
|
projectorVersion: 1,
|
|
teams,
|
|
format,
|
|
seriesGraph,
|
|
facts,
|
|
});
|
|
|
|
expect(projectedSeries(afterGrandFinal, 'GF_RESET')).toMatchObject({
|
|
seriesId: resetDefinition.id,
|
|
status: 'ready',
|
|
entries: [
|
|
{ slot: 'A', teamId: 'team-1', gameWins: 0 },
|
|
{ slot: 'B', teamId: 'team-2', gameWins: 0 },
|
|
],
|
|
});
|
|
expect(afterGrandFinal).not.toHaveProperty('outcome');
|
|
expect(afterGrandFinal.gameSlots).toEqual(
|
|
expect.arrayContaining([
|
|
expect.objectContaining({
|
|
seriesId: resetDefinition.id,
|
|
status: 'ready',
|
|
entries: [
|
|
{ slot: 'A', teamId: 'team-1' },
|
|
{ slot: 'B', teamId: 'team-2' },
|
|
],
|
|
}),
|
|
]),
|
|
);
|
|
});
|
|
|
|
it('makes a voided GameSlot ready again when its Series can be replayed', () => {
|
|
const competitionId = 'competition-replay-voided-slot';
|
|
const teams = [seededTeam('team-1', 1), seededTeam('team-2', 2)];
|
|
const format = {
|
|
type: 'paired-series' as const,
|
|
rule: fixedGames(1),
|
|
};
|
|
const seriesGraph = generateSeriesGraph({
|
|
competitionId,
|
|
teams,
|
|
format,
|
|
});
|
|
const definition = seriesGraph[0];
|
|
const accepted = acceptedGameFactory(competitionId)(
|
|
definition,
|
|
1,
|
|
'team-1',
|
|
'team-2',
|
|
'team-1',
|
|
);
|
|
|
|
const projection = projectCompetition({
|
|
competitionId,
|
|
revision: 2,
|
|
projectorVersion: 1,
|
|
teams,
|
|
format,
|
|
seriesGraph,
|
|
facts: [
|
|
accepted,
|
|
{
|
|
id: 'fact-void-game-1',
|
|
type: 'game-voided',
|
|
competitionId,
|
|
revision: 2,
|
|
ordinalWithinRevision: 1,
|
|
commandId: 'command-void-game-1',
|
|
schemaVersion: 1,
|
|
actor: { type: 'user' as const },
|
|
occurredAt: new Date('2026-07-12T00:00:02.000Z'),
|
|
recordedAt: new Date('2026-07-12T00:01:02.000Z'),
|
|
gameAcceptedFactId: accepted.id,
|
|
reason: 'Incorrect Riot evidence',
|
|
},
|
|
],
|
|
});
|
|
|
|
expect(projection.phase).toBe('ready');
|
|
expect(projection.series[0]).toMatchObject({ status: 'ready' });
|
|
expect(projection.gameSlots[0]).toMatchObject({ status: 'ready' });
|
|
expect(projection.games).toEqual([]);
|
|
});
|
|
|
|
it('rejects an accepted game that targets a Series outside the graph', () => {
|
|
const competitionId = 'competition-unknown-game-series';
|
|
const teams = [seededTeam('team-1', 1), seededTeam('team-2', 2)];
|
|
const format = {
|
|
type: 'paired-series' as const,
|
|
rule: fixedGames(1),
|
|
};
|
|
const seriesGraph = generateSeriesGraph({ competitionId, teams, format });
|
|
const accepted = acceptedGameFactory(competitionId)(
|
|
seriesGraph[0],
|
|
1,
|
|
'team-1',
|
|
'team-2',
|
|
'team-1',
|
|
);
|
|
|
|
expectCompetitionDomainError(
|
|
() =>
|
|
projectCompetition({
|
|
competitionId,
|
|
revision: 1,
|
|
projectorVersion: 1,
|
|
teams,
|
|
format,
|
|
seriesGraph,
|
|
facts: [{ ...accepted, seriesId: 'series-does-not-exist' }],
|
|
}),
|
|
'GAME_SERIES_MISMATCH',
|
|
);
|
|
});
|
|
|
|
it('rejects an accepted game that targets an unknown GameSlot', () => {
|
|
const competitionId = 'competition-unknown-game-slot';
|
|
const teams = [seededTeam('team-1', 1), seededTeam('team-2', 2)];
|
|
const format = {
|
|
type: 'paired-series' as const,
|
|
rule: fixedGames(1),
|
|
};
|
|
const seriesGraph = generateSeriesGraph({ competitionId, teams, format });
|
|
const accepted = acceptedGameFactory(competitionId)(
|
|
seriesGraph[0],
|
|
1,
|
|
'team-1',
|
|
'team-2',
|
|
'team-1',
|
|
);
|
|
|
|
expectCompetitionDomainError(
|
|
() =>
|
|
projectCompetition({
|
|
competitionId,
|
|
revision: 1,
|
|
projectorVersion: 1,
|
|
teams,
|
|
format,
|
|
seriesGraph,
|
|
facts: [{ ...accepted, gameSlotId: 'slot-does-not-exist' }],
|
|
}),
|
|
'GAME_SLOT_INVALID',
|
|
);
|
|
});
|
|
|
|
it('rejects an adjudication that targets a Series outside the graph', () => {
|
|
const competitionId = 'competition-unknown-adjudication-series';
|
|
const teams = [seededTeam('team-1', 1), seededTeam('team-2', 2)];
|
|
const format = {
|
|
type: 'paired-series' as const,
|
|
rule: fixedGames(1),
|
|
};
|
|
const seriesGraph = generateSeriesGraph({ competitionId, teams, format });
|
|
|
|
expectCompetitionDomainError(
|
|
() =>
|
|
projectCompetition({
|
|
competitionId,
|
|
revision: 1,
|
|
projectorVersion: 1,
|
|
teams,
|
|
format,
|
|
seriesGraph,
|
|
facts: [
|
|
{
|
|
id: 'fact-unknown-adjudication-series',
|
|
type: 'series-adjudicated',
|
|
competitionId,
|
|
revision: 1,
|
|
ordinalWithinRevision: 1,
|
|
commandId: 'command-unknown-adjudication-series',
|
|
schemaVersion: 1,
|
|
actor: { type: 'user' },
|
|
occurredAt: new Date('2026-07-12T00:00:01.000Z'),
|
|
recordedAt: new Date('2026-07-12T00:00:02.000Z'),
|
|
seriesId: 'series-does-not-exist',
|
|
decision: { type: 'administrative', winnerTeamId: 'team-1' },
|
|
reason: 'Manual correction',
|
|
},
|
|
],
|
|
}),
|
|
'ADJUDICATION_INVALID',
|
|
);
|
|
});
|
|
|
|
it('completes an eight-team double elimination with shared elimination-round placements', () => {
|
|
const competitionId = 'competition-eight-team-completion';
|
|
const teams = Array.from({ length: 8 }, (_, index) =>
|
|
seededTeam(`team-${index + 1}`, index + 1),
|
|
);
|
|
const format = doubleEliminationFormat({ upperWinsRequired: 1 });
|
|
const seriesGraph = generateSeriesGraph({ competitionId, teams, format });
|
|
const accept = acceptedGameFactory(competitionId);
|
|
const facts: ReturnType<typeof accept>[] = [];
|
|
let projection = projectCompetition({
|
|
competitionId,
|
|
revision: 0,
|
|
projectorVersion: 1,
|
|
teams,
|
|
format,
|
|
seriesGraph,
|
|
facts,
|
|
});
|
|
|
|
for (let guard = 0; projection.phase !== 'completed'; guard += 1) {
|
|
if (guard > seriesGraph.length) {
|
|
throw new Error('Double-elimination projection did not converge.');
|
|
}
|
|
const playable = projection.series.find(
|
|
(series) => series.status === 'ready',
|
|
);
|
|
if (!playable) throw new Error('Missing playable Series.');
|
|
const definition = seriesGraph.find(
|
|
(series) => series.id === playable.seriesId,
|
|
);
|
|
const [entryA, entryB] = playable.entries;
|
|
if (!definition || !entryA.teamId || !entryB.teamId) {
|
|
throw new Error('Playable Series has unresolved participants.');
|
|
}
|
|
facts.push(
|
|
accept(definition, 1, entryA.teamId, entryB.teamId, entryA.teamId),
|
|
);
|
|
projection = projectCompetition({
|
|
competitionId,
|
|
revision: facts.length,
|
|
projectorVersion: 1,
|
|
teams,
|
|
format,
|
|
seriesGraph,
|
|
facts,
|
|
});
|
|
}
|
|
|
|
expect(projection.outcome?.type).toBe('double-elimination');
|
|
if (projection.outcome?.type !== 'double-elimination') {
|
|
throw new Error('Expected a double-elimination outcome.');
|
|
}
|
|
expect(typeof projection.outcome.championTeamId).toBe('string');
|
|
expect(projection.outcome.placements).toHaveLength(8);
|
|
expect(projection.placements?.map(({ rank }) => rank)).toEqual([
|
|
1, 2, 3, 4, 5, 5, 7, 7,
|
|
]);
|
|
});
|
|
});
|
|
|
|
function fixedGames(gameCount: number) {
|
|
return {
|
|
type: 'fixed-games' as const,
|
|
gameCount,
|
|
drawPolicy: 'allow' as const,
|
|
};
|
|
}
|
|
|
|
function seededTeam(teamId: string, seedNumber: number) {
|
|
return { teamId, displayOrder: seedNumber, seedNumber };
|
|
}
|
|
|
|
function doubleEliminationFormat(params: { upperWinsRequired: 1 | 2 }) {
|
|
const upper =
|
|
params.upperWinsRequired === 1
|
|
? { type: 'first-to' as const, winsRequired: 1, maxGames: 1 }
|
|
: { type: 'first-to' as const, winsRequired: 2, maxGames: 3 };
|
|
const singleGame = {
|
|
type: 'first-to' as const,
|
|
winsRequired: 1,
|
|
maxGames: 1,
|
|
};
|
|
return {
|
|
type: 'double-elimination' as const,
|
|
resetFinalEnabled: true,
|
|
rules: {
|
|
upper,
|
|
lower: singleGame,
|
|
grandFinal: singleGame,
|
|
resetFinal: singleGame,
|
|
},
|
|
};
|
|
}
|
|
|
|
function requireSeries<
|
|
T extends { code?: string; id: string; gameSlots: Array<GameSlotLike> },
|
|
>(graph: T[], code: string): T {
|
|
const definition = graph.find((series) => series.code === code);
|
|
if (!definition) throw new Error(`Missing series ${code}`);
|
|
return definition;
|
|
}
|
|
|
|
function requireRoundRobinSeries<
|
|
T extends {
|
|
id: string;
|
|
gameSlots: Array<GameSlotLike>;
|
|
slots: Array<{ source: { type: string; teamId?: string } }>;
|
|
},
|
|
>(graph: T[], leftTeamId: string, rightTeamId: string): T {
|
|
const expected = new Set([leftTeamId, rightTeamId]);
|
|
const definition = graph.find((series) => {
|
|
const actual = series.slots
|
|
.filter((slot) => slot.source.type === 'team')
|
|
.map((slot) => slot.source.teamId);
|
|
return (
|
|
actual.length === 2 && actual.every((teamId) => expected.has(teamId!))
|
|
);
|
|
});
|
|
if (!definition) {
|
|
throw new Error(`Missing round-robin series ${leftTeamId}/${rightTeamId}`);
|
|
}
|
|
return definition;
|
|
}
|
|
|
|
function acceptedGameFactory(competitionId: string) {
|
|
let revision = 0;
|
|
return <T extends { id: string; gameSlots: Array<GameSlotLike> }>(
|
|
definition: T,
|
|
ordinal: number,
|
|
blueTeamId: string,
|
|
redTeamId: string,
|
|
winnerTeamId: string,
|
|
) => {
|
|
revision += 1;
|
|
const gameSlot = definition.gameSlots.find(
|
|
(candidate) => candidate.ordinal === ordinal,
|
|
);
|
|
if (!gameSlot) {
|
|
throw new Error(`Missing game slot ${definition.id}/${ordinal}`);
|
|
}
|
|
return {
|
|
id: `fact-${revision}`,
|
|
type: 'game-accepted' as const,
|
|
competitionId,
|
|
revision,
|
|
ordinalWithinRevision: 1,
|
|
commandId: `command-${revision}`,
|
|
schemaVersion: 1,
|
|
actor: { type: 'system' as const },
|
|
occurredAt: new Date(
|
|
`2026-07-12T00:00:${String(revision).padStart(2, '0')}.000Z`,
|
|
),
|
|
recordedAt: new Date(
|
|
`2026-07-12T00:01:${String(revision).padStart(2, '0')}.000Z`,
|
|
),
|
|
gameId: `game-${revision}`,
|
|
seriesId: definition.id,
|
|
gameSlotId: gameSlot.id,
|
|
ordinal,
|
|
evidence: {
|
|
type: 'riot' as const,
|
|
riotMatchId: `KR_${competitionId}_${revision}`,
|
|
payloadHash: `sha256:${competitionId}:${revision}`,
|
|
},
|
|
mapping: {
|
|
mappingVersion: 1,
|
|
confidence: 1,
|
|
blueTeamId,
|
|
redTeamId,
|
|
winnerTeamId,
|
|
participantAssignments: [],
|
|
},
|
|
};
|
|
};
|
|
}
|
|
|
|
function projectedSeries<
|
|
T extends { series: Array<{ code?: string; seriesId: string }> },
|
|
>(projection: T, code: string) {
|
|
const series = projection.series.find((candidate) => candidate.code === code);
|
|
if (!series) throw new Error(`Missing projected series ${code}`);
|
|
return series;
|
|
}
|
|
|
|
function standing(teamId: string, rank: number) {
|
|
return {
|
|
teamId,
|
|
rank,
|
|
seriesPlayed: 2,
|
|
seriesWins: 1,
|
|
seriesLosses: 1,
|
|
seriesDraws: 0,
|
|
gameWins: 1,
|
|
gameLosses: 1,
|
|
gameDiff: 0,
|
|
};
|
|
}
|
|
|
|
interface GameSlotLike {
|
|
id: string;
|
|
ordinal: number;
|
|
}
|
|
|
|
function expectCompetitionDomainError(
|
|
action: () => unknown,
|
|
expectedCode: CompetitionDomainErrorCode,
|
|
): void {
|
|
let thrown: unknown;
|
|
try {
|
|
action();
|
|
} catch (error: unknown) {
|
|
thrown = error;
|
|
}
|
|
|
|
expect(thrown).toBeInstanceOf(CompetitionDomainError);
|
|
if (!(thrown instanceof CompetitionDomainError)) {
|
|
throw new Error(
|
|
`Expected CompetitionDomainError, received ${String(thrown)}`,
|
|
);
|
|
}
|
|
expect(thrown.code).toBe(expectedCode);
|
|
}
|