import test from 'node:test'; import assert from 'node:assert/strict'; import { parseOdds, deVigTwoWay, solveImpliedLambda, calcSlipMetrics } from '../src/engine/math.js'; import { parseSlatePayload, attachEspnMetadata } from '../src/engine/schema.js'; import { PRESETS, evaluateLeg, evaluateSlate } from '../src/engine/riskEngine.js'; import { optimizeSlips } from '../src/engine/optimizer.js'; import { computeSlateWindow } from '../src/scraper.js'; function makeCleanMatch(overrides = {}) { return { id: 'clean_match', kickoffEAT: '20:00 EAT', league: 'UEFA Champions League', tier: 1, isReserveOrYouth: false, isPostIntlBreak: false, hasVerifiedTeamNews: true, home: { name: 'Arsenal', gp: 12, gf: 22, ga: 10, xg: 21.5, xga: 11.0, managerGamesInCharge: 100, newManagerTightenedDefense: false, confirmedOut: { gk: 0, startingCB: 0, otherDef: 0, mid: 0, topScorer: 0, otherAtt: 0, names: [] }, doubts5050: { gk: 0, startingCB: 0, otherDef: 0, topScorer: 0, otherAtt: 0, names: [] }, keyStartersToConfirm: ['Bukayo Saka'], }, away: { name: 'Benfica', gp: 12, gf: 20, ga: 14, xg: 19.6, xga: 14.5, managerGamesInCharge: 25, newManagerTightenedDefense: false, confirmedOut: { gk: 0, startingCB: 0, otherDef: 0, mid: 0, topScorer: 0, otherAtt: 0, names: [] }, doubts5050: { gk: 0, startingCB: 0, otherDef: 0, topScorer: 0, otherAtt: 0, names: [] }, keyStartersToConfirm: ['Vangelis Pavlidis'], }, moneyline: { home: 1.65, draw: 4.0, away: 5.0 }, totals: { '1.5': { over: 1.24, under: 4.10, sportybetOver: 1.23 }, '2.5': { over: 1.72, under: 2.15, sportybetOver: 1.70 }, '3.5': { over: 2.80, under: 1.44, sportybetUnder: 1.42 }, }, weather: { tempC: 11, windKmh: 9, precipType: 'none' }, notes: 'Balanced UCL clash with full squads.', ...overrides, }; } test('odds parser and Power de-vigging', () => { assert.equal(parseOdds('1.85'), 1.85); assert.equal(parseOdds('4/5'), 1.8); assert.equal(parseOdds('-200'), 1.5); assert.equal(parseOdds('+150'), 2.5); const dv = deVigTwoWay(1.90, 1.90); assert.ok(Math.abs(dv.fairOver - 0.5) < 0.001); assert.ok(Math.abs(dv.vigPct - 5.26) < 0.2); const lambda = solveImpliedLambda(2.5, 0.55); assert.ok(lambda > 2.6 && lambda < 3.1); }); test('computeSlateWindow enforces 13:00 EAT -> 01:30 EAT finish deadline (latest kickoff 23:30 EAT)', () => { // 2026-11-24 13:00 EAT == 2026-11-24 10:00:00Z const now = new Date('2026-11-24T10:00:00Z'); const win = computeSlateWindow(now, { minLeadMinutes: 75, finishDeadlineEAT: '01:30' }); // Earliest kickoff = 14:15 EAT == 11:15Z assert.equal(win.earliestKickoff.toISOString(), '2026-11-24T11:15:00.000Z'); // Finish deadline = 2026-11-25 01:30 EAT == 2026-11-24 22:30:00Z assert.equal(win.finishDeadlineIso, '2026-11-24T22:30:00.000Z'); // Latest kickoff (2h before finish) = 23:30 EAT == 20:30:00Z assert.equal(win.latestKickoff.toISOString(), '2026-11-24T20:30:00.000Z'); }); test('symmetric Over and Under risk vetoes catch historical traps', () => { const slate = parseSlatePayload({ slateDate: '2026-11-24', matches: [ // Under blowout trap (heavy favorite + short O2.5) makeCleanMatch({ id: 'blowout_trap', moneyline: { home: 1.28, draw: 5.8, away: 11.0 }, totals: { '2.5': { over: 1.42, under: 2.85 }, '3.5': { over: 2.10, under: 1.72 }, }, }), // Under defensive luck + CB injury trap makeCleanMatch({ id: 'def_luck_trap', home: { ...makeCleanMatch().home, ga: 10, xga: 17.0, confirmedOut: { gk: 0, startingCB: 1, otherDef: 0, mid: 0, topScorer: 0, otherAtt: 0, names: ['CB1'] }, }, totals: { '3.5': { over: 3.10, under: 1.38 } }, }), // Over finishing bubble trap (+6.5 GF vs xG) makeCleanMatch({ id: 'att_bubble_trap', home: { ...makeCleanMatch().home, gf: 28, xg: 21.0 }, totals: { '2.5': { over: 1.45, under: 2.75 } }, }), // Unverified team news veto makeCleanMatch({ id: 'no_news', hasVerifiedTeamNews: false, }), ], }); const blowout = evaluateLeg(slate.matches[0], '3.5', 'under', PRESETS.sniper); assert.equal(blowout.status, 'VETOED'); assert.ok(blowout.hardVetoes.some((v) => v.code === 'HEAVY_FAVORITE_BLOWOUT')); const defLuck = evaluateLeg(slate.matches[1], '3.5', 'under', PRESETS.sniper); assert.equal(defLuck.status, 'VETOED'); assert.ok(defLuck.hardVetoes.some((v) => v.code === 'DEF_XGA_LUCK_PLUS_INJURY')); const attBubble = evaluateLeg(slate.matches[2], '2.5', 'over', PRESETS.sniper); assert.equal(attBubble.status, 'VETOED'); assert.ok(attBubble.hardVetoes.some((v) => v.code === 'ATT_XG_BUBBLE')); const noNews = evaluateLeg(slate.matches[3], '1.5', 'over', PRESETS.sniper); assert.equal(noNews.status, 'VETOED'); assert.ok(noNews.hardVetoes.some((v) => v.code === 'NO_VERIFIED_TEAM_NEWS')); }); test('optimizer selects single best CLEAN Sniper pick only and returns NO_BET when slate is dirty', () => { const cleanSlate = parseSlatePayload({ slateDate: '2026-11-24', matches: [ makeCleanMatch({ id: 'm1', totals: { '2.5': { over: 1.56, under: 2.45, sportybetOver: 1.55 } }, }), makeCleanMatch({ id: 'm2', home: { ...makeCleanMatch().home, name: 'Inter Milan' }, away: { ...makeCleanMatch().away, name: 'Monaco' }, totals: { '2.5': { over: 1.72, under: 2.15, sportybetOver: 1.70 } }, }), ], }); const evaluated = evaluateSlate(cleanSlate, PRESETS.sniper); const opt = optimizeSlips(evaluated, PRESETS.sniper); assert.equal(opt.recommendation, 'BET'); assert.equal(opt.primarySlip.legs.length, 1); assert.equal(opt.alternativeSlip, null); assert.equal(opt.primarySlip.metrics.consensusOdds, 1.56); assert.equal(opt.primaryChecklist.length, 1); // Dirty slate -> disciplined NO_BET const dirtySlate = parseSlatePayload({ slateDate: '2026-11-24', matches: [makeCleanMatch({ id: 'dirty1', hasVerifiedTeamNews: false })], }); const dirtyOpt = optimizeSlips(evaluateSlate(dirtySlate, PRESETS.sniper), PRESETS.sniper); assert.equal(dirtyOpt.recommendation, 'NO_BET'); }); test('attachEspnMetadata links scouted matches to ESPN IDs for auto settlement', () => { const slate = parseSlatePayload({ slateDate: '2026-11-24', matches: [makeCleanMatch({ id: 'arsenal_benfica' })], }); attachEspnMetadata(slate, [ { eventId: '777888', league: 'uefa.champions', leagueName: 'UEFA Champions League', homeTeam: 'Arsenal', awayTeam: 'SL Benfica', kickoffIso: '2026-11-24T20:00:00Z', kickoffEAT: 'Nov 24, 23:00 EAT', tier: 1, }, ]); assert.equal(slate.matches[0].espnEventId, '777888'); assert.equal(slate.matches[0].espnLeague, 'uefa.champions'); assert.equal(calcSlipMetrics([evaluateLeg(slate.matches[0], '1.5', 'over', PRESETS.sniper)]).legCount, 1); }); test('cross-competition gf vs xg mismatch (e.g. domestic GF + 4-game UCL xG) does NOT trigger false ATT_XG_BUBBLE veto', () => { const slate = parseSlatePayload({ slateDate: '2026-11-24', matches: [ makeCleanMatch({ id: 'mancity_napoli', home: { ...makeCleanMatch().home, name: 'Manchester City', gp: 11, gf: 22, ga: 14, xg: 9.9, xga: 4.1 }, away: { ...makeCleanMatch().away, name: 'Napoli', gp: 12, gf: 19, ga: 14, xg: null, xga: null }, totals: { '1.5': { over: 1.22, under: 4.20 } }, }), ], }); const leg = evaluateLeg(slate.matches[0], '1.5', 'over', PRESETS.sniper); assert.equal(leg.xgSampleMismatch, true); assert.equal(leg.status, 'CLEAN'); assert.equal(leg.hardVetoes.some((v) => v.code === 'ATT_XG_BUBBLE'), false); }); test('optimizer separates primaryChecklist and alternativeChecklist per slip on multi-leg preset', () => { const mk = (id, h, a) => makeCleanMatch({ id, home: { ...makeCleanMatch().home, name: h }, away: { ...makeCleanMatch().away, name: a }, totals: { '1.5': { over: 1.48, under: 2.75, sportybetOver: 1.46 } }, }); const slate = parseSlatePayload({ slateDate: '2026-11-24', matches: [ mk('m1', 'Arsenal', 'Benfica'), mk('m2', 'Inter', 'Monaco'), mk('m3', 'Bayern', 'Celtic'), mk('m4', 'Real Madrid', 'PSV'), ], }); const opt = optimizeSlips(evaluateSlate(slate, PRESETS.balanced), PRESETS.balanced); assert.equal(opt.recommendation, 'BET'); assert.ok(opt.alternativeSlip); assert.equal(opt.primaryChecklist.length, 2); assert.equal(opt.alternativeChecklist.length, 2); const pIds = new Set(opt.primaryChecklist.map((c) => c.matchId)); assert.ok(opt.alternativeChecklist.every((c) => !pIds.has(c.matchId))); }); test('parseSlatePayload recovers unwrapped comma-separated match objects missing opening {"matches":[', () => { const m1 = JSON.stringify(makeCleanMatch({ id: 'm1' })); const m2 = JSON.stringify(makeCleanMatch({ id: 'm2' })); const partialPaste = `${m1},\n${m2}\n]\n}`; const parsed = parseSlatePayload(partialPaste); assert.equal(parsed.matches.length, 2); assert.equal(parsed.matches[0].id, 'm1'); assert.equal(parsed.matches[1].id, 'm2'); // Single bare match object also recovers const single = parseSlatePayload(m1); assert.equal(single.matches.length, 1); assert.equal(single.matches[0].id, 'm1'); });