459 lines
12 KiB
TypeScript
459 lines
12 KiB
TypeScript
import { projectSeries } from './series-projector';
|
|
|
|
describe('projectSeries', () => {
|
|
it.each([
|
|
['a known local time', '21:30'],
|
|
['an unknown local time', null],
|
|
] as const)('preserves an optional Series schedule with %s', (_, time) => {
|
|
const schedule = {
|
|
date: '2026-07-15',
|
|
time,
|
|
timeZone: 'Asia/Seoul' as const,
|
|
};
|
|
const definition = {
|
|
...seriesDefinition({
|
|
id: `series-scheduled-${time ?? 'unknown'}`,
|
|
stage: 'upper',
|
|
rule: { type: 'first-to' as const, winsRequired: 1, maxGames: 1 },
|
|
}),
|
|
schedule,
|
|
};
|
|
|
|
const projection = projectSeries({
|
|
definition,
|
|
participants: ['team-8', 'team-3'],
|
|
facts: [],
|
|
});
|
|
|
|
expect(projection).toMatchObject({
|
|
status: 'ready',
|
|
schedule,
|
|
});
|
|
});
|
|
|
|
it('keeps an existing definition without a schedule explicitly unscheduled', () => {
|
|
const definition = seriesDefinition({
|
|
id: 'series-unscheduled',
|
|
stage: 'upper',
|
|
rule: { type: 'first-to', winsRequired: 1, maxGames: 1 },
|
|
});
|
|
|
|
const projection = projectSeries({
|
|
definition,
|
|
participants: ['team-8', 'team-3'],
|
|
facts: [],
|
|
});
|
|
|
|
expect(projection).not.toHaveProperty('schedule');
|
|
});
|
|
|
|
it.each([
|
|
[
|
|
'an impossible calendar date',
|
|
{ date: '2026-02-30', time: '21:30', timeZone: 'Asia/Seoul' },
|
|
],
|
|
[
|
|
'an out-of-range local time',
|
|
{ date: '2026-07-15', time: '24:00', timeZone: 'Asia/Seoul' },
|
|
],
|
|
[
|
|
'a non-canonical time zone',
|
|
{ date: '2026-07-15', time: '21:30', timeZone: 'UTC' },
|
|
],
|
|
])('rejects a Series schedule with %s', (_, schedule) => {
|
|
const definition = {
|
|
...seriesDefinition({
|
|
id: `series-invalid-schedule-${schedule.date}-${schedule.time}`,
|
|
stage: 'upper',
|
|
rule: { type: 'first-to' as const, winsRequired: 1, maxGames: 1 },
|
|
}),
|
|
schedule,
|
|
} as unknown as Parameters<typeof projectSeries>[0]['definition'];
|
|
|
|
expectInvariant(
|
|
() =>
|
|
projectSeries({
|
|
definition,
|
|
participants: ['team-8', 'team-3'],
|
|
facts: [],
|
|
}),
|
|
'SERIES_DEFINITION_INVALID',
|
|
);
|
|
});
|
|
|
|
it.each([null, false, ''])(
|
|
'rejects a non-object Series schedule value %p',
|
|
(schedule) => {
|
|
const definition = {
|
|
...seriesDefinition({
|
|
id: 'series-invalid-schedule-shape',
|
|
stage: 'upper',
|
|
rule: { type: 'first-to' as const, winsRequired: 1, maxGames: 1 },
|
|
}),
|
|
schedule,
|
|
} as unknown as Parameters<typeof projectSeries>[0]['definition'];
|
|
|
|
expectInvariant(
|
|
() =>
|
|
projectSeries({
|
|
definition,
|
|
participants: ['team-8', 'team-3'],
|
|
facts: [],
|
|
}),
|
|
'SERIES_DEFINITION_INVALID',
|
|
);
|
|
},
|
|
);
|
|
|
|
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',
|
|
);
|
|
});
|
|
|
|
it('allows an inactive descendant after all of its historical activity was reversed', () => {
|
|
const definition = seriesDefinition({
|
|
id: 'series-reversed-descendant',
|
|
stage: 'upper',
|
|
rule: { type: 'first-to', winsRequired: 1, maxGames: 1 },
|
|
});
|
|
const accepted = acceptedGame({
|
|
definition,
|
|
ordinal: 1,
|
|
blueTeamId: 'team-8',
|
|
redTeamId: 'team-3',
|
|
winnerTeamId: 'team-8',
|
|
});
|
|
|
|
expect(
|
|
projectSeries({
|
|
definition,
|
|
participants: [null, null],
|
|
active: false,
|
|
facts: [
|
|
accepted,
|
|
{
|
|
id: 'fact-series-reversed-descendant-void',
|
|
type: 'game-voided',
|
|
competitionId: 'competition-series-projector',
|
|
revision: 2,
|
|
ordinalWithinRevision: 1,
|
|
commandId: 'command-series-reversed-descendant-void',
|
|
schemaVersion: 1,
|
|
actor: { type: 'user' },
|
|
occurredAt: new Date('2026-07-12T00:02:00.000Z'),
|
|
recordedAt: new Date('2026-07-12T00:02:01.000Z'),
|
|
gameAcceptedFactId: accepted.id,
|
|
reason: 'Cascade reversal',
|
|
},
|
|
],
|
|
}),
|
|
).toMatchObject({ status: 'inactive' });
|
|
});
|
|
|
|
it('rejects an unfinishable persisted first-to definition', () => {
|
|
const definition = seriesDefinition({
|
|
id: 'series-unfinishable',
|
|
stage: 'upper',
|
|
rule: { type: 'first-to', winsRequired: 2, maxGames: 2 },
|
|
});
|
|
|
|
expectInvariant(
|
|
() =>
|
|
projectSeries({
|
|
definition,
|
|
participants: ['team-8', 'team-3'],
|
|
facts: [],
|
|
}),
|
|
'SERIES_DEFINITION_INVALID',
|
|
);
|
|
});
|
|
});
|
|
|
|
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 });
|
|
}
|
|
}
|