import { projectCompetition } from '../../../competition/domain/competition-projector'; import { planCompetitionMigration, type LegacyMatchInput, type ReadyCompetitionMigrationPlan, } from './20260712-competition-domain'; describe('planCompetitionMigration', () => { it('produces deterministic, idempotent identities and maps legacy bracket mode explicitly', () => { const legacy = pairedMatch({ gameMode: 'bracket' }); const first = planCompetitionMigration(legacy); const second = planCompetitionMigration(structuredClone(legacy)); expect(first).toEqual(second); const plan = requireReady(first); expect(plan.competition.id).toMatch(/^[a-f0-9]{24}$/); expect(plan.competition.teams.map((team) => team.teamId)).toEqual( expect.arrayContaining([ expect.stringMatching(/^[a-f0-9]{24}$/), expect.stringMatching(/^[a-f0-9]{24}$/), ]), ); expect(plan.competition.format).toMatchObject({ type: 'paired-series' }); expect(plan.warnings).toContainEqual( expect.objectContaining({ code: 'LEGACY_BRACKET_MAPPED_TO_PAIRED_SERIES', }), ); }); it('preserves Riot-backed and normalized-only games as different evidence kinds', () => { const legacy = pairedMatch({ gameCount: 2 }); legacy.result = { games: [ legacyGame({ gameNumber: 1, riotMatchId: 'KR_WITH_ARCHIVE', blueTeamIndex: 1, redTeamIndex: 2, winnerTeamIndex: 1, riotArchivePresent: true, }), legacyGame({ gameNumber: 2, riotMatchId: 'KR_NORMALIZED_ONLY', blueTeamIndex: 2, redTeamIndex: 1, winnerTeamIndex: 2, riotArchivePresent: false, }), ], seriesResults: [], }; const plan = requireReady(planCompetitionMigration(legacy)); const games = plan.facts.filter((fact) => fact.type === 'game-accepted'); expect(games).toHaveLength(2); expect(games[0]).toMatchObject({ evidence: { type: 'riot', riotMatchId: 'KR_WITH_ARCHIVE', }, }); expect(games[1]).toMatchObject({ evidence: { type: 'legacy', legacyReference: expect.stringContaining('KR_NORMALIZED_ONLY'), }, }); }); it('turns a winner-only Series into an adjudication without fabricating games', () => { const legacy = pairedMatch({ gameCount: 3 }); legacy.result = { games: [], seriesResults: [ { teamAIndex: 1, teamBIndex: 2, teamAWins: 0, teamBWins: 0, winnerTeamIndex: 2, }, ], }; const plan = requireReady(planCompetitionMigration(legacy)); expect(plan.facts).toHaveLength(1); expect(plan.facts[0]).toMatchObject({ type: 'series-adjudicated', decision: { type: 'administrative', winnerTeamId: plan.competition.teams[1].teamId, }, reason: 'legacy-winner-only-result', }); expect(plan.facts).not.toContainEqual( expect.objectContaining({ type: 'game-accepted' }), ); }); it('quarantines an ambiguous legacy game assignment instead of guessing', () => { const legacy = pairedMatch({ teams: [ legacyTeam(1, 'One'), legacyTeam(2, 'Two'), legacyTeam(3, 'Three'), legacyTeam(4, 'Four'), ], }); legacy.gameSchedule = [ { gameNumber: 7, teamAIndex: 1, teamBIndex: 2 }, { gameNumber: 7, teamAIndex: 3, teamBIndex: 4 }, ]; legacy.result = { games: [ legacyGame({ gameNumber: 7, riotMatchId: 'KR_AMBIGUOUS', blueTeamIndex: 1, redTeamIndex: 2, winnerTeamIndex: 1, riotArchivePresent: true, }), ], seriesResults: [], }; const plan = planCompetitionMigration(legacy); expect(plan).toMatchObject({ status: 'quarantined', reasons: expect.arrayContaining([ expect.objectContaining({ code: 'AMBIGUOUS_GAME_ASSIGNMENT' }), ]), }); }); it('repairs the data.txt WB_R2_M2 positional bug from accepted game evidence', () => { const legacy = loadDataTxtFixture(); const plan = requireReady(planCompetitionMigration(legacy)); const projection = projectCompetition({ competitionId: plan.competition.id, revision: plan.competition.revision, projectorVersion: plan.competition.projectorVersion, teams: plan.competition.teams, format: plan.competition.format, seriesGraph: plan.competition.seriesGraph, facts: plan.facts, }); const team3 = plan.competition.teams.find( (team) => team.legacyTeamIndex === 3, ); const team8 = plan.competition.teams.find( (team) => team.legacyTeamIndex === 8, ); const series = (code: string) => projection.series.find((candidate) => candidate.code === code); expect(team3).toBeDefined(); expect(team8).toBeDefined(); expect(series('WB_R2_M2')).toMatchObject({ status: 'completed', entries: [ { slot: 'A', teamId: team8!.teamId, gameWins: 2 }, { slot: 'B', teamId: team3!.teamId, gameWins: 1 }, ], outcome: { type: 'played', winnerTeamId: team8!.teamId, loserTeamId: team3!.teamId, }, }); expect(series('WB_FINAL')).toMatchObject({ entries: expect.arrayContaining([ { slot: 'B', teamId: team8!.teamId, gameWins: 0 }, ]), }); expect(series('LB_R2_M1')).toMatchObject({ entries: expect.arrayContaining([ { slot: 'B', teamId: team3!.teamId, gameWins: 0 }, ]), }); }); }); function pairedMatch( overrides: Partial = {}, ): LegacyMatchInput { return { _id: 'match-legacy-paired', communityId: 'community-1', gameMode: 'pair', gameCount: 1, status: 'in-progress', teams: [legacyTeam(1, 'One'), legacyTeam(2, 'Two')], gameSchedule: [{ gameNumber: 1, teamAIndex: 1, teamBIndex: 2 }], createdAt: '2026-07-01T00:00:00.000Z', updatedAt: '2026-07-02T00:00:00.000Z', ...overrides, }; } function legacyTeam(teamIndex: number, name: string) { return { teamIndex, name, captainUserId: `captain-${teamIndex}`, members: [], }; } function legacyGame( overrides: Partial['games'][number]>, ): NonNullable['games'][number] { return { gameNumber: 1, riotMatchId: 'KR_DEFAULT', blueTeamIndex: 1, redTeamIndex: 2, winnerTeamIndex: 1, gameCreation: 1782864000000, gameDuration: 1800, participants: [], ...overrides, }; } function requireReady( plan: ReturnType, ): ReadyCompetitionMigrationPlan { if (plan.status !== 'ready') { throw new Error( `Expected ready migration plan: ${JSON.stringify(plan.reasons)}`, ); } return plan; } function loadDataTxtFixture(): LegacyMatchInput { const seededTeamIndexes = [1, 8, 3, 4, 5, 6, 7, 2]; const games = [ ['WB_R1_M3', 8, 7, 8], ['WB_R1_M3', 7, 8, 8], ['WB_R1_M4', 3, 6, 3], ['WB_R1_M4', 6, 3, 3], ['WB_R2_M2', 3, 8, 8], ['WB_R2_M2', 8, 3, 3], ['WB_R2_M2', 3, 8, 8], ] as const; return { _id: '6a3e0c2c8ade90b14b72f335', communityId: '69ebe0adf627c11be2f020a1', gameMode: 'double-elimination', gameCount: 3, status: 'in-progress', teams: Array.from({ length: 8 }, (_, index) => legacyTeam( index + 1, index + 1 === 3 ? '해온해줘' : index + 1 === 8 ? '똥칼라파워' : `${index + 1}팀`, ), ), doubleElimination: { teamCount: 8, resetFinalEnabled: true, seeds: seededTeamIndexes.map((teamIndex, index) => ({ seedNumber: index + 1, teamIndex, })), bracketMatches: [], }, result: { games: games.map( ( [bracketMatchId, blueTeamIndex, redTeamIndex, winnerTeamIndex], index, ) => legacyGame({ gameNumber: index + 1, bracketMatchId, riotMatchId: `KR_DATA_TXT_${index + 1}`, riotArchivePresent: true, blueTeamIndex, redTeamIndex, winnerTeamIndex, gameCreation: 1782864000000 + index * 60_000, }), ), seriesResults: [], }, createdAt: '2026-06-27T05:18:00.000Z', updatedAt: '2026-07-12T17:17:00.000Z', }; }