fix: keep best-of-three grand finals open for results
Allow a separate grand final format and repair prematurely completed finals without dropping accepted games. Replay the correction record alongside subsequent results.
This commit is contained in:
1 parent
38a89b5374
commit
7e0cd7cb19
21 files changed
+914
-23
No files matched your search
@@ -0,0 +1,293 @@
|
||||
import mongoose from 'mongoose';
|
||||
import { projectCompetition } from '../../../competition/domain/competition-projector';
|
||||
import { generateSeriesGraph } from '../../../competition/domain/series-graph';
|
||||
import {
|
||||
parseGrandFinalRepairOptions,
|
||||
planGrandFinalBo3Repair,
|
||||
repairGrandFinalBo3,
|
||||
} from './repair-grand-final-bo3';
|
||||
|
||||
describe('grand final BO3 repair', () => {
|
||||
it.each([false, true])(
|
||||
'keeps game 28 and finishes only after two wins (split: %s)',
|
||||
(split) => {
|
||||
const input = finishedTournament();
|
||||
const plan = planGrandFinalBo3Repair(input);
|
||||
|
||||
expect(plan.before.phase).toBe('completed');
|
||||
expect(plan.preservedGame).toBe(28);
|
||||
expect(plan.newGameNumbers).toEqual([29, 30]);
|
||||
expect(plan.projection.games).toEqual(plan.before.games);
|
||||
expect(plan.projection).toMatchObject({
|
||||
phase: 'running',
|
||||
placements: [],
|
||||
});
|
||||
expect(plan.projection).not.toHaveProperty('outcome');
|
||||
expect(
|
||||
plan.projection.series.find(
|
||||
({ seriesId }) => seriesId === plan.seriesId,
|
||||
),
|
||||
).toMatchObject({
|
||||
status: 'in-progress',
|
||||
entries: [
|
||||
{ slot: 'A', gameWins: 1 },
|
||||
{ slot: 'B', gameWins: 0 },
|
||||
],
|
||||
});
|
||||
|
||||
const facts = [
|
||||
...input.facts,
|
||||
{
|
||||
id: 'rule-audit',
|
||||
type: 'grand-final-rule-corrected',
|
||||
seriesId: plan.seriesId,
|
||||
revision: plan.projection.sourceRevision,
|
||||
ordinalWithinRevision: 1,
|
||||
},
|
||||
];
|
||||
const replay = () =>
|
||||
projectCompetition({
|
||||
...input,
|
||||
facts,
|
||||
revision: facts.length,
|
||||
format: plan.format,
|
||||
seriesGraph: plan.seriesGraph,
|
||||
});
|
||||
expect(replay()).toEqual(plan.projection);
|
||||
|
||||
const second = replay().gameSlots.find(
|
||||
({ displayNumber }) => displayNumber === 29,
|
||||
)!;
|
||||
facts.push(acceptedGame(second, facts.length + 1, split ? 'B' : 'A'));
|
||||
if (split) {
|
||||
expect(replay().phase).toBe('running');
|
||||
const third = replay().gameSlots.find(
|
||||
({ displayNumber }) => displayNumber === 30,
|
||||
)!;
|
||||
expect(third.status).toBe('ready');
|
||||
facts.push(acceptedGame(third, facts.length + 1, 'A'));
|
||||
}
|
||||
const completed = replay();
|
||||
expect(completed.phase).toBe('completed');
|
||||
expect(
|
||||
completed.gameSlots.find(({ displayNumber }) => displayNumber === 30)
|
||||
?.status,
|
||||
).toBe(split ? 'completed' : 'inactive');
|
||||
expect(completed.games.at(-1)?.displayNumber).toBe(split ? 30 : 29);
|
||||
},
|
||||
);
|
||||
|
||||
it('refuses to reopen a bracket that enables a reset final', () => {
|
||||
const input = finishedTournament();
|
||||
expect(() =>
|
||||
planGrandFinalBo3Repair({
|
||||
...input,
|
||||
format: { ...input.format, resetFinalEnabled: true },
|
||||
}),
|
||||
).toThrow('no reset or later series');
|
||||
});
|
||||
|
||||
it('requires a target and a reviewed revision before applying', () => {
|
||||
const target = ['--match-id', '000000000000000000000001', '--env', 'prod'];
|
||||
expect(parseGrandFinalRepairOptions(target)).toMatchObject({
|
||||
apply: false,
|
||||
});
|
||||
expect(() => parseGrandFinalRepairOptions(['--apply', ...target])).toThrow(
|
||||
'--expected-revision',
|
||||
);
|
||||
expect(
|
||||
parseGrandFinalRepairOptions([
|
||||
'--apply',
|
||||
...target,
|
||||
'--expected-revision',
|
||||
'23',
|
||||
]),
|
||||
).toMatchObject({ apply: true, expectedRevision: 23 });
|
||||
});
|
||||
|
||||
it('previews without writes and applies the graph, match status and audit in one transaction', async () => {
|
||||
const input = finishedTournament();
|
||||
const competitionId = new mongoose.Types.ObjectId(input.competitionId);
|
||||
const matchId = new mongoose.Types.ObjectId();
|
||||
const communityId = new mongoose.Types.ObjectId();
|
||||
const match = {
|
||||
_id: matchId,
|
||||
competitionId,
|
||||
communityId,
|
||||
gameMode: 'double-elimination',
|
||||
status: 'finished',
|
||||
teamBuiltAt: new Date(),
|
||||
doubleElimination: { resetFinalEnabled: false },
|
||||
resultPollState: { state: 'complete' },
|
||||
};
|
||||
const competition = {
|
||||
...input,
|
||||
_id: competitionId,
|
||||
schemaVersion: 1,
|
||||
phase: 'completed',
|
||||
teams: input.teams.map((team) => ({
|
||||
...team,
|
||||
teamId: new mongoose.Types.ObjectId(team.teamId),
|
||||
})),
|
||||
projection: projectCompetition(input),
|
||||
};
|
||||
const query = (value: unknown) => ({
|
||||
session: jest.fn().mockReturnThis(),
|
||||
lean: jest.fn().mockReturnThis(),
|
||||
sort: jest.fn().mockReturnThis(),
|
||||
exec: jest.fn().mockResolvedValue(value),
|
||||
});
|
||||
const update = () => jest.fn(() => query({ matchedCount: 1 }));
|
||||
const models = {
|
||||
match: { findById: jest.fn(() => query(match)), updateOne: update() },
|
||||
competition: {
|
||||
findOne: jest.fn(() => query(competition)),
|
||||
updateOne: update(),
|
||||
},
|
||||
competitionFact: {
|
||||
find: jest.fn(() => query(input.facts)),
|
||||
insertMany: jest.fn(),
|
||||
},
|
||||
competitionOutbox: { insertMany: jest.fn() },
|
||||
};
|
||||
const session = {
|
||||
withTransaction: jest.fn((run: () => unknown) => run()),
|
||||
endSession: jest.fn(),
|
||||
};
|
||||
const connections = {
|
||||
getModel: jest.fn((name: keyof typeof models) => models[name]),
|
||||
createSession: jest.fn().mockResolvedValue(session),
|
||||
} as never;
|
||||
const options = { matchId, nodeEnv: 'prod', apply: false };
|
||||
|
||||
const preview = await repairGrandFinalBo3(connections, options);
|
||||
expect(preview).toMatchObject({
|
||||
mode: 'dry-run',
|
||||
preservedGame: 28,
|
||||
newGameNumbers: [29, 30],
|
||||
});
|
||||
expect(models.match.updateOne).not.toHaveBeenCalled();
|
||||
expect(models.competition.updateOne).not.toHaveBeenCalled();
|
||||
expect(models.competitionFact.insertMany).not.toHaveBeenCalled();
|
||||
expect(models.competitionOutbox.insertMany).not.toHaveBeenCalled();
|
||||
|
||||
await expect(
|
||||
repairGrandFinalBo3(connections, {
|
||||
...options,
|
||||
apply: true,
|
||||
expectedRevision: preview.revisionBefore,
|
||||
}),
|
||||
).resolves.toMatchObject({
|
||||
mode: 'applied',
|
||||
phase: 'completed -> running',
|
||||
});
|
||||
expect(session.withTransaction).toHaveBeenCalledTimes(1);
|
||||
expect(models.match.updateOne).toHaveBeenCalledWith(
|
||||
{ _id: matchId, status: 'finished', competitionId },
|
||||
{
|
||||
$set: expect.objectContaining({
|
||||
status: 'in-progress',
|
||||
'resultPollState.state': 'polling',
|
||||
}),
|
||||
},
|
||||
{ session, runValidators: true },
|
||||
);
|
||||
expect(models.competition.updateOne).toHaveBeenCalledWith(
|
||||
{ _id: competitionId, revision: preview.revisionBefore },
|
||||
{
|
||||
$set: expect.objectContaining({
|
||||
phase: 'running',
|
||||
revision: preview.revisionAfter,
|
||||
}),
|
||||
},
|
||||
{ session, runValidators: true },
|
||||
);
|
||||
expect(models.competitionFact.insertMany).toHaveBeenCalledWith(
|
||||
[
|
||||
expect.objectContaining({
|
||||
type: 'grand-final-rule-corrected',
|
||||
revision: preview.revisionAfter,
|
||||
}),
|
||||
],
|
||||
{ session },
|
||||
);
|
||||
expect(models.competitionOutbox.insertMany).toHaveBeenCalledWith(
|
||||
[
|
||||
expect.objectContaining({
|
||||
type: 'competition.projection-updated',
|
||||
revision: preview.revisionAfter,
|
||||
}),
|
||||
],
|
||||
{ session },
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
function finishedTournament() {
|
||||
const competitionId = '000000000000000000000001';
|
||||
const teams = Array.from({ length: 8 }, (_, index) => ({
|
||||
teamId: (index + 1).toString(16).padStart(24, '0'),
|
||||
displayOrder: index + 1,
|
||||
seedNumber: index + 1,
|
||||
}));
|
||||
const bo1 = { type: 'first-to', maxGames: 1, winsRequired: 1 } as const;
|
||||
const format = {
|
||||
type: 'double-elimination' as const,
|
||||
teamCount: 8 as const,
|
||||
resetFinalEnabled: false,
|
||||
rules: {
|
||||
upper: { ...bo1, maxGames: 3, winsRequired: 2 },
|
||||
lower: bo1,
|
||||
grandFinal: bo1,
|
||||
resetFinal: bo1,
|
||||
},
|
||||
};
|
||||
const seriesGraph = generateSeriesGraph({ competitionId, teams, format });
|
||||
const facts: Record<string, unknown>[] = [];
|
||||
const input = {
|
||||
competitionId,
|
||||
teams,
|
||||
format,
|
||||
seriesGraph,
|
||||
facts,
|
||||
projectorVersion: 1,
|
||||
revision: 0,
|
||||
};
|
||||
for (let index = 0; index < 28; index += 1) {
|
||||
const slot = projectCompetition(input).gameSlots.find(
|
||||
({ status }) => status === 'ready',
|
||||
);
|
||||
if (!slot) break;
|
||||
facts.push(acceptedGame(slot, index + 1, 'A'));
|
||||
input.revision += 1;
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
function acceptedGame(
|
||||
slot: ReturnType<typeof projectCompetition>['gameSlots'][number],
|
||||
revision: number,
|
||||
winner: 'A' | 'B',
|
||||
): Record<string, unknown> {
|
||||
const [a, b] = slot.entries;
|
||||
const recordedAt = new Date(Date.UTC(2026, 6, 26, 0, revision)).toISOString();
|
||||
return {
|
||||
id: `fact-${revision}`,
|
||||
type: 'game-accepted',
|
||||
revision,
|
||||
ordinalWithinRevision: 1,
|
||||
gameId: `game-${revision}`,
|
||||
seriesId: slot.seriesId,
|
||||
gameSlotId: slot.id,
|
||||
ordinal: slot.ordinal,
|
||||
recordedAt,
|
||||
occurredAt: recordedAt,
|
||||
evidence: { type: 'riot', riotMatchId: `KR_${revision}` },
|
||||
mapping: {
|
||||
blueTeamId: a.teamId,
|
||||
redTeamId: b.teamId,
|
||||
winnerTeamId: winner === 'A' ? a.teamId : b.teamId,
|
||||
participantAssignments: [],
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user