// Session 45 — ESPN NBA/WNBA stats fallback. parseAthleteStats is pure; the // fetch path is exercised with an injected http client. const espn = require('../../src/services/adapters/espnStatsAdapter'); const svc = require('../../src/services/playerIntelService'); describe('parseAthleteStats (defensive)', () => { it('pulls per-game averages from the ESPN categories shape', () => { const payload = { statistics: { splits: { categories: [ { stats: [ { name: 'avgPoints', value: 28.1 }, { name: 'avgRebounds', value: 8.2 }, { name: 'avgAssists', value: 6.4 }, ] }, ] } }, }; const ci = espn.parseAthleteStats(payload); expect(ci.ppg).toBe(28.1); expect(ci.rpg).toBe(8.2); expect(ci.apg).toBe(6.4); }); it('returns null for an unrecognized / empty shape (graceful)', () => { expect(espn.parseAthleteStats(null)).toBeNull(); expect(espn.parseAthleteStats({ nonsense: true })).toBeNull(); expect(espn.parseAthleteStats({ statistics: { splits: { categories: [{ stats: [{ name: 'foo', value: 1 }] }] } } })).toBeNull(); }); }); describe('getSeasonAverages (injected http)', () => { it('resolves an athlete and parses stats', async () => { const http = { get: async (url) => { if (url.includes('/search')) return { data: { items: [{ id: 123, displayName: 'Luka Doncic', team: { abbreviation: 'DAL' }, position: { abbreviation: 'G' } }] } }; return { data: { statistics: { splits: { categories: [{ stats: [{ name: 'avgPoints', value: 33 }, { name: 'avgAssists', value: 9 }] }] } } } }; }, }; const r = await espn.getSeasonAverages('Luka Doncic', 'nba', { http }); expect(r.found).toBe(true); expect(r.team).toBe('DAL'); expect(r.classifierInput.ppg).toBe(33); // Wave 2A — the REAL ESPN athlete id is surfaced (headshot CDN), not discarded. expect(r.espnId).toBe('123'); }); it('Wave 2A — a non-numeric uid degrades espnId to null (never fabricated)', async () => { const http = { get: async (url) => { if (url.includes('/search')) return { data: { items: [{ uid: 's:40~l:46~a:999', displayName: 'X', team: {}, position: {} }] } }; return { data: { statistics: { splits: { categories: [{ stats: [{ name: 'avgPoints', value: 10 }] }] } } } }; }, }; const r = await espn.getSeasonAverages('X', 'nba', { http }); expect(r.found).toBe(true); expect(r.espnId).toBeNull(); }); it('degrades to found:false when ESPN errors', async () => { const http = { get: async () => { throw new Error('espn down'); } }; expect((await espn.getSeasonAverages('X', 'nba', { http })).found).toBe(false); }); it('returns found:false for non-basketball sports', async () => { expect((await espn.getSeasonAverages('X', 'mlb')).found).toBe(false); }); }); describe('resolvePlayerStats wires the ESPN fallback for NBA', () => { it('falls back to ESPN when nbaStatsClient is offline → classifies', async () => { const r = await svc.resolvePlayerStats('Luka Doncic', 'nba', { nbaClient: { getSeasonAvg: async () => { throw new Error('python offline'); } }, espnStats: { getSeasonAverages: async () => ({ found: true, team: 'DAL', position: 'G', classifierInput: { ppg: 33, apg: 9, rpg: 8 }, espnId: '3945274' }) }, }); expect(r.found).toBe(true); expect(r.team).toBe('DAL'); expect(r.classifierInput.ppg).toBe(33); // Wave 2A — espnId surfaces through resolvePlayerStats → the snapshot grade. expect(r.espnId).toBe('3945274'); }); it('found:false when both sources are empty', async () => { const r = await svc.resolvePlayerStats('Nobody', 'nba', { nbaClient: { getSeasonAvg: async () => null }, espnStats: { getSeasonAverages: async () => ({ found: false }) }, }); expect(r.found).toBe(false); }); });