Intelligence, Advanced Charting & Trading Platform
test(integration): exercise license-to-gate-to-threshold-to-Bale mock and ambiguous no-retry outcome
davoodmehraban89 committed
Oct 21, 2026 at 09:23 UTC
2234bb0b61bc07cace202abc5f3f16ff726307f6
1 file changed
+69
packages/market-domain/test/integrated-alert-safety.test.mjs
new
+69
@@ -0,0 +1,69 @@
1
+import test from 'node:test';import assert from 'node:assert/strict';
2
+import {evaluateQuoteGate} from '../src/alert-data-gate.mjs';
3
+import {stepAlert,markNotified} from '../src/informational-alert-engine.mjs';
4
+import {notificationRecord,claimDelivery,recordDeliveryOutcome} from '../src/notification-outbox.mjs';
5
+import {baleOneMessage} from '../src/bale-once-adapter.mjs';
6
+import {inspectTwoSources} from '../src/licensed-provider-selector.mjs';
7
+
8
+const now=Date.parse('2026-10-21T12:30:00.000Z');
9
+const t=i=>new Date(now-300000+i*60000).toISOString();
10
+const entitlement={publicDisplay:true,derivedAlerts:true};
11
+const o=(i,price,extra={})=>({provider:'S1',symbol:'USD_IRT_FREE',unit:'IRT_PER_USD',
12
+ side:'SELL',price,timestamp:t(i),permission:entitlement,...extra});
13
+const cfg={nowMs:now,config:{maxAgeMs:10*60000,recoverySamples:1}};
14
+const rule={id:'user-optin-test',provider:'S1',symbol:'USD_IRT_FREE',unit:'IRT_PER_USD',
15
+ legs:[{type:'ABOVE',side:'SELL',threshold:1050}]};
16
+const gate=(state,quote,opts={})=>evaluateQuoteGate(state,quote,{...cfg,...opts});
17
+test('functional integration: spike quarantined until second fresh independent sample; then one threshold event',()=>{
18
+ const a=gate(null,o(0,1000));
19
+ const before=stepAlert(rule,null,o(0,1000),a);
20
+ assert.equal(before.events.length,0);
21
+ const spike=gate(a.state,o(1,1100));
22
+ assert.equal(spike.status,'SPIKE_PENDING');
23
+ const blocked=stepAlert(rule,before.state,o(1,1100),spike);
24
+ assert.equal(blocked.events.length,0);
25
+ assert.equal(blocked.reason,'QUALITY_HOLD');
26
+ const confirmed=gate(spike.state,o(2,1101));
27
+ assert.equal(confirmed.status,'GOOD');
28
+ const fired=stepAlert(rule,blocked.state,o(2,1101),confirmed);
29
+ assert.equal(fired.events.length,1);
30
+ assert.equal(fired.events[0].kind,'INFORMATIONAL_THRESHOLD');
31
+ assert.equal(stepAlert(rule,fired.state,o(3,1101),gate(confirmed.state,o(3,1101))).events.length,0);
32
+});
33
+test('security integration: rights and cross-source disagreement block all downstream alerts',()=>{
34
+ const unlicensed=o(1,1100,{permission:null});
35
+ assert.equal(gate(null,unlicensed).accepted,null);
36
+ assert.equal(stepAlert(rule,null,unlicensed,gate(null,unlicensed)).events.length,0);
37
+ const a=o(1,1100),b=o(1,1300,{provider:'S2'});
38
+ const checked=inspectTwoSources(a,b,{nowMs:now,maxAgeMs:600000});
39
+ assert.equal(checked.status,'SOURCE_DIVERGENCE');
40
+ assert.equal(checked.observation,null);
41
+ assert.equal(checked.alertsSuspended,true);
42
+});
43
+test('send-outcome integration: accepted Bale response advances, ambiguous outcome is terminal',async()=>{
44
+ const start=gate(null,o(2,1101));
45
+ const fired=stepAlert(rule,null,o(2,1101),start);
46
+ assert.equal(fired.events.length,1);
47
+ const eventId=fired.events[0].id;
48
+ const message=notificationRecord({id:'msg-1',eventId,recipientChannel:'BALE',createdAt:t(2)});
49
+ let outbox=claimDelivery(message,{now:t(2)}).record;
50
+ const token='123456:syntheticTransportTokenPlaceholderOnly';
51
+ const accepted=await baleOneMessage({token,chatId:'12345',text:'آزمون اطلاعرسانی، توصیه مالی نیست',
52
+ transport:async url=>url.endsWith('/getChat')?
53
+ {status:200,json:async()=>({ok:true,result:{id:12345,type:'private'}})}:
54
+ {status:200,json:async()=>({ok:true,result:{chat:{id:12345},message_id:7}})}});
55
+ assert.equal(accepted.status,'SENT');
56
+ outbox=recordDeliveryOutcome(outbox,{at:t(3),success:true,providerMessageId:accepted.messageId});
57
+ assert.equal(outbox.status,'SENT');
58
+ const notified=markNotified(rule,fired.state,{eventId,at:t(3),deliveryId:outbox.providerMessageId});
59
+ assert.equal(notified.phase,'NOTIFIED');
60
+ let n=claimDelivery(notificationRecord({id:'msg-2',eventId,recipientChannel:'BALE',createdAt:t(2)}),{now:t(2)}).record;
61
+ const unknown=await baleOneMessage({token,chatId:'12345',text:'آزمون',
62
+ transport:async url=>url.endsWith('/getChat')?
63
+ {status:200,json:async()=>({ok:true,result:{id:12345,type:'private'}})}:
64
+ {status:503,json:async()=>({ok:false})}});
65
+ assert.equal(unknown.status,'UNKNOWN');
66
+ n=recordDeliveryOutcome(n,{at:t(3),success:false,unknown:true});
67
+ assert.equal(n.status,'UNKNOWN');
68
+ assert.equal(claimDelivery(n,{now:t(4)}).claimed,false);
69
+});