561 lines
16 KiB
TypeScript
561 lines
16 KiB
TypeScript
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].evidence.type).toBe('legacy');
|
|
if (games[1].evidence.type !== 'legacy') {
|
|
throw new Error('Expected normalized-only legacy evidence.');
|
|
}
|
|
expect(games[1].evidence.legacyReference).toContain('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);
|
|
|
|
expectQuarantineReason(plan, 'AMBIGUOUS_GAME_ASSIGNMENT');
|
|
});
|
|
|
|
it('preserves a persisted non-canonical round-robin schedule', () => {
|
|
const schedule = [
|
|
[1, 1, 1, 2],
|
|
[2, 1, 3, 4],
|
|
[3, 1, 5, 6],
|
|
[4, 1, 7, 8],
|
|
[5, 2, 1, 3],
|
|
[6, 2, 2, 4],
|
|
[7, 2, 5, 7],
|
|
[8, 2, 6, 8],
|
|
[9, 3, 1, 4],
|
|
[10, 3, 2, 3],
|
|
[11, 3, 5, 8],
|
|
[12, 3, 6, 7],
|
|
] as const;
|
|
const legacy = pairedMatch({
|
|
_id: 'match-persisted-round-robin-schedule',
|
|
gameMode: 'round-robin',
|
|
gameCount: 1,
|
|
roundRobinMaxRounds: 3,
|
|
teams: Array.from({ length: 8 }, (_, index) =>
|
|
legacyTeam(index + 1, `${index + 1}팀`),
|
|
),
|
|
gameSchedule: schedule.map(
|
|
([gameNumber, roundNumber, teamAIndex, teamBIndex]) => ({
|
|
gameNumber,
|
|
roundNumber,
|
|
teamAIndex,
|
|
teamBIndex,
|
|
}),
|
|
),
|
|
result: {
|
|
games: schedule.map(([gameNumber, , teamAIndex, teamBIndex]) =>
|
|
legacyGame({
|
|
gameNumber,
|
|
riotMatchId: `KR_PERSISTED_RR_${gameNumber}`,
|
|
blueTeamIndex: teamAIndex,
|
|
redTeamIndex: teamBIndex,
|
|
winnerTeamIndex: teamAIndex,
|
|
riotArchivePresent: true,
|
|
}),
|
|
),
|
|
seriesResults: [],
|
|
},
|
|
});
|
|
|
|
const plan = requireReady(planCompetitionMigration(legacy));
|
|
|
|
expect(plan.competition.format.type).toBe('round-robin');
|
|
if (plan.competition.format.type !== 'round-robin') {
|
|
throw new Error('Expected a round-robin format.');
|
|
}
|
|
expect(
|
|
plan.competition.format.pairings?.some(
|
|
(pairing) =>
|
|
pairing.roundNumber === 3 &&
|
|
pairing.matchNumber === 4 &&
|
|
pairing.teamAId === plan.competition.teams[5].teamId &&
|
|
pairing.teamBId === plan.competition.teams[6].teamId,
|
|
),
|
|
).toBe(true);
|
|
expect(
|
|
plan.facts.filter(({ type }) => type === 'game-accepted'),
|
|
).toHaveLength(12);
|
|
});
|
|
|
|
it('maps a result-less reserved TournamentCode by its global game number', () => {
|
|
const legacy = pairedMatch({
|
|
_id: 'match-future-reserved-code',
|
|
gameCount: 2,
|
|
teams: [
|
|
legacyTeam(1, 'One'),
|
|
legacyTeam(2, 'Two'),
|
|
legacyTeam(3, 'Three'),
|
|
legacyTeam(4, 'Four'),
|
|
],
|
|
gameSchedule: undefined,
|
|
result: { games: [], seriesResults: [] },
|
|
tournamentCodes: [
|
|
{
|
|
_id: 'future-code-for-global-game-3',
|
|
state: 'reserved',
|
|
gameNumber: 3,
|
|
},
|
|
],
|
|
});
|
|
|
|
const plan = requireReady(planCompetitionMigration(legacy));
|
|
const expectedSeries = plan.competition.seriesGraph.find(
|
|
(series) => series.code === 'PAIR_M2',
|
|
);
|
|
|
|
expect(expectedSeries).toBeDefined();
|
|
expect(plan.assignments).toContainEqual(
|
|
expect.objectContaining({
|
|
tournamentCodeId: 'future-code-for-global-game-3',
|
|
seriesId: expectedSeries!.id,
|
|
gameSlotId: expectedSeries!.gameSlots[0].id,
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('maps persisted WB_FINAL global game numbers to distinct stable slots', () => {
|
|
const legacy = loadDataTxtFixture();
|
|
legacy.result = { games: [], seriesResults: [] };
|
|
legacy.doubleElimination!.bracketMatches = dataTxtUpperBracketTopology();
|
|
legacy.tournamentCodes = [19, 20, 21].map((gameNumber, index) => ({
|
|
_id: `wb-final-code-${index + 1}`,
|
|
state: 'reserved' as const,
|
|
bracketMatchId: 'WB_FINAL',
|
|
gameNumber,
|
|
}));
|
|
|
|
const plan = requireReady(planCompetitionMigration(legacy));
|
|
const wbFinal = plan.competition.seriesGraph.find(
|
|
(series) => series.code === 'WB_FINAL',
|
|
);
|
|
|
|
expect(wbFinal).toBeDefined();
|
|
expect(wbFinal!.gameSlots).toHaveLength(3);
|
|
expect(plan.assignments).toHaveLength(3);
|
|
for (const [index, slot] of wbFinal!.gameSlots.entries()) {
|
|
expect(plan.assignments).toContainEqual(
|
|
expect.objectContaining({
|
|
tournamentCodeId: `wb-final-code-${index + 1}`,
|
|
seriesId: wbFinal!.id,
|
|
gameSlotId: slot.id,
|
|
}),
|
|
);
|
|
}
|
|
});
|
|
|
|
it('quarantines a TournamentCode whose bracket identity conflicts with its global game number', () => {
|
|
const legacy = loadDataTxtFixture();
|
|
legacy.result = { games: [], seriesResults: [] };
|
|
legacy.doubleElimination!.bracketMatches = dataTxtUpperBracketTopology();
|
|
legacy.tournamentCodes = [
|
|
{
|
|
_id: 'conflicting-wb-final-code',
|
|
state: 'reserved',
|
|
bracketMatchId: 'WB_FINAL',
|
|
gameNumber: 1,
|
|
},
|
|
];
|
|
|
|
const plan = planCompetitionMigration(legacy);
|
|
|
|
expectQuarantineReason(plan, 'TOURNAMENT_CODE_ASSIGNMENT_CONFLICT');
|
|
});
|
|
|
|
it('quarantines conflicting Riot evidence and global game number', () => {
|
|
const legacy = pairedMatch({
|
|
_id: 'match-conflicting-code-evidence',
|
|
gameCount: 2,
|
|
gameSchedule: undefined,
|
|
result: {
|
|
games: [
|
|
legacyGame({
|
|
gameNumber: 1,
|
|
riotMatchId: 'KR_CONFLICTING_CODE_EVIDENCE',
|
|
blueTeamIndex: 1,
|
|
redTeamIndex: 2,
|
|
winnerTeamIndex: 1,
|
|
riotArchivePresent: true,
|
|
}),
|
|
],
|
|
seriesResults: [],
|
|
},
|
|
tournamentCodes: [
|
|
{
|
|
_id: 'code-with-conflicting-evidence',
|
|
state: 'used',
|
|
gameNumber: 2,
|
|
riotMatchId: 'KR_CONFLICTING_CODE_EVIDENCE',
|
|
},
|
|
],
|
|
});
|
|
|
|
expectQuarantineReason(
|
|
planCompetitionMigration(legacy),
|
|
'TOURNAMENT_CODE_ASSIGNMENT_CONFLICT',
|
|
);
|
|
});
|
|
|
|
it('quarantines a TournamentCode game number outside the Competition topology', () => {
|
|
const legacy = pairedMatch({
|
|
_id: 'match-out-of-range-code',
|
|
gameSchedule: undefined,
|
|
result: { games: [], seriesResults: [] },
|
|
tournamentCodes: [
|
|
{
|
|
_id: 'out-of-range-code',
|
|
state: 'reserved',
|
|
gameNumber: 99,
|
|
},
|
|
],
|
|
});
|
|
|
|
expectQuarantineReason(
|
|
planCompetitionMigration(legacy),
|
|
'TOURNAMENT_CODE_ASSIGNMENT_MAPPING_NOT_FOUND',
|
|
);
|
|
});
|
|
|
|
it('does not guess the first slot of a multi-slot Series without exact slot evidence', () => {
|
|
const legacy = pairedMatch({
|
|
_id: 'match-multi-slot-code-without-evidence',
|
|
gameCount: 3,
|
|
result: { games: [], seriesResults: [] },
|
|
tournamentCodes: [
|
|
{
|
|
_id: 'code-with-series-only',
|
|
state: 'reserved',
|
|
bracketMatchId: 'PAIR_M1',
|
|
},
|
|
],
|
|
});
|
|
|
|
const plan = planCompetitionMigration(legacy);
|
|
|
|
expectQuarantineReason(
|
|
plan,
|
|
'TOURNAMENT_CODE_ASSIGNMENT_MAPPING_NOT_FOUND',
|
|
);
|
|
});
|
|
|
|
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')?.entries.some(
|
|
(entry) =>
|
|
entry.slot === 'B' &&
|
|
entry.teamId === team8!.teamId &&
|
|
entry.gameWins === 0,
|
|
),
|
|
).toBe(true);
|
|
expect(
|
|
series('LB_R2_M1')?.entries.some(
|
|
(entry) =>
|
|
entry.slot === 'B' &&
|
|
entry.teamId === team3!.teamId &&
|
|
entry.gameWins === 0,
|
|
),
|
|
).toBe(true);
|
|
});
|
|
});
|
|
|
|
function pairedMatch(
|
|
overrides: Partial<LegacyMatchInput> = {},
|
|
): 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<NonNullable<LegacyMatchInput['result']>['games'][number]>,
|
|
): NonNullable<LegacyMatchInput['result']>['games'][number] {
|
|
return {
|
|
gameNumber: 1,
|
|
riotMatchId: 'KR_DEFAULT',
|
|
blueTeamIndex: 1,
|
|
redTeamIndex: 2,
|
|
winnerTeamIndex: 1,
|
|
gameCreation: 1782864000000,
|
|
gameDuration: 1800,
|
|
participants: [],
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function requireReady(
|
|
plan: ReturnType<typeof planCompetitionMigration>,
|
|
): ReadyCompetitionMigrationPlan {
|
|
if (plan.status !== 'ready') {
|
|
throw new Error(
|
|
`Expected ready migration plan: ${JSON.stringify(plan.reasons)}`,
|
|
);
|
|
}
|
|
return plan;
|
|
}
|
|
|
|
function expectQuarantineReason(
|
|
plan: ReturnType<typeof planCompetitionMigration>,
|
|
code: string,
|
|
): void {
|
|
expect(plan.status).toBe('quarantined');
|
|
if (plan.status !== 'quarantined') {
|
|
throw new Error(
|
|
`Expected a quarantined migration plan, got ${plan.status}.`,
|
|
);
|
|
}
|
|
expect(plan.reasons.some((reason) => reason.code === code)).toBe(true);
|
|
}
|
|
|
|
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',
|
|
};
|
|
}
|
|
|
|
function dataTxtUpperBracketTopology() {
|
|
return [
|
|
'WB_R1_M1',
|
|
'WB_R1_M2',
|
|
'WB_R1_M3',
|
|
'WB_R1_M4',
|
|
'WB_R2_M1',
|
|
'WB_R2_M2',
|
|
'WB_FINAL',
|
|
].map((bracketMatchId, index) => ({
|
|
bracketMatchId,
|
|
gameNumber: index + 1,
|
|
gameCount: 3,
|
|
requiredWins: 2,
|
|
bracketSide: 'winner' as const,
|
|
}));
|
|
}
|