fix: normalize reused tournament code lifecycle

This commit is contained in:
dahyeon committed 2026-07-13 04:52:29 +00:00
1 parent 6b1c450752
commit 489d040d8f
3 files changed
+188 -21

No files matched your search

@@ -542,7 +542,10 @@ describe('planCompetitionMigration', () => {
_id: 'used-code-by-slot',
state: 'used',
gameNumber: 1,
reservedAt: '2026-07-01T00:00:00.000Z',
usedAt: '2026-07-02T00:00:00.000Z',
releasedAt: '2026-07-01T12:00:00.000Z',
voidedAt: '2026-07-01T18:00:00.000Z',
},
],
});
@@ -550,6 +553,12 @@ describe('planCompetitionMigration', () => {
const plan = requireReady(planCompetitionMigration(legacy));
expect(plan.assignments).toHaveLength(1);
expect(plan.assignments[0]).toMatchObject({
state: 'used',
usedAt: '2026-07-02T00:00:00.000Z',
});
expect(plan.assignments[0]).not.toHaveProperty('releasedAt');
expect(plan.assignments[0]).not.toHaveProperty('voidedAt');
expect(plan.warnings).toEqual([]);
});
@@ -565,6 +574,7 @@ describe('planCompetitionMigration', () => {
gameNumber: 1,
reservedAt: '2026-07-01T00:00:00.000Z',
usedAt: '2026-07-02T00:00:00.000Z',
releasedAt: '2026-07-04T00:00:00.000Z',
voidedAt: '2026-07-03T00:00:00.000Z',
},
],
@@ -578,6 +588,130 @@ describe('planCompetitionMigration', () => {
voidedAt: '2026-07-03T00:00:00.000Z',
}),
);
expect(plan.assignments[0]).not.toHaveProperty('releasedAt');
});
it('ignores stale terminal timestamps from an earlier lifecycle when a code is reserved again', () => {
const legacy = pairedMatch({
_id: 'match-reused-reserved-code-timeline',
gameCount: 1,
gameSchedule: undefined,
result: { games: [], seriesResults: [] },
tournamentCodes: [
{
_id: 'reused-reserved-code',
state: 'reserved',
gameNumber: 1,
reservedAt: '2026-07-03T00:00:00.000Z',
usedAt: '2026-07-01T00:00:00.000Z',
releasedAt: '2026-07-02T00:00:00.000Z',
voidedAt: '2026-07-02T00:00:00.000Z',
riotMatchId: 'KR_STALE_RESERVED_LIFECYCLE',
},
],
});
const plan = requireReady(planCompetitionMigration(legacy));
expect(plan.assignments).toContainEqual(
expect.objectContaining({
state: 'reserved',
reservedAt: '2026-07-03T00:00:00.000Z',
}),
);
expect(plan.assignments[0]).not.toHaveProperty('usedAt');
expect(plan.assignments[0]).not.toHaveProperty('releasedAt');
expect(plan.assignments[0]).not.toHaveProperty('voidedAt');
expect(plan.assignments[0]).not.toHaveProperty('riotMatchId');
});
it('preserves only the applicable lifecycle for a released code', () => {
const legacy = pairedMatch({
_id: 'match-released-code-timeline',
gameCount: 1,
gameSchedule: undefined,
result: { games: [], seriesResults: [] },
tournamentCodes: [
{
_id: 'released-code-with-stale-terminal-fields',
state: 'released',
gameNumber: 1,
reservedAt: '2026-07-03T00:00:00.000Z',
usedAt: '2026-07-01T00:00:00.000Z',
releasedAt: '2026-07-04T00:00:00.000Z',
voidedAt: '2026-07-02T00:00:00.000Z',
riotMatchId: 'KR_STALE_RELEASED_LIFECYCLE',
},
],
});
const plan = requireReady(planCompetitionMigration(legacy));
expect(plan.assignments[0]).toMatchObject({
state: 'released',
reservedAt: '2026-07-03T00:00:00.000Z',
releasedAt: '2026-07-04T00:00:00.000Z',
});
expect(plan.assignments[0]).not.toHaveProperty('usedAt');
expect(plan.assignments[0]).not.toHaveProperty('voidedAt');
expect(plan.assignments[0]).not.toHaveProperty('riotMatchId');
});
it('uses updatedAt as voidedAt for the production reversal document shape', () => {
const legacy = pairedMatch({
_id: 'match-voided-code-updated-at-fallback',
gameCount: 1,
gameSchedule: undefined,
result: { games: [], seriesResults: [] },
tournamentCodes: [
{
_id: 'voided-code-updated-at-fallback',
state: 'voided',
gameNumber: 1,
reservedAt: '2026-07-01T00:00:00.000Z',
usedAt: '2026-07-02T00:00:00.000Z',
updatedAt: '2026-07-03T00:00:00.000Z',
},
],
});
const plan = requireReady(planCompetitionMigration(legacy));
expect(plan.assignments[0]).toMatchObject({
state: 'voided',
voidedAt: '2026-07-03T00:00:00.000Z',
});
});
it('still quarantines an inverted timestamp from the current lifecycle', () => {
const legacy = pairedMatch({
_id: 'match-inverted-current-code-timeline',
gameCount: 1,
gameSchedule: undefined,
result: {
games: [
legacyGame({
gameNumber: 1,
riotMatchId: 'KR_INVERTED_CURRENT_TIMELINE',
riotArchivePresent: true,
}),
],
seriesResults: [],
},
tournamentCodes: [
{
_id: 'used-code-inverted-current-timeline',
state: 'used',
gameNumber: 1,
reservedAt: '2026-07-03T00:00:00.000Z',
usedAt: '2026-07-02T00:00:00.000Z',
},
],
});
const plan = planCompetitionMigration(legacy);
expectQuarantineReason(plan, 'INVALID_TOURNAMENT_CODE_TIMELINE');
});
it('maps a result-less reserved TournamentCode by its global game number', () => {
@@ -2042,14 +2042,18 @@ function buildAssignments(
continue;
}
const tournamentCodeId = opaqueString(code._id);
const riotMatchId =
code.state === 'used' || code.state === 'voided'
? code.riotMatchId
: undefined;
const gameFact = facts.find(
(fact): fact is MigrationGameAcceptedFact =>
fact.type === 'game-accepted' &&
code.riotMatchId !== undefined &&
riotMatchId !== undefined &&
((fact.evidence.type === 'riot' &&
fact.evidence.riotMatchId === code.riotMatchId) ||
fact.evidence.riotMatchId === riotMatchId) ||
(fact.evidence.type === 'legacy' &&
fact.evidence.legacyReference.endsWith(`:${code.riotMatchId}`))),
fact.evidence.legacyReference.endsWith(`:${riotMatchId}`))),
);
const resolution = resolveTournamentCodeAssignmentTarget({
input,
@@ -2081,9 +2085,9 @@ function buildAssignments(
continue;
}
if (
code.riotMatchId &&
riotMatchId &&
acceptedGame &&
!acceptedGameHasRiotIdentity(acceptedGame, code.riotMatchId)
!acceptedGameHasRiotIdentity(acceptedGame, riotMatchId)
) {
reasons.push(
issue(
@@ -2092,7 +2096,7 @@ function buildAssignments(
{
tournamentCodeId,
gameSlotId: slot.id,
riotMatchId: code.riotMatchId,
riotMatchId,
},
),
);
@@ -2117,7 +2121,7 @@ function buildAssignments(
tournamentCodeId,
state: code.state,
...timestamps,
...(code.riotMatchId ? { riotMatchId: code.riotMatchId } : {}),
...(riotMatchId ? { riotMatchId } : {}),
commandId: stableId(
competitionId,
'migration-code-command',
@@ -2176,10 +2180,17 @@ function tournamentCodeTimestamps(
const raw = {
createdAt: optionalDate(code.createdAt),
reservedAt: optionalDate(code.reservedAt),
usedAt: optionalDate(code.usedAt),
releasedAt: optionalDate(code.releasedAt),
voidedAt: optionalDate(code.voidedAt),
updatedAt: optionalDate(code.updatedAt),
usedAt:
code.state === 'used' || code.state === 'voided'
? optionalDate(code.usedAt)
: undefined,
releasedAt:
code.state === 'released' ? optionalDate(code.releasedAt) : undefined,
voidedAt: code.state === 'voided' ? optionalDate(code.voidedAt) : undefined,
updatedAt:
code.state === 'voided' && code.voidedAt == null
? optionalDate(code.updatedAt)
: undefined,
};
if (Object.values(raw).some((value) => value === null)) {
reasons.push(
@@ -2192,20 +2203,29 @@ function tournamentCodeTimestamps(
return null;
}
// TournamentCode pool documents can be restored and reserved again while
// terminal timestamps from a previous binding remain on the document. A
// Competition assignment represents only the current binding, so preserve
// the lifecycle fields that are reachable for its current state.
const reservedAt =
raw.reservedAt ??
raw.createdAt ??
raw.usedAt ??
raw.releasedAt ??
raw.voidedAt ??
(code.state === 'used' || code.state === 'voided'
? raw.usedAt
: undefined) ??
(code.state === 'released' ? raw.releasedAt : undefined) ??
(code.state === 'voided' ? raw.voidedAt : undefined) ??
sourceFallbackDate(input);
const usedAt = raw.usedAt ?? undefined;
const releasedAt = raw.releasedAt ?? undefined;
const usedAt =
code.state === 'used' || code.state === 'voided'
? (raw.usedAt ?? undefined)
: undefined;
const releasedAt =
code.state === 'released' ? (raw.releasedAt ?? undefined) : undefined;
const voidedAt =
raw.voidedAt ??
(code.state === 'voided'
? (raw.updatedAt ?? releasedAt ?? usedAt ?? reservedAt)
: undefined);
code.state === 'voided'
? (raw.voidedAt ?? raw.updatedAt ?? usedAt ?? reservedAt)
: undefined;
const ordered = [reservedAt, usedAt, releasedAt, voidedAt].filter(
(value): value is string => value !== undefined,
);
@@ -2214,7 +2234,14 @@ function tournamentCodeTimestamps(
issue(
'INVALID_TOURNAMENT_CODE_TIMELINE',
'TournamentCode lifecycle timestamps are not chronological.',
{ tournamentCodeId },
{
tournamentCodeId,
state: code.state,
reservedAt,
...(usedAt ? { usedAt } : {}),
...(releasedAt ? { releasedAt } : {}),
...(voidedAt ? { voidedAt } : {}),
},
),
);
return null;
+6
View File
@@ -295,6 +295,12 @@ Migration builds new collections without dual canonical writes.
Series/GameSlot must agree. A multi-slot Series is never assigned to its first
GameSlot as a fallback; missing, conflicting, duplicated, or out-of-range evidence
is quarantined without changing the legacy Match or TournamentCode.
- TournamentCode pool documents may retain timestamps from an earlier binding after
reuse. Migration preserves only the lifecycle reachable from the current state
(`reserved`, `used`, `released`, or `voided`) and validates that current lifecycle;
stale timestamps from a previous binding are not copied into the new assignment.
Riot match identity is likewise carried only for the current `used` or `voided`
binding, never from a restored `reserved` or `released` pool document.
- Dry-run is the default and emits machine-readable and human-readable reports. Every
ready/apply result includes a deterministic `planHash` over the graph, facts, and
TournamentCode targets so audits detect target changes even when counts stay equal.