diff --git a/app/features/tournament-bracket/core/PreparedMaps.test.ts b/app/features/tournament-bracket/core/PreparedMaps.test.ts index ba6146167..cc98492e5 100644 --- a/app/features/tournament-bracket/core/PreparedMaps.test.ts +++ b/app/features/tournament-bracket/core/PreparedMaps.test.ts @@ -198,6 +198,38 @@ describe("PreparedMaps - trimPreparedEliminationMaps", () => { ); }); + test("trimmed rounds have the same round ids (SE)", () => { + const trimmed = PreparedMaps.trimPreparedEliminationMaps({ + preparedMaps: EIGHT_TEAM_SE_PREPARED, + teamCount: 4, + tournament, + type: "single_elimination", + }); + + const actualBracket = tournament.generateMatchesData( + [1, 2, 3, 4], + "single_elimination", + ); + + for (const round of actualBracket.round) { + expect( + trimmed!.maps.some((map) => map.roundId === round.id), + `Round ID ${round.id} not found in the actual bracket`, + ).toBeTrue(); + } + }); + + test("trimmed rounds start with round id 0", () => { + const trimmed = PreparedMaps.trimPreparedEliminationMaps({ + preparedMaps: EIGHT_TEAM_SE_PREPARED, + teamCount: 4, + tournament, + type: "single_elimination", + }); + + expect(trimmed?.maps[0].roundId).toBe(0); + }); + test("trims the maps (SE - disappearing 3rd place match)", () => { const trimmed = PreparedMaps.trimPreparedEliminationMaps({ preparedMaps: EIGHT_TEAM_SE_PREPARED, @@ -239,6 +271,27 @@ describe("PreparedMaps - trimPreparedEliminationMaps", () => { ).toBe(expectedFinalsCount); }); + test("trimmed rounds have the same round ids (DE)", () => { + const trimmed = PreparedMaps.trimPreparedEliminationMaps({ + preparedMaps: EIGHT_TEAM_DE_PREPARED, + teamCount: 4, + tournament, + type: "double_elimination", + }); + + const actualBracket = tournament.generateMatchesData( + [1, 2, 3, 4], + "double_elimination", + ); + + for (const round of actualBracket.round) { + expect( + trimmed!.maps.some((map) => map.roundId === round.id), + `Round ID ${round.id} not found in the actual bracket`, + ).toBeTrue(); + } + }); + const FOUR_TEAM_SE_PREPARED: PreparedMapsType = { maps: [ { diff --git a/app/features/tournament-bracket/core/PreparedMaps.ts b/app/features/tournament-bracket/core/PreparedMaps.ts index 7a2e60092..fda8525bc 100644 --- a/app/features/tournament-bracket/core/PreparedMaps.ts +++ b/app/features/tournament-bracket/core/PreparedMaps.ts @@ -1,6 +1,7 @@ import compare from "just-compare"; import type { PreparedMaps } from "~/db/tables"; import { nullFilledArray, removeDuplicates } from "~/utils/arrays"; +import invariant from "~/utils/invariant"; import type { Tournament } from "./Tournament"; /** Returns the prepared maps for one exact bracket index OR maps of a "sibling bracket" i.e. bracket that has the same sources */ @@ -130,8 +131,11 @@ function trimMapsByTeamCount({ const actualRoundsCount = actualRoundsForGroup.length; - const trimmedRounds = preparedRoundsForGroup.slice( - preparedRoundsForGroup.length - actualRoundsCount, + const trimmedRounds = roundsWithVirtualIds( + preparedRoundsForGroup.slice( + preparedRoundsForGroup.length - actualRoundsCount, + ), + actualRoundsForGroup.map((r) => r.id).sort((a, b) => a - b), ); result.maps = result.maps.filter((r) => r.groupId !== groupId); @@ -148,3 +152,12 @@ function trimMapsByTeamCount({ return result; } + +function roundsWithVirtualIds( + rounds: T[], + virtualIds: number[], +) { + invariant(rounds.length === virtualIds.length, "Round id length mismatch"); + + return rounds.map((r, i) => ({ ...r, roundId: virtualIds[i] })); +}