feat: expose revision-safe competition API
This commit is contained in:
1 parent
f3286e7fd1
commit
55e5b8df70
11 files changed
+2443
-19
No files matched your search
@@ -0,0 +1,319 @@
|
||||
import { ForbiddenException } from '@nestjs/common';
|
||||
import { Types } from 'mongoose';
|
||||
import { ConnectionService } from '../global/database/connection.service';
|
||||
import { CompetitionBootstrapService } from './competition-bootstrap.service';
|
||||
import { CompetitionQueryService } from './competition-query.service';
|
||||
|
||||
describe('CompetitionQueryService', () => {
|
||||
it('bootstraps a built Match once when its Competition is first read', async () => {
|
||||
const harness = createHarness({ competition: null });
|
||||
const bootstrapped = competitionDocument();
|
||||
harness.bootstrapService.bootstrap.mockResolvedValue(bootstrapped);
|
||||
const service = createService(harness);
|
||||
|
||||
const result = await service.getCompetition({
|
||||
communityId: COMMUNITY_ID,
|
||||
matchId: MATCH_ID,
|
||||
});
|
||||
|
||||
expect(harness.matchModel.findOne).toHaveBeenCalledWith({
|
||||
_id: MATCH_ID,
|
||||
communityId: COMMUNITY_ID,
|
||||
});
|
||||
expect(harness.bootstrapService.bootstrap).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ _id: MATCH_ID, status: 'team-built' }),
|
||||
);
|
||||
expect(result).toBe(bootstrapped);
|
||||
});
|
||||
|
||||
it('returns an explicit team view with codes only for the callers team', async () => {
|
||||
const harness = createHarness();
|
||||
const service = createService(harness);
|
||||
|
||||
const result = await service.getTeamView({
|
||||
communityId: COMMUNITY_ID,
|
||||
matchId: MATCH_ID,
|
||||
userId: TEAM_A_USER_ID,
|
||||
});
|
||||
|
||||
expect(result).toMatchObject({
|
||||
competitionId: COMPETITION_ID.toHexString(),
|
||||
revision: 4,
|
||||
teamId: TEAM_A_ID.toHexString(),
|
||||
activeSeries: [expect.objectContaining({ seriesId: SERIES_ID })],
|
||||
tournamentCodes: [
|
||||
{
|
||||
gameSlotId: GAME_SLOT_ID,
|
||||
seriesId: SERIES_ID,
|
||||
ordinal: 1,
|
||||
displayNumber: 1,
|
||||
state: 'reserved',
|
||||
code: 'TEAM-A-SECRET-CODE',
|
||||
expiresAt: '2026-07-13T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(JSON.stringify(result)).not.toContain('OPPONENT-SECRET-CODE');
|
||||
expect(harness.tournamentCodeModel.find).toHaveBeenCalledWith({
|
||||
_id: { $in: [TEAM_A_CODE_ID] },
|
||||
});
|
||||
});
|
||||
|
||||
it('does not reveal a team view to an authenticated non-participant', async () => {
|
||||
const service = createService(createHarness());
|
||||
|
||||
await expect(
|
||||
service.getTeamView({
|
||||
communityId: COMMUNITY_ID,
|
||||
matchId: MATCH_ID,
|
||||
userId: new Types.ObjectId('64c000000000000000000099'),
|
||||
}),
|
||||
).rejects.toBeInstanceOf(ForbiddenException);
|
||||
});
|
||||
|
||||
it('pages facts in immutable revision order using an opaque cursor', async () => {
|
||||
const harness = createHarness();
|
||||
harness.factModel.find.mockReturnValueOnce(
|
||||
queryResult([
|
||||
factDocument('64c000000000000000000031', 1, 1),
|
||||
factDocument('64c000000000000000000032', 2, 1),
|
||||
factDocument('64c000000000000000000033', 3, 1),
|
||||
]),
|
||||
);
|
||||
const service = createService(harness);
|
||||
|
||||
const first = await service.listFacts({
|
||||
communityId: COMMUNITY_ID,
|
||||
matchId: MATCH_ID,
|
||||
limit: 2,
|
||||
});
|
||||
|
||||
expect(first.facts).toHaveLength(2);
|
||||
expect(first.nextCursor).toEqual(expect.any(String));
|
||||
expect(first.facts[0]).toMatchObject({
|
||||
id: '64c000000000000000000031',
|
||||
competitionId: COMPETITION_ID.toHexString(),
|
||||
type: 'game-accepted',
|
||||
revision: 1,
|
||||
});
|
||||
expect(first.facts[0]).not.toHaveProperty('requestHash');
|
||||
expect(first.facts[0]).not.toHaveProperty('idempotencyKey');
|
||||
});
|
||||
});
|
||||
|
||||
const COMMUNITY_ID = new Types.ObjectId('64c000000000000000000001');
|
||||
const MATCH_ID = new Types.ObjectId('64c000000000000000000002');
|
||||
const COMPETITION_ID = new Types.ObjectId('64c000000000000000000003');
|
||||
const TEAM_A_ID = new Types.ObjectId('64c000000000000000000004');
|
||||
const TEAM_B_ID = new Types.ObjectId('64c000000000000000000005');
|
||||
const TEAM_A_USER_ID = new Types.ObjectId('64c000000000000000000006');
|
||||
const TEAM_B_USER_ID = new Types.ObjectId('64c000000000000000000007');
|
||||
const TEAM_A_CODE_ID = new Types.ObjectId('64c000000000000000000008');
|
||||
const TEAM_B_CODE_ID = new Types.ObjectId('64c000000000000000000009');
|
||||
const SERIES_ID = 'series-1';
|
||||
const GAME_SLOT_ID = 'slot-1';
|
||||
|
||||
function createService(harness: ReturnType<typeof createHarness>) {
|
||||
return new CompetitionQueryService(
|
||||
harness.connectionService as ConnectionService,
|
||||
harness.bootstrapService as unknown as CompetitionBootstrapService,
|
||||
);
|
||||
}
|
||||
|
||||
function createHarness(options: { competition?: unknown } = {}) {
|
||||
const competition =
|
||||
options.competition === undefined
|
||||
? competitionDocument()
|
||||
: options.competition;
|
||||
const competitionModel = {
|
||||
findOne: jest.fn().mockReturnValue(queryResult(competition)),
|
||||
};
|
||||
const matchModel = {
|
||||
findOne: jest.fn().mockReturnValue(
|
||||
queryResult({
|
||||
_id: MATCH_ID,
|
||||
communityId: COMMUNITY_ID,
|
||||
status: 'team-built',
|
||||
teams: [{ teamIndex: 1 }, { teamIndex: 2 }],
|
||||
participants: [],
|
||||
gameMode: 'pair',
|
||||
gameCount: 3,
|
||||
}),
|
||||
),
|
||||
};
|
||||
const assignmentModel = {
|
||||
find: jest.fn().mockReturnValue(
|
||||
queryResult([
|
||||
{
|
||||
competitionId: COMPETITION_ID,
|
||||
seriesId: SERIES_ID,
|
||||
gameSlotId: GAME_SLOT_ID,
|
||||
tournamentCodeId: TEAM_A_CODE_ID,
|
||||
state: 'reserved',
|
||||
},
|
||||
{
|
||||
competitionId: COMPETITION_ID,
|
||||
seriesId: 'series-opponent-only',
|
||||
gameSlotId: 'slot-opponent-only',
|
||||
tournamentCodeId: TEAM_B_CODE_ID,
|
||||
state: 'reserved',
|
||||
},
|
||||
]),
|
||||
),
|
||||
};
|
||||
const tournamentCodeModel = {
|
||||
find: jest.fn().mockReturnValue(
|
||||
queryResult([
|
||||
{
|
||||
_id: TEAM_A_CODE_ID,
|
||||
code: 'TEAM-A-SECRET-CODE',
|
||||
expiresAt: new Date('2026-07-13T00:00:00.000Z'),
|
||||
},
|
||||
{
|
||||
_id: TEAM_B_CODE_ID,
|
||||
code: 'OPPONENT-SECRET-CODE',
|
||||
expiresAt: new Date('2026-07-13T00:00:00.000Z'),
|
||||
},
|
||||
]),
|
||||
),
|
||||
};
|
||||
const factModel = {
|
||||
find: jest.fn().mockReturnValue(queryResult([])),
|
||||
};
|
||||
const models: Record<string, unknown> = {
|
||||
competition: competitionModel,
|
||||
match: matchModel,
|
||||
tournamentCodeAssignment: assignmentModel,
|
||||
tournamentCode: tournamentCodeModel,
|
||||
competitionFact: factModel,
|
||||
};
|
||||
return {
|
||||
competitionModel,
|
||||
matchModel,
|
||||
assignmentModel,
|
||||
tournamentCodeModel,
|
||||
factModel,
|
||||
connectionService: {
|
||||
getModel: jest.fn((name: string) => Promise.resolve(models[name])),
|
||||
},
|
||||
bootstrapService: { bootstrap: jest.fn() },
|
||||
};
|
||||
}
|
||||
|
||||
function competitionDocument() {
|
||||
return {
|
||||
_id: COMPETITION_ID,
|
||||
matchId: MATCH_ID,
|
||||
communityId: COMMUNITY_ID,
|
||||
schemaVersion: 1,
|
||||
projectorVersion: 1,
|
||||
revision: 4,
|
||||
phase: 'running',
|
||||
teams: [
|
||||
{
|
||||
teamId: TEAM_A_ID,
|
||||
displayOrder: 1,
|
||||
seedNumber: 1,
|
||||
name: 'Team A',
|
||||
members: [{ userId: TEAM_A_USER_ID, displayName: 'A' }],
|
||||
},
|
||||
{
|
||||
teamId: TEAM_B_ID,
|
||||
displayOrder: 2,
|
||||
seedNumber: 2,
|
||||
name: 'Team B',
|
||||
members: [{ userId: TEAM_B_USER_ID, displayName: 'B' }],
|
||||
},
|
||||
],
|
||||
format: {
|
||||
type: 'paired-series',
|
||||
rule: { type: 'fixed-games', gameCount: 3, drawPolicy: 'allow' },
|
||||
},
|
||||
seriesGraph: [],
|
||||
projection: {
|
||||
sourceRevision: 4,
|
||||
projectorVersion: 1,
|
||||
phase: 'running',
|
||||
formatType: 'paired-series',
|
||||
series: [
|
||||
{
|
||||
seriesId: SERIES_ID,
|
||||
status: 'in-progress',
|
||||
stage: { type: 'pair', displayOrder: 1 },
|
||||
rule: { type: 'fixed-games', gameCount: 3, drawPolicy: 'allow' },
|
||||
entries: [
|
||||
{ slot: 'A', teamId: TEAM_A_ID.toHexString(), gameWins: 1 },
|
||||
{ slot: 'B', teamId: TEAM_B_ID.toHexString(), gameWins: 0 },
|
||||
],
|
||||
},
|
||||
{
|
||||
seriesId: 'series-opponent-only',
|
||||
status: 'in-progress',
|
||||
stage: { type: 'pair', displayOrder: 2 },
|
||||
rule: { type: 'fixed-games', gameCount: 3, drawPolicy: 'allow' },
|
||||
entries: [
|
||||
{ slot: 'A', teamId: TEAM_B_ID.toHexString(), gameWins: 0 },
|
||||
{ slot: 'B', teamId: 'other-team', gameWins: 1 },
|
||||
],
|
||||
},
|
||||
],
|
||||
games: [],
|
||||
gameSlots: [
|
||||
{
|
||||
id: GAME_SLOT_ID,
|
||||
seriesId: SERIES_ID,
|
||||
ordinal: 1,
|
||||
displayNumber: 1,
|
||||
status: 'reserved',
|
||||
entries: [],
|
||||
},
|
||||
],
|
||||
rounds: [],
|
||||
connectors: [],
|
||||
},
|
||||
createdAt: new Date('2026-07-11T00:00:00.000Z'),
|
||||
updatedAt: new Date('2026-07-12T00:00:00.000Z'),
|
||||
};
|
||||
}
|
||||
|
||||
function factDocument(id: string, revision: number, ordinal: number) {
|
||||
return {
|
||||
_id: new Types.ObjectId(id),
|
||||
competitionId: COMPETITION_ID,
|
||||
type: 'game-accepted',
|
||||
revision,
|
||||
ordinalWithinRevision: ordinal,
|
||||
commandId: `command-${revision}`,
|
||||
idempotencyKey: `secret-${revision}`,
|
||||
requestHash: `hash-${revision}`,
|
||||
schemaVersion: 1,
|
||||
actor: { type: 'system' },
|
||||
occurredAt: new Date('2026-07-12T00:00:00.000Z'),
|
||||
recordedAt: new Date('2026-07-12T00:00:01.000Z'),
|
||||
gameId: new Types.ObjectId('64c000000000000000000041'),
|
||||
seriesId: new Types.ObjectId('64c000000000000000000042'),
|
||||
gameSlotId: new Types.ObjectId('64c000000000000000000043'),
|
||||
ordinal: 1,
|
||||
evidence: { type: 'legacy', legacyReference: 'legacy-1' },
|
||||
mapping: {
|
||||
mappingVersion: 1,
|
||||
confidence: 1,
|
||||
blueTeamId: TEAM_A_ID,
|
||||
redTeamId: TEAM_B_ID,
|
||||
winnerTeamId: TEAM_A_ID,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function queryResult<T>(value: T) {
|
||||
const query = {
|
||||
sort: jest.fn(),
|
||||
limit: jest.fn(),
|
||||
lean: jest.fn(),
|
||||
exec: jest.fn().mockResolvedValue(value),
|
||||
};
|
||||
query.sort.mockReturnValue(query);
|
||||
query.limit.mockReturnValue(query);
|
||||
query.lean.mockReturnValue(query);
|
||||
return query;
|
||||
}
|
||||
Reference in new issue
Block a user