feat: project competition results by team identity
This commit is contained in:
1 parent
39112ff7ff
commit
5d4fe443d0
9 files changed
+3408
No files matched your search
@@ -0,0 +1,296 @@
|
||||
import { projectSeries } from './series-projector';
|
||||
|
||||
describe('projectSeries', () => {
|
||||
it('keeps a fixed-games pair in progress until every game slot is filled', () => {
|
||||
const definition = seriesDefinition({
|
||||
id: 'series-pair',
|
||||
stage: 'pair',
|
||||
rule: {
|
||||
type: 'fixed-games',
|
||||
gameCount: 3,
|
||||
drawPolicy: 'allow',
|
||||
},
|
||||
});
|
||||
const firstTwoGames = [
|
||||
acceptedGame({
|
||||
definition,
|
||||
ordinal: 1,
|
||||
blueTeamId: 'team-8',
|
||||
redTeamId: 'team-3',
|
||||
winnerTeamId: 'team-8',
|
||||
}),
|
||||
acceptedGame({
|
||||
definition,
|
||||
ordinal: 2,
|
||||
blueTeamId: 'team-3',
|
||||
redTeamId: 'team-8',
|
||||
winnerTeamId: 'team-8',
|
||||
}),
|
||||
];
|
||||
|
||||
const inProgress = projectSeries({
|
||||
definition,
|
||||
participants: ['team-8', 'team-3'],
|
||||
facts: firstTwoGames,
|
||||
});
|
||||
|
||||
expect(inProgress).toMatchObject({
|
||||
status: 'in-progress',
|
||||
leaderTeamId: 'team-8',
|
||||
entries: [
|
||||
{ slot: 'A', teamId: 'team-8', gameWins: 2 },
|
||||
{ slot: 'B', teamId: 'team-3', gameWins: 0 },
|
||||
],
|
||||
});
|
||||
expect(inProgress).not.toHaveProperty('outcome');
|
||||
|
||||
const completed = projectSeries({
|
||||
definition,
|
||||
participants: ['team-8', 'team-3'],
|
||||
facts: [
|
||||
...firstTwoGames,
|
||||
acceptedGame({
|
||||
definition,
|
||||
ordinal: 3,
|
||||
blueTeamId: 'team-8',
|
||||
redTeamId: 'team-3',
|
||||
winnerTeamId: 'team-3',
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
expect(completed).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(completed).not.toHaveProperty('leaderTeamId');
|
||||
});
|
||||
|
||||
it('reports a leader during first-to play and a winner only after the threshold', () => {
|
||||
const definition = seriesDefinition({
|
||||
id: 'series-first-to',
|
||||
stage: 'upper',
|
||||
rule: { type: 'first-to', winsRequired: 2, maxGames: 3 },
|
||||
});
|
||||
const firstGame = acceptedGame({
|
||||
definition,
|
||||
ordinal: 1,
|
||||
blueTeamId: 'team-8',
|
||||
redTeamId: 'team-3',
|
||||
winnerTeamId: 'team-8',
|
||||
});
|
||||
|
||||
const oneToZero = projectSeries({
|
||||
definition,
|
||||
participants: ['team-8', 'team-3'],
|
||||
facts: [firstGame],
|
||||
});
|
||||
|
||||
expect(oneToZero).toMatchObject({
|
||||
status: 'in-progress',
|
||||
leaderTeamId: 'team-8',
|
||||
});
|
||||
expect(oneToZero).not.toHaveProperty('outcome');
|
||||
|
||||
const twoToOne = projectSeries({
|
||||
definition,
|
||||
participants: ['team-8', 'team-3'],
|
||||
facts: [
|
||||
firstGame,
|
||||
acceptedGame({
|
||||
definition,
|
||||
ordinal: 2,
|
||||
blueTeamId: 'team-8',
|
||||
redTeamId: 'team-3',
|
||||
winnerTeamId: 'team-3',
|
||||
}),
|
||||
acceptedGame({
|
||||
definition,
|
||||
ordinal: 3,
|
||||
blueTeamId: 'team-3',
|
||||
redTeamId: 'team-8',
|
||||
winnerTeamId: 'team-8',
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
expect(twoToOne).toMatchObject({
|
||||
status: 'completed',
|
||||
outcome: {
|
||||
type: 'played',
|
||||
winnerTeamId: 'team-8',
|
||||
loserTeamId: 'team-3',
|
||||
decidedByGameIds: [
|
||||
'game-series-first-to-1',
|
||||
'game-series-first-to-2',
|
||||
'game-series-first-to-3',
|
||||
],
|
||||
},
|
||||
});
|
||||
expect(twoToOne).not.toHaveProperty('leaderTeamId');
|
||||
});
|
||||
|
||||
it('rejects accepted evidence whose teams do not match the series participants', () => {
|
||||
const definition = seriesDefinition({
|
||||
id: 'series-invalid-team',
|
||||
stage: 'upper',
|
||||
rule: { type: 'first-to', winsRequired: 2, maxGames: 3 },
|
||||
});
|
||||
|
||||
expectInvariant(
|
||||
() =>
|
||||
projectSeries({
|
||||
definition,
|
||||
participants: ['team-8', 'team-3'],
|
||||
facts: [
|
||||
acceptedGame({
|
||||
definition,
|
||||
ordinal: 1,
|
||||
blueTeamId: 'team-8',
|
||||
redTeamId: 'team-9',
|
||||
winnerTeamId: 'team-8',
|
||||
}),
|
||||
],
|
||||
}),
|
||||
'GAME_TEAM_MISMATCH',
|
||||
);
|
||||
});
|
||||
|
||||
it('rejects a game accepted after a first-to series was already complete', () => {
|
||||
const definition = seriesDefinition({
|
||||
id: 'series-extra-game',
|
||||
stage: 'upper',
|
||||
rule: { type: 'first-to', winsRequired: 2, maxGames: 3 },
|
||||
});
|
||||
|
||||
expectInvariant(
|
||||
() =>
|
||||
projectSeries({
|
||||
definition,
|
||||
participants: ['team-8', 'team-3'],
|
||||
facts: [
|
||||
acceptedGame({
|
||||
definition,
|
||||
ordinal: 1,
|
||||
blueTeamId: 'team-8',
|
||||
redTeamId: 'team-3',
|
||||
winnerTeamId: 'team-8',
|
||||
}),
|
||||
acceptedGame({
|
||||
definition,
|
||||
ordinal: 2,
|
||||
blueTeamId: 'team-3',
|
||||
redTeamId: 'team-8',
|
||||
winnerTeamId: 'team-8',
|
||||
}),
|
||||
acceptedGame({
|
||||
definition,
|
||||
ordinal: 3,
|
||||
blueTeamId: 'team-8',
|
||||
redTeamId: 'team-3',
|
||||
winnerTeamId: 'team-3',
|
||||
}),
|
||||
],
|
||||
}),
|
||||
'SERIES_ALREADY_COMPLETED',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
function seriesDefinition(params: {
|
||||
id: string;
|
||||
stage: 'pair' | 'upper';
|
||||
rule:
|
||||
| {
|
||||
type: 'fixed-games';
|
||||
gameCount: number;
|
||||
drawPolicy: 'allow';
|
||||
}
|
||||
| { type: 'first-to'; winsRequired: number; maxGames: number };
|
||||
}) {
|
||||
const gameCount =
|
||||
params.rule.type === 'fixed-games'
|
||||
? params.rule.gameCount
|
||||
: params.rule.maxGames;
|
||||
return {
|
||||
id: params.id,
|
||||
code: params.id,
|
||||
stage: { type: params.stage, displayOrder: 1 },
|
||||
slots: [
|
||||
{
|
||||
position: 'A' as const,
|
||||
source: { type: 'team' as const, teamId: 'team-8' },
|
||||
},
|
||||
{
|
||||
position: 'B' as const,
|
||||
source: { type: 'team' as const, teamId: 'team-3' },
|
||||
},
|
||||
],
|
||||
rule: params.rule,
|
||||
gameSlots: Array.from({ length: gameCount }, (_, index) => ({
|
||||
id: `${params.id}-slot-${index + 1}`,
|
||||
seriesId: params.id,
|
||||
ordinal: index + 1,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
function acceptedGame(params: {
|
||||
definition: ReturnType<typeof seriesDefinition>;
|
||||
ordinal: number;
|
||||
blueTeamId: string;
|
||||
redTeamId: string;
|
||||
winnerTeamId: string;
|
||||
}) {
|
||||
const gameSlot = params.definition.gameSlots.find(
|
||||
(slot) => slot.ordinal === params.ordinal,
|
||||
);
|
||||
if (!gameSlot) throw new Error(`Missing game slot ${params.ordinal}`);
|
||||
|
||||
return {
|
||||
id: `fact-${params.definition.id}-${params.ordinal}`,
|
||||
type: 'game-accepted' as const,
|
||||
competitionId: 'competition-series-projector',
|
||||
revision: params.ordinal,
|
||||
ordinalWithinRevision: 1,
|
||||
commandId: `command-${params.definition.id}-${params.ordinal}`,
|
||||
schemaVersion: 1,
|
||||
actor: { type: 'system' as const },
|
||||
occurredAt: new Date(`2026-07-12T00:0${params.ordinal}:00.000Z`),
|
||||
recordedAt: new Date(`2026-07-12T00:0${params.ordinal}:01.000Z`),
|
||||
gameId: `game-${params.definition.id}-${params.ordinal}`,
|
||||
seriesId: params.definition.id,
|
||||
gameSlotId: gameSlot.id,
|
||||
ordinal: params.ordinal,
|
||||
evidence: {
|
||||
type: 'riot' as const,
|
||||
riotMatchId: `KR_${params.definition.id}_${params.ordinal}`,
|
||||
payloadHash: `sha256:${params.definition.id}:${params.ordinal}`,
|
||||
},
|
||||
mapping: {
|
||||
mappingVersion: 1,
|
||||
confidence: 1,
|
||||
blueTeamId: params.blueTeamId,
|
||||
redTeamId: params.redTeamId,
|
||||
winnerTeamId: params.winnerTeamId,
|
||||
participantAssignments: [],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function expectInvariant(action: () => unknown, code: string) {
|
||||
try {
|
||||
action();
|
||||
throw new Error(`Expected invariant ${code}`);
|
||||
} catch (error) {
|
||||
expect(error).toMatchObject({ code });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user