fix: make competition reversals replayable
This commit is contained in:
1 parent
4d6fa2625e
commit
ce07bfa0c8
6 files changed
+169
-13
No files matched your search
@@ -253,6 +253,59 @@ describe('projectCompetition', () => {
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it('makes a voided GameSlot ready again when its Series can be replayed', () => {
|
||||
const competitionId = 'competition-replay-voided-slot';
|
||||
const teams = [seededTeam('team-1', 1), seededTeam('team-2', 2)];
|
||||
const format = {
|
||||
type: 'paired-series' as const,
|
||||
rule: fixedGames(1),
|
||||
};
|
||||
const seriesGraph = generateSeriesGraph({
|
||||
competitionId,
|
||||
teams,
|
||||
format,
|
||||
});
|
||||
const definition = seriesGraph[0];
|
||||
const accepted = acceptedGameFactory(competitionId)(
|
||||
definition,
|
||||
1,
|
||||
'team-1',
|
||||
'team-2',
|
||||
'team-1',
|
||||
);
|
||||
|
||||
const projection = projectCompetition({
|
||||
competitionId,
|
||||
revision: 2,
|
||||
projectorVersion: 1,
|
||||
teams,
|
||||
format,
|
||||
seriesGraph,
|
||||
facts: [
|
||||
accepted,
|
||||
{
|
||||
id: 'fact-void-game-1',
|
||||
type: 'game-voided',
|
||||
competitionId,
|
||||
revision: 2,
|
||||
ordinalWithinRevision: 1,
|
||||
commandId: 'command-void-game-1',
|
||||
schemaVersion: 1,
|
||||
actor: { type: 'user' as const },
|
||||
occurredAt: new Date('2026-07-12T00:00:02.000Z'),
|
||||
recordedAt: new Date('2026-07-12T00:01:02.000Z'),
|
||||
gameAcceptedFactId: accepted.id,
|
||||
reason: 'Incorrect Riot evidence',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
expect(projection.phase).toBe('ready');
|
||||
expect(projection.series[0]).toMatchObject({ status: 'ready' });
|
||||
expect(projection.gameSlots[0]).toMatchObject({ status: 'ready' });
|
||||
expect(projection.games).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
function fixedGames(gameCount: number) {
|
||||
|
||||
@@ -666,20 +666,17 @@ function projectGamesAndSlots(
|
||||
} {
|
||||
const acceptedByFactId = new Map<string, AcceptedGameProjectionFact>();
|
||||
const activeBySlotId = new Map<string, AcceptedGameProjectionFact>();
|
||||
const voidedSlotIds = new Set<string>();
|
||||
|
||||
for (const fact of projectionGameFacts(facts)) {
|
||||
if (fact.type === 'game-accepted') {
|
||||
acceptedByFactId.set(fact.id, fact);
|
||||
activeBySlotId.set(fact.gameSlotId, fact);
|
||||
voidedSlotIds.delete(fact.gameSlotId);
|
||||
continue;
|
||||
}
|
||||
const accepted = acceptedByFactId.get(fact.gameAcceptedFactId);
|
||||
if (!accepted) continue;
|
||||
if (activeBySlotId.get(accepted.gameSlotId)?.id === accepted.id) {
|
||||
activeBySlotId.delete(accepted.gameSlotId);
|
||||
voidedSlotIds.add(accepted.gameSlotId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -756,9 +753,7 @@ function projectGamesAndSlots(
|
||||
const game = activeBySlotId.get(slot.id);
|
||||
const status = game
|
||||
? ('completed' as const)
|
||||
: voidedSlotIds.has(slot.id)
|
||||
? ('voided' as const)
|
||||
: gameSlotStatus(projected.status);
|
||||
: gameSlotStatus(projected.status);
|
||||
return {
|
||||
id: slot.id,
|
||||
seriesId: definition.id,
|
||||
|
||||
@@ -53,15 +53,39 @@ describe('Competition reversal planning', () => {
|
||||
facts,
|
||||
assignments,
|
||||
});
|
||||
const extraAssignments = [
|
||||
...assignments,
|
||||
assignment('assignment-extra-z', 'series-child', 'slot-z', 'reserved'),
|
||||
assignment('assignment-extra-a', 'series-child', 'slot-a', 'reserved'),
|
||||
];
|
||||
const unorderedFirst = previewSeriesReversal({
|
||||
competitionId: 'competition-1',
|
||||
revision: 2,
|
||||
seriesId: 'series-parent',
|
||||
mode: 'cascade',
|
||||
graph,
|
||||
facts,
|
||||
assignments: extraAssignments,
|
||||
});
|
||||
const unorderedSecond = previewSeriesReversal({
|
||||
competitionId: 'competition-1',
|
||||
revision: 2,
|
||||
seriesId: 'series-parent',
|
||||
mode: 'cascade',
|
||||
graph,
|
||||
facts,
|
||||
assignments: [...extraAssignments].reverse(),
|
||||
});
|
||||
|
||||
expect(second).toEqual(first);
|
||||
expect(unorderedSecond).toEqual(unorderedFirst);
|
||||
expect(first).toEqual({
|
||||
sourceRevision: 2,
|
||||
impactHash: expect.stringMatching(/^[a-f0-9]{64}$/),
|
||||
impact: {
|
||||
seriesIds: ['series-parent', 'series-child'],
|
||||
factIds: ['fact-parent', 'fact-child'],
|
||||
gameSlotIds: ['slot-parent', 'slot-child'],
|
||||
gameSlotIds: ['slot-child', 'slot-parent'],
|
||||
teamIds: ['team-3', 'team-8'],
|
||||
releasesReservedCodes: 1,
|
||||
voidsUsedCodes: 1,
|
||||
|
||||
@@ -175,7 +175,7 @@ export function previewSeriesReversal(
|
||||
const impact: SeriesReversalImpact = {
|
||||
seriesIds: seriesOrder,
|
||||
factIds: selectedFacts.map(({ id }) => id),
|
||||
gameSlotIds: [...gameSlotIds],
|
||||
gameSlotIds: [...gameSlotIds].sort(),
|
||||
teamIds: [...teamIds].sort(),
|
||||
releasesReservedCodes: assignments.filter(
|
||||
({ state }) => state === 'reserved',
|
||||
|
||||
@@ -203,6 +203,46 @@ describe('projectSeries', () => {
|
||||
'SERIES_ALREADY_COMPLETED',
|
||||
);
|
||||
});
|
||||
|
||||
it('allows an inactive descendant after all of its historical activity was reversed', () => {
|
||||
const definition = seriesDefinition({
|
||||
id: 'series-reversed-descendant',
|
||||
stage: 'upper',
|
||||
rule: { type: 'first-to', winsRequired: 1, maxGames: 1 },
|
||||
});
|
||||
const accepted = acceptedGame({
|
||||
definition,
|
||||
ordinal: 1,
|
||||
blueTeamId: 'team-8',
|
||||
redTeamId: 'team-3',
|
||||
winnerTeamId: 'team-8',
|
||||
});
|
||||
|
||||
expect(
|
||||
projectSeries({
|
||||
definition,
|
||||
participants: [null, null],
|
||||
active: false,
|
||||
facts: [
|
||||
accepted,
|
||||
{
|
||||
id: 'fact-series-reversed-descendant-void',
|
||||
type: 'game-voided',
|
||||
competitionId: 'competition-series-projector',
|
||||
revision: 2,
|
||||
ordinalWithinRevision: 1,
|
||||
commandId: 'command-series-reversed-descendant-void',
|
||||
schemaVersion: 1,
|
||||
actor: { type: 'user' },
|
||||
occurredAt: new Date('2026-07-12T00:02:00.000Z'),
|
||||
recordedAt: new Date('2026-07-12T00:02:01.000Z'),
|
||||
gameAcceptedFactId: accepted.id,
|
||||
reason: 'Cascade reversal',
|
||||
},
|
||||
],
|
||||
}),
|
||||
).toMatchObject({ status: 'inactive' });
|
||||
});
|
||||
});
|
||||
|
||||
function seriesDefinition(params: {
|
||||
|
||||
@@ -508,13 +508,57 @@ function assertNoSeriesActivity(
|
||||
seriesId: string,
|
||||
facts: CompetitionFact[],
|
||||
): void {
|
||||
const hasActivity = facts.some(
|
||||
(fact) =>
|
||||
(fact.type === 'game-accepted' || fact.type === 'series-adjudicated') &&
|
||||
fact.seriesId === seriesId,
|
||||
const acceptedSeriesById = new Map(
|
||||
facts
|
||||
.filter((fact): fact is GameAcceptedFact => fact.type === 'game-accepted')
|
||||
.map((fact) => [fact.id, fact.seriesId]),
|
||||
);
|
||||
const adjudicationSeriesById = new Map(
|
||||
facts
|
||||
.filter(
|
||||
(fact): fact is SeriesAdjudicatedFact =>
|
||||
fact.type === 'series-adjudicated',
|
||||
)
|
||||
.map((fact) => [fact.id, fact.seriesId]),
|
||||
);
|
||||
const activeGames = new Set<string>();
|
||||
const activeAdjudications = new Set<string>();
|
||||
|
||||
for (const fact of facts) {
|
||||
switch (fact.type) {
|
||||
case 'game-accepted':
|
||||
if (fact.seriesId === seriesId) activeGames.add(fact.id);
|
||||
break;
|
||||
case 'game-voided':
|
||||
if (acceptedSeriesById.get(fact.gameAcceptedFactId) === seriesId) {
|
||||
assertCompetitionInvariant(
|
||||
activeGames.delete(fact.gameAcceptedFactId),
|
||||
'FACT_ALREADY_REVERSED',
|
||||
`Game fact ${fact.gameAcceptedFactId} is already voided`,
|
||||
{ factId: fact.id, referencedFactId: fact.gameAcceptedFactId },
|
||||
);
|
||||
}
|
||||
break;
|
||||
case 'series-adjudicated':
|
||||
if (fact.seriesId === seriesId) activeAdjudications.add(fact.id);
|
||||
break;
|
||||
case 'adjudication-reversed':
|
||||
if (
|
||||
adjudicationSeriesById.get(fact.adjudicationFactId) === seriesId
|
||||
) {
|
||||
assertCompetitionInvariant(
|
||||
activeAdjudications.delete(fact.adjudicationFactId),
|
||||
'FACT_ALREADY_REVERSED',
|
||||
`Adjudication fact ${fact.adjudicationFactId} is already reversed`,
|
||||
{ factId: fact.id, referencedFactId: fact.adjudicationFactId },
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assertCompetitionInvariant(
|
||||
!hasActivity,
|
||||
activeGames.size === 0 && activeAdjudications.size === 0,
|
||||
'SERIES_PARTICIPANTS_INVALID',
|
||||
`Series ${seriesId} cannot contain facts before both participants are active`,
|
||||
{ seriesId },
|
||||
|
||||
Reference in new issue
Block a user