Files
vyndr/tests/unit/cascadeService.test.js
T
builtbykev 2366660f5e feat: Feature 2.2 — Line Movement + Cascade Detection
Line movement system:
- Baseline capture on first odds fetch of the day
- Movement detection >= 0.5 points with direction (up/down)
- Sharp money heuristic (sharp_action/public_action/unknown)
- GET /api/movements with player, stat_type, min_movement filters
- Movements included in GET /api/odds/nba live responses

Cascade detection system:
- Scratch detection: player props disappear from 2+ books
- Affected user lookup via scan_sessions + picks
- Parlay re-grade without scratched legs
- cascade_alerts created for affected users
- GET /api/alerts (Analyst/Desk only), PATCH /api/alerts/:id/read

Zero extra Odds API credits — all detection piggybacks on existing fetches.
Migration 002: line_baselines, line_movements, cascade_alerts tables.

30 new tests, 188 total (161 Node.js + 27 Python), all passing.
Phase 2 Core Product COMPLETE.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-21 14:21:34 -04:00

86 lines
3.0 KiB
JavaScript

const mockRedis = { get: jest.fn(), set: jest.fn() };
jest.mock('../../src/utils/redis', () => ({ getRedisClient: () => mockRedis }));
const mockSupabaseFrom = jest.fn();
jest.mock('../../src/utils/supabase', () => ({
getSupabaseServiceClient: () => ({ from: mockSupabaseFrom }),
}));
const { detectScratches } = require('../../src/services/cascadeService');
beforeEach(() => {
jest.clearAllMocks();
mockRedis.get.mockResolvedValue(null);
mockRedis.set.mockResolvedValue('OK');
});
function makeProps(players) {
const props = [];
for (const player of players) {
// Add from 2 books to qualify
props.push({ player, stat_type: 'points', book: 'draftkings', line: 26.5 });
props.push({ player, stat_type: 'points', book: 'fanduel', line: 27.0 });
}
return props;
}
describe('cascadeService', () => {
test('first fetch: no previous data, no scratches', async () => {
mockRedis.get.mockResolvedValue(null);
const result = await detectScratches('nba', makeProps(['Jokic', 'LeBron']));
expect(result.scratchedPlayers).toEqual([]);
});
test('player present in previous but absent in current: flagged as scratch', async () => {
// Previous fetch had Jokic + LeBron
mockRedis.get.mockResolvedValue(JSON.stringify(['Jokic', 'LeBron']));
// Mock DB queries for processScratches
mockSupabaseFrom.mockImplementation((table) => {
if (table === 'picks') {
return {
select: () => ({
eq: (col, val) => ({
gte: () => Promise.resolve({ data: [], error: null }),
}),
}),
};
}
return {
select: () => ({ eq: () => ({ gte: () => Promise.resolve({ data: [] }) }) }),
};
});
// Current fetch only has Jokic
const result = await detectScratches('nba', makeProps(['Jokic']));
expect(result.scratchedPlayers).toContain('LeBron');
});
test('player in only 1 book: not flagged (data gap)', async () => {
mockRedis.get.mockResolvedValue(JSON.stringify(['Jokic', 'LeBron']));
// Current: Jokic from 2 books, LeBron from only 1 book
const props = [
{ player: 'Jokic', stat_type: 'points', book: 'draftkings', line: 26.5 },
{ player: 'Jokic', stat_type: 'points', book: 'fanduel', line: 27.0 },
{ player: 'LeBron', stat_type: 'points', book: 'draftkings', line: 25.5 },
// LeBron only from 1 book — doesn't qualify as "having props from 2+ books"
];
mockSupabaseFrom.mockReturnValue({
select: () => ({ eq: () => ({ gte: () => Promise.resolve({ data: [] }) }) }),
});
const result = await detectScratches('nba', props);
// LeBron doesn't qualify for currentPlayers (only 1 book), so flagged as scratch
expect(result.scratchedPlayers).toContain('LeBron');
});
test('no players disappear: empty scratches', async () => {
mockRedis.get.mockResolvedValue(JSON.stringify(['Jokic', 'LeBron']));
const result = await detectScratches('nba', makeProps(['Jokic', 'LeBron']));
expect(result.scratchedPlayers).toEqual([]);
});
});