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 {dispatchLeasedBale} from '../packages/market-domain/src/leased-bale-dispatch.mjs'; |
| 6 | |
| 7 | const root=process.env.CHARTNOVA_PINNED_PR33_MODULE_DIR; |
| 8 | if(!root||!root.startsWith('/'))throw Error('Pinned PR33 adapter directory required'); |
| 9 | const {baleOneMessage}=await import(pathToFileURL(resolve(root,'bale-once-adapter.mjs')).href); |
| 10 | const outboxId='e1111111-1111-4111-8111-111111111111'; |
| 11 | const claimToken='f2222222-2222-4222-8222-222222222222'; |
| 12 | const at='2026-10-22T12:00:00.000Z'; |
| 13 | const token='123456:aVeryLongSyntheticNeverRealToken'; |
| 14 | const recipientRef='c3333333-3333-4333-8333-333333333333'; |
| 15 | const resolver=async({recipientRef:ref,channel})=>({verified:channel==='BALE', |
| 16 | recipientRef:ref,chatId:'12345'}); |
| 17 | const base={outboxId,claimToken,recipientRef,resolveRecipient:resolver, |
| 18 | at,token,text:'پیام آزمایشی بدون ارسال واقعی'}; |
| 19 | function fakeStore({consented=true}={}){ |
| 20 | let permit=false,finished=null; |
| 21 | return{get permit(){return permit},get finished(){return finished}, |
| 22 | async query(sql,params){ |
| 23 | assert.equal(params[0],outboxId); |
| 24 | assert.equal(params[1],claimToken); |
| 25 | if(sql.includes('alert_notification_')) { |
| 26 | assert.equal(params[2],recipientRef); |
| 27 | assert.equal(params[3],'BALE'); |
| 28 | } |
| 29 | if(sql.includes('alert_notification_begin_egress')){ |
| 30 | const ok=!permit&&consented; |
| 31 | if(ok)permit=true; |
| 32 | return {rows:[{permitted:ok}]}; |
| 33 | } |
| 34 | if(sql.includes('alert_notification_egress_allowed')) |
| 35 | return {rows:[{permitted:consented}]}; |
| 36 | if(sql.includes('notification_outbox_finish')){ |
| 37 | if(!permit)throw Error('unexpected finish without permit'); |
| 38 | finished=params[2]; |
| 39 | return {rows:[{outcome:finished}]}; |
| 40 | } |
| 41 | throw Error('unexpected SQL'); |
| 42 | } |
| 43 | }; |
| 44 | } |
| 45 | function wrappedBale(transport){ |
| 46 | return x=>baleOneMessage({...x,transport}); |
| 47 | } |
| 48 | function fakeTransport(counters,{group=false,fail=false}={}){ |
| 49 | return async url=>{ |
| 50 | if(url.endsWith('/getChat')){ |
| 51 | counters.preflight++; |
| 52 | return {status:200,json:async()=>({ok:true,result:{id:12345, |
| 53 | type:group?'group':'private'}})}; |
| 54 | } |
| 55 | if(url.endsWith('/sendMessage')){ |
| 56 | counters.send++; |
| 57 | return fail?{status:503,json:async()=>({ok:false})}: |
| 58 | {status:200,json:async()=>({ok:true,result:{message_id:99,chat:{id:12345}}})}; |
| 59 | } |
| 60 | throw Error('unexpected request route'); |
| 61 | }; |
| 62 | } |
| 63 | test('real Bale adapter: exactly one private-chat synthetic send after both SQL checks',async()=>{ |
| 64 | const db=fakeStore(),counters={preflight:0,send:0}; |
| 65 | const run=()=>dispatchLeasedBale({...base,db, |
| 66 | baleOneMessage:wrappedBale(fakeTransport(counters))}); |
| 67 | const results=await Promise.all([run(),run(),run()]); |
| 68 | assert.equal(results.filter(x=>x.status==='PROVIDER_ACCEPTED').length,1); |
| 69 | assert.equal(results.filter(x=>x.status==='BLOCKED').length,2); |
| 70 | assert.equal(db.finished,'SENT'); |
| 71 | assert.equal(counters.preflight,1); |
| 72 | assert.equal(counters.send,1); |
| 73 | assert.equal(results[0].readConfirmed,false); |
| 74 | assert.ok(!JSON.stringify(results).includes(token)); |
| 75 | }); |
| 76 | test('recipient revocation before permit: no message, no network',async()=>{ |
| 77 | const db=fakeStore({consented:false}),counters={preflight:0,send:0}; |
| 78 | const result=await dispatchLeasedBale({...base,db, |
| 79 | baleOneMessage:wrappedBale(fakeTransport(counters))}); |
| 80 | assert.equal(result.status,'BLOCKED'); |
| 81 | assert.equal(counters.preflight,0);assert.equal(counters.send,0); |
| 82 | }); |
| 83 | test('revoked after permit but before network: no message, terminal UNKNOWN',async()=>{ |
| 84 | const db=fakeStore({consented:true}); |
| 85 | let calls=0; |
| 86 | const query=db.query.bind(db); |
| 87 | db.query=async(sql,params)=>{ |
| 88 | const r=await query(sql,params); |
| 89 | if(sql.includes('alert_notification_begin_egress'))calls++; |
| 90 | if(sql.includes('alert_notification_egress_allowed')) |
| 91 | return {rows:[{permitted:false}]}; |
| 92 | return r; |
| 93 | }; |
| 94 | const counters={preflight:0,send:0}; |
| 95 | const result=await dispatchLeasedBale({...base,db, |
| 96 | baleOneMessage:wrappedBale(fakeTransport(counters))}); |
| 97 | assert.equal(calls,1); |
| 98 | assert.equal(result.status,'BLOCKED');assert.equal(result.sent,false); |
| 99 | assert.equal(db.finished,'UNKNOWN'); |
| 100 | assert.equal(counters.send,0); |
| 101 | }); |
| 102 | test('ambiguous provider HTTP result: no automatic retry despite 503 response',async()=>{ |
| 103 | const db=fakeStore(),counters={preflight:0,send:0}; |
| 104 | const one=()=>dispatchLeasedBale({...base,db, |
| 105 | baleOneMessage:wrappedBale(fakeTransport(counters,{fail:true}))}); |
| 106 | const result=await one(); |
| 107 | assert.equal(result.status,'UNKNOWN'); |
| 108 | assert.equal(db.finished,'UNKNOWN'); |
| 109 | const again=await one(); |
| 110 | assert.equal(again.status,'BLOCKED'); |
| 111 | assert.equal(counters.send,1); |
| 112 | }); |
| 113 | test('group chat rejected in real PR33 adapter; cannot claim accepted',async()=>{ |
| 114 | const db=fakeStore(),counters={preflight:0,send:0}; |
| 115 | const result=await dispatchLeasedBale({...base,db, |
| 116 | baleOneMessage:wrappedBale(fakeTransport(counters,{group:true}))}); |
| 117 | assert.equal(result.status,'UNKNOWN'); |
| 118 | assert.equal(counters.send,0);assert.equal(db.finished,'UNKNOWN'); |
| 119 | }); |
| 120 | test('wrong trusted-recipient lookup cannot redirect alerts to arbitrary chats',async()=>{ |
| 121 | const db=fakeStore(),counters={preflight:0,send:0}; |
| 122 | const wrong=async()=>({verified:true,recipientRef:'d4444444-4444-4444-8444-444444444444',chatId:'12345'}); |
| 123 | const res=await dispatchLeasedBale({...base,db,resolveRecipient:wrong, |
| 124 | baleOneMessage:wrappedBale(fakeTransport(counters))}); |
| 125 | assert.equal(res.status,'BLOCKED'); |
| 126 | assert.equal(res.reason,'RECIPIENT_MAPPING_UNVERIFIED'); |
| 127 | assert.equal(db.permit,false); |
| 128 | assert.equal(counters.send,0); |
| 129 | }); |
| 130 | test('unverified lookup never obtains a send permit even with a valid DB lease',async()=>{ |
| 131 | const db=fakeStore(),counters={preflight:0,send:0}; |
| 132 | const wrong=async({recipientRef})=>({verified:false,recipientRef,chatId:'12345'}); |
| 133 | const result=await dispatchLeasedBale({...base,db,resolveRecipient:wrong, |
| 134 | baleOneMessage:wrappedBale(fakeTransport(counters))}); |
| 135 | assert.equal(result.status,'BLOCKED'); |
| 136 | assert.equal(db.permit,false); |
| 137 | assert.equal(counters.preflight,0); |
| 138 | assert.equal(counters.send,0); |
| 139 | }); |
| 140 | test('incorrect ID and missing trusted SQL driver fail before network',async()=>{ |
| 141 | await assert.rejects(()=>dispatchLeasedBale({...base,db:null, |
| 142 | baleOneMessage:()=>{throw Error('network cannot happen')}}),/invalid server dispatch/); |
| 143 | await assert.rejects(()=>dispatchLeasedBale({...base,outboxId:'bad', |
| 144 | db:fakeStore(),baleOneMessage:()=>{throw Error('network cannot happen')}}),/invalid server dispatch/); |
| 145 | }); |