Intelligence, Advanced Charting & Trading Platform

1 import test from 'node:test';import assert from 'node:assert/strict';
2 import {notificationRecord,claimDelivery,recordDeliveryOutcome,acknowledgeDelivery,notificationUiStatus} from '../src/notification-outbox.mjs';
3 const at=n=>new Date(Date.parse('2026-10-21T12:00:00Z')+n*60000).toISOString();
4 const fresh=()=>notificationRecord({id:'n1',eventId:'e1',recipientChannel:'BALE',createdAt:at(0),maxAttempts:2});
5 test('fresh pending message has visible UI state and no send side effects',()=>{
6 const n=fresh();assert.equal(n.status,'PENDING');assert.ok(notificationUiStatus(n).includes('صف'))
7 });
8 test('lease excludes concurrent claim in state machine',()=>{
9 let n=fresh();let q=claimDelivery(n,{now:at(0)});assert.equal(q.claimed,true);
10 n=q.record;assert.equal(n.attempts,1);
11 assert.equal(claimDelivery(n,{now:at(0)}).claimed,false);
12 });
13 test('success requires explicit provider proof; never marks read confirmation automatically',()=>{
14 let n=claimDelivery(fresh(),{now:at(0)}).record;
15 assert.throws(()=>recordDeliveryOutcome(n,{at:at(1),success:true}));
16 n=recordDeliveryOutcome(n,{at:at(1),success:true,providerMessageId:'42'});
17 assert.equal(n.status,'SENT');assert.ok(notificationUiStatus(n).includes('تأییدنشده'));
18 assert.equal(claimDelivery(n,{now:at(2)}).claimed,false);
19 n=acknowledgeDelivery(n,{at:at(3)});assert.equal(n.status,'ACKNOWLEDGED')
20 });
21 test('bounded retry after failure; no extra claim after exhausted',()=>{
22 let n=claimDelivery(fresh(),{now:at(0)}).record;
23 n=recordDeliveryOutcome(n,{at:at(1),success:false,retryDelayMs:60000});
24 assert.equal(n.status,'FAILED');
25 assert.equal(claimDelivery(n,{now:at(1)}).claimed,false);
26 n=claimDelivery(n,{now:at(2)}).record;
27 assert.equal(n.attempts,2);
28 n=recordDeliveryOutcome(n,{at:at(3),success:false});
29 assert.equal(n.reason,'ATTEMPTS_EXHAUSTED');
30 assert.equal(claimDelivery(n,{now:at(9)}).claimed,false)
31 });
32 test('expired in-flight lease is unknown, not automatically retried',()=>{
33 let n=claimDelivery(fresh(),{now:at(0),leaseMs:30000}).record;
34 assert.equal(claimDelivery(n,{now:at(0)}).claimed,false);
35 const expired=claimDelivery(n,{now:at(1)});
36 assert.equal(expired.claimed,false);
37 assert.equal(expired.record.status,'UNKNOWN');
38 assert.equal(expired.record.reason,'LEASE_EXPIRED_DELIVERY_UNCERTAIN');
39 assert.equal(claimDelivery(expired.record,{now:at(9)}).claimed,false);
40 });
41 test('reject malformed channels and negative retries',()=>{
42 assert.throws(()=>notificationRecord({id:'x',eventId:'e',recipientChannel:'TELEGRAM',createdAt:at(0)}));
43 assert.throws(()=>claimDelivery(fresh(),{now:at(0),leaseMs:-1}));
44 assert.throws(()=>acknowledgeDelivery(fresh(),{at:at(1)}));
45 });
46
47 test('transport timeout after sending has unknown result and never retries',()=>{
48 let n=claimDelivery(fresh(),{now:at(0)}).record;
49 n=recordDeliveryOutcome(n,{at:at(1),success:false,unknown:true});
50 assert.equal(n.status,'UNKNOWN');
51 assert.equal(n.reason,'OUTCOME_UNKNOWN_RECONCILE_MANUALLY');
52 assert.equal(n.nextRetryAt,null);
53 assert.equal(claimDelivery(n,{now:at(20)}).claimed,false);
54 assert.match(notificationUiStatus(n),/نامشخص/);
55 });
56 test('unknown delivery cannot assert success or go backwards in time',()=>{
57 const n=claimDelivery(fresh(),{now:at(2)}).record;
58 assert.throws(()=>recordDeliveryOutcome(n,{at:at(3),success:true,unknown:true}));
59 assert.throws(()=>recordDeliveryOutcome(n,{at:at(1),success:false}));
60 assert.equal(recordDeliveryOutcome(n,{at:at(3),success:false}).status,'FAILED');
61 });
62
63 test('crash immediately after message attempt cannot cause a second send after lease expires',()=>{
64 const r=notificationRecord({id:'outbox-crash',eventId:'event-crash',
65 recipientChannel:'BALE',createdAt:at(0)});
66 const first=claimDelivery(r,{now:at(0),leaseMs:1000});
67 assert.equal(first.claimed,true);
68 const second=claimDelivery(first.record,{now:at(1)});
69 assert.equal(second.claimed,false);
70 assert.equal(second.record.status,'UNKNOWN');
71 assert.equal(second.record.nextRetryAt,null);
72 assert.match(notificationUiStatus(second.record),/نامشخص/);
73 });