fix: clone hydrated double-elimination brackets
This commit is contained in:
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,
|
||||
|
||||
@@ -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<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[] {
|
||||
for (let index = items.length - 1; index > 0; index -= 1) {
|
||||
const swapIndex = Math.floor(rng() * (index + 1));
|
||||
|
||||
Reference in new issue
Block a user