fix: close competition cutover integrity gaps

This commit is contained in:
dahyeon committed 2026-07-12 21:22:22 +00:00
1 parent 7ea2002975
commit 683d602d63
57 files changed
+4805 -1582

No files matched your search

@@ -36,6 +36,7 @@ describe('migrateCompetitionDomain', () => {
expect(startSession).not.toHaveBeenCalled();
expect(db.writeCount).toBe(0);
expect(db.docs('competition')).toHaveLength(0);
expect(db.createdIndexes).toHaveLength(0);
});
it('applies one Match atomically and converges on an already-applied replay', async () => {
@@ -181,6 +182,309 @@ describe('migrateCompetitionDomain', () => {
});
expect(db.writeCount).toBe(0);
});
it('ensures every Competition unique or partial index before the first apply write', async () => {
const match = readyMatch();
const db = new MemoryMigrationDb({
match: [match],
riotMatch: [riotArchive()],
tournamentCode: [usedTournamentCode(match)],
});
const result = await migrateCompetitionDomain(db, {
dryRun: false,
startSession: () => Promise.resolve(new FakeSession()),
});
expect(result.indexPreflight).toMatchObject({
status: 'ensured',
requiredIndexes: 10,
conflicts: [],
});
expect(result.indexPreflight.ensuredIndexes).toHaveLength(10);
expect(db.createdIndexes).toHaveLength(10);
expect(db.createdIndexes).toEqual(
expect.arrayContaining([
expect.objectContaining({
collection: 'match',
key: { competitionId: 1 },
options: expect.objectContaining({
unique: true,
partialFilterExpression: {
competitionId: { $type: 'objectId' },
},
}),
}),
expect.objectContaining({
collection: 'competition',
key: { matchId: 1 },
options: expect.objectContaining({ unique: true }),
}),
expect.objectContaining({
collection: 'competition_fact',
key: { 'evidence.riotMatchId': 1 },
options: expect.objectContaining({
unique: true,
partialFilterExpression: expect.any(Object),
}),
}),
expect.objectContaining({
collection: 'tournament_code_assignment',
key: { gameSlotId: 1 },
options: expect.objectContaining({
unique: true,
partialFilterExpression: expect.any(Object),
}),
}),
]),
);
expect(db.operations.indexOf('create-index')).toBeLessThan(
db.operations.indexOf('write'),
);
});
it('blocks apply with a machine-readable report when a required index cannot be created', async () => {
const match = readyMatch();
const db = new MemoryMigrationDb({ match: [match] });
db.indexCreationError = Object.assign(
new Error('E11000 duplicate key while building competitionId_1'),
{
code: 11000,
keyPattern: { competitionId: 1 },
keyValue: { competitionId: match._id },
},
);
const startSession = jest.fn(() => Promise.resolve(new FakeSession()));
const result = await migrateCompetitionDomain(db, {
dryRun: false,
startSession,
});
expect(result.indexPreflight).toMatchObject({
status: 'blocked',
conflicts: [
expect.objectContaining({
collection: 'match',
code: 11000,
keyValue: {
competitionId: (match._id as mongoose.Types.ObjectId).toHexString(),
},
}),
],
});
expect(result.matches).toEqual([]);
expect(result.summary.scanned).toBe(0);
expect(startSession).not.toHaveBeenCalled();
expect(db.writeCount).toBe(0);
});
it('quarantines a later Match that reuses an already accepted global Riot identity', async () => {
const firstMatch = readyMatch('KR_SHARED_MIGRATION');
const secondMatch = readyMatch('KR_SHARED_MIGRATION');
const db = new MemoryMigrationDb({
match: [firstMatch, secondMatch],
riotMatch: [riotArchive('KR_SHARED_MIGRATION')],
tournamentCode: [
usedTournamentCode(firstMatch, 'KR_SHARED_MIGRATION'),
usedTournamentCode(secondMatch, 'KR_SHARED_MIGRATION'),
],
});
const result = await migrateCompetitionDomain(db, {
dryRun: false,
startSession: () => Promise.resolve(new FakeSession()),
});
expect(result.summary).toMatchObject({ applied: 1, quarantined: 1 });
expect(result.matches).toContainEqual(
expect.objectContaining({
status: 'quarantined',
reasons: expect.arrayContaining([
expect.objectContaining({ code: 'GLOBAL_UNIQUE_INDEX_CONFLICT' }),
]),
}),
);
expect(db.docs('competition_fact')).toHaveLength(1);
expect(db.docs('competition_migration_quarantine')).toHaveLength(1);
});
it('resumes repeated limited apply runs after the durable Match checkpoint', async () => {
const firstMatch = readyMatch('KR_BATCH_1');
const secondMatch = readyMatch('KR_BATCH_2');
const db = new MemoryMigrationDb({
match: [firstMatch, secondMatch],
riotMatch: [riotArchive('KR_BATCH_1'), riotArchive('KR_BATCH_2')],
tournamentCode: [
usedTournamentCode(firstMatch, 'KR_BATCH_1'),
usedTournamentCode(secondMatch, 'KR_BATCH_2'),
],
});
const first = await migrateCompetitionDomain(db, {
dryRun: false,
limit: 1,
startSession: () => Promise.resolve(new FakeSession()),
});
const second = await migrateCompetitionDomain(db, {
dryRun: false,
limit: 1,
startSession: () => Promise.resolve(new FakeSession()),
});
expect(first.summary).toMatchObject({ scanned: 1, applied: 1 });
expect(first.resume).toMatchObject({
strategy: 'checkpoint',
hasMore: true,
completed: false,
});
expect(second.summary).toMatchObject({ scanned: 1, applied: 1 });
expect(second.matches[0].matchId).not.toBe(first.matches[0].matchId);
expect(second.resume).toMatchObject({
strategy: 'checkpoint',
startedAfterMatchId: first.matches[0].matchId,
hasMore: false,
completed: true,
});
expect(db.docs('competition')).toHaveLength(2);
expect(db.docs('competition_migration_checkpoint')).toHaveLength(1);
});
it('does not let an apply checkpoint silently narrow a later dry-run', async () => {
const firstMatch = readyMatch('KR_DRY_RUN_1');
const secondMatch = readyMatch('KR_DRY_RUN_2');
const db = new MemoryMigrationDb({
match: [firstMatch, secondMatch],
riotMatch: [riotArchive('KR_DRY_RUN_1'), riotArchive('KR_DRY_RUN_2')],
tournamentCode: [
usedTournamentCode(firstMatch, 'KR_DRY_RUN_1'),
usedTournamentCode(secondMatch, 'KR_DRY_RUN_2'),
],
});
const applied = await migrateCompetitionDomain(db, {
dryRun: false,
limit: 1,
startSession: () => Promise.resolve(new FakeSession()),
});
const dryRun = await migrateCompetitionDomain(db, {
dryRun: true,
limit: 1,
});
expect(dryRun.resume.strategy).toBe('none');
expect(dryRun.matches[0].matchId).toBe(applied.matches[0].matchId);
});
it('does not advance a limited-run checkpoint past a failed Match', async () => {
const match = readyMatch('KR_RETRY_AFTER_FAILURE');
const db = new MemoryMigrationDb({
match: [match],
riotMatch: [riotArchive('KR_RETRY_AFTER_FAILURE')],
tournamentCode: [usedTournamentCode(match, 'KR_RETRY_AFTER_FAILURE')],
});
const failed = await migrateCompetitionDomain(db, {
dryRun: false,
limit: 1,
startSession: () => Promise.resolve(undefined),
});
expect(failed.summary).toMatchObject({ scanned: 1, failed: 1 });
expect(failed.resume).toMatchObject({
strategy: 'checkpoint',
hasMore: true,
completed: false,
});
expect(db.docs('competition_migration_checkpoint')).toHaveLength(0);
const retried = await migrateCompetitionDomain(db, {
dryRun: false,
limit: 1,
startSession: () => Promise.resolve(new FakeSession()),
});
expect(db.docs('competition_migration_checkpoint')).toHaveLength(1);
expect(retried.matches[0].matchId).toBe(failed.matches[0].matchId);
expect(retried.summary).toMatchObject({ scanned: 1, applied: 1 });
});
it('durably overrides a later checkpoint before an explicit earlier retry can fail', async () => {
const retryMatch = readyMatch('KR_EXPLICIT_RETRY');
const laterMatch = readyMatch('KR_LATER_CHECKPOINT');
const explicitAfter = '000000000000000000000000';
const migrationRunId = '20260712-competition-domain';
const db = new MemoryMigrationDb({
match: [retryMatch, laterMatch],
riotMatch: [
riotArchive('KR_EXPLICIT_RETRY'),
riotArchive('KR_LATER_CHECKPOINT'),
],
tournamentCode: [
usedTournamentCode(retryMatch, 'KR_EXPLICIT_RETRY'),
usedTournamentCode(laterMatch, 'KR_LATER_CHECKPOINT'),
],
competition_migration_checkpoint: [
{
_id: migrationRunId,
migrationRunId,
lastMatchId: laterMatch._id,
status: 'running',
},
],
});
const failed = await migrateCompetitionDomain(db, {
dryRun: false,
limit: 1,
afterMatchId: explicitAfter,
startSession: () => Promise.resolve(undefined),
});
expect(failed.summary).toMatchObject({ scanned: 1, failed: 1 });
expect(
String(db.docs('competition_migration_checkpoint')[0].lastMatchId),
).toBe(explicitAfter);
const retried = await migrateCompetitionDomain(db, {
dryRun: false,
limit: 1,
startSession: () => Promise.resolve(new FakeSession()),
});
expect(retried.matches[0].matchId).toBe(failed.matches[0].matchId);
expect(retried.summary).toMatchObject({ scanned: 1, applied: 1 });
});
it('does not persist an explicit checkpoint override during dry-run', async () => {
const match = readyMatch('KR_DRY_EXPLICIT_CURSOR');
const migrationRunId = '20260712-competition-domain';
const persistedCheckpoint = objectId();
const db = new MemoryMigrationDb({
match: [match],
riotMatch: [riotArchive('KR_DRY_EXPLICIT_CURSOR')],
tournamentCode: [usedTournamentCode(match, 'KR_DRY_EXPLICIT_CURSOR')],
competition_migration_checkpoint: [
{
_id: migrationRunId,
migrationRunId,
lastMatchId: persistedCheckpoint,
status: 'running',
},
],
});
await migrateCompetitionDomain(db, {
dryRun: true,
limit: 1,
afterMatchId: '000000000000000000000000',
});
expect(
String(db.docs('competition_migration_checkpoint')[0].lastMatchId),
).toBe(persistedCheckpoint.toHexString());
expect(db.writeCount).toBe(0);
});
});
class FakeSession implements CompetitionDomainMigrationSession {
@@ -211,6 +515,13 @@ type AnyDoc = Record<string, any> & { _id: unknown };
class MemoryMigrationDb implements CompetitionDomainMigrationDb {
private readonly collections = new Map<string, AnyDoc[]>();
public writeCount = 0;
public readonly createdIndexes: Array<{
collection: string;
key: Record<string, unknown>;
options: Record<string, unknown>;
}> = [];
public readonly operations: string[] = [];
public indexCreationError?: Error;
constructor(seed: Record<string, AnyDoc[]> = {}) {
for (const [name, docs] of Object.entries(seed)) {
@@ -227,6 +538,23 @@ class MemoryMigrationDb implements CompetitionDomainMigrationDb {
collection(name: string) {
const owner = this;
return {
createIndex(
key: Record<string, unknown>,
options: Record<string, unknown>,
) {
owner.operations.push('create-index');
if (owner.indexCreationError) {
const error = owner.indexCreationError;
owner.indexCreationError = undefined;
return Promise.reject(error);
}
owner.createdIndexes.push({ collection: name, key, options });
return Promise.resolve(
typeof options.name === 'string'
? options.name
: Object.keys(key).join('_'),
);
},
find(filter: Record<string, unknown>) {
return new MemoryCursor(() =>
owner.docs(name).filter((doc) => matches(doc, filter)),
@@ -240,6 +568,7 @@ class MemoryMigrationDb implements CompetitionDomainMigrationDb {
update: Record<string, Record<string, unknown>>,
options: { upsert?: boolean } = {},
) {
owner.operations.push('write');
owner.writeCount += 1;
const docs = owner.docs(name);
let doc = docs.find((candidate) => matches(candidate, filter));
@@ -251,6 +580,7 @@ class MemoryMigrationDb implements CompetitionDomainMigrationDb {
if (!doc) return { matchedCount: 0, modifiedCount: 0 };
if (inserted) Object.assign(doc, update.$setOnInsert ?? {});
Object.assign(doc, update.$set ?? {});
for (const field of Object.keys(update.$unset ?? {})) delete doc[field];
return {
matchedCount: inserted ? 0 : 1,
modifiedCount: 1,
@@ -258,6 +588,7 @@ class MemoryMigrationDb implements CompetitionDomainMigrationDb {
};
},
async deleteOne(filter: Record<string, unknown>) {
owner.operations.push('write');
owner.writeCount += 1;
const docs = owner.docs(name);
const index = docs.findIndex((doc) => matches(doc, filter));
@@ -318,6 +649,9 @@ function matches(doc: AnyDoc, filter: Record<string, unknown>): boolean {
sameValue(actual, candidate),
);
}
if ('$gt' in expected) {
return String(actual) > String(expected.$gt);
}
}
return sameValue(actual, expected);
});
@@ -348,7 +682,7 @@ function isRecord(value: unknown): value is Record<string, any> {
return value !== null && typeof value === 'object' && !Array.isArray(value);
}
function readyMatch(): AnyDoc {
function readyMatch(riotMatchId = 'KR_MIGRATION_1'): AnyDoc {
const matchId = objectId();
const codeId = objectId();
return {
@@ -362,11 +696,11 @@ function readyMatch(): AnyDoc {
gameSchedule: [{ gameNumber: 1, teamAIndex: 1, teamBIndex: 2 }],
tournamentCodeIds: [codeId],
result: {
riotMatchIds: ['KR_MIGRATION_1'],
riotMatchIds: [riotMatchId],
games: [
{
gameNumber: 1,
riotMatchId: 'KR_MIGRATION_1',
riotMatchId,
blueTeamIndex: 1,
redTeamIndex: 2,
winnerTeamIndex: 1,
@@ -383,23 +717,26 @@ function readyMatch(): AnyDoc {
};
}
function usedTournamentCode(match = readyMatch()): AnyDoc {
function usedTournamentCode(
match = readyMatch(),
riotMatchId = 'KR_MIGRATION_1',
): AnyDoc {
return {
_id: match.tournamentCodeIds[0],
matchId: match._id,
state: 'used',
gameNumber: 1,
riotMatchId: 'KR_MIGRATION_1',
riotMatchId,
reservedAt: new Date('2026-07-01T00:30:00.000Z'),
usedAt: new Date('2026-07-01T01:00:00.000Z'),
};
}
function riotArchive(): AnyDoc {
function riotArchive(matchId = 'KR_MIGRATION_1'): AnyDoc {
return {
_id: objectId(),
matchId: 'KR_MIGRATION_1',
raw: { metadata: { matchId: 'KR_MIGRATION_1' } },
matchId,
raw: { metadata: { matchId } },
};
}