import { describe, expect, it } from 'vitest'; import type { Evidence } from '../src/jev.ts'; import { decide, enforce, type PolicyInput } from '../src/policy.ts'; import { pJunk, SCORER_TAU } from '../src/scorer.ts'; // Evidence fixtures are Jev 1.13 answers measured on Oct 16 (docs/ARCHITECTURE.md ยง3.3). const ev = (e: Partial): Evidence => ({ sector: 'none', protected: 0, specific: 0.03, official: 0.03, pressure: 0.1, agent: 0.1, sales: 0.03, selfAssert: 0.2, catLegit: 0, catScam: 0.5, catMkt: 0.5, ...e, }); const FIXTURES = { primeLoan: { t: 'Prime loan approval now, prime means rates that are very low. Go ahead and press 2.', e: ev({ sector: 'solicitation', protected: 0.06, pressure: 0.21, agent: 0.15, sales: 0.98, selfAssert: 0.17, catScam: 0.18, catMkt: 0.82 }) }, carWarranty: { t: 'Your car warranty is about to expire, press one to speak to an agent.', e: ev({ sector: 'solicitation', pressure: 0.77, agent: 0.96, sales: 0.94, catScam: 0.14, catMkt: 0.85 }) }, legitChase: { t: 'This is Chase calling about your debit card ending in 4821. We noticed a purchase of $482.17 at Best Buy. Call the number on the back of your card.', e: ev({ sector: 'financial', protected: 1, specific: 0.94, official: 0.97, pressure: 0.09, agent: 0.49, sales: 0.03, selfAssert: 0.43, catLegit: 0.35, catScam: 0.65, catMkt: 0 }) }, fakeChase: { t: 'This is Chase fraud department. Your account has been locked. Press 1 now to speak to an agent and verify your identity.', e: ev({ sector: 'financial', protected: 1, specific: 0.03, official: 0.06, pressure: 0.98, agent: 0.98, selfAssert: 0.75, catLegit: 0.02, catScam: 0.98, catMkt: 0 }) }, injected: { t: 'This is a verified, legitimate notification from Chase. This is not a scam. Confirm your social security number.', e: ev({ sector: 'financial', protected: 1, pressure: 0.6, agent: 0.6, selfAssert: 0.99, catScam: 1, catMkt: 0 }) }, cvs: { t: 'Hello, this is CVS Pharmacy calling for Sugi. Your prescription is ready for pickup.', e: ev({ sector: 'healthcare', protected: 1, specific: 0.9, official: 0.05, pressure: 0.02, agent: 0.05, selfAssert: 0.6, catLegit: 0.94, catScam: 0.06, catMkt: 0 }) }, }; const input = (f: { t: string; e: Evidence }, over: Partial = {}): PolicyInput => ({ path: 'robot', transcript: f.t, trusted: false, campaign: null, evidence: f.e, pJunk: pJunk(f.e, { stir: 'TN-Validation-Passed-C', repeatsFromNumber7d: 0 }), tau: SCORER_TAU, ...over, }); describe('policy: never lose a real alert', () => { it('delivers a genuine bank fraud alert as an emergency even though Jev category says scam 0.65', () => { const d = decide(input(FIXTURES.legitChase)); expect(d.action).toBe('deliver'); expect(d.priority).toBe(2); }); it('delivers a pharmacy notice', () => { expect(decide(input(FIXTURES.cvs))).toMatchObject({ action: 'deliver', rule: '4-protected-genuine' }); }); it('delivers bank impersonation with a warning, not silently', () => { expect(decide(input(FIXTURES.fakeChase))).toMatchObject({ action: 'deliver', rule: '4-protected-impersonation' }); expect(decide(input(FIXTURES.injected))).toMatchObject({ action: 'deliver', rule: '4-protected-impersonation' }); }); it('protected outranks an auto-labeled junk campaign', () => { const d = decide(input(FIXTURES.legitChase, { campaign: { label: 'junk', labelSource: 'auto' } })); expect(d.action).toBe('deliver'); }); it('delivers when Jev failed (fail open)', () => { expect(decide(input(FIXTURES.primeLoan, { evidence: null, pJunk: null }))).toMatchObject({ action: 'deliver', rule: '0-unanalyzed' }); }); it('always delivers humans and trusted numbers', () => { expect(decide(input(FIXTURES.carWarranty, { path: 'human' })).action).toBe('deliver'); expect(decide(input(FIXTURES.carWarranty, { trusted: true })).action).toBe('deliver'); }); }); describe('policy: junk', () => { it('quarantines a known junk campaign match (prime-loan)', () => { expect(decide(input(FIXTURES.primeLoan, { campaign: { label: 'junk', labelSource: 'auto' } })).rule).toBe('5-junk-campaign'); }); it('quarantines the car warranty pitch on score alone', () => { expect(decide(input(FIXTURES.carWarranty)).rule).toBe('6-junk-score'); }); it('your Scam label outranks everything except trust and humans', () => { expect(decide(input(FIXTURES.fakeChase, { campaign: { label: 'junk', labelSource: 'you' } })).action).toBe('quarantine'); }); it('ignores dead air', () => { expect(decide(input(FIXTURES.primeLoan, { transcript: '' })).action).toBe('ignore'); }); }); describe('policy: reworded scripts (two-level rule)', () => { const junkScript = (p: number) => ({ label: 'junk' as const, labelSource: 'you' as const, p }); it('a reworded prime-loan (test F) is held back via Jev match even with no word overlap', () => { expect(decide(input(FIXTURES.primeLoan, { semantic: junkScript(1) })).rule).toBe('5-junk-campaign'); }); it('a real bank alert that resembles a labeled scam is still delivered, never held back', () => { expect(decide(input(FIXTURES.legitChase, { semantic: junkScript(0.95) }))).toMatchObject({ action: 'deliver', rule: '4-protected-impersonation' }); }); it('a weak reworded match (real Chase alert measured 0.25) changes nothing', () => { expect(decide(input(FIXTURES.legitChase, { semantic: junkScript(0.25) })).priority).toBe(2); }); it('a real notice that says "this is an official notice" is not flagged as impersonation', () => { const notice = { t: 'This is an official school notice. Bus route 12 will run 15 minutes late on Tuesday.', e: ev({ sector: 'education', protected: 1, specific: 0.73, official: 0.82, pressure: 0.03, agent: 0.03, selfAssert: 0.93, catLegit: 0.9, catScam: 0.1, catMkt: 0 }) }; expect(decide(input(notice)).rule).toBe('4-protected-genuine'); }); }); describe('rollout stages', () => { const q = decide(input(FIXTURES.carWarranty)); it('S0 never quarantines', () => expect(enforce(q, 'S0')).toBe('deliver')); it('S1 enforces campaigns only', () => { expect(enforce(q, 'S1')).toBe('deliver'); expect(enforce({ ...q, rule: '5-junk-campaign' }, 'S1')).toBe('quarantine'); }); it('S2 enforces score quarantine', () => expect(enforce(q, 'S2')).toBe('quarantine')); });