'use strict'; /** * ONE SEAM, TWO GATES, AND A DARK DEFAULT. */ const sp = require('../../src/services/model/servedProbability'); const pc = require('../../src/services/model/probabilityContract'); const registry = require('../../src/services/model/artifactRegistry'); const gating = require('../../src/utils/snapshotGating'); const ERA = 'engine1@2026-08-07-fullwindow'; const A = registry.load('mlb', 'hits'); const row = (over = {}) => ({ sport: 'mlb', stat_type: 'hits', model_version: ERA, p_win: 0.65, confidence: 65, ev_pct: 12.3, value: true, takeable: true, model_odds: -186, book_odds: -110, grade: 'C+', side: 'over', player: 'P', line: 0.5, ...over }); const LIVE_ON = { live: 'ON' }; describe('live is OFF by default and that is load-bearing', () => { it('the default state is OFF with the flag named as the reason', () => { const st = pc.liveState({}); expect(st.live).toBe('OFF'); expect(st.flag_set).toBe(false); expect(st.blocked_reason).toBe('runtime flag not set'); }); it('rows pass through BYTE-IDENTICAL with live off', () => { const rows = [row(), row({ p_win: 0.91 }), row({ stat_type: 'total_bases' })]; const before = JSON.stringify(rows); expect(JSON.stringify(sp.applyToRows(rows))).toBe(before); expect(JSON.stringify(gating.stripModelPrice(rows, 'desk'))).toBe(before); }); it('only the exact string 1 sets the flag', () => { for (const v of ['true', 'yes', 'on', '0', ' 1 ', '', '01']) { expect(pc.liveState({ PROBABILITY_CONTRACT_LIVE: v }).flag_set).toBe(false); } expect(pc.liveState({ PROBABILITY_CONTRACT_LIVE: '1' }).flag_set).toBe(true); }); }); describe('BOTH gates are required — neither activates alone', () => { it('the runtime flag alone does NOT serve an unapproved artifact', () => { const st = pc.liveState({ PROBABILITY_CONTRACT_LIVE: '1' }, { load: () => ({ ...A, approved_for_live: false }) }); expect(st.flag_set).toBe(true); expect(st.live).toBe('OFF'); expect(st.blocked_reason).toMatch(/not approved_for_live|not servable/); }); it('the runtime flag alone does NOT serve an unservable artifact', () => { const st = pc.liveState({ PROBABILITY_CONTRACT_LIVE: '1' }, { load: () => ({ ...A, approved_for_live: true, servable: false }) }); expect(st.live).toBe('OFF'); }); it('artifact approval alone does NOT activate behaviour', () => { expect(A.approved_for_live).toBe(true); // it IS promoted expect(pc.liveState({}).live).toBe('OFF'); // and behaviour is still off }); it('both together turn it on', () => { expect(pc.liveState({ PROBABILITY_CONTRACT_LIVE: '1' }).live).toBe('ON'); }); }); describe('when live is on', () => { it('a CERTIFIED row serves the calibrated number and derives everything from it', () => { const [out] = sp.applyToRows([row()], { liveState: LIVE_ON }); const expected = registry.applyCurve(A, 0.65); expect(out.p_win).toBe(Math.round(expected * 1000) / 1000); expect(out.confidence).toBe(Math.round(out.p_win * 100)); expect(out.confidence_basis).toBe('served_probability'); expect(out.probability_state).toBe(pc.STATE.CERTIFIED_CALIBRATED); const { evPct } = require('../../src/utils/devig'); expect(out.ev_pct).toBe(evPct(out.p_win, -110)); expect(out.ev_pct).not.toBe(evPct(0.65, -110)); // NOT from raw expect(out.raw_model_probability).toBe(0.65); // raw survives expect(out.grade).toBe('C+'); // grade untouched expect(out.side).toBe('over'); // side untouched }); it('an UNCERTIFIED row loses the number AND every claim derived from it', () => { const [out] = sp.applyToRows([row({ p_win: 0.91, grade: 'B+' })], { liveState: LIVE_ON }); expect(out.p_win).toBeNull(); expect(out.confidence).toBeNull(); expect(out.ev_pct).toBeNull(); expect(out.value).toBeNull(); expect(out.model_odds).toBeNull(); expect(out.kelly).toBeNull(); // and what must survive expect(out.raw_model_probability).toBe(0.91); expect(out.grade).toBe('B+'); expect(out.side).toBe('over'); expect(out.book_odds).toBe(-110); expect(out.player).toBe('P'); // the Read still exists }); it('a stat outside the contract is returned untouched', () => { const tb = row({ stat_type: 'total_bases' }); const [out] = sp.applyToRows([tb], { liveState: LIVE_ON }); expect(out).toEqual(tb); }); it('a different model era is returned untouched', () => { const old = row({ model_version: 'engine1@2026-07-20' }); const [out] = sp.applyToRows([old], { liveState: LIVE_ON }); expect(out).toEqual(old); }); }); describe('one seam', () => { it('serving routes reach it through stripModelPrice, not by duplicating logic', () => { const fs = require('fs'); const path = require('path'); const gsrc = fs.readFileSync(path.join(__dirname, '../../src/utils/snapshotGating.js'), 'utf8'); expect(gsrc).toContain("require('../services/model/servedProbability').applyToRows(rows)"); // no consumer may hold calibration logic of its own for (const f of ['src/routes/snapshot.js', 'src/routes/heroProp.js', 'src/services/topGradedService.js']) { const src = fs.readFileSync(path.join(__dirname, '../../', f), 'utf8') .replace(/\/\*[\s\S]*?\*\//g, '').replace(/^\s*\/\/.*$/gm, ''); for (const forbidden of ['applyCurve', 'applyIsotonic', 'artifactRegistry', 'served_curve']) { expect(src).not.toContain(forbidden); } } }); });