Sessions 7e+7f: Grade adapter, normalize consolidation, computeFeatures, analyzeViaEngine1, scan/parlay migrated to engine1
This commit is contained in:
@@ -0,0 +1,213 @@
|
||||
// Fix 2 (Session 7f) — verifies the end-to-end shape from
|
||||
// computeFeaturesForProp → engine1 → adapter → concrete reasoning.
|
||||
|
||||
const mockComputeReturn = { current: null };
|
||||
jest.mock('../../src/services/intelligence/computeFeatures', () => ({
|
||||
computeFeaturesForProp: async () => mockComputeReturn.current,
|
||||
}));
|
||||
|
||||
const mockEngine1Return = { current: null };
|
||||
jest.mock('../../src/services/intelligence/engine1', () => ({
|
||||
gradeProp: () => mockEngine1Return.current,
|
||||
}));
|
||||
|
||||
const { analyzeViaEngine1 } = require('../../src/services/intelligence/analyzeViaEngine1');
|
||||
|
||||
beforeEach(() => {
|
||||
mockComputeReturn.current = null;
|
||||
mockEngine1Return.current = null;
|
||||
});
|
||||
|
||||
describe('analyzeViaEngine1 — happy path', () => {
|
||||
test('produces the full legacy shape with concrete numbers', async () => {
|
||||
mockComputeReturn.current = {
|
||||
features: {
|
||||
l5_avg: 28.4, l20_avg: 26.1, home_away: 1.0,
|
||||
opp_rank_stat: 0.82, rest_days: 2,
|
||||
},
|
||||
trap: { composite: 0.1, signals: {}, recommendation: 'proceed' },
|
||||
consistency: { consistency: 'reliable', cv: 0.18, score: 0.7, games: 20 },
|
||||
prop: { line: 25.5, direction: 'over' },
|
||||
meta: {
|
||||
player: 'Jalen Brunson', statType: 'points', line: 25.5,
|
||||
direction: 'over', book: 'draftkings', sport: 'nba',
|
||||
teamAbbr: 'NYK', opponentAbbr: 'BOS', gameId: 'ev-1',
|
||||
isHome: true, gameLogs: [{ points: 28 }], errors: [],
|
||||
},
|
||||
};
|
||||
mockEngine1Return.current = {
|
||||
grade: 'A-', confidence: 0.78,
|
||||
top_factors: ['l5_hot_vs_line', 'weak_opponent_defense', 'home_game'],
|
||||
all_factors: ['l5_hot_vs_line', 'weak_opponent_defense', 'home_game', 'rested_2plus'],
|
||||
};
|
||||
|
||||
const out = await analyzeViaEngine1({
|
||||
player: 'Jalen Brunson', stat_type: 'points', line: 25.5, direction: 'over', book: 'draftkings', sport: 'nba',
|
||||
});
|
||||
|
||||
// Adapter-collapsed grade.
|
||||
expect(out.grade).toBe('A');
|
||||
expect(out.confidence).toBe(78);
|
||||
expect(out.player).toBe('Jalen Brunson');
|
||||
expect(out.stat_type).toBe('points');
|
||||
expect(out.line).toBe(25.5);
|
||||
expect(out.direction).toBe('over');
|
||||
expect(out.book).toBe('draftkings');
|
||||
|
||||
// Reasoning has concrete numbers, not abstract factor labels.
|
||||
expect(out.reasoning.summary).toContain('28.4');
|
||||
expect(out.reasoning.summary).toContain('26.1');
|
||||
expect(out.reasoning.summary).toContain('Jalen Brunson');
|
||||
expect(out.reasoning.summary).toContain('BOS');
|
||||
expect(out.reasoning.summary).toContain('Engine 1 graded A-');
|
||||
|
||||
// Legacy-shaped steps object — named sub-blocks for backward compat
|
||||
// with the analyze integration test, plus a `narrative` array for
|
||||
// the line-by-line breakdown.
|
||||
expect(typeof out.reasoning.steps).toBe('object');
|
||||
expect(out.reasoning.steps).toHaveProperty('season_avg');
|
||||
expect(out.reasoning.steps).toHaveProperty('recent_form');
|
||||
expect(out.reasoning.steps).toHaveProperty('situational');
|
||||
expect(out.reasoning.steps).toHaveProperty('final_grade');
|
||||
expect(Array.isArray(out.reasoning.steps.narrative)).toBe(true);
|
||||
expect(out.reasoning.steps.narrative.length).toBeGreaterThan(0);
|
||||
expect(out.reasoning.steps.narrative[0]).toHaveProperty('step');
|
||||
expect(out.reasoning.steps.narrative[0]).toHaveProperty('detail');
|
||||
// Real numbers in the sub-blocks.
|
||||
expect(out.reasoning.steps.season_avg.value).toBe(26.1);
|
||||
expect(out.reasoning.steps.recent_form.value).toBe(28.4);
|
||||
|
||||
// edge_pct computed from l5_avg vs line.
|
||||
// (28.4 - 25.5) / 25.5 * 100 = ~11.4
|
||||
expect(out.edge_pct).toBeCloseTo(11.4, 1);
|
||||
|
||||
expect(Array.isArray(out.kill_conditions_triggered)).toBe(true);
|
||||
});
|
||||
|
||||
test('away game + strong defense + back-to-back surfaces in reasoning', async () => {
|
||||
mockComputeReturn.current = {
|
||||
features: { l5_avg: 18, l20_avg: 22, home_away: 0.0, opp_rank_stat: 0.15, rest_days: 0 },
|
||||
trap: { composite: 0.6, signals: {} },
|
||||
consistency: { consistency: 'reliable', score: 0.7 },
|
||||
prop: { line: 24.5, direction: 'over' },
|
||||
meta: { player: 'P', statType: 'points', book: 'dk', sport: 'nba',
|
||||
teamAbbr: 'X', opponentAbbr: 'OKC', gameId: 'g', isHome: false,
|
||||
gameLogs: [{ points: 20 }], errors: [] },
|
||||
};
|
||||
mockEngine1Return.current = {
|
||||
grade: 'D', confidence: 0.2,
|
||||
top_factors: ['l5_cold_vs_line', 'top_opponent_defense', 'back_to_back'],
|
||||
all_factors: ['l5_cold_vs_line', 'top_opponent_defense', 'back_to_back'],
|
||||
};
|
||||
|
||||
const out = await analyzeViaEngine1({
|
||||
player: 'P', stat_type: 'points', line: 24.5, direction: 'over', sport: 'nba',
|
||||
});
|
||||
|
||||
expect(out.grade).toBe('D');
|
||||
expect(out.reasoning.summary).toContain('Playing on the road');
|
||||
expect(out.reasoning.summary).toContain('OKC');
|
||||
expect(out.reasoning.summary).toContain('top-tier defense');
|
||||
expect(out.reasoning.summary).toContain('Back-to-back');
|
||||
expect(out.reasoning.summary).toContain('leans against the play');
|
||||
});
|
||||
});
|
||||
|
||||
describe('analyzeViaEngine1 — graceful degradation', () => {
|
||||
test('hard fallback when everything failed (no features, no logs, no consistency)', async () => {
|
||||
mockComputeReturn.current = {
|
||||
features: {},
|
||||
trap: { composite: 0, signals: {} },
|
||||
consistency: { consistency: 'unknown', score: null, games: 0 },
|
||||
prop: { line: 25, direction: 'over' },
|
||||
meta: { player: 'Ghost', statType: 'points', book: 'dk', sport: 'nba',
|
||||
teamAbbr: null, opponentAbbr: null, gameId: null, isHome: null,
|
||||
gameLogs: [], errors: ['player_not_found_in_id_map', 'no_game_scheduled_today'] },
|
||||
};
|
||||
|
||||
const out = await analyzeViaEngine1({
|
||||
player: 'Ghost', stat_type: 'points', line: 25, direction: 'over', sport: 'nba',
|
||||
});
|
||||
|
||||
expect(out.grade).toBe('C');
|
||||
expect(out.confidence).toBe(10);
|
||||
expect(out.reasoning.summary).toMatch(/Unable to compute|provisional/);
|
||||
expect(out.reasoning.summary).toContain("couldn't find");
|
||||
expect(out.kill_conditions_triggered).toEqual([]);
|
||||
});
|
||||
|
||||
test('partial data (player found, no game) still grades via engine1', async () => {
|
||||
mockComputeReturn.current = {
|
||||
features: {},
|
||||
trap: { composite: 0, signals: {} },
|
||||
consistency: { consistency: 'reliable', cv: 0.2, score: 0.7, games: 20 },
|
||||
prop: { line: 25, direction: 'over' },
|
||||
meta: { player: 'P', statType: 'points', book: 'dk', sport: 'nba',
|
||||
teamAbbr: 'NYK', opponentAbbr: null, gameId: null, isHome: null,
|
||||
gameLogs: [{ points: 25 }], errors: ['no_game_scheduled_today'] },
|
||||
};
|
||||
mockEngine1Return.current = {
|
||||
grade: 'C', confidence: 0.4,
|
||||
top_factors: [], all_factors: [],
|
||||
};
|
||||
|
||||
const out = await analyzeViaEngine1({
|
||||
player: 'P', stat_type: 'points', line: 25, direction: 'over', sport: 'nba',
|
||||
});
|
||||
|
||||
// Did NOT fall through to fallbackLegacyResult — engine1 was invoked.
|
||||
expect(out.grade).toBe('C');
|
||||
expect(out.confidence).toBe(40);
|
||||
expect(out.reasoning.summary).toContain('No game scheduled');
|
||||
});
|
||||
});
|
||||
|
||||
describe('analyzeViaEngine1 — interface verifications', () => {
|
||||
test('does not throw when computeFeaturesForProp resolves with errors', async () => {
|
||||
mockComputeReturn.current = {
|
||||
features: { l5_avg: 20 },
|
||||
trap: { composite: 0, signals: {} },
|
||||
consistency: { consistency: 'unknown', score: null, games: 0 },
|
||||
prop: { line: 25, direction: 'over' },
|
||||
meta: { sport: 'nba', errors: ['no_features_computed'] },
|
||||
};
|
||||
mockEngine1Return.current = { grade: 'C', confidence: 0.3, top_factors: [], all_factors: [] };
|
||||
const out = await analyzeViaEngine1({
|
||||
player: 'X', stat_type: 'points', line: 25, direction: 'over', sport: 'nba',
|
||||
});
|
||||
expect(out).toBeDefined();
|
||||
expect(out.grade).toBeDefined();
|
||||
});
|
||||
|
||||
test('every legacy field DemoScan reads is present', async () => {
|
||||
mockComputeReturn.current = {
|
||||
features: { l5_avg: 28 },
|
||||
trap: { composite: 0, signals: {} },
|
||||
consistency: { consistency: 'reliable', score: 0.7 },
|
||||
prop: { line: 25, direction: 'over' },
|
||||
meta: { sport: 'nba', errors: [] },
|
||||
};
|
||||
mockEngine1Return.current = { grade: 'A-', confidence: 0.7, top_factors: ['x'], all_factors: ['x'] };
|
||||
const out = await analyzeViaEngine1({
|
||||
player: 'X', stat_type: 'points', line: 25, direction: 'over', sport: 'nba',
|
||||
});
|
||||
// DemoScan reads: grade, confidence, reasoning.summary,
|
||||
// kill_conditions_triggered[].code, edge_pct, line, player, stat_type.
|
||||
expect(out.grade).toBeDefined();
|
||||
expect(typeof out.confidence).toBe('number');
|
||||
expect(typeof out.reasoning.summary).toBe('string');
|
||||
expect(Array.isArray(out.kill_conditions_triggered)).toBe(true);
|
||||
expect(typeof out.edge_pct).toBe('number');
|
||||
expect(out.player).toBeDefined();
|
||||
expect(out.stat_type).toBeDefined();
|
||||
expect(out.line).toBeDefined();
|
||||
});
|
||||
|
||||
test('does not import from legacy path (no propAnalyzer/grader/UnifiedOddsProvider)', () => {
|
||||
const fs = require('fs');
|
||||
const src = fs.readFileSync(require.resolve('../../src/services/intelligence/analyzeViaEngine1.js'), 'utf8');
|
||||
expect(src).not.toMatch(/propAnalyzer/);
|
||||
expect(src).not.toMatch(/require.*['"]\.\.\/grader/);
|
||||
expect(src).not.toMatch(/UnifiedOddsProvider/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,185 @@
|
||||
// Fix 1 (Session 7f) — computeFeaturesForProp must never throw, and
|
||||
// must return the engine1 input shape (features/trap/consistency/prop)
|
||||
// even when every upstream is missing.
|
||||
|
||||
const mockSupabaseState = {
|
||||
rosterRow: null,
|
||||
error: null,
|
||||
};
|
||||
jest.mock('../../src/utils/supabase', () => ({
|
||||
getSupabaseServiceClient: () => ({
|
||||
from() {
|
||||
const proxy = {
|
||||
select() { return proxy; },
|
||||
eq() { return proxy; },
|
||||
limit() { return proxy; },
|
||||
maybeSingle: () => Promise.resolve({ data: mockSupabaseState.rosterRow, error: mockSupabaseState.error }),
|
||||
};
|
||||
return proxy;
|
||||
},
|
||||
}),
|
||||
}));
|
||||
|
||||
const mockAxiosGet = jest.fn();
|
||||
jest.mock('axios', () => ({ get: (...args) => mockAxiosGet(...args) }));
|
||||
|
||||
const mockFeatures = { current: {}, throws: false };
|
||||
jest.mock('../../src/services/intelligence/featureCache', () => ({
|
||||
getFeatures: async () => {
|
||||
if (mockFeatures.throws) throw new Error('feature-fetch boom');
|
||||
return { features: mockFeatures.current, meta: {} };
|
||||
},
|
||||
}));
|
||||
|
||||
const mockTrap = { current: null, throws: false };
|
||||
jest.mock('../../src/services/intelligence/trapDetection', () => ({
|
||||
getTrapScore: async () => {
|
||||
if (mockTrap.throws) throw new Error('trap boom');
|
||||
return mockTrap.current;
|
||||
},
|
||||
normalizeName: (n) => n,
|
||||
}));
|
||||
|
||||
const mockLogs = { current: null };
|
||||
const mockConsistency = { current: null };
|
||||
jest.mock('../../src/services/intelligence/gameLogService', () => ({
|
||||
getGameLogs: async () => mockLogs.current,
|
||||
getCareerPlayoffGames: async () => null,
|
||||
getWithWithoutStats: async () => null,
|
||||
}));
|
||||
jest.mock('../../src/services/intelligence/consistencyScore', () => ({
|
||||
getConsistency: async () => mockConsistency.current || { consistency: 'reliable', cv: 0.2, score: 0.7, games: 20 },
|
||||
}));
|
||||
|
||||
const { computeFeaturesForProp } = require('../../src/services/intelligence/computeFeatures');
|
||||
|
||||
beforeEach(() => {
|
||||
mockSupabaseState.rosterRow = null;
|
||||
mockSupabaseState.error = null;
|
||||
mockAxiosGet.mockReset();
|
||||
mockFeatures.current = {};
|
||||
mockFeatures.throws = false;
|
||||
mockTrap.current = null;
|
||||
mockTrap.throws = false;
|
||||
mockLogs.current = null;
|
||||
mockConsistency.current = null;
|
||||
});
|
||||
|
||||
function nbaScoreboard(events) {
|
||||
return { status: 200, data: { events } };
|
||||
}
|
||||
function game(id, homeAbbr, awayAbbr) {
|
||||
return {
|
||||
id,
|
||||
competitions: [{
|
||||
competitors: [
|
||||
{ homeAway: 'home', team: { abbreviation: homeAbbr } },
|
||||
{ homeAway: 'away', team: { abbreviation: awayAbbr } },
|
||||
],
|
||||
}],
|
||||
};
|
||||
}
|
||||
|
||||
describe('computeFeaturesForProp — happy path', () => {
|
||||
test('resolves player + game + features + trap + consistency', async () => {
|
||||
mockSupabaseState.rosterRow = {
|
||||
display_name: 'Jalen Brunson', normalized_name: 'jalen brunson',
|
||||
espn_id: '3934672', team_abbr: 'NYK', sport: 'nba',
|
||||
};
|
||||
mockAxiosGet.mockResolvedValue(nbaScoreboard([game('ev-1', 'NYK', 'BOS')]));
|
||||
mockFeatures.current = { l5_avg: 28.4, l20_avg: 26.1, home_away: 1.0, opp_rank_stat: 0.82 };
|
||||
mockTrap.current = { composite: 0.12, signals: {}, active_count: 1, recommendation: 'proceed' };
|
||||
mockLogs.current = [{ points: 28 }, { points: 26 }];
|
||||
|
||||
const out = await computeFeaturesForProp({
|
||||
player: 'Jalen Brunson', stat_type: 'points', line: 25.5, direction: 'over', sport: 'nba',
|
||||
});
|
||||
|
||||
expect(out.features.l5_avg).toBe(28.4);
|
||||
expect(out.features.home_away).toBe(1.0);
|
||||
expect(out.trap.composite).toBe(0.12);
|
||||
expect(out.consistency.consistency).toBe('reliable');
|
||||
expect(out.prop).toEqual({ line: 25.5, direction: 'over' });
|
||||
expect(out.meta.teamAbbr).toBe('NYK');
|
||||
expect(out.meta.opponentAbbr).toBe('BOS');
|
||||
expect(out.meta.gameId).toBe('ev-1');
|
||||
expect(out.meta.isHome).toBe(true);
|
||||
expect(out.meta.errors).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('computeFeaturesForProp — graceful degradation', () => {
|
||||
test('player not in player_id_map → errors logged, partial result returned', async () => {
|
||||
mockSupabaseState.rosterRow = null;
|
||||
const out = await computeFeaturesForProp({
|
||||
player: 'Unknown Person', stat_type: 'points', line: 20, direction: 'over', sport: 'nba',
|
||||
});
|
||||
expect(out.meta.errors).toContain('player_not_found_in_id_map');
|
||||
expect(out.meta.errors).toContain('no_game_scheduled_today');
|
||||
expect(out.meta.teamAbbr).toBeNull();
|
||||
expect(out.meta.gameId).toBeNull();
|
||||
expect(out.features).toEqual({});
|
||||
expect(out.prop.line).toBe(20);
|
||||
});
|
||||
|
||||
test('player found but no game today → still returns features attempt', async () => {
|
||||
mockSupabaseState.rosterRow = { team_abbr: 'NYK', espn_id: '1', sport: 'nba' };
|
||||
mockAxiosGet.mockResolvedValue(nbaScoreboard([])); // empty slate
|
||||
const out = await computeFeaturesForProp({
|
||||
player: 'Some Player', stat_type: 'points', line: 22, direction: 'over', sport: 'nba',
|
||||
});
|
||||
expect(out.meta.errors).toContain('no_game_scheduled_today');
|
||||
expect(out.meta.teamAbbr).toBe('NYK');
|
||||
expect(out.meta.gameId).toBeNull();
|
||||
});
|
||||
|
||||
test('feature fetch throws → empty features + error noted, no crash', async () => {
|
||||
mockSupabaseState.rosterRow = { team_abbr: 'NYK', espn_id: '1', sport: 'nba' };
|
||||
mockAxiosGet.mockResolvedValue(nbaScoreboard([game('e2', 'NYK', 'BOS')]));
|
||||
mockFeatures.throws = true;
|
||||
const out = await computeFeaturesForProp({
|
||||
player: 'Brunson', stat_type: 'points', line: 25, direction: 'over', sport: 'nba',
|
||||
});
|
||||
expect(out.meta.errors).toContain('no_features_computed');
|
||||
expect(out.features).toEqual({});
|
||||
expect(out.trap).toBeDefined();
|
||||
});
|
||||
|
||||
test('trap detection throws → defaults to no signals', async () => {
|
||||
mockSupabaseState.rosterRow = { team_abbr: 'NYK', espn_id: '1', sport: 'nba' };
|
||||
mockAxiosGet.mockResolvedValue(nbaScoreboard([game('e3', 'NYK', 'BOS')]));
|
||||
mockFeatures.current = { l5_avg: 25 };
|
||||
mockTrap.throws = true;
|
||||
const out = await computeFeaturesForProp({
|
||||
player: 'Brunson', stat_type: 'points', line: 25, direction: 'over', sport: 'nba',
|
||||
});
|
||||
expect(out.trap).toMatchObject({ composite: 0, recommendation: 'proceed' });
|
||||
});
|
||||
|
||||
test('missing required fields surfaces in errors but still returns shape', async () => {
|
||||
const out = await computeFeaturesForProp({});
|
||||
expect(out.meta.errors[0]).toMatch(/missing required fields/);
|
||||
expect(out.features).toBeDefined();
|
||||
expect(out.trap).toBeDefined();
|
||||
expect(out.consistency).toBeDefined();
|
||||
expect(out.prop).toBeDefined();
|
||||
});
|
||||
|
||||
test('scoreboard fetch throws → no game noted, no crash', async () => {
|
||||
mockSupabaseState.rosterRow = { team_abbr: 'NYK', espn_id: '1', sport: 'nba' };
|
||||
mockAxiosGet.mockRejectedValue(new Error('espn down'));
|
||||
const out = await computeFeaturesForProp({
|
||||
player: 'B', stat_type: 'points', line: 25, direction: 'over', sport: 'nba',
|
||||
});
|
||||
expect(out.meta.gameId).toBeNull();
|
||||
expect(out.meta.errors).toContain('no_game_scheduled_today');
|
||||
});
|
||||
|
||||
test('does not import from legacy path (no propAnalyzer/grader/UnifiedOddsProvider)', () => {
|
||||
const fs = require('fs');
|
||||
const src = fs.readFileSync(require.resolve('../../src/services/intelligence/computeFeatures.js'), 'utf8');
|
||||
expect(src).not.toMatch(/propAnalyzer/);
|
||||
expect(src).not.toMatch(/require.*['"]\.\.\/grader/);
|
||||
expect(src).not.toMatch(/UnifiedOddsProvider/);
|
||||
});
|
||||
});
|
||||
@@ -1,13 +1,15 @@
|
||||
// PERF-2 (Session 7d): proves analyzeProp runs in parallel inside
|
||||
// scanParlay. We mock analyzeProp to sleep — a sequential loop would
|
||||
// take N × delay, parallel allSettled takes ~delay.
|
||||
// scanParlay. ARCH-1 Step 4 (Session 7f): the call target rotated from
|
||||
// `propAnalyzer.analyzeProp` to `intelligence/analyzeViaEngine1`. Mock
|
||||
// target updated to follow; the assertion (parallel timing) is
|
||||
// unchanged.
|
||||
|
||||
let mockAnalyzeDelayMs = 100;
|
||||
let mockAnalyzeCallTimes = [];
|
||||
let mockAnalyzeRejectIndices = new Set();
|
||||
|
||||
jest.mock('../../src/services/propAnalyzer', () => ({
|
||||
analyzeProp: async (leg) => {
|
||||
jest.mock('../../src/services/intelligence/analyzeViaEngine1', () => ({
|
||||
analyzeViaEngine1: async (leg) => {
|
||||
const startedAt = Date.now();
|
||||
mockAnalyzeCallTimes.push(startedAt);
|
||||
await new Promise((r) => setTimeout(r, mockAnalyzeDelayMs));
|
||||
@@ -16,8 +18,11 @@ jest.mock('../../src/services/propAnalyzer', () => ({
|
||||
}
|
||||
return {
|
||||
...leg,
|
||||
// Mock keeps the legacy-shape 4-letter grade so the existing
|
||||
// value-level assertion ('A-') is preserved verbatim. Real
|
||||
// analyzeViaEngine1 also emits the 4-letter shape via the adapter.
|
||||
grade: 'A-',
|
||||
confidence: 0.78,
|
||||
confidence: 78,
|
||||
edge_pct: 5.2,
|
||||
reasoning: { summary: 'ok', steps: {} },
|
||||
kill_conditions_triggered: [],
|
||||
|
||||
Reference in New Issue
Block a user