Files
vyndr/tests/unit/playerIntelWiring.test.js
T
builtbykev 7969a4971a Session 44: Make it visible — VYNDR archetype names, grade intel, schedule fix, landing page (2061 tests)
Frontend + wiring only. Wires existing backend into the pages users see.

- VYNDR Original archetype rename (41) across archetypeService.js + lib/
  archetypes.js + ArchetypeBadge, each keeping legacyName (resolves stale data).
  Judge -> BOMBER. Old POWER PULL slot -> WHIFF strikeout-artist pitcher.
- BACKEND_HANDOFF.md: canonical frontend<->backend data contract.
- Grade card intel: scan/page.tsx now forwards the engine's intel fields
  (season_avg/form/usage/matchup_grade/archetype/...) into mapScanToGradeResult
  -> STAT CONTEXT + VYNDR INTELLIGENCE sections populate. The chain already
  preserved them (tierGating + /api/scan spread); the page was dropping them.
- Schedule freshness: slateAdapter.isRelevantGame drops completed games >24h
  old; Slate.filteredGames applies it. (TTL already 60s.)
- Landing: Features.tsx rewritten to user-facing copy (no Point-biserial/Zone
  14/ABS/Phi-coefficient).
- Depth chart Next proxies added (/api/stats/lineup|depth|cascade) - were 404.
- GameCard swap DEFERRED (Kev): legacy on-demand card stays as a bridge until
  the snapshot pipeline populates the grades cache; vyndr/GameCard swaps in then.

Backend 2045 -> 2061 tests (+16), 167 suites. Web build clean (exit 0).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-18 20:15:38 -04:00

90 lines
4.0 KiB
JavaScript

// Session 43 — real-stats wiring into playerIntelService. Adapters are injected
// so these run pure (no statsapi.mlb.com, no Python NBA service).
const svc = require('../../src/services/playerIntelService');
// A fake mlbStatsAdapter.getPlayerStats returning a power-pull hitter line.
const judgeAdapter = {
async getPlayerStats() {
return {
found: true, id: 592450, name: 'Aaron Judge', team: 'New York Yankees', position: 'RF', group: 'hitting',
season: { avg: '.288', homeRuns: 34, rbi: 87, ops: '1.012', gamesPlayed: 92, stolenBases: 9, runs: 80, doubles: 18, strikeOuts: 120, plateAppearances: 400, atBats: 330 },
last10: [
{ date: '2026-06-15', opponent: 'Boston Red Sox', stat: { hits: 2, atBats: 4, homeRuns: 1 } },
{ date: '2026-06-16', opponent: 'Tampa Bay Rays', stat: { hits: 1, atBats: 3, homeRuns: 0 } },
],
};
},
};
const aceAdapter = {
async getPlayerStats() {
return {
found: true, id: 1, name: 'Tarik Skubal', team: 'Detroit Tigers', position: 'P', group: 'pitching',
season: { era: '2.41', strikeOuts: 130, inningsPitched: '110.0', whip: '0.92', gamesStarted: 17, strikeoutsPer9Inn: '10.6', saves: 0 },
last10: [{ date: '2026-06-14', opponent: 'Chicago White Sox', stat: { inningsPitched: '7.0', strikeOuts: 9 } }],
};
},
};
describe('resolvePlayerStats (MLB, injected adapter)', () => {
it('normalizes a hitter into classifier input + display rows + last10', async () => {
const r = await svc.resolvePlayerStats('Aaron Judge', 'mlb', { mlbAdapter: judgeAdapter });
expect(r.found).toBe(true);
expect(r.team).toBe('New York Yankees');
expect(r.classifierInput.hr).toBe(34);
expect(r.classifierInput.k_rate).toBeGreaterThan(0);
expect(r.season.find((s) => s.k === 'HR').v).toBe('34');
expect(r.season.find((s) => s.k === 'AVG').v).toBe('.288');
expect(r.last10.length).toBe(2);
expect(r.last10[0].stat).toContain('HR'); // most-recent-first summary
});
it('normalizes a pitcher (group=pitching) into ERA/K9/role', async () => {
const r = await svc.resolvePlayerStats('Tarik Skubal', 'mlb', { mlbAdapter: aceAdapter });
expect(r.classifierInput.era).toBeCloseTo(2.41, 2);
expect(r.classifierInput.k9).toBeCloseTo(10.6, 1);
expect(r.classifierInput.role).toBe('SP');
expect(r.season.find((s) => s.k === 'ERA').v).toBe('2.41');
});
it('returns found:false when the adapter has no data', async () => {
const r = await svc.resolvePlayerStats('Nobody', 'mlb', { mlbAdapter: { async getPlayerStats() { return { found: false }; } } });
expect(r.found).toBe(false);
});
});
describe('getPlayerIntel with real stats (Session 43)', () => {
it('returns found:true + real season + an archetype classified from real stats', async () => {
const r = await svc.getPlayerIntel('Aaron Judge', 'mlb', {
cacheGet: async () => null,
resolveStats: (name, sport) => svc.resolvePlayerStats(name, sport, { mlbAdapter: judgeAdapter }),
});
expect(r.found).toBe(true);
expect(r.team).toBe('New York Yankees');
expect(r.season.length).toBeGreaterThan(0);
// 34 HR + high K-rate → POWER PULL, not the empty-stats fallback.
expect(['BOMBER', 'BOMBER', 'DRIVER']).toContain(r.archetype.primary.name);
expect(r.archetype.primary.name).not.toBe('FLEX');
});
it('classifies an ace pitcher from real stats', async () => {
const r = await svc.getPlayerIntel('Tarik Skubal', 'mlb', {
cacheGet: async () => null,
resolveStats: (name, sport) => svc.resolvePlayerStats(name, sport, { mlbAdapter: aceAdapter }),
});
expect(r.archetype.primary.name).toBe('ALPHA');
expect(r.found).toBe(true);
});
it('still degrades gracefully when no stats and no props (found:false)', async () => {
const r = await svc.getPlayerIntel('Ghost Player', 'mlb', {
cacheGet: async () => null,
resolveStats: async () => ({ found: false }),
});
expect(r.found).toBe(false);
expect(r.archetype.primary).toBeTruthy(); // fallback archetype still present
expect(r.season).toEqual([]);
});
});