Files
vyndr/tests/unit/espnStatsAdapter.test.js
builtbykev 47ada9013c Wave 2A: real player headshots — sport-agnostic id threaded from ingestion
Threads a REAL athlete id from the snapshot's per-player stats resolve (zero
new I/O) → enriched grade → grades:{sport} → slate strip → PlayerAvatar. Real
photo where an id resolves; team-colored monogram (never a gray silhouette,
never a broken image) where it can't. Ids are never fabricated.

Ingestion (Addition 1):
- espnStatsAdapter.getSeasonAverages now RETURNS the resolved ESPN athlete id
  (was discarded) as espnId; non-numeric uid degrades to null.
- playerIntelService surfaces MLBAM playerId (MLB) / ESPN espnId (NBA/WNBA).
- snapshotService captures both per player and stores them on the enriched
  grade beside archetype/team (null when unresolved → monogram path).

Thread → component:
- slateAdapter.buildPlayerStripsFromProps carries playerId/espnId onto each
  strip; StatStrip → PlayerAvatar (accepts both ids; getHeadshotUrl routes by
  sport: MLB→mlbstatic, NBA/WNBA→a.espncdn).
- Silhouette surfaces rewired to PlayerAvatar (branded monogram on null):
  scan search dropdown (guarded MLBAM p.id) + tonight chips, SearchModal,
  HotListPanel, GradeResultCard header. Scan grade card feeds the picked
  MLBAM id through gradeAdapter.
- playerHeadshot pure URL logic extracted to CommonJS playerHeadshotUrl.js
  (unit-testable; the .ts re-exports it). nfl/nhl added to ESPN_SPORT_PATH.

Tests: tests/unit/headshotThread.test.js (per-league URL + id thread + monogram
null path) + extended snapshotService/espnStatsAdapter suites. Full suite
241 suites / 2915 green; next build exit 0.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-13 13:18:59 -04:00

90 lines
3.8 KiB
JavaScript

// 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);
});
});