feat: unify competition schedule and results

This commit is contained in:
dahyeon committed 2026-07-15 01:01:49 -07:00
1 parent 4e10998794
commit c9140b8622
12 files changed
+910 -245

No files matched your search

@@ -51,6 +51,11 @@ export interface SeriesDefinition {
{ position: 'B'; source: ParticipantSource },
];
rule: SeriesRule;
schedule?: {
date: string;
time: string | null;
timeZone: 'Asia/Seoul';
};
activation?: {
type: 'grand-final-reset-required';
grandFinalSeriesId: string;
@@ -1,6 +1,110 @@
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',
@@ -13,6 +13,12 @@ export type SeriesRule =
drawPolicy: 'allow' | 'tiebreak-required';
};
export interface SeriesSchedule {
date: string;
time: string | null;
timeZone: 'Asia/Seoul';
}
export interface SeriesDefinitionLike {
id: string;
code?: string;
@@ -26,6 +32,7 @@ export interface SeriesDefinitionLike {
source: Readonly<Record<string, unknown>>;
}>;
rule: SeriesRule;
schedule?: SeriesSchedule;
gameSlots: ReadonlyArray<{
id: string;
seriesId: string;
@@ -75,6 +82,7 @@ interface SeriesProjectionBase {
code?: string;
stage: SeriesDefinitionLike['stage'];
rule: SeriesRule;
schedule?: SeriesSchedule;
entries: [SeriesProjectionEntry, SeriesProjectionEntry];
}
@@ -311,6 +319,15 @@ function validateDefinition(definition: SeriesDefinitionLike): void {
`Series ${definition.id} must define exactly the local A and B slots`,
);
const schedule: unknown = definition.schedule;
if (schedule !== undefined) {
assertCompetitionInvariant(
isSeriesSchedule(schedule),
'SERIES_DEFINITION_INVALID',
`Series ${definition.id} has an invalid schedule`,
);
}
const requiredSlots =
definition.rule.type === 'fixed-games'
? definition.rule.gameCount
@@ -753,6 +770,7 @@ function projectionBase(
...(definition.code ? { code: definition.code } : {}),
stage: definition.stage,
rule: definition.rule,
...(definition.schedule ? { schedule: { ...definition.schedule } } : {}),
entries: [
{ slot: 'A', teamId: participants[0], gameWins: 0 },
{ slot: 'B', teamId: participants[1], gameWins: 0 },
@@ -760,6 +778,35 @@ function projectionBase(
};
}
function isValidLocalDate(value: string): boolean {
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
if (!match) return false;
const year = Number(match[1]);
const month = Number(match[2]);
const day = Number(match[3]);
const date = new Date(Date.UTC(year, month - 1, day));
return (
date.getUTCFullYear() === year &&
date.getUTCMonth() === month - 1 &&
date.getUTCDate() === day
);
}
function isSeriesSchedule(value: unknown): value is SeriesSchedule {
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
return false;
}
const schedule = value as Record<string, unknown>;
return (
typeof schedule.date === 'string' &&
isValidLocalDate(schedule.date) &&
(schedule.time === null ||
(typeof schedule.time === 'string' &&
/^(?:[01]\d|2[0-3]):[0-5]\d$/.test(schedule.time))) &&
schedule.timeZone === 'Asia/Seoul'
);
}
function entriesFor(
participants: [string, string],
winsByTeam: Map<string, number>,