import mongoose from 'mongoose'; import type { MatchDocument } from '../global/database/schema'; import type { RiotMatchDto, RiotMatchTimelineDto } from '../riot/riot.types'; import { MatchStatsFragmentBuilder } from './match-stats-fragment.builder'; function makeMatch(): MatchDocument { const userId = new mongoose.Types.ObjectId(); return { _id: new mongoose.Types.ObjectId(), communityId: new mongoose.Types.ObjectId(), title: '내전', scheduledAt: new Date('2026-05-03T00:00:00.000Z'), status: 'finished', gameCount: 1, participants: [ { userId, puuid: 'puuid-1', gameName: 'Player', profileIconId: 7, }, ], teams: [ { teamIndex: 1, name: 'A', members: [{ userId, assignedPosition: 'TOP' }], }, { teamIndex: 2, name: 'B', members: [] }, ], result: { winnerTeamIndex: 1, riotMatchIds: ['KR_1'], games: [ { gameNumber: 1, riotMatchId: 'KR_1', blueTeamIndex: 1, redTeamIndex: 2, winnerTeamIndex: 1, winnerSide: 'blue', gameCreation: 1_000, gameDuration: 1_800, participants: [ { userId, teamIndex: 1, side: 'blue', championId: 266, kills: 5, deaths: 1, assists: 7, win: true, }, ], }, ], seriesResults: [ { teamAIndex: 1, teamBIndex: 2, teamAWins: 1, teamBWins: 0, winnerTeamIndex: 1, }, ], }, } as unknown as MatchDocument; } describe('MatchStatsFragmentBuilder', () => { it('builds a fallback fragment without raw Riot match data', () => { const builder = new MatchStatsFragmentBuilder(); const match = makeMatch(); const fragment = builder.buildFragment({ match, rawByRiotMatchId: new Map(), }); expect(fragment.matchCard.matchId).toBe(match._id.toString()); expect(fragment.gameCreationMin).toBe(1_000); expect(fragment.gameCreationMax).toBe(1_000); expect(fragment.userIds.map((id) => id.toString())).toEqual([ match.participants[0].userId.toString(), ]); expect(fragment.championIds).toEqual([266]); expect(fragment.records).toHaveLength(1); expect(fragment.records[0]).toMatchObject({ summonerName: 'Player', championId: 266, kills: 5, deaths: 1, assists: 7, win: true, }); expect(fragment.gameDetails[0].teams[0].participants).toHaveLength(1); }); it('builds rich set detail from raw Riot match data', () => { const builder = new MatchStatsFragmentBuilder(); const match = makeMatch(); const raw = { metadata: { dataVersion: '2', matchId: 'KR_1', participants: ['puuid-1'], }, info: { gameCreation: 1_000, gameDuration: 1_800, gameId: 1, gameMode: 'CLASSIC', gameName: 'teambuilder-match-1', gameStartTimestamp: 1_000, gameType: 'CUSTOM_GAME', gameVersion: '16.11.1.1234', mapId: 11, participants: [ { assists: 7, champExperience: 15_000, champLevel: 15, championId: 266, championName: 'Aatrox', deaths: 1, firstBloodAssist: false, firstBloodKill: true, goldEarned: 12_345, goldSpent: 11_000, individualPosition: 'TOP', item0: 1054, item1: 3071, item2: 0, item3: 0, item4: 0, item5: 0, item6: 3340, kills: 5, neutralMinionsKilled: 8, participantId: 1, perks: { statPerks: { defense: 5001, flex: 5008, offense: 5005 }, styles: [ { description: 'primaryStyle', style: 8000, selections: [ { perk: 8010, var1: 0, var2: 0, var3: 0 }, { perk: 9111, var1: 0, var2: 0, var3: 0 }, { perk: 9105, var1: 0, var2: 0, var3: 0 }, { perk: 8299, var1: 0, var2: 0, var3: 0 }, ], }, { description: 'subStyle', style: 8400, selections: [ { perk: 8444, var1: 0, var2: 0, var3: 0 }, { perk: 8451, var1: 0, var2: 0, var3: 0 }, ], }, ], }, puuid: 'puuid-1', riotIdGameName: 'Player', riotIdTagLine: 'KR1', summoner1Id: 4, summoner2Id: 12, summonerId: 'summoner-1', summonerLevel: 100, summonerName: 'Player', teamId: 100, teamPosition: 'TOP', timePlayed: 1_800, totalDamageDealtToChampions: 18_765, totalDamageTaken: 24_321, totalMinionsKilled: 220, visionScore: 18, visionWardsBoughtInGame: 2, wardsKilled: 3, wardsPlaced: 11, win: true, }, ], platformId: 'KR', queueId: 0, teams: [ { bans: [ { championId: 14, pickTurn: 1 }, { championId: 43, pickTurn: 2 }, ], objectives: { baron: { first: true, kills: 1 }, champion: { first: true, kills: 5 }, dragon: { first: true, kills: 2 }, horde: { first: false, kills: 3 }, inhibitor: { first: false, kills: 0 }, riftHerald: { first: false, kills: 0 }, tower: { first: true, kills: 7 }, }, teamId: 100, win: true, }, ], }, }; const timeline = { metadata: { dataVersion: '2', matchId: 'KR_1', participants: ['puuid-1'], }, info: { frameInterval: 60_000, frames: [ { timestamp: 0, participantFrames: { '1': { participantId: 1, totalGold: 500, xp: 0 }, }, }, { timestamp: 60_000, events: [ { type: 'ITEM_PURCHASED', timestamp: 10_000, participantId: 1, itemId: 1054, }, { type: 'SKILL_LEVEL_UP', timestamp: 11_000, participantId: 1, skillSlot: 1, levelUpType: 'NORMAL', }, { type: 'ITEM_DESTROYED', timestamp: 12_000, participantId: 1, itemId: 2003, }, { type: 'ITEM_UNDO', timestamp: 13_000, participantId: 1, beforeId: 1054, afterId: 3071, }, { type: 'ELITE_MONSTER_KILL', timestamp: 20_000, killerTeamId: 100, monsterType: 'DRAGON', monsterSubType: 'FIRE_DRAGON', }, { type: 'ELITE_MONSTER_KILL', timestamp: 30_000, killerTeamId: 100, monsterType: 'DRAGON', monsterSubType: 'ELDER_DRAGON', }, { type: 'ELITE_MONSTER_KILL', timestamp: 40_000, killerId: 1, monsterType: 'HORDE', }, { type: 'SKILL_LEVEL_UP', timestamp: 45_000, participantId: 1, skillSlot: 3, levelUpType: 'NORMAL', }, { type: 'ELITE_MONSTER_KILL', timestamp: 50_000, killerTeamId: 100, monsterType: 'BARON_NASHOR', }, { type: 'ELITE_MONSTER_KILL', timestamp: 55_000, killerTeamId: 100, monsterType: 'RIFTHERALD', }, ], participantFrames: { '1': { participantId: 1, totalGold: 1_150, xp: 550 }, }, }, ], }, }; const fragment = builder.buildFragment({ match, rawByRiotMatchId: new Map([['KR_1', raw as RiotMatchDto]]), timelineByRiotMatchId: new Map([ ['KR_1', timeline as RiotMatchTimelineDto], ]), }); expect(fragment.gameDetails[0]).toMatchObject({ gameVersion: '16.11.1.1234', objectiveEvents: [ { timestamp: 20, side: 'blue', type: 'dragon', monsterSubType: 'FIRE_DRAGON', }, { timestamp: 30, side: 'blue', type: 'elderDragon', monsterSubType: 'ELDER_DRAGON', }, { timestamp: 40, side: 'blue', type: 'voidGrub' }, { timestamp: 50, side: 'blue', type: 'baron' }, { timestamp: 55, side: 'blue', type: 'riftHerald' }, ], timeline: [ { timestamp: 0, blue: { gold: 500, xp: 0 }, red: { gold: 0, xp: 0 }, goldDiff: 500, xpDiff: 0, }, { timestamp: 60, blue: { gold: 1_150, xp: 550 }, red: { gold: 0, xp: 0 }, goldDiff: 1_150, xpDiff: 550, }, ], }); expect(fragment.gameDetails[0].teams[0].bans).toEqual([ { championId: 14, pickTurn: 1 }, { championId: 43, pickTurn: 2 }, ]); expect(fragment.gameDetails[0].teams[0].objectives).toMatchObject({ baron: 1, dragon: 1, elderDragon: 1, inhibitor: 0, riftHerald: 1, tower: 7, voidGrub: 1, }); expect(fragment.records[0]).toMatchObject({ championLevel: 15, cs: 228, damageDealtToChampions: 18_765, damageTaken: 24_321, firstBloodKill: true, firstBloodAssist: false, controlWardsBought: 2, wardsKilled: 3, wardsPlaced: 11, perks: { primaryStyle: 8000, subStyle: 8400, keystone: 8010, selectedPerkIds: [8010, 9111, 9105, 8299, 8444, 8451], styles: [ { styleId: 8000, perkIds: [8010, 9111, 9105, 8299] }, { styleId: 8400, perkIds: [8444, 8451] }, ], statPerks: { defense: 5001, flex: 5008, offense: 5005 }, }, itemBuild: [ { timestamp: 10, itemId: 1054, type: 'purchased' }, { timestamp: 12, itemId: 2003, type: 'destroyed' }, { timestamp: 13, itemId: 3071, type: 'undo', beforeId: 1054, afterId: 3071, }, ], skillBuild: [ { timestamp: 11, skillSlot: 1, level: 1, levelUpType: 'NORMAL' }, { timestamp: 45, skillSlot: 3, level: 2, levelUpType: 'NORMAL' }, ], }); }); });