Intelligence, Advanced Charting & Trading Platform
test(risk): cover alert confirmation, OCO, long/short ATR trailing, invalid-side and idempotency
davoodmehraban89 committed
Oct 21, 2026 at 08:50 UTC
bbc80d409a3ee0816003e68f6ec33b95a6e3c3ba
1 file changed
+92
packages/market-domain/test/informational-alert-engine.test.mjs
new
+92
@@ -0,0 +1,92 @@
1
+import test from 'node:test';import assert from 'node:assert/strict';
2
+import {initialAlertState,stepAlert,markNotified,closeAlert} from '../src/informational-alert-engine.mjs';
3
+const at=i=>new Date(Date.parse('2026-10-20T12:00:00Z')+i*60000).toISOString();
4
+const q=(i,p,extras={})=>({timestamp:at(i),price:p,provider:'S1',symbol:'USD_IRT_FREE',side:'SELL',...extras});
5
+const g=x=>({status:'GOOD',alertsSuspended:false,accepted:x});
6
+const leg=(type,extras={})=>({type,side:'SELL',threshold:100,position:'LONG',entry:100,...extras});
7
+const rule=(id,legs,extra={})=>({id,legs,...extra});
8
+test('above/below thresholds generate one event, no repeated triggers',()=>{
9
+ for(const [kind,price] of [['ABOVE',102],['BELOW',98]]){
10
+ const r=rule('one',[leg(kind)]),tick=q(0,price);
11
+ let v=stepAlert(r,null,tick,g(tick));
12
+ assert.equal(v.state.phase,'TRIGGERED');assert.equal(v.events.length,1);
13
+ let again=stepAlert(r,v.state,q(1,price),g(q(1,price)));
14
+ assert.equal(again.events.length,0);
15
+ }
16
+});
17
+test('missing/STALE gate suspends, no trigger even if price crossed',()=>{
18
+ const r=rule('one',[leg('ABOVE')]),tick=q(0,110);
19
+ for(const gate of [null,{status:'STALE',alertsSuspended:true,accepted:null}]){
20
+ const v=stepAlert(r,null,tick,gate);assert.equal(v.events.length,0);
21
+ assert.equal(v.reason,'QUALITY_HOLD')
22
+ }
23
+});
24
+test('confirmation N valid consecutive samples with gap and out of order protection',()=>{
25
+ const r=rule('x',[leg('ABOVE')],{confirmSamples:2,maxGapMs:60000});
26
+ let t=q(0,110),v=stepAlert(r,null,t,g(t));
27
+ assert.equal(v.reason,'AWAITING_CONFIRMATION');
28
+ t=q(0,110);let dup=stepAlert(r,v.state,t,g(t));assert.equal(dup.reason,'DUPLICATE');
29
+ t=q(2,110);v=stepAlert(r,v.state,t,g(t));assert.equal(v.reason,'AWAITING_CONFIRMATION');
30
+ t=q(3,111);v=stepAlert(r,v.state,t,g(t));assert.equal(v.events.length,1);
31
+});
32
+test('time duration confirmation needs true elapsed time',()=>{
33
+ const r=rule('t',[leg('BELOW')],{confirmMs:120000});
34
+ let x=q(0,80),v=stepAlert(r,null,x,g(x));assert.equal(v.events.length,0);
35
+ x=q(1,80);v=stepAlert(r,v.state,x,g(x));assert.equal(v.events.length,0);
36
+ x=q(2,80);v=stepAlert(r,v.state,x,g(x));assert.equal(v.events.length,1)
37
+});
38
+test('trailing long highest and stop never fall: only SELL exit',()=>{
39
+ const r=rule('long',[leg('TRAILING_STOP',{mode:'ATR',distance:2})]);
40
+ let x=q(0,100,{atr:2}),v=stepAlert(r,null,x,g(x));
41
+ assert.equal(v.state.highWater,100);assert.equal(v.state.stop,96);
42
+ x=q(1,110,{atr:2});v=stepAlert(r,v.state,x,g(x));assert.equal(v.state.stop,106);
43
+ x=q(2,108,{atr:4});v=stepAlert(r,v.state,x,g(x));assert.equal(v.state.stop,106);
44
+ x=q(3,105,{atr:2});v=stepAlert(r,v.state,x,g(x));
45
+ assert.equal(v.state.phase,'TRIGGERED');assert.equal(v.events[0].side,'SELL');
46
+});
47
+test('short trailing uses BUY price, low water decreases and stop never increases',()=>{
48
+ const r=rule('short',[leg('TRAILING_STOP',{position:'SHORT',side:'BUY',mode:'ATR',distance:1})]);
49
+ let x=q(0,100,{side:'BUY',atr:2}),v=stepAlert(r,null,x,g(x));
50
+ assert.equal(v.state.stop,102);
51
+ x=q(1,90,{side:'BUY',atr:3});v=stepAlert(r,v.state,x,g(x));
52
+ assert.equal(v.state.stop,93);
53
+ x=q(2,91,{side:'BUY',atr:10});v=stepAlert(r,v.state,x,g(x));
54
+ assert.equal(v.state.stop,93);
55
+ x=q(3,94,{side:'BUY',atr:1});v=stepAlert(r,v.state,x,g(x));
56
+ assert.equal(v.events.length,1)
57
+});
58
+test('missing ATR never silently substitutes constant pct',()=>{
59
+ const r=rule('atr',[leg('TRAILING_STOP',{mode:'ATR',distance:2})]);
60
+ const x=q(0,100);const v=stepAlert(r,null,x,g(x));
61
+ assert.equal(v.reason,'ATR_DATA_UNAVAILABLE');assert.equal(v.events.length,0);
62
+});
63
+test('optional fixed-percent trailing, rejects inappropriate percentages',()=>{
64
+ const r=rule('percent',[leg('TRAILING_STOP',{mode:'PCT',distance:5})]);
65
+ let x=q(0,100),v=stepAlert(r,null,x,g(x));assert.equal(v.state.stop,95);
66
+ x=q(1,94);v=stepAlert(r,v.state,x,g(x));assert.equal(v.events.length,1);
67
+ assert.throws(()=>stepAlert(rule('bad',[leg('TRAILING_STOP',{mode:'PCT',distance:100})]),null,x,g(x)))
68
+});
69
+test('OCO simultaneous SL and TP chooses protective stop and cancels other',()=>{
70
+ const r=rule('oco',[leg('STOP_LOSS',{threshold:105}),leg('TAKE_PROFIT',{threshold:100})]);
71
+ const x=q(0,103),v=stepAlert(r,null,x,g(x));
72
+ assert.equal(v.events.length,1);
73
+ assert.equal(v.events[0].ruleType,'STOP_LOSS');
74
+ assert.deepEqual(v.events[0].cancelledOcoLegs,[1])
75
+});
76
+test('notification state requires verified delivery ID and can close only in order',()=>{
77
+ const r=rule('id',[leg('ABOVE')],{cooldownMs:600000}),x=q(0,101),v=stepAlert(r,null,x,g(x));
78
+ assert.throws(()=>markNotified(r,v.state,{eventId:'bad',at:at(1),deliveryId:'ok'}));
79
+ assert.throws(()=>closeAlert(v.state,at(1)));
80
+ const sent=markNotified(r,v.state,{eventId:v.events[0].id,at:at(1),deliveryId:'provider-id'});
81
+ assert.equal(sent.phase,'NOTIFIED');assert.equal(sent.cooldownUntil,at(11));
82
+ assert.equal(stepAlert(r,sent,q(2,110),g(q(2,110))).events.length,0);
83
+ assert.equal(closeAlert(sent,at(2)).phase,'CLOSED')
84
+});
85
+test('stop loss side must correspond to liquidation side',()=>{
86
+ assert.throws(()=>stepAlert(rule('bad',[leg('STOP_LOSS',{side:'BUY'})]),null,q(0,99),g(q(0,99))))
87
+});
88
+test('nonmatching quote side and malformed alerts produce no event',()=>{
89
+ const r=rule('x',[leg('ABOVE')]);
90
+ const x=q(0,110,{side:'BUY'});assert.equal(stepAlert(r,null,x,g(x)).reason,'QUOTE_SIDE_OR_PRICE_INVALID');
91
+ assert.throws(()=>stepAlert({id:'x',legs:[]},null,x,g(x)));
92
+});