fix: clone hydrated double-elimination brackets

This commit is contained in:
dahyeon committed 2026-07-07 18:06:16 -07:00
1 parent e188828423
commit 03358cab4f
2 files changed
+83 -16

No files matched your search

@@ -8,6 +8,21 @@ import {
swapDoubleEliminationSeeds, swapDoubleEliminationSeeds,
} from './double-elimination.util'; } from './double-elimination.util';
function mongooseSubdocumentLike<T extends object>(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', () => { describe('double-elimination util', () => {
it('builds a 4-team bracket with winner, loser, and grand final matches', () => { it('builds a 4-team bracket with winner, loser, and grand final matches', () => {
const bracket = buildDoubleEliminationBracket([1, 2, 3, 4], { const bracket = buildDoubleEliminationBracket([1, 2, 3, 4], {
@@ -124,6 +139,31 @@ describe('double-elimination util', () => {
).toBe(1); ).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', () => { it('moves winners and losers through a 4-team bracket and records top 3', () => {
let bracket = buildDoubleEliminationBracket([1, 2, 3, 4], { let bracket = buildDoubleEliminationBracket([1, 2, 3, 4], {
resetFinalEnabled: false, resetFinalEnabled: false,
@@ -734,7 +734,14 @@ function requireBracketMatch(
const bracketMatch = bracket.bracketMatches.find( const bracketMatch = bracket.bracketMatches.find(
(match) => match.bracketMatchId === bracketMatchId, (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; return bracketMatch;
} }
@@ -778,26 +785,46 @@ function requiredWinsForGameCount(gameCount: number): number {
function cloneBracket( function cloneBracket(
bracket: DoubleEliminationBracket, bracket: DoubleEliminationBracket,
): DoubleEliminationBracket { ): DoubleEliminationBracket {
const plainBracket = toPlainObject(bracket);
return { return {
...bracket, ...plainBracket,
seeds: bracket.seeds.map((seed) => ({ ...seed })), seeds: plainBracket.seeds.map((seed) => ({ ...toPlainObject(seed) })),
bracketMatches: bracket.bracketMatches.map((match) => ({ bracketMatches: plainBracket.bracketMatches.map((rawMatch) => {
...match, const match = toPlainObject(rawMatch);
teamASource: match.teamASource ? { ...match.teamASource } : undefined, return {
teamBSource: match.teamBSource ? { ...match.teamBSource } : undefined, ...match,
winnerAdvancesTo: match.winnerAdvancesTo teamASource: match.teamASource
? { ...match.winnerAdvancesTo } ? { ...toPlainObject(match.teamASource) }
: undefined, : undefined,
loserAdvancesTo: match.loserAdvancesTo teamBSource: match.teamBSource
? { ...match.loserAdvancesTo } ? { ...toPlainObject(match.teamBSource) }
: undefined, : undefined,
walkover: match.walkover ? { ...match.walkover } : 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<T>(value: T): T {
const candidate = value as { toObject?: () => T };
if (candidate && typeof candidate.toObject === 'function') {
return candidate.toObject();
}
return value;
}
function shuffleInPlace<T>(items: T[], rng: () => number = Math.random): T[] { function shuffleInPlace<T>(items: T[], rng: () => number = Math.random): T[] {
for (let index = items.length - 1; index > 0; index -= 1) { for (let index = items.length - 1; index > 0; index -= 1) {
const swapIndex = Math.floor(rng() * (index + 1)); const swapIndex = Math.floor(rng() * (index + 1));