import { COMPETITION_OUTBOX_STATES, CompetitionFactSchema, CompetitionOutboxSchema, CompetitionSchema, TournamentCodeAssignmentSchema, competitionModelDefinitions, } from './competition.schema'; import { MatchSchema, MatchStatsSchema, tournamentModelDefinitions, } from '../global/database/schema'; describe('Competition persistence schemas', () => { it('registers every Competition-owned collection', () => { expect( competitionModelDefinitions.map(({ name, schemaName }) => ({ name, schemaName, })), ).toEqual([ { name: 'competition', schemaName: 'competition' }, { name: 'competitionFact', schemaName: 'competition_fact' }, { name: 'competitionOutbox', schemaName: 'competition_outbox' }, { name: 'tournamentCodeAssignment', schemaName: 'tournament_code_assignment', }, ]); expect(tournamentModelDefinitions).toEqual( expect.arrayContaining( competitionModelDefinitions.map(({ name, schemaName }) => expect.objectContaining({ name, schemaName }), ), ), ); }); it('links Match and stats projections to the Competition revision', () => { expect(MatchSchema.path('competitionId')).toBeDefined(); expect(MatchStatsSchema.path('sourceCompetitionId')).toBeDefined(); expect(MatchStatsSchema.path('sourceRevision')).toBeDefined(); expect(MatchStatsSchema.path('projectorVersion')).toBeDefined(); expectIndexes(MatchSchema, [ { keys: { competitionId: 1 }, options: { unique: true }, }, ]); }); it('enforces one Competition per Match and monotonic revisions', () => { expectIndexes(CompetitionSchema, [ { keys: { matchId: 1 }, options: { unique: true }, }, { keys: { communityId: 1, phase: 1, updatedAt: -1 }, }, ]); }); it('makes fact ordering and command replay identities unique', () => { expect(CompetitionFactSchema.path('startedAt')).toBeDefined(); expect(CompetitionFactSchema.path('durationSeconds')).toBeDefined(); expect(CompetitionFactSchema.path('participants')).toBeDefined(); expectIndexes(CompetitionFactSchema, [ { keys: { competitionId: 1, revision: 1, ordinalWithinRevision: 1 }, options: { unique: true }, }, { keys: { competitionId: 1, commandId: 1, ordinalWithinRevision: 1 }, options: { unique: true }, }, { keys: { 'evidence.riotMatchId': 1 }, options: { unique: true }, }, ]); }); it('prevents duplicate outbox events', () => { expectIndexes(CompetitionOutboxSchema, [ { keys: { eventId: 1 }, options: { unique: true }, }, { keys: { state: 1, availableAt: 1 }, }, ]); }); it('defines a terminal dead-letter state for poison outbox events', () => { expect(COMPETITION_OUTBOX_STATES).toContain('dead-letter'); expect(CompetitionOutboxSchema.path('state').options.enum).toContain( 'dead-letter', ); }); it('allows only one active assignment for a code or GameSlot', () => { expectIndexes(TournamentCodeAssignmentSchema, [ { keys: { gameSlotId: 1 }, options: { unique: true, partialFilterExpression: { state: { $in: ['reserved', 'used'] } }, }, }, { keys: { tournamentCodeId: 1 }, options: { unique: true, partialFilterExpression: { state: { $in: ['reserved', 'used'] } }, }, }, { keys: { riotMatchId: 1 }, options: { unique: true }, }, ]); }); }); function expectIndexes( schema: { indexes(): Array<[Record, Record]>; }, expected: Array<{ keys: Record; options?: Record; }>, ) { const indexes = schema.indexes(); for (const candidate of expected) { expect(indexes).toEqual( expect.arrayContaining([ [candidate.keys, expect.objectContaining(candidate.options ?? {})], ]), ); } }