fix: recover legacy tournament code transitions
This commit is contained in:
1 parent
489d040d8f
commit
ef47de7e4f
5 files changed
+775
-5
No files matched your search
+410
-4
@@ -683,28 +683,434 @@ describe('planCompetitionMigration', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('still quarantines an inverted timestamp from the current lifecycle', () => {
|
||||
it('normalizes a post-game reservation timestamp for an accepted used code', () => {
|
||||
const legacy = pairedMatch({
|
||||
_id: 'match-inverted-current-code-timeline',
|
||||
_id: 'match-post-game-used-code-reservation',
|
||||
gameCount: 1,
|
||||
gameSchedule: undefined,
|
||||
updatedAt: '2026-07-03T00:06:00.000Z',
|
||||
result: {
|
||||
games: [
|
||||
legacyGame({
|
||||
gameNumber: 1,
|
||||
riotMatchId: 'KR_INVERTED_CURRENT_TIMELINE',
|
||||
riotMatchId: 'KR_POST_GAME_CODE_RESERVATION',
|
||||
riotArchivePresent: true,
|
||||
riotGameEndedAt: '2026-07-02T00:00:00.000Z',
|
||||
riotGameEndedAtSource: 'gameEndTimestamp',
|
||||
}),
|
||||
],
|
||||
seriesResults: [],
|
||||
},
|
||||
tournamentCodes: [
|
||||
{
|
||||
_id: 'used-code-inverted-current-timeline',
|
||||
_id: 'used-code-reserved-after-game',
|
||||
state: 'used',
|
||||
gameNumber: 1,
|
||||
reservedAt: '2026-07-03T00:00:00.000Z',
|
||||
usedAt: '2026-07-02T00:00:00.000Z',
|
||||
updatedAt: '2026-07-03T00:05:00.000Z',
|
||||
riotMatchId: 'KR_POST_GAME_CODE_RESERVATION',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const plan = requireReady(planCompetitionMigration(legacy));
|
||||
|
||||
expect(plan.assignments[0]).toMatchObject({
|
||||
state: 'used',
|
||||
reservedAt: '2026-07-03T00:00:00.000Z',
|
||||
usedAt: '2026-07-03T00:05:00.000Z',
|
||||
});
|
||||
expect(plan.warnings).toContainEqual({
|
||||
code: 'LEGACY_USED_CODE_TRANSITION_TIME_NORMALIZED',
|
||||
message:
|
||||
'Legacy used TournamentCode usedAt represented Riot game time and was normalized to its persisted transition time.',
|
||||
details: {
|
||||
tournamentCodeId: 'used-code-reserved-after-game',
|
||||
originalReservedAt: '2026-07-03T00:00:00.000Z',
|
||||
originalUsedAt: '2026-07-02T00:00:00.000Z',
|
||||
sourceUpdatedAt: '2026-07-03T00:05:00.000Z',
|
||||
normalizedUsedAt: '2026-07-03T00:05:00.000Z',
|
||||
factRecordedAt: '2026-07-03T00:06:00.000Z',
|
||||
legacyOccurrenceSource: 'riot-archive-gameEndTimestamp',
|
||||
riotMatchId: 'KR_POST_GAME_CODE_RESERVATION',
|
||||
gameSlotId: plan.assignments[0].gameSlotId,
|
||||
strategy: 'use-persisted-transition-time',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('normalizes a chronological legacy Riot occurrence to the persisted used transition', () => {
|
||||
const legacy = pairedMatch({
|
||||
_id: 'match-chronological-legacy-used-occurrence',
|
||||
gameCount: 1,
|
||||
gameSchedule: undefined,
|
||||
updatedAt: '2026-07-02T00:06:00.000Z',
|
||||
result: {
|
||||
games: [
|
||||
legacyGame({
|
||||
gameNumber: 1,
|
||||
riotMatchId: 'KR_CHRONOLOGICAL_LEGACY_USED_OCCURRENCE',
|
||||
riotArchivePresent: true,
|
||||
riotGameEndedAt: '2026-07-02T00:00:00.000Z',
|
||||
riotGameEndedAtSource: 'gameEndTimestamp',
|
||||
}),
|
||||
],
|
||||
seriesResults: [],
|
||||
},
|
||||
tournamentCodes: [
|
||||
{
|
||||
_id: 'chronological-legacy-used-occurrence',
|
||||
state: 'used',
|
||||
gameNumber: 1,
|
||||
reservedAt: '2026-07-01T00:00:00.000Z',
|
||||
usedAt: '2026-07-02T00:00:00.000Z',
|
||||
updatedAt: '2026-07-02T00:05:00.000Z',
|
||||
riotMatchId: 'KR_CHRONOLOGICAL_LEGACY_USED_OCCURRENCE',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const plan = requireReady(planCompetitionMigration(legacy));
|
||||
|
||||
expect(plan.assignments[0]).toMatchObject({
|
||||
reservedAt: '2026-07-01T00:00:00.000Z',
|
||||
usedAt: '2026-07-02T00:05:00.000Z',
|
||||
});
|
||||
expect(plan.warnings).toContainEqual(
|
||||
expect.objectContaining({
|
||||
code: 'LEGACY_USED_CODE_TRANSITION_TIME_NORMALIZED',
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('recognizes the legacy Riot gameCreation fallback occurrence path', () => {
|
||||
const legacy = pairedMatch({
|
||||
_id: 'match-legacy-game-creation-used-occurrence',
|
||||
gameCount: 1,
|
||||
gameSchedule: undefined,
|
||||
updatedAt: '2026-07-02T00:06:00.000Z',
|
||||
result: {
|
||||
games: [
|
||||
legacyGame({
|
||||
gameNumber: 1,
|
||||
riotMatchId: 'KR_LEGACY_GAME_CREATION_USED_OCCURRENCE',
|
||||
riotArchivePresent: true,
|
||||
riotGameEndedAt: '2026-07-02T00:00:00.000Z',
|
||||
riotGameEndedAtSource: 'gameCreation',
|
||||
}),
|
||||
],
|
||||
seriesResults: [],
|
||||
},
|
||||
tournamentCodes: [
|
||||
{
|
||||
_id: 'legacy-game-creation-used-occurrence',
|
||||
state: 'used',
|
||||
gameNumber: 1,
|
||||
reservedAt: '2026-07-01T00:00:00.000Z',
|
||||
usedAt: '2026-07-02T00:00:00.000Z',
|
||||
updatedAt: '2026-07-02T00:05:00.000Z',
|
||||
riotMatchId: 'KR_LEGACY_GAME_CREATION_USED_OCCURRENCE',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const plan = requireReady(planCompetitionMigration(legacy));
|
||||
|
||||
expect(plan.assignments[0].usedAt).toBe('2026-07-02T00:05:00.000Z');
|
||||
const warning = plan.warnings.find(
|
||||
({ code }) => code === 'LEGACY_USED_CODE_TRANSITION_TIME_NORMALIZED',
|
||||
);
|
||||
expect(warning?.details).toMatchObject({
|
||||
legacyOccurrenceSource: 'riot-archive-gameCreation',
|
||||
});
|
||||
});
|
||||
|
||||
it('recognizes the legacy repair gameCreation plus duration occurrence path', () => {
|
||||
const legacy = pairedMatch({
|
||||
_id: 'match-repaired-legacy-used-occurrence',
|
||||
gameCount: 1,
|
||||
gameSchedule: undefined,
|
||||
updatedAt: '2026-07-03T00:06:00.000Z',
|
||||
result: {
|
||||
games: [
|
||||
legacyGame({
|
||||
gameNumber: 1,
|
||||
riotMatchId: 'KR_REPAIRED_LEGACY_USED_OCCURRENCE',
|
||||
riotArchivePresent: true,
|
||||
gameCreation: '2026-07-02T00:00:00.000Z',
|
||||
gameDuration: 1800,
|
||||
riotGameEndedAt: '2026-07-02T00:30:17.000Z',
|
||||
riotGameEndedAtSource: 'gameEndTimestamp',
|
||||
}),
|
||||
],
|
||||
seriesResults: [],
|
||||
},
|
||||
tournamentCodes: [
|
||||
{
|
||||
_id: 'repaired-legacy-used-occurrence',
|
||||
state: 'used',
|
||||
gameNumber: 1,
|
||||
reservedAt: '2026-07-01T00:00:00.000Z',
|
||||
usedAt: '2026-07-02T00:30:00.000Z',
|
||||
updatedAt: '2026-07-03T00:05:00.000Z',
|
||||
riotMatchId: 'KR_REPAIRED_LEGACY_USED_OCCURRENCE',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const plan = requireReady(planCompetitionMigration(legacy));
|
||||
|
||||
expect(plan.assignments[0]).toMatchObject({
|
||||
reservedAt: '2026-07-01T00:00:00.000Z',
|
||||
usedAt: '2026-07-03T00:05:00.000Z',
|
||||
});
|
||||
const warning = plan.warnings.find(
|
||||
({ code }) => code === 'LEGACY_USED_CODE_TRANSITION_TIME_NORMALIZED',
|
||||
);
|
||||
expect(warning?.details).toMatchObject({
|
||||
legacyOccurrenceSource: 'legacy-gameCreation-plus-duration',
|
||||
});
|
||||
});
|
||||
|
||||
it('does not normalize an inverted used-code lifecycle without Riot archive evidence', () => {
|
||||
const legacy = pairedMatch({
|
||||
_id: 'match-unverified-used-code-transition',
|
||||
gameCount: 1,
|
||||
gameSchedule: undefined,
|
||||
updatedAt: '2026-07-03T00:06:00.000Z',
|
||||
result: {
|
||||
games: [
|
||||
legacyGame({
|
||||
gameNumber: 1,
|
||||
riotMatchId: 'KR_UNVERIFIED_USED_CODE_TRANSITION',
|
||||
riotArchivePresent: false,
|
||||
}),
|
||||
],
|
||||
seriesResults: [],
|
||||
},
|
||||
tournamentCodes: [
|
||||
{
|
||||
_id: 'unverified-used-code-transition',
|
||||
state: 'used',
|
||||
gameNumber: 1,
|
||||
reservedAt: '2026-07-03T00:00:00.000Z',
|
||||
usedAt: '2026-07-02T00:00:00.000Z',
|
||||
updatedAt: '2026-07-03T00:05:00.000Z',
|
||||
riotMatchId: 'KR_UNVERIFIED_USED_CODE_TRANSITION',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const plan = planCompetitionMigration(legacy);
|
||||
|
||||
expectQuarantineReason(plan, 'INVALID_TOURNAMENT_CODE_TIMELINE');
|
||||
});
|
||||
|
||||
it('preserves a chronological usedAt that does not match a known occurrence encoding', () => {
|
||||
const legacy = pairedMatch({
|
||||
_id: 'match-current-used-transition-semantics',
|
||||
gameCount: 1,
|
||||
gameSchedule: undefined,
|
||||
updatedAt: '2026-07-02T00:10:00.000Z',
|
||||
result: {
|
||||
games: [
|
||||
legacyGame({
|
||||
gameNumber: 1,
|
||||
riotMatchId: 'KR_CURRENT_USED_TRANSITION_SEMANTICS',
|
||||
riotArchivePresent: true,
|
||||
riotGameEndedAt: '2026-07-02T00:00:00.000Z',
|
||||
riotGameEndedAtSource: 'gameEndTimestamp',
|
||||
}),
|
||||
],
|
||||
seriesResults: [],
|
||||
},
|
||||
tournamentCodes: [
|
||||
{
|
||||
_id: 'current-used-transition-semantics',
|
||||
state: 'used',
|
||||
gameNumber: 1,
|
||||
reservedAt: '2026-07-01T00:00:00.000Z',
|
||||
usedAt: '2026-07-02T00:10:00.000Z',
|
||||
updatedAt: '2026-07-02T00:10:00.000Z',
|
||||
riotMatchId: 'KR_CURRENT_USED_TRANSITION_SEMANTICS',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const plan = requireReady(planCompetitionMigration(legacy));
|
||||
|
||||
expect(plan.assignments[0].usedAt).toBe('2026-07-02T00:10:00.000Z');
|
||||
expect(
|
||||
plan.warnings.some(
|
||||
({ code }) => code === 'LEGACY_USED_CODE_TRANSITION_TIME_NORMALIZED',
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it('quarantines an inverted used timeline with an out-of-range legacy duration without throwing', () => {
|
||||
const legacy = pairedMatch({
|
||||
_id: 'match-out-of-range-legacy-duration',
|
||||
gameCount: 1,
|
||||
gameSchedule: undefined,
|
||||
updatedAt: '2026-07-03T00:06:00.000Z',
|
||||
result: {
|
||||
games: [
|
||||
legacyGame({
|
||||
gameNumber: 1,
|
||||
riotMatchId: 'KR_OUT_OF_RANGE_LEGACY_DURATION',
|
||||
riotArchivePresent: true,
|
||||
gameCreation: '2026-07-02T00:00:00.000Z',
|
||||
gameDuration: Number.MAX_VALUE,
|
||||
}),
|
||||
],
|
||||
seriesResults: [],
|
||||
},
|
||||
tournamentCodes: [
|
||||
{
|
||||
_id: 'out-of-range-legacy-duration',
|
||||
state: 'used',
|
||||
gameNumber: 1,
|
||||
reservedAt: '2026-07-03T00:00:00.000Z',
|
||||
usedAt: '2026-07-02T00:00:00.000Z',
|
||||
updatedAt: '2026-07-03T00:05:00.000Z',
|
||||
riotMatchId: 'KR_OUT_OF_RANGE_LEGACY_DURATION',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const plan = planCompetitionMigration(legacy);
|
||||
|
||||
expectQuarantineReason(plan, 'INVALID_TOURNAMENT_CODE_TIMELINE');
|
||||
});
|
||||
|
||||
it('does not normalize an inverted used-code lifecycle with an invalid transition time', () => {
|
||||
const legacy = pairedMatch({
|
||||
_id: 'match-invalid-used-code-transition-time',
|
||||
gameCount: 1,
|
||||
gameSchedule: undefined,
|
||||
updatedAt: '2026-07-03T00:06:00.000Z',
|
||||
result: {
|
||||
games: [
|
||||
legacyGame({
|
||||
gameNumber: 1,
|
||||
riotMatchId: 'KR_INVALID_USED_CODE_TRANSITION_TIME',
|
||||
riotArchivePresent: true,
|
||||
riotGameEndedAt: '2026-07-02T00:00:00.000Z',
|
||||
riotGameEndedAtSource: 'gameEndTimestamp',
|
||||
}),
|
||||
],
|
||||
seriesResults: [],
|
||||
},
|
||||
tournamentCodes: [
|
||||
{
|
||||
_id: 'invalid-used-code-transition-time',
|
||||
state: 'used',
|
||||
gameNumber: 1,
|
||||
reservedAt: '2026-07-03T00:00:00.000Z',
|
||||
usedAt: '2026-07-02T00:00:00.000Z',
|
||||
updatedAt: '2026-07-02T12:00:00.000Z',
|
||||
riotMatchId: 'KR_INVALID_USED_CODE_TRANSITION_TIME',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const plan = planCompetitionMigration(legacy);
|
||||
|
||||
expectQuarantineReason(plan, 'INVALID_TOURNAMENT_CODE_TIMELINE');
|
||||
});
|
||||
|
||||
it.each<[string, string | undefined]>([
|
||||
['missing', undefined],
|
||||
['earlier than the Riot occurrence', '2026-07-01T12:00:00.000Z'],
|
||||
])(
|
||||
'quarantines a recognized legacy occurrence when persisted transition time is %s',
|
||||
(_label, updatedAt) => {
|
||||
const legacy = pairedMatch({
|
||||
_id: `match-invalid-persisted-transition-${_label}`,
|
||||
gameCount: 1,
|
||||
gameSchedule: undefined,
|
||||
updatedAt: '2026-07-03T00:06:00.000Z',
|
||||
result: {
|
||||
games: [
|
||||
legacyGame({
|
||||
gameNumber: 1,
|
||||
riotMatchId: 'KR_INVALID_PERSISTED_TRANSITION',
|
||||
riotArchivePresent: true,
|
||||
riotGameEndedAt: '2026-07-02T00:00:00.000Z',
|
||||
riotGameEndedAtSource: 'gameEndTimestamp',
|
||||
}),
|
||||
],
|
||||
seriesResults: [],
|
||||
},
|
||||
tournamentCodes: [
|
||||
{
|
||||
_id: `invalid-persisted-transition-${_label}`,
|
||||
state: 'used',
|
||||
gameNumber: 1,
|
||||
reservedAt: '2026-07-01T00:00:00.000Z',
|
||||
usedAt: '2026-07-02T00:00:00.000Z',
|
||||
...(updatedAt ? { updatedAt } : {}),
|
||||
riotMatchId: 'KR_INVALID_PERSISTED_TRANSITION',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const plan = planCompetitionMigration(legacy);
|
||||
|
||||
expectQuarantineReason(plan, 'INVALID_TOURNAMENT_CODE_TIMELINE');
|
||||
},
|
||||
);
|
||||
|
||||
it('does not normalize a used-code transition recorded after the accepted fact', () => {
|
||||
const legacy = pairedMatch({
|
||||
_id: 'match-used-code-transition-after-fact',
|
||||
gameCount: 1,
|
||||
gameSchedule: undefined,
|
||||
updatedAt: '2026-07-03T00:04:00.000Z',
|
||||
result: {
|
||||
games: [
|
||||
legacyGame({
|
||||
gameNumber: 1,
|
||||
riotMatchId: 'KR_USED_CODE_TRANSITION_AFTER_FACT',
|
||||
riotArchivePresent: true,
|
||||
riotGameEndedAt: '2026-07-02T00:00:00.000Z',
|
||||
riotGameEndedAtSource: 'gameEndTimestamp',
|
||||
}),
|
||||
],
|
||||
seriesResults: [],
|
||||
},
|
||||
tournamentCodes: [
|
||||
{
|
||||
_id: 'used-code-transition-after-fact',
|
||||
state: 'used',
|
||||
gameNumber: 1,
|
||||
reservedAt: '2026-07-03T00:00:00.000Z',
|
||||
usedAt: '2026-07-02T00:00:00.000Z',
|
||||
updatedAt: '2026-07-03T00:05:00.000Z',
|
||||
riotMatchId: 'KR_USED_CODE_TRANSITION_AFTER_FACT',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const plan = planCompetitionMigration(legacy);
|
||||
|
||||
expectQuarantineReason(plan, 'INVALID_TOURNAMENT_CODE_TIMELINE');
|
||||
});
|
||||
|
||||
it('still quarantines an inverted released-code lifecycle', () => {
|
||||
const legacy = pairedMatch({
|
||||
_id: 'match-inverted-released-code-timeline',
|
||||
gameCount: 1,
|
||||
gameSchedule: undefined,
|
||||
result: { games: [], seriesResults: [] },
|
||||
tournamentCodes: [
|
||||
{
|
||||
_id: 'released-code-inverted-current-timeline',
|
||||
state: 'released',
|
||||
gameNumber: 1,
|
||||
reservedAt: '2026-07-03T00:00:00.000Z',
|
||||
releasedAt: '2026-07-02T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
@@ -46,6 +46,8 @@ interface LegacyGameInput {
|
||||
winnerSide?: 'blue' | 'red';
|
||||
gameCreation?: number | string | Date;
|
||||
gameDuration?: number;
|
||||
riotGameEndedAt?: number | string | Date;
|
||||
riotGameEndedAtSource?: 'gameEndTimestamp' | 'gameCreation';
|
||||
participants?: Array<{
|
||||
userId?: unknown;
|
||||
teamIndex?: number;
|
||||
@@ -54,6 +56,16 @@ interface LegacyGameInput {
|
||||
}>;
|
||||
}
|
||||
|
||||
type LegacyUsedOccurrenceSource =
|
||||
| 'riot-archive-gameEndTimestamp'
|
||||
| 'riot-archive-gameCreation'
|
||||
| 'legacy-gameCreation-plus-duration';
|
||||
|
||||
interface LegacyUsedOccurrenceEvidence {
|
||||
at: string;
|
||||
source: LegacyUsedOccurrenceSource;
|
||||
}
|
||||
|
||||
interface LegacySeriesResultInput {
|
||||
bracketMatchId?: string;
|
||||
teamAIndex: number;
|
||||
@@ -410,6 +422,7 @@ export function planCompetitionMigration(
|
||||
seriesGraph,
|
||||
teamBuild.teamIdByLegacyIndex,
|
||||
reasons,
|
||||
warnings,
|
||||
);
|
||||
if (reasons.length > 0) {
|
||||
return quarantined(matchId, reasons, warnings);
|
||||
@@ -2030,6 +2043,7 @@ function buildAssignments(
|
||||
graph: SeriesDefinition[],
|
||||
teamIds: ReadonlyMap<number, string>,
|
||||
reasons: CompetitionMigrationIssue[],
|
||||
warnings: CompetitionMigrationIssue[],
|
||||
): CompetitionMigrationAssignment[] {
|
||||
const result: CompetitionMigrationAssignment[] = [];
|
||||
for (const code of input.tournamentCodes ?? []) {
|
||||
@@ -2102,11 +2116,28 @@ function buildAssignments(
|
||||
);
|
||||
continue;
|
||||
}
|
||||
const legacyGame = riotMatchId
|
||||
? input.result?.games.find(
|
||||
(game) => game.riotMatchId?.trim() === riotMatchId,
|
||||
)
|
||||
: undefined;
|
||||
const timestamps = tournamentCodeTimestamps(
|
||||
input,
|
||||
code,
|
||||
tournamentCodeId,
|
||||
reasons,
|
||||
warnings,
|
||||
riotMatchId &&
|
||||
acceptedGame?.evidence.type === 'riot' &&
|
||||
acceptedGame.evidence.riotMatchId === riotMatchId &&
|
||||
legacyGame
|
||||
? {
|
||||
riotMatchId,
|
||||
gameSlotId: slot.id,
|
||||
factRecordedAt: acceptedGame.recordedAt,
|
||||
legacyOccurrences: legacyUsedOccurrenceEvidence(legacyGame),
|
||||
}
|
||||
: undefined,
|
||||
);
|
||||
if (!timestamps) continue;
|
||||
result.push({
|
||||
@@ -2173,6 +2204,13 @@ function tournamentCodeTimestamps(
|
||||
code: LegacyTournamentCodeInput,
|
||||
tournamentCodeId: string,
|
||||
reasons: CompetitionMigrationIssue[],
|
||||
warnings: CompetitionMigrationIssue[],
|
||||
corroboratedRiotTransition?: {
|
||||
riotMatchId: string;
|
||||
gameSlotId: string;
|
||||
factRecordedAt: string;
|
||||
legacyOccurrences: LegacyUsedOccurrenceEvidence[];
|
||||
},
|
||||
): Pick<
|
||||
CompetitionMigrationAssignment,
|
||||
'reservedAt' | 'usedAt' | 'releasedAt' | 'voidedAt'
|
||||
@@ -2216,7 +2254,7 @@ function tournamentCodeTimestamps(
|
||||
(code.state === 'released' ? raw.releasedAt : undefined) ??
|
||||
(code.state === 'voided' ? raw.voidedAt : undefined) ??
|
||||
sourceFallbackDate(input);
|
||||
const usedAt =
|
||||
let usedAt =
|
||||
code.state === 'used' || code.state === 'voided'
|
||||
? (raw.usedAt ?? undefined)
|
||||
: undefined;
|
||||
@@ -2226,6 +2264,67 @@ function tournamentCodeTimestamps(
|
||||
code.state === 'voided'
|
||||
? (raw.voidedAt ?? raw.updatedAt ?? usedAt ?? reservedAt)
|
||||
: undefined;
|
||||
|
||||
// Legacy result collection and repair paths recorded usedAt from Riot game
|
||||
// occurrence, while updatedAt captured the persisted state transition. Keep
|
||||
// target lifecycle meaning uniform by using that transition only when an
|
||||
// archived Riot fact, exact identity, and source occurrence all corroborate.
|
||||
const occurrenceEvidence = corroboratedRiotTransition?.legacyOccurrences.find(
|
||||
(candidate) => candidate.at === raw.usedAt,
|
||||
);
|
||||
if (
|
||||
code.state === 'used' &&
|
||||
raw.reservedAt &&
|
||||
raw.usedAt &&
|
||||
occurrenceEvidence &&
|
||||
corroboratedRiotTransition
|
||||
) {
|
||||
const persistedTransitionAt = optionalDate(code.updatedAt);
|
||||
if (
|
||||
!persistedTransitionAt ||
|
||||
persistedTransitionAt < reservedAt ||
|
||||
persistedTransitionAt < raw.usedAt ||
|
||||
persistedTransitionAt > corroboratedRiotTransition.factRecordedAt
|
||||
) {
|
||||
reasons.push(
|
||||
issue(
|
||||
'INVALID_TOURNAMENT_CODE_TIMELINE',
|
||||
'Legacy used TournamentCode has no valid persisted transition time.',
|
||||
{
|
||||
tournamentCodeId,
|
||||
state: code.state,
|
||||
reservedAt,
|
||||
originalUsedAt: raw.usedAt,
|
||||
sourceUpdatedAt: persistedTransitionAt,
|
||||
factRecordedAt: corroboratedRiotTransition.factRecordedAt,
|
||||
},
|
||||
),
|
||||
);
|
||||
return null;
|
||||
}
|
||||
const originalUsedAt = raw.usedAt;
|
||||
usedAt = persistedTransitionAt;
|
||||
if (originalUsedAt !== usedAt) {
|
||||
warnings.push(
|
||||
issue(
|
||||
'LEGACY_USED_CODE_TRANSITION_TIME_NORMALIZED',
|
||||
'Legacy used TournamentCode usedAt represented Riot game time and was normalized to its persisted transition time.',
|
||||
{
|
||||
tournamentCodeId,
|
||||
originalReservedAt: reservedAt,
|
||||
originalUsedAt,
|
||||
sourceUpdatedAt: persistedTransitionAt,
|
||||
normalizedUsedAt: usedAt,
|
||||
factRecordedAt: corroboratedRiotTransition.factRecordedAt,
|
||||
legacyOccurrenceSource: occurrenceEvidence.source,
|
||||
riotMatchId: corroboratedRiotTransition.riotMatchId,
|
||||
gameSlotId: corroboratedRiotTransition.gameSlotId,
|
||||
strategy: 'use-persisted-transition-time',
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
const ordered = [reservedAt, usedAt, releasedAt, voidedAt].filter(
|
||||
(value): value is string => value !== undefined,
|
||||
);
|
||||
@@ -2282,6 +2381,50 @@ function gameOccurrence(
|
||||
return normalizeDate(game.gameCreation, sourceFallbackDate(input));
|
||||
}
|
||||
|
||||
function legacyUsedOccurrenceEvidence(
|
||||
game: LegacyGameInput,
|
||||
): LegacyUsedOccurrenceEvidence[] {
|
||||
const candidates: LegacyUsedOccurrenceEvidence[] = [];
|
||||
const archivedGameEnd = optionalRiotDate(game.riotGameEndedAt);
|
||||
if (archivedGameEnd && game.riotGameEndedAtSource) {
|
||||
candidates.push({
|
||||
at: archivedGameEnd,
|
||||
source:
|
||||
game.riotGameEndedAtSource === 'gameEndTimestamp'
|
||||
? 'riot-archive-gameEndTimestamp'
|
||||
: 'riot-archive-gameCreation',
|
||||
});
|
||||
}
|
||||
|
||||
const gameCreation = optionalRiotDate(game.gameCreation);
|
||||
if (
|
||||
gameCreation &&
|
||||
typeof game.gameDuration === 'number' &&
|
||||
Number.isFinite(game.gameDuration) &&
|
||||
game.gameDuration >= 0
|
||||
) {
|
||||
const computedGameEnd = optionalDate(
|
||||
Date.parse(gameCreation) + game.gameDuration * 1000,
|
||||
);
|
||||
if (computedGameEnd) {
|
||||
candidates.push({
|
||||
at: computedGameEnd,
|
||||
source: 'legacy-gameCreation-plus-duration',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return candidates.filter(
|
||||
(candidate, index) =>
|
||||
candidates.findIndex((other) => other.at === candidate.at) === index,
|
||||
);
|
||||
}
|
||||
|
||||
function optionalRiotDate(value: unknown): string | null | undefined {
|
||||
if (typeof value !== 'number') return optionalDate(value);
|
||||
return optionalDate(value < 10_000_000_000 ? value * 1000 : value);
|
||||
}
|
||||
|
||||
function compareLegacyGames(
|
||||
left: Omit<PlannedGame, 'ordinal'>,
|
||||
right: Omit<PlannedGame, 'ordinal'>,
|
||||
|
||||
@@ -244,6 +244,162 @@ describe('migrateCompetitionDomain', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('hydrates Riot occurrence evidence and persists the normalized used transition idempotently', async () => {
|
||||
const riotMatchId = 'KR_LEGACY_USED_TRANSITION';
|
||||
const match = readyMatch(riotMatchId);
|
||||
match.updatedAt = new Date('2026-07-03T00:06:00.000Z');
|
||||
const legacyOccurrenceAt = new Date(1_782_864_000_000 + 1800 * 1000);
|
||||
const code = usedTournamentCode(match, riotMatchId);
|
||||
code.reservedAt = new Date('2026-07-03T00:00:00.000Z');
|
||||
code.usedAt = legacyOccurrenceAt;
|
||||
code.updatedAt = new Date('2026-07-03T00:05:00.000Z');
|
||||
const originalCode = canonical(code);
|
||||
const archive = riotArchive(riotMatchId);
|
||||
archive.raw = {
|
||||
info: {
|
||||
gameCreation: 1_782_864_000_000,
|
||||
gameEndTimestamp: legacyOccurrenceAt.getTime(),
|
||||
},
|
||||
};
|
||||
const db = new MemoryMigrationDb({
|
||||
match: [match],
|
||||
riotMatch: [archive],
|
||||
tournamentCode: [code],
|
||||
});
|
||||
|
||||
const dryRun = await migrateCompetitionDomain(db);
|
||||
|
||||
expect(dryRun.summary).toMatchObject({
|
||||
ready: 1,
|
||||
quarantined: 0,
|
||||
warnings: 1,
|
||||
});
|
||||
const warning = dryRun.matches[0].warnings.find(
|
||||
({ code: warningCode }) =>
|
||||
warningCode === 'LEGACY_USED_CODE_TRANSITION_TIME_NORMALIZED',
|
||||
);
|
||||
expect(warning?.details).toMatchObject({
|
||||
originalReservedAt: '2026-07-03T00:00:00.000Z',
|
||||
originalUsedAt: legacyOccurrenceAt.toISOString(),
|
||||
sourceUpdatedAt: '2026-07-03T00:05:00.000Z',
|
||||
normalizedUsedAt: '2026-07-03T00:05:00.000Z',
|
||||
legacyOccurrenceSource: 'riot-archive-gameEndTimestamp',
|
||||
riotMatchId,
|
||||
strategy: 'use-persisted-transition-time',
|
||||
});
|
||||
expect(db.writeCount).toBe(0);
|
||||
|
||||
const firstApply = await migrateCompetitionDomain(db, {
|
||||
dryRun: false,
|
||||
startSession: () => Promise.resolve(new FakeSession()),
|
||||
});
|
||||
|
||||
expect(firstApply.summary).toMatchObject({ applied: 1, failed: 0 });
|
||||
expect(db.docs('tournament_code_assignment')).toContainEqual(
|
||||
expect.objectContaining({
|
||||
state: 'used',
|
||||
reservedAt: new Date('2026-07-03T00:00:00.000Z'),
|
||||
usedAt: new Date('2026-07-03T00:05:00.000Z'),
|
||||
createdAt: new Date('2026-07-03T00:00:00.000Z'),
|
||||
updatedAt: new Date('2026-07-03T00:05:00.000Z'),
|
||||
}),
|
||||
);
|
||||
expect(canonical(db.docs('tournamentCode')[0])).toBe(originalCode);
|
||||
|
||||
const replay = await migrateCompetitionDomain(db, {
|
||||
dryRun: false,
|
||||
startSession: () => Promise.resolve(new FakeSession()),
|
||||
});
|
||||
|
||||
expect(replay.summary).toMatchObject({
|
||||
applied: 0,
|
||||
alreadyApplied: 1,
|
||||
failed: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
[
|
||||
'raw.info.gameCreation',
|
||||
{ raw: { info: { gameCreation: 1_782_864_000_000 } } },
|
||||
],
|
||||
[
|
||||
'top-level gameCreation',
|
||||
{
|
||||
gameCreation: 1_782_864_000_000,
|
||||
raw: { metadata: { matchId: 'KR_GAME_CREATION_FALLBACK' } },
|
||||
},
|
||||
],
|
||||
])(
|
||||
'hydrates the %s Riot occurrence fallback',
|
||||
async (_label, archiveFields) => {
|
||||
const riotMatchId = 'KR_GAME_CREATION_FALLBACK';
|
||||
const match = readyMatch(riotMatchId);
|
||||
const code = usedTournamentCode(match, riotMatchId);
|
||||
code.reservedAt = new Date('2026-07-01T00:10:00.000Z');
|
||||
code.usedAt = new Date('2026-07-01T00:00:00.000Z');
|
||||
code.updatedAt = new Date('2026-07-01T00:20:00.000Z');
|
||||
const archive = Object.assign(riotArchive(riotMatchId), archiveFields);
|
||||
const db = new MemoryMigrationDb({
|
||||
match: [match],
|
||||
riotMatch: [archive],
|
||||
tournamentCode: [code],
|
||||
});
|
||||
|
||||
const result = await migrateCompetitionDomain(db);
|
||||
|
||||
expect(result.summary).toMatchObject({ ready: 1, quarantined: 0 });
|
||||
const warning = result.matches[0].warnings.find(
|
||||
({ code: warningCode }) =>
|
||||
warningCode === 'LEGACY_USED_CODE_TRANSITION_TIME_NORMALIZED',
|
||||
);
|
||||
expect(warning?.details).toMatchObject({
|
||||
originalUsedAt: '2026-07-01T00:00:00.000Z',
|
||||
normalizedUsedAt: '2026-07-01T00:20:00.000Z',
|
||||
legacyOccurrenceSource: 'riot-archive-gameCreation',
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
['null', null],
|
||||
['blank', ''],
|
||||
['invalid', 'not-a-date'],
|
||||
])(
|
||||
'falls back to archive gameCreation when gameEndTimestamp is %s',
|
||||
async (_label, invalidGameEnd) => {
|
||||
const riotMatchId = 'KR_INVALID_ARCHIVE_GAME_END';
|
||||
const match = readyMatch(riotMatchId);
|
||||
const code = usedTournamentCode(match, riotMatchId);
|
||||
code.reservedAt = new Date('2026-07-01T00:10:00.000Z');
|
||||
code.usedAt = new Date('2026-07-01T00:00:00.000Z');
|
||||
code.updatedAt = new Date('2026-07-01T00:20:00.000Z');
|
||||
const archive = riotArchive(riotMatchId);
|
||||
archive.raw = {
|
||||
info: {
|
||||
gameEndTimestamp: invalidGameEnd,
|
||||
gameCreation: 1_782_864_000_000,
|
||||
},
|
||||
};
|
||||
const db = new MemoryMigrationDb({
|
||||
match: [match],
|
||||
riotMatch: [archive],
|
||||
tournamentCode: [code],
|
||||
});
|
||||
|
||||
const result = await migrateCompetitionDomain(db);
|
||||
|
||||
expect(result.summary).toMatchObject({ ready: 1, quarantined: 0 });
|
||||
const warning = result.matches[0].warnings.find(
|
||||
({ code: warningCode }) =>
|
||||
warningCode === 'LEGACY_USED_CODE_TRANSITION_TIME_NORMALIZED',
|
||||
);
|
||||
expect(warning?.details).toMatchObject({
|
||||
legacyOccurrenceSource: 'riot-archive-gameCreation',
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it('quarantines an ambiguous Match without changing any legacy Match field', async () => {
|
||||
const match = ambiguousMatch();
|
||||
const before = canonical(match);
|
||||
|
||||
@@ -856,15 +856,67 @@ function hydrateLegacyGameEvidence(
|
||||
typeof game.payloadHash === 'string' && game.payloadHash.trim().length > 0
|
||||
? game.payloadHash
|
||||
: undefined;
|
||||
const archiveRaw = isRecord(archive.raw) ? archive.raw : undefined;
|
||||
const archiveInfo = isRecord(archiveRaw?.info) ? archiveRaw.info : undefined;
|
||||
const riotGameEndEvidence = resolveRiotGameEndEvidence(
|
||||
archiveInfo,
|
||||
archive.gameCreation,
|
||||
);
|
||||
return {
|
||||
...game,
|
||||
riotArchivePresent: true,
|
||||
...(riotGameEndEvidence === undefined
|
||||
? {}
|
||||
: {
|
||||
riotGameEndedAt: riotGameEndEvidence.value,
|
||||
riotGameEndedAtSource: riotGameEndEvidence.source,
|
||||
}),
|
||||
payloadHash:
|
||||
existingPayloadHash ??
|
||||
`sha256:${createNodeHash(canonicalJson(archive.raw ?? archive))}`,
|
||||
};
|
||||
}
|
||||
|
||||
function resolveRiotGameEndEvidence(
|
||||
archiveInfo: Record<string, unknown> | undefined,
|
||||
archiveGameCreation: unknown,
|
||||
):
|
||||
| {
|
||||
value: unknown;
|
||||
source: 'gameEndTimestamp' | 'gameCreation';
|
||||
}
|
||||
| undefined {
|
||||
const candidates = [
|
||||
{
|
||||
value: archiveInfo?.gameEndTimestamp,
|
||||
source: 'gameEndTimestamp' as const,
|
||||
},
|
||||
{
|
||||
value: archiveInfo?.gameCreation,
|
||||
source: 'gameCreation' as const,
|
||||
},
|
||||
{ value: archiveGameCreation, source: 'gameCreation' as const },
|
||||
];
|
||||
return candidates.find(({ value }) => isValidRiotTimestamp(value));
|
||||
}
|
||||
|
||||
function isValidRiotTimestamp(value: unknown): boolean {
|
||||
if (value === undefined || value === null || value === '') return false;
|
||||
if (typeof value === 'string' && value.trim() === '') return false;
|
||||
if (typeof value === 'number' && !Number.isFinite(value)) return false;
|
||||
const normalized =
|
||||
typeof value === 'number' && value < 10_000_000_000 ? value * 1000 : value;
|
||||
try {
|
||||
const date =
|
||||
normalized instanceof Date
|
||||
? normalized
|
||||
: new Date(normalized as string | number);
|
||||
return !Number.isNaN(date.getTime());
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function upsertReadyPlan(
|
||||
db: CompetitionDomainMigrationDb,
|
||||
plan: ReadyCompetitionMigrationPlan,
|
||||
|
||||
@@ -301,6 +301,19 @@ Migration builds new collections without dual canonical writes.
|
||||
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.
|
||||
- Historical result collection and repair paths stored Riot occurrence time (archive
|
||||
game end/game creation, or game creation plus duration) in legacy `usedAt`, whereas
|
||||
Competition assignments define `usedAt` as the persisted state-transition time.
|
||||
Migration maps recognized legacy occurrence values to `TournamentCode.updatedAt`
|
||||
for every `used` assignment only when an archive-backed accepted game has the same
|
||||
Riot identity and
|
||||
`max(reservedAt, original usedAt) <= updatedAt <= fact.recordedAt`. This also
|
||||
safely covers post-game attachment where legacy `usedAt < reservedAt`. Source
|
||||
values, evidence strategy, slot identity, and normalized time are emitted as an
|
||||
audit warning. A recognized occurrence value without a valid persisted transition,
|
||||
and any uncorroborated inverted lifecycle, remains quarantined. A chronological
|
||||
`usedAt` that does not match a known occurrence encoding continues through the
|
||||
ordinary lifecycle validation because it may already represent transition time.
|
||||
- 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.
|
||||
|
||||
Reference in new issue
Block a user