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

@@ -1,29 +1,24 @@
import { ForbiddenException } from '@nestjs/common';
import { ConflictException, 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 () => {
it('does not let a public read race the team-build bootstrap workflow', 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,
});
await expect(
service.getCompetition({
communityId: COMMUNITY_ID,
matchId: MATCH_ID,
}),
).rejects.toBeInstanceOf(ConflictException);
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 () => {
@@ -56,6 +51,7 @@ describe('CompetitionQueryService', () => {
expect(JSON.stringify(result)).not.toContain('OPPONENT-SECRET-CODE');
expect(harness.tournamentCodeModel.find).toHaveBeenCalledWith({
_id: { $in: [TEAM_A_CODE_ID] },
state: { $in: ['reserved', 'used'] },
});
});
@@ -71,6 +67,20 @@ describe('CompetitionQueryService', () => {
).rejects.toBeInstanceOf(ForbiddenException);
});
it('does not expose a stale code whose active assignment no longer matches', async () => {
const harness = createHarness();
harness.tournamentCodeModel.find.mockReturnValueOnce(queryResult([]));
const service = createService(harness);
await expect(
service.getTeamView({
communityId: COMMUNITY_ID,
matchId: MATCH_ID,
userId: TEAM_A_USER_ID,
}),
).rejects.toBeInstanceOf(ConflictException);
});
it('pages facts in immutable revision order using an opaque cursor', async () => {
const harness = createHarness();
harness.factModel.find.mockReturnValueOnce(
@@ -116,7 +126,6 @@ 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,
);
}
@@ -167,11 +176,15 @@ function createHarness(options: { competition?: unknown } = {}) {
{
_id: TEAM_A_CODE_ID,
code: 'TEAM-A-SECRET-CODE',
state: 'reserved',
matchId: MATCH_ID,
expiresAt: new Date('2026-07-13T00:00:00.000Z'),
},
{
_id: TEAM_B_CODE_ID,
code: 'OPPONENT-SECRET-CODE',
state: 'reserved',
matchId: MATCH_ID,
expiresAt: new Date('2026-07-13T00:00:00.000Z'),
},
]),
@@ -196,7 +209,6 @@ function createHarness(options: { competition?: unknown } = {}) {
connectionService: {
getModel: jest.fn((name: string) => Promise.resolve(models[name])),
},
bootstrapService: { bootstrap: jest.fn() },
};
}