'use strict'; /** * ARTIFACT IDENTITY IS NOT FIT-POLICY CERTIFICATION. * * A digest says WHICH mapping ran. It says nothing about whether the procedure * that produced it should be trusted to produce tomorrow's. */ const fp = require('../../src/services/model/fitPolicy'); const svc = require('../../src/services/model/probabilityContractService'); const cal = require('../../src/services/model/calibration'); const ERA = 'engine1@2026-08-07-fullwindow'; const artifact = (over = {}) => ({ estimator_type: 'isotonic', model_version: ERA, fit_n: 6084, training_cutoff: '2026-08-21', knot_digest: 'd9d571d728ba76de', served_curve_digest: '9d731cd88ba073d7', certified_bands: [[0.50, 0.80]], ...over, }); describe('the declared procedure', () => { it('pins every decision that would otherwise be implicit', () => { // If any of these change, they change DELIBERATELY and this test says so. expect(fp.POLICY_V1.policy_version).toBe('mlb-hits-isotonic-refit@v1'); expect(fp.POLICY_V1.sport).toBe('mlb'); expect(fp.POLICY_V1.stat).toBe('hits'); expect(fp.POLICY_V1.model_version).toBe(ERA); expect(fp.POLICY_V1.fit_algorithm).toBe('isotonic-pav'); expect(fp.POLICY_V1.min_fit_rows).toBe(200); expect(fp.POLICY_V1.data_selection.horizon).toBe('game_date < fit_as_of'); expect(fp.POLICY_V1.support_contract.certified_bands).toEqual([[0.50, 0.80]]); expect(fp.POLICY_V1.support_contract.may_be_widened_by_refit).toBe(false); }); it('a refit may never widen the region it is trusted in', () => { const r = fp.validate(artifact({ certified_bands: [[0.50, 0.95]] }), { era_counts: { [ERA]: 6084 } }); expect(r.valid).toBe(false); expect(r.violations).toContain(fp.VIOLATION.SUPPORT_WIDENED); expect(r.servable).toBe(false); }); }); describe('the minimum automatic gates', () => { it('passes only an era-restricted fit', () => { const r = fp.validate(artifact(), { era_counts: { [ERA]: 6084 } }); expect(r.valid).toBe(true); expect(r.servable).toBe(true); }); it('REFUSES the production era mix — the measured state today', () => { const r = fp.validate(artifact(), { era_counts: { [ERA]: 2792, 'engine1@2026-07-20': 3292 } }); expect(r.valid).toBe(false); expect(r.violations).toContain(fp.VIOLATION.ERA_NOT_RESTRICTED); expect(r.servable).toBe(false); }); it('"we did not check" is a violation, not a pass', () => { // An unverified restriction is exactly the state production is in. const r = fp.validate(artifact(), {}); expect(r.valid).toBe(false); expect(r.violations).toContain(fp.VIOLATION.ERA_NOT_RESTRICTED); }); it('catches a missing identity, a wrong era, a wrong estimator and a thin fit', () => { const ev = { era_counts: { [ERA]: 6084 } }; expect(fp.validate(artifact({ knot_digest: null }), ev).violations).toContain(fp.VIOLATION.NO_ARTIFACT_IDENTITY); expect(fp.validate(artifact({ training_cutoff: null }), ev).violations).toContain(fp.VIOLATION.NO_TRAINING_CUTOFF); expect(fp.validate(artifact({ model_version: 'engine1@2026-07-20' }), ev).violations).toContain(fp.VIOLATION.ERA_MISMATCH); expect(fp.validate(artifact({ estimator_type: 'low_param' }), ev).violations).toContain(fp.VIOLATION.ESTIMATOR_MISMATCH); expect(fp.validate(artifact({ fit_n: 12 }), ev).violations).toContain(fp.VIOLATION.INSUFFICIENT_FIT_ROWS); }); it('a null artifact is refused, never treated as vacuously valid', () => { expect(fp.validate(null).valid).toBe(false); expect(fp.validate(null).servable).toBe(false); }); it('servable is never true while any violation stands — there is no override', () => { for (const bad of [{ fit_n: 1 }, { model_version: 'x' }, { estimator_type: 'y' }, { knot_digest: null }]) { expect(fp.validate(artifact(bad), { era_counts: { [ERA]: 6084 } }).servable).toBe(false); } expect(fp.validate.length).toBeLessThanOrEqual(3); // no force/override argument }); }); describe('the promoted artifact carries its policy state', () => { const registry = require('../../src/services/model/artifactRegistry'); it('the shipped artifact PASSES its own policy — detection became repair', () => { const a = registry.load('mlb', 'hits'); expect(a.fit_policy_version || a.procedure_version).toBe('mlb-hits-isotonic-refit@v1'); expect(a.fit_policy_valid).toBe(true); expect(a.fit_policy_violations).toEqual([]); expect(a.servable).toBe(true); }); it('the policy is RE-VALIDATED at load, so a hand-edited file cannot smuggle one in', () => { const a = registry.load('mlb', 'hits'); // the loader recomputes from era_audit rather than trusting the file's flag const recomputed = fp.validate({ ...a, estimator_type: 'isotonic' }, { era_counts: a.era_audit.era_counts }); expect(recomputed.valid).toBe(true); const lying = fp.validate({ ...a, estimator_type: 'isotonic' }, { era_counts: { [ERA]: 100, 'engine1@2026-07-20': 1 } }); expect(lying.valid).toBe(false); expect(lying.servable).toBe(false); }); });