feat(api): collect tournament results

This commit is contained in:
dahyeon committed 2026-04-29 00:44:00 +09:00
1 parent 45c7fe34ca
commit 57e3c68ea8
8 files changed
+1645

No files matched your search

@@ -0,0 +1,263 @@
import mongoose from 'mongoose';
import type { MatchDocument } from '../global/database/schema';
import type { RiotMatchDto } from '../riot/riot.types';
import { TournamentResultService } from './tournament-result.service';
type TournamentResultServiceInternals = TournamentResultService & {
resolveRequiredGameSlots(match: MatchDocument): number;
resolveSeriesRefForGameNumber(
match: MatchDocument,
gameNumber: number,
): { teamAIndex: number; teamBIndex: number };
};
function createService() {
return new TournamentResultService(
{} as never,
{} as never,
{} as never,
{} as never,
{} as never,
) as unknown as TournamentResultServiceInternals;
}
interface MatchOverrides {
gameCount?: number;
gameMode?: 'pair' | 'bracket' | 'round-robin';
participants?: Array<{ excluded?: boolean }>;
teams?: Array<{ teamIndex: number }>;
gameSchedule?: Array<{
gameNumber: number;
teamAIndex: number;
teamBIndex: number;
}>;
}
function createMatch(overrides: MatchOverrides): MatchDocument {
return {
gameCount: 1,
gameMode: 'pair',
participants: [],
teams: [],
gameSchedule: [],
...overrides,
} as unknown as MatchDocument;
}
describe('TournamentResultService round-robin scheduling', () => {
it('calculates required slots for round-robin polling', () => {
const service = createService();
const match = createMatch({
gameMode: 'round-robin',
gameCount: 1,
teams: Array.from({ length: 6 }, (_, index) => ({
teamIndex: index + 1,
})),
});
expect(service.resolveRequiredGameSlots(match)).toBe(15);
});
it('uses persisted round-robin schedule when mapping a game number', () => {
const service = createService();
const match = createMatch({
gameMode: 'round-robin',
gameSchedule: [
{ gameNumber: 1, teamAIndex: 3, teamBIndex: 6 },
{ gameNumber: 2, teamAIndex: 1, teamBIndex: 5 },
],
});
expect(service.resolveSeriesRefForGameNumber(match, 2)).toEqual({
teamAIndex: 1,
teamBIndex: 5,
});
});
it('keeps pair game number mapping unchanged', () => {
const service = createService();
const match = createMatch({ gameCount: 3 });
expect(service.resolveSeriesRefForGameNumber(match, 4)).toEqual({
teamAIndex: 3,
teamBIndex: 4,
});
});
});
describe('TournamentResultService manual result collection', () => {
function createManualFixture() {
const communityId = new mongoose.Types.ObjectId();
const matchId = new mongoose.Types.ObjectId();
const users = Array.from({ length: 10 }, (_, index) => ({
userId: new mongoose.Types.ObjectId(),
puuid: `puuid-${index + 1}`,
}));
const match = {
_id: matchId,
communityId,
gameCount: 1,
gameMode: 'pair',
teamBuiltAt: new Date('2026-05-03T10:00:00.000Z'),
status: 'team-built',
tournamentCodeIds: [],
gameSchedule: [],
participants: users.map((user) => ({
userId: user.userId,
puuid: user.puuid,
excluded: false,
})),
teams: [
{
teamIndex: 1,
members: users.slice(0, 5).map((user) => ({ userId: user.userId })),
},
{
teamIndex: 2,
members: users.slice(5).map((user) => ({ userId: user.userId })),
},
],
save: jest.fn().mockResolvedValue(undefined),
} as unknown as MatchDocument & {
save: jest.Mock;
result?: MatchDocument['result'];
};
const detail = {
metadata: {
matchId: 'KR_1234567890',
participants: users.map((user) => user.puuid),
},
info: {
gameCreation: Date.parse('2026-05-03T10:30:00.000Z'),
gameDuration: 1800,
participants: users.map((user, index) => ({
puuid: user.puuid,
teamId: index < 5 ? 100 : 200,
championId: 1,
kills: 1,
deaths: 1,
assists: 1,
win: index < 5,
})),
teams: [
{ teamId: 100, win: true },
{ teamId: 200, win: false },
],
},
} as unknown as RiotMatchDto;
const communityFindById = jest.fn().mockReturnValue({
select: jest.fn().mockReturnValue({
lean: jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValue({ riotRegion: 'asia' }),
}),
}),
});
const riotMatchFindOne = jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValue(null),
});
const riotMatchUpdateOne = jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValue(undefined),
});
const tournamentCodeUpdateOne = jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValue(undefined),
});
const connectionService = {
getModel: jest.fn().mockImplementation(async (name: string) => {
if (name === 'match') {
return { findById: jest.fn().mockResolvedValue(match) };
}
if (name === 'community') {
return { findById: communityFindById };
}
if (name === 'riotMatch') {
return {
findOne: riotMatchFindOne,
updateOne: riotMatchUpdateOne,
};
}
if (name === 'tournamentCode') {
return { updateOne: tournamentCodeUpdateOne };
}
return {};
}),
};
const riotMatchService = {
getMatchIdsByPuuid: jest.fn().mockResolvedValue(['KR_1234567890']),
getMatch: jest.fn().mockResolvedValue(detail),
};
const service = new TournamentResultService(
connectionService as never,
riotMatchService as never,
{} as never,
{ invalidateCommunity: jest.fn().mockResolvedValue(undefined) } as never,
{ buildAndUpsert: jest.fn().mockResolvedValue(undefined) } as never,
);
return {
service,
match,
communityId,
matchId,
riotMatchService,
tournamentCodeUpdateOne,
};
}
it('collects by time window without setting Riot match type', async () => {
const { service, match, communityId, matchId, riotMatchService } =
createManualFixture();
const result = await service.collectManualByWindow({
matchId,
communityId,
startTime: new Date('2026-05-03T10:00:00.000Z'),
endTime: new Date('2026-05-03T11:00:00.000Z'),
});
expect(result).toMatchObject({
scanned: 1,
matched: 1,
gameNumbers: [1],
riotMatchIds: ['KR_1234567890'],
});
expect(riotMatchService.getMatchIdsByPuuid).toHaveBeenCalledWith(
'puuid-1',
expect.not.objectContaining({ type: expect.anything() }),
);
expect(match.result?.games).toHaveLength(1);
expect(match.status).toBe('finished');
});
it('collects directly by Riot match id', async () => {
const {
service,
match,
communityId,
matchId,
riotMatchService,
tournamentCodeUpdateOne,
} = createManualFixture();
const result = await service.collectManualByRiotMatch({
matchId,
communityId,
riotMatchId: ' KR_1234567890 ',
});
expect(result.matched).toBe(1);
expect(riotMatchService.getMatchIdsByPuuid).not.toHaveBeenCalled();
expect(riotMatchService.getMatch).toHaveBeenCalledWith('KR_1234567890', {
region: 'asia',
});
expect(tournamentCodeUpdateOne).toHaveBeenCalledWith(
expect.objectContaining({
matchId,
gameNumber: 1,
}),
expect.objectContaining({
$set: expect.objectContaining({ riotMatchId: 'KR_1234567890' }),
}),
);
expect(match.result?.games[0].winnerTeamIndex).toBe(1);
});
});