import { FEATURE_SCHEMA_VERSION } from '@persona-lenz/shared'; import type { ConnectionService } from '../global/database/connection.service'; import { AnalysisReadService } from './analysis-read.service'; function createLeanExec(value: T) { return { lean: jest.fn(() => ({ exec: jest.fn().mockResolvedValue(value), })), }; } describe('AnalysisReadService', () => { it('queries analysis jobs by jobId and serializes public failed errors', async () => { const findOne = jest.fn(() => createLeanExec({ error: 'internal host redis.local refused connection', jobId: 'analysis-job-1', progress: { fetchedMatches: 0, totalMatches: 20 }, status: 'failed', }), ); const service = new AnalysisReadService({ getModel: jest.fn().mockResolvedValue({ findOne }), } as unknown as ConnectionService); await expect(service.getJobStatus('analysis-job-1')).resolves.toEqual({ error: 'Analysis failed during processing.', jobId: 'analysis-job-1', progress: { fetchedMatches: 0, totalMatches: 20 }, status: 'failed', }); expect(findOne).toHaveBeenCalledWith({ jobId: 'analysis-job-1' }); }); it('preserves public Riot account not-found job errors', async () => { const findOne = jest.fn(() => createLeanExec({ error: 'Riot account was not found for the selected region.', jobId: 'analysis-job-1', progress: { fetchedMatches: 0, totalMatches: 0 }, stage: 'resolve-account', status: 'failed', }), ); const service = new AnalysisReadService({ getModel: jest.fn().mockResolvedValue({ findOne }), } as unknown as ConnectionService); await expect(service.getJobStatus('analysis-job-1')).resolves.toEqual({ error: 'Riot account was not found for the selected region.', jobId: 'analysis-job-1', progress: { fetchedMatches: 0, totalMatches: 0 }, stage: 'resolve-account', status: 'failed', }); }); it('distinguishes analyzed accounts with no ranked games from missing accounts', async () => { const accountFindOne = jest.fn(() => createLeanExec({ gameName: 'Hide on bush', profileIconId: 1, tagLine: 'KR1', tiers: [], }), ); const profileFindOne = jest.fn(() => createLeanExec(null)); const service = new AnalysisReadService({ getModel: jest.fn((modelKey: string) => Promise.resolve( modelKey === 'account' ? { findOne: accountFindOne } : { findOne: profileFindOne }, ), ), } as unknown as ConnectionService); await expect( service.getLatestPlayerAnalysis('target-puuid', 420), ).rejects.toThrow('분석할 랭크 게임이 없음'); }); it('queries account and profile by puuid/queue and applies lower-is-better scoring', async () => { const accountFindOne = jest.fn(() => createLeanExec({ gameName: 'Hide on bush', profileIconId: 1, tagLine: 'KR1', tiers: [], }), ); const profileFindOne = jest.fn(() => createLeanExec({ championPool: [{ championId: 103, games: 4, wins: 3 }], confidence: 0.9, featureSchemaVersion: FEATURE_SCHEMA_VERSION, mainRole: 'MIDDLE', profile: { kda: 0.8, killParticipation: 0.7, damageShare: 0.6, damagePerMin: 0.5, goldPerMin: 0.4, csPerMin: 0.3, visionScorePerMin: 0.2, wardsKilledPerMin: 0.1, controlWardsPlaced: 0.2, deathsPerMin: 0.1, objectiveParticipation: 0.6, soloKills: 0.4, }, queueId: 420, sampleGameCount: 12, updatedAt: new Date('2026-07-02T00:00:00.000Z'), }), ); const service = new AnalysisReadService({ getModel: jest.fn((modelKey: string) => Promise.resolve( modelKey === 'account' ? { findOne: accountFindOne } : { findOne: profileFindOne }, ), ), } as unknown as ConnectionService); const analysis = await service.getLatestPlayerAnalysis('target-puuid', 420); expect(accountFindOne).toHaveBeenCalledWith({ puuid: 'target-puuid' }); expect(profileFindOne).toHaveBeenCalledWith({ puuid: 'target-puuid', queueId: 420, featureSchemaVersion: FEATURE_SCHEMA_VERSION, }); expect(analysis.profile.playstyle.kda).toBe(0.8); expect(analysis.profile.playstyle.goldDiffAt10).toBeUndefined(); expect(analysis.profile.playstyle.objectivePreWardRate).toBeUndefined(); expect( analysis.strengths.some((metric) => metric.metricId === 'goldDiffAt10'), ).toBe(false); expect( analysis.weaknesses.some( (metric) => metric.metricId === 'deathWithShutdownBountyGivenRate', ), ).toBe(false); expect(analysis.strengths).toEqual([ { metricId: 'deathsPerMin', value: 0.1 }, { metricId: 'kda', value: 0.8 }, { metricId: 'killParticipation', value: 0.7 }, ]); expect(analysis.weaknesses).toEqual([ { metricId: 'wardsKilledPerMin', value: 0.1 }, { metricId: 'visionScorePerMin', value: 0.2 }, { metricId: 'controlWardsPlaced', value: 0.2 }, ]); }); });