Sessions 7e+7f: Grade adapter, normalize consolidation, computeFeatures, analyzeViaEngine1, scan/parlay migrated to engine1
This commit is contained in:
@@ -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/);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user