feat: add double elimination match mode

This commit is contained in:
dahyeon committed 2026-06-26 05:00:08 -07:00
1 parent b9aaf55193
commit e12f182e7f
19 files changed
+3415 -58

No files matched your search

@@ -2,6 +2,55 @@ import mongoose from 'mongoose';
import { MatchService } from './match.service';
describe('MatchService', () => {
function createServiceWithCreateModels() {
const createdPayloads: Record<string, unknown>[] = [];
const MatchModel = {
create: jest.fn().mockImplementation(async (payload) => {
createdPayloads.push(payload);
return payload;
}),
exists: jest.fn().mockResolvedValue(null),
};
const CommunityModel = {
findById: jest.fn().mockReturnValue({
select: jest.fn().mockReturnValue({
lean: jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValue({ matchGameCount: 3 }),
}),
}),
}),
};
const connectionService = {
getModel: jest.fn().mockImplementation(async (name: string) => {
if (name === 'match') return MatchModel;
if (name === 'community') return CommunityModel;
return {};
}),
};
const tournamentCodeService = {
buildSlots: jest.fn().mockResolvedValue([]),
releaseFromMatch: jest.fn().mockResolvedValue(undefined),
};
const communityStatsService = {
invalidateCommunity: jest.fn().mockResolvedValue(undefined),
};
const matchStatsService = {
buildAndUpsert: jest.fn().mockResolvedValue(undefined),
removeByMatch: jest.fn().mockResolvedValue(undefined),
};
return {
service: new MatchService(
connectionService as never,
tournamentCodeService as never,
communityStatsService as never,
matchStatsService as never,
),
MatchModel,
createdPayloads,
};
}
function createService(match: Record<string, unknown>) {
const MatchModel = {
findById: jest.fn().mockResolvedValue(match),
@@ -254,6 +303,57 @@ describe('MatchService', () => {
expect(match.save).toHaveBeenCalledTimes(1);
});
it('normalizes double-elimination match creation', async () => {
const { service, createdPayloads } = createServiceWithCreateModels();
const communityId = new mongoose.Types.ObjectId();
await service.create({
communityId,
data: {
title: 'Double elimination',
scheduledAt: new Date('2026-06-01T10:00:00.000Z'),
maxParticipants: 20,
gameCount: 5,
gameMode: 'double-elimination',
doubleEliminationResetFinalEnabled: true,
},
});
expect(createdPayloads[0]).toMatchObject({
communityId,
maxParticipants: 20,
gameCount: 1,
gameMode: 'double-elimination',
roundRobinMaxRounds: undefined,
doubleElimination: {
teamCount: 4,
resetFinalEnabled: true,
resetFinalCreated: false,
seeds: [],
bracketMatches: [],
placements: {},
events: [],
},
});
});
it('rejects unsupported double-elimination participant counts', async () => {
const { service, MatchModel } = createServiceWithCreateModels();
await expect(
service.create({
communityId: new mongoose.Types.ObjectId(),
data: {
title: 'Invalid double elimination',
scheduledAt: new Date('2026-06-01T10:00:00.000Z'),
maxParticipants: 30,
gameMode: 'double-elimination',
},
}),
).rejects.toThrow('Double elimination supports only 20 or 40 participants.');
expect(MatchModel.create).not.toHaveBeenCalled();
});
it('clears game schedule when a built match returns to recruiting', async () => {
const communityId = new mongoose.Types.ObjectId();
const match = {
@@ -310,7 +410,7 @@ describe('MatchService', () => {
patch: { roundRobinMaxRounds: 3 },
}),
).rejects.toThrow(
'maxParticipants, gameMode, and roundRobinMaxRounds can only be changed while recruiting.',
'maxParticipants, gameMode, roundRobinMaxRounds, and doubleEliminationResetFinalEnabled can only be changed while recruiting.',
);
expect(match.set).not.toHaveBeenCalled();
});