fix: reject unfinishable first-to rules
This commit is contained in:
1 parent
0c2c7b59fc
commit
78a2c7cf0d
4 files changed
+47
-5
No files matched your search
@@ -86,6 +86,32 @@ describe('generateSeriesGraph', () => {
|
|||||||
expect(series).not.toHaveProperty('loserAdvancesTo');
|
expect(series).not.toHaveProperty('loserAdvancesTo');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('rejects a first-to rule that can exhaust every GameSlot without a winner', () => {
|
||||||
|
try {
|
||||||
|
generateSeriesGraph({
|
||||||
|
competitionId: 'competition-unfinishable-first-to',
|
||||||
|
teams: Array.from({ length: 4 }, (_, index) => ({
|
||||||
|
teamId: `team-${index + 1}`,
|
||||||
|
displayOrder: index + 1,
|
||||||
|
seedNumber: index + 1,
|
||||||
|
})),
|
||||||
|
format: {
|
||||||
|
type: 'double-elimination',
|
||||||
|
resetFinalEnabled: false,
|
||||||
|
rules: {
|
||||||
|
upper: firstTo(2, 2),
|
||||||
|
lower: firstTo(1, 1),
|
||||||
|
grandFinal: firstTo(1, 1),
|
||||||
|
resetFinal: firstTo(1, 1),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
throw new Error('Expected INVALID_FIRST_TO_RULE');
|
||||||
|
} catch (error) {
|
||||||
|
expect(error).toMatchObject({ code: 'INVALID_FIRST_TO_RULE' });
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function firstTo(winsRequired: number, maxGames: number) {
|
function firstTo(winsRequired: number, maxGames: number) {
|
||||||
|
|||||||
@@ -561,7 +561,7 @@ function assertFirstToRule(rule: FirstToSeriesRule): void {
|
|||||||
!Number.isInteger(rule.winsRequired) ||
|
!Number.isInteger(rule.winsRequired) ||
|
||||||
rule.winsRequired < 1 ||
|
rule.winsRequired < 1 ||
|
||||||
!Number.isInteger(rule.maxGames) ||
|
!Number.isInteger(rule.maxGames) ||
|
||||||
rule.maxGames < rule.winsRequired
|
rule.maxGames < rule.winsRequired * 2 - 1
|
||||||
) {
|
) {
|
||||||
throw graphError(
|
throw graphError(
|
||||||
'INVALID_FIRST_TO_RULE',
|
'INVALID_FIRST_TO_RULE',
|
||||||
|
|||||||
@@ -243,6 +243,24 @@ describe('projectSeries', () => {
|
|||||||
}),
|
}),
|
||||||
).toMatchObject({ status: 'inactive' });
|
).toMatchObject({ status: 'inactive' });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('rejects an unfinishable persisted first-to definition', () => {
|
||||||
|
const definition = seriesDefinition({
|
||||||
|
id: 'series-unfinishable',
|
||||||
|
stage: 'upper',
|
||||||
|
rule: { type: 'first-to', winsRequired: 2, maxGames: 2 },
|
||||||
|
});
|
||||||
|
|
||||||
|
expectInvariant(
|
||||||
|
() =>
|
||||||
|
projectSeries({
|
||||||
|
definition,
|
||||||
|
participants: ['team-8', 'team-3'],
|
||||||
|
facts: [],
|
||||||
|
}),
|
||||||
|
'SERIES_DEFINITION_INVALID',
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function seriesDefinition(params: {
|
function seriesDefinition(params: {
|
||||||
|
|||||||
@@ -324,7 +324,7 @@ function validateDefinition(definition: SeriesDefinitionLike): void {
|
|||||||
assertCompetitionInvariant(
|
assertCompetitionInvariant(
|
||||||
Number.isInteger(definition.rule.winsRequired) &&
|
Number.isInteger(definition.rule.winsRequired) &&
|
||||||
definition.rule.winsRequired > 0 &&
|
definition.rule.winsRequired > 0 &&
|
||||||
definition.rule.winsRequired <= definition.rule.maxGames,
|
definition.rule.maxGames >= definition.rule.winsRequired * 2 - 1,
|
||||||
'SERIES_DEFINITION_INVALID',
|
'SERIES_DEFINITION_INVALID',
|
||||||
`Series ${definition.id} has an invalid first-to threshold`,
|
`Series ${definition.id} has an invalid first-to threshold`,
|
||||||
);
|
);
|
||||||
@@ -543,9 +543,7 @@ function assertNoSeriesActivity(
|
|||||||
if (fact.seriesId === seriesId) activeAdjudications.add(fact.id);
|
if (fact.seriesId === seriesId) activeAdjudications.add(fact.id);
|
||||||
break;
|
break;
|
||||||
case 'adjudication-reversed':
|
case 'adjudication-reversed':
|
||||||
if (
|
if (adjudicationSeriesById.get(fact.adjudicationFactId) === seriesId) {
|
||||||
adjudicationSeriesById.get(fact.adjudicationFactId) === seriesId
|
|
||||||
) {
|
|
||||||
assertCompetitionInvariant(
|
assertCompetitionInvariant(
|
||||||
activeAdjudications.delete(fact.adjudicationFactId),
|
activeAdjudications.delete(fact.adjudicationFactId),
|
||||||
'FACT_ALREADY_REVERSED',
|
'FACT_ALREADY_REVERSED',
|
||||||
|
|||||||
Reference in new issue
Block a user