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,
} 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', () => {
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,