From 03358cab4fe6eb8a6d2d4dd2e24e541842252d47 Mon Sep 17 00:00:00 2001 From: dahyeon Date: Tue, 7 Jul 2026 18:06:16 -0700 Subject: [PATCH] fix: clone hydrated double-elimination brackets --- .../double-elimination.util.spec.ts | 40 +++++++++++++ .../double-elimination.util.ts | 59 ++++++++++++++----- 2 files changed, 83 insertions(+), 16 deletions(-) diff --git a/apps/api/src/modules/double-elimination/double-elimination.util.spec.ts b/apps/api/src/modules/double-elimination/double-elimination.util.spec.ts index 93214559..e943b40f 100644 --- a/apps/api/src/modules/double-elimination/double-elimination.util.spec.ts +++ b/apps/api/src/modules/double-elimination/double-elimination.util.spec.ts @@ -8,6 +8,21 @@ import { swapDoubleEliminationSeeds, } from './double-elimination.util'; +function mongooseSubdocumentLike(value: T): T { + const wrapper = { + toObject: () => value, + } as T & { toObject: () => T }; + + for (const [key, entryValue] of Object.entries(value)) { + Object.defineProperty(wrapper, key, { + enumerable: false, + get: () => entryValue, + }); + } + + return wrapper; +} + describe('double-elimination util', () => { it('builds a 4-team bracket with winner, loser, and grand final matches', () => { const bracket = buildDoubleEliminationBracket([1, 2, 3, 4], { @@ -124,6 +139,31 @@ describe('double-elimination util', () => { ).toBe(1); }); + it('completes a hydrated Mongoose-style bracket without losing subdocument fields', () => { + const bracket = buildDoubleEliminationBracket([1, 2, 3, 4], { + resetFinalEnabled: false, + }); + const hydratedLikeBracket = { + ...bracket, + seeds: bracket.seeds.map(mongooseSubdocumentLike), + bracketMatches: bracket.bracketMatches.map(mongooseSubdocumentLike), + events: bracket.events.map(mongooseSubdocumentLike), + }; + + const next = completeDoubleEliminationBracketMatch(hydratedLikeBracket, { + bracketMatchId: 'WB_R1_M1', + winnerTeamIndex: 1, + completedBy: 'riot-result', + }); + + expect( + next.bracketMatches.find((match) => match.bracketMatchId === 'WB_FINAL'), + ).toMatchObject({ teamAIndex: 1 }); + expect( + next.bracketMatches.find((match) => match.bracketMatchId === 'LB_R1_M1'), + ).toMatchObject({ teamAIndex: 4 }); + }); + it('moves winners and losers through a 4-team bracket and records top 3', () => { let bracket = buildDoubleEliminationBracket([1, 2, 3, 4], { resetFinalEnabled: false, diff --git a/apps/api/src/modules/double-elimination/double-elimination.util.ts b/apps/api/src/modules/double-elimination/double-elimination.util.ts index 08f3373d..73dd5c58 100644 --- a/apps/api/src/modules/double-elimination/double-elimination.util.ts +++ b/apps/api/src/modules/double-elimination/double-elimination.util.ts @@ -734,7 +734,14 @@ function requireBracketMatch( const bracketMatch = bracket.bracketMatches.find( (match) => match.bracketMatchId === bracketMatchId, ); - if (!bracketMatch) throw new Error('Bracket match not found.'); + if (!bracketMatch) { + throw new Error( + `Bracket match not found: ${bracketMatchId}. Available: ${bracket.bracketMatches + .map((match) => match.bracketMatchId) + .filter(Boolean) + .join(', ')}`, + ); + } return bracketMatch; } @@ -778,26 +785,46 @@ function requiredWinsForGameCount(gameCount: number): number { function cloneBracket( bracket: DoubleEliminationBracket, ): DoubleEliminationBracket { + const plainBracket = toPlainObject(bracket); return { - ...bracket, - seeds: bracket.seeds.map((seed) => ({ ...seed })), - bracketMatches: bracket.bracketMatches.map((match) => ({ - ...match, - teamASource: match.teamASource ? { ...match.teamASource } : undefined, - teamBSource: match.teamBSource ? { ...match.teamBSource } : undefined, - winnerAdvancesTo: match.winnerAdvancesTo - ? { ...match.winnerAdvancesTo } - : undefined, - loserAdvancesTo: match.loserAdvancesTo - ? { ...match.loserAdvancesTo } - : undefined, - walkover: match.walkover ? { ...match.walkover } : undefined, + ...plainBracket, + seeds: plainBracket.seeds.map((seed) => ({ ...toPlainObject(seed) })), + bracketMatches: plainBracket.bracketMatches.map((rawMatch) => { + const match = toPlainObject(rawMatch); + return { + ...match, + teamASource: match.teamASource + ? { ...toPlainObject(match.teamASource) } + : undefined, + teamBSource: match.teamBSource + ? { ...toPlainObject(match.teamBSource) } + : undefined, + winnerAdvancesTo: match.winnerAdvancesTo + ? { ...toPlainObject(match.winnerAdvancesTo) } + : undefined, + loserAdvancesTo: match.loserAdvancesTo + ? { ...toPlainObject(match.loserAdvancesTo) } + : undefined, + walkover: match.walkover + ? { ...toPlainObject(match.walkover) } + : undefined, + }; + }), + placements: { ...toPlainObject(plainBracket.placements) }, + events: plainBracket.events.map((event) => ({ + ...toPlainObject(event), })), - placements: { ...bracket.placements }, - events: bracket.events.map((event) => ({ ...event })), }; } +function toPlainObject(value: T): T { + const candidate = value as { toObject?: () => T }; + if (candidate && typeof candidate.toObject === 'function') { + return candidate.toObject(); + } + return value; +} + function shuffleInPlace(items: T[], rng: () => number = Math.random): T[] { for (let index = items.length - 1; index > 0; index -= 1) { const swapIndex = Math.floor(rng() * (index + 1));