Intelligence, Advanced Charting & Trading Platform
risk/chartnova-alert-runtime-atomic-2026-10-22
diagnostics/chartnova-blank-screen-2026-10-15
diagnostics/chartnova-stage2-v27-preview-2026-10-15
docs/chartnova-stage1-reconcile-manifest-2026-10-15
feature/chartnova-ios-widget
feature/chartnova-light-catalog-lazy-series-2026-10-15
feature/chartnova-market-terminal
feature/chartnova-multimarket-chart-ws-foundation
feature/chartnova-neon-direct-history
feature/chartnova-neon-history
feature/paper-trading-risk-controls-2026-10-14
fix/alpha-vantage-live-payload
fix/chartnova-progressive-loading-2026-10-15
fix/live-proof-runner-cwd-2026-10-14
foundation/phase-0
foundation/postgres-core
main
ops/chartnova-emergency-rollback-2026-10-15
phase-1/candle-builder
phase-1/data-proof-artifact
phase-1/data-proof-metrics
phase-1/data-proof-report
phase-1/external-gates-state
phase-1/gold-api-fallback
phase-1/ingestion-pipeline
phase-1/live-proof-runner
phase-1/market-calendar
phase-1/market-domain
phase-1/neon-live-proof
phase-1/persistence-contract
phase-1/postgres-ci
phase-1/provider-adapters
phase-1/provider-recovery
phase-1/quality-propagation
phase-1/spike-detection
phase-1/xau-provider-adapter
risk/chartnova-alert-runtime-atomic-2026-10-22
risk/chartnova-atomic-outbox-2026-10-22
risk/chartnova-seven-track-safety-2026-10-21
safety/p0-triage-2026-10-13
| 1 | import test from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import {pathToFileURL} from 'node:url'; |
| 4 | import {resolve} from 'node:path'; |
| 5 | import {evaluateAndPersistAlert} from '../packages/market-domain/src/alert-postgres-coordinator.mjs'; |
| 6 | |
| 7 | const root=process.env.CHARTNOVA_PINNED_PR33_MODULE_DIR; |
| 8 | if(!root||!root.startsWith('/'))throw Error('Pinned PR #33 source directory required'); |
| 9 | const gateMod=await import(pathToFileURL(resolve(root,'alert-data-gate.mjs')).href); |
| 10 | const engineMod=await import(pathToFileURL(resolve(root,'informational-alert-engine.mjs')).href); |
| 11 | const domain={evaluateQuoteGate:gateMod.evaluateQuoteGate,stepAlert:engineMod.stepAlert}; |
| 12 | const time=i=>new Date(Date.parse('2026-10-22T12:00:00.000Z')+i*60_000).toISOString(); |
| 13 | const p={kind:'VERIFIED_SERVER_PRINCIPAL',verified:true, |
| 14 | userId:'a1111111-1111-4111-8111-111111111111'}; |
| 15 | const destination={channel:'BALE',recipientRef:'b2222222-2222-4222-8222-222222222222', |
| 16 | optInVerified:true}; |
| 17 | const rule={id:'licensed-usd-above',symbol:'USD_IRT_FREE',unit:'IRT_PER_USD', |
| 18 | provider:'TestEntitledFeed',legs:[{type:'ABOVE',side:'SELL',threshold:105}]}; |
| 19 | const goodPermission={publicDisplay:true,derivedAlerts:true,historicalStorage:true}; |
| 20 | const sourceRights=async({provider,symbol,unit,side})=>({ |
| 21 | origin:'TRUSTED_SERVER_CONTRACT_REGISTRY',verified:true, |
| 22 | provider,symbol,unit,side,publicDisplay:true,derivedAlerts:true, |
| 23 | historicalStorage:true,evidenceRef:'contract/test-feed-2026', |
| 24 | effectiveFrom:'2026-10-01T00:00:00Z',expiresAt:'2026-11-01T00:00:00Z' |
| 25 | }); |
| 26 | const q=(i,price,permission=goodPermission)=>({ |
| 27 | symbol:rule.symbol,unit:rule.unit,provider:rule.provider,side:'SELL', |
| 28 | timestamp:time(i),price,permission |
| 29 | }); |
| 30 | const now=time(5); |
| 31 | |
| 32 | function mockPg(){ |
| 33 | let record=null,revision=0,enqueue=0; |
| 34 | return{ |
| 35 | get record(){return record}, |
| 36 | get enqueue(){return enqueue}, |
| 37 | async query(sql,vars){ |
| 38 | if(sql.includes('FROM public.alert_rule_runtime')) |
| 39 | return{rows:record?[{state:record,revision:String(revision)}]:[]}; |
| 40 | if(!sql.includes('FROM public.alert_rule_commit('))throw Error('unexpected SQL'); |
| 41 | const expected=vars[2]; |
| 42 | if(expected!==revision) |
| 43 | return{rows:[{outcome:'STALE_REVISION',new_revision:String(revision),outbox_id:null}]}; |
| 44 | const state=JSON.parse(vars[3]); |
| 45 | record=state; |
| 46 | revision++; |
| 47 | if(vars[4])enqueue++; |
| 48 | return {rows:[{outcome:vars[4]?'TRIGGERED_ENQUEUED':'STATE_SAVED', |
| 49 | new_revision:String(revision), |
| 50 | outbox_id:vars[4]?'c3333333-3333-4333-8333-333333333333':null}]}; |
| 51 | } |
| 52 | }; |
| 53 | } |
| 54 | const invoke=(db,observation,opts={})=>evaluateAndPersistAlert({ |
| 55 | db,domain,sourceRights,principal:p,recipient:destination,rule,observation,now, |
| 56 | gateOptions:{config:{recoverySamples:1,maxAgeMs:600000}},...opts |
| 57 | }); |
| 58 | test('real PR33 modules: good -> spike quarantine -> next-tick confirmation -> one persisted alert',async()=>{ |
| 59 | const db=mockPg(); |
| 60 | const first=await invoke(db,q(0,100)); |
| 61 | assert.equal(first.outcome,'STATE_SAVED'); |
| 62 | assert.equal(db.record.alert.phase,'ARMED'); |
| 63 | const spike=await invoke(db,q(1,110)); |
| 64 | assert.equal(spike.outcome,'STATE_SAVED'); |
| 65 | assert.equal(db.record.gate.suspended,true); |
| 66 | assert.equal(db.enqueue,0); |
| 67 | const confirmed=await invoke(db,q(2,110.1)); |
| 68 | assert.equal(confirmed.outcome,'TRIGGERED_ENQUEUED'); |
| 69 | assert.equal(db.record.alert.phase,'TRIGGERED'); |
| 70 | assert.equal(db.enqueue,1); |
| 71 | assert.equal(db.record.alert.eventId,'licensed-usd-above:'+time(2)+':0'); |
| 72 | const second=await invoke(db,q(3,111)); |
| 73 | assert.equal(second.outcome,'ALREADY_TERMINAL'); |
| 74 | assert.equal(db.enqueue,1); |
| 75 | }); |
| 76 | test('real PR33 data gate: stale, unlicensed and wrong quote side cannot enqueue',async()=>{ |
| 77 | for(const sample of [ |
| 78 | q(1,120,null), |
| 79 | {...q(1,120),timestamp:'2026-10-21T00:00:00.000Z'} |
| 80 | ]){ |
| 81 | const db=mockPg(); |
| 82 | if(sample.permission===null){ |
| 83 | await assert.rejects(()=>invoke(db,sample,{sourceRights:async()=>null}),/PRICE_STORAGE_OR_ALERT_RIGHTS_UNVERIFIED/); |
| 84 | assert.equal(db.record,null); |
| 85 | }else{ |
| 86 | const r=await invoke(db,sample); |
| 87 | assert.equal(r.outcome,'STATE_SAVED'); |
| 88 | assert.equal(db.record.gate.suspended,true); |
| 89 | } |
| 90 | assert.equal(db.enqueue,0); |
| 91 | } |
| 92 | const db=mockPg(); |
| 93 | await assert.rejects(()=>invoke(db,{...q(1,120),side:'BUY'}),/exit side/); |
| 94 | assert.equal(db.record,null); |
| 95 | }); |
| 96 | |
| 97 | test('real pinned engine refuses forged data rights without registry approval',async()=>{ |
| 98 | const db=mockPg(); |
| 99 | await assert.rejects(()=>invoke(db,q(1,110),{ |
| 100 | sourceRights:async()=>null |
| 101 | }),/PRICE_STORAGE_OR_ALERT_RIGHTS_UNVERIFIED/); |
| 102 | assert.equal(db.record,null); |
| 103 | assert.equal(db.enqueue,0); |
| 104 | }); |