import mongoose from 'mongoose'; import { buildRepairPlan } from './repair-double-elimination-series-rules'; import type { MatchDocument, TournamentCodeDocument } from '../schema'; function createMatch(overrides: Partial): MatchDocument { return { _id: new mongoose.Types.ObjectId(), gameCount: 3, tournamentCodeIds: [], doubleElimination: { teamCount: 8, resetFinalEnabled: false, resetFinalCreated: false, seeds: [], bracketMatches: [], placements: {}, events: [], }, ...overrides, } as unknown as MatchDocument; } function createCode( overrides: Partial, ): TournamentCodeDocument { return { _id: new mongoose.Types.ObjectId(), code: 'CODE', state: 'reserved', ...overrides, } as unknown as TournamentCodeDocument; } describe('repair-double-elimination-series-rules buildRepairPlan', () => { it('plans missing ready series slots even when result games already exist', () => { const match = createMatch({ result: { riotMatchIds: ['KR_1'], games: [ { gameNumber: 1, bracketMatchId: 'WB_R2_M1', riotMatchId: 'KR_1', blueTeamIndex: 4, redTeamIndex: 6, winnerTeamIndex: 4, winnerSide: 'blue', gameCreation: Date.parse('2026-05-03T00:00:00.000Z'), gameDuration: 1800, participants: [], }, ], seriesResults: [], }, doubleElimination: { teamCount: 8, resetFinalEnabled: false, resetFinalCreated: false, seeds: [], bracketMatches: [ { bracketMatchId: 'WB_R2_M1', gameNumber: 1, gameCount: 3, requiredWins: 2, bracketSide: 'winner', roundNumber: 2, matchNumber: 1, state: 'ready', teamAIndex: 4, teamBIndex: 6, }, ], placements: {}, events: [], }, }); const existingFirstSetCode = createCode({ bracketMatchId: 'WB_R2_M1', gameNumber: 1, }); const plan = buildRepairPlan(match, [existingFirstSetCode], 3); expect(plan.ruleRepairSkippedReason).toBe('result-games-exist'); expect(plan.codeUpdates).toEqual([]); expect(plan.missingReadySlots).toEqual([ { bracketMatchId: 'WB_R2_M1', gameNumber: 2, seriesGameNumber: 2, seriesGameCount: 3, }, { bracketMatchId: 'WB_R2_M1', gameNumber: 3, seriesGameNumber: 3, seriesGameCount: 3, }, ]); }); });