import mongoose from 'mongoose'; import { MatchHistoryService } from './match-history.service'; describe('MatchHistoryService.getChampionStats', () => { it('aggregates champion stats in MongoDB with secondaryPreferred reads', async () => { const communityId = new mongoose.Types.ObjectId(); const userId = new mongoose.Types.ObjectId(); const aggregateExec = jest.fn().mockResolvedValue([ { championId: 103, games: 2, wins: 1, totalKills: 15, totalDeaths: 5, totalAssists: 20, totalCs: 320, totalGameMinutes: 62, doubleKills: 2, tripleKills: 1, quadraKills: 0, pentaKills: 0, lastFetchedAt: new Date('2026-01-03T00:00:00.000Z'), }, { championId: 64, games: 1, wins: 1, totalKills: 5, totalDeaths: 0, totalAssists: 7, totalCs: 110, totalGameMinutes: 25, doubleKills: 0, tripleKills: 0, quadraKills: 0, pentaKills: 0, lastFetchedAt: new Date('2026-01-04T00:00:00.000Z'), }, ]); const read = jest.fn().mockReturnThis(); const aggregate = jest.fn().mockReturnValue({ read, exec: aggregateExec }); const UserModel = { findById: jest.fn().mockReturnValue({ exec: jest.fn().mockResolvedValue({ _id: userId, communityId, riotAccounts: [ { puuid: 'puuid-1', isPrimary: true, recentStatsFetchedAt: new Date('2026-01-05T00:00:00.000Z'), }, ], }), }), }; const CommunityModel = { findById: jest.fn().mockReturnValue({ exec: jest.fn().mockResolvedValue({ _id: communityId, seasonStartedAt: new Date('2026-01-01T00:00:00.000Z'), seasons: [ { id: 'current', label: '현재 시즌', startedAt: new Date('2026-01-01T00:00:00.000Z'), }, ], }), }), }; const connectionService = { getModel: jest.fn(async (name: string) => { if (name === 'user') return UserModel; if (name === 'community') return CommunityModel; if (name === 'riotMatch') return { aggregate }; throw new Error(`Unexpected model: ${name}`); }), }; const service = new MatchHistoryService(connectionService as never); const response = await service.getChampionStats({ communityId, userId, filters: { queueGroup: 'summoners-rift', rank: 'all' }, }); expect(aggregate).toHaveBeenCalledTimes(1); expect(read).toHaveBeenCalledWith('secondaryPreferred'); expect(aggregateExec).toHaveBeenCalledTimes(1); expect(aggregate.mock.calls[0][0]).toEqual( expect.arrayContaining([ expect.objectContaining({ $match: expect.objectContaining({ participantPuuids: 'puuid-1', gameCreation: { $gte: new Date('2026-01-01T00:00:00.000Z').getTime(), }, }), }), { $limit: 500 }, expect.objectContaining({ $group: expect.any(Object) }), ]), ); expect(response.champions).toEqual([ expect.objectContaining({ championId: 103, games: 2, wins: 1, losses: 1, winRate: 50, avgKills: 7.5, avgDeaths: 2.5, avgAssists: 10, kda: 7, avgCs: 160, }), expect.objectContaining({ championId: 64, games: 1, wins: 1, losses: 0, winRate: 100, kda: 12, }), ]); expect(response.totals).toEqual( expect.objectContaining({ championId: 0, games: 3, wins: 2, losses: 1, totalKills: 20, totalDeaths: 5, totalAssists: 27, }), ); expect(response.lastFetchedAt).toBe('2026-01-03T00:00:00.000Z'); }); });