Intelligence, Advanced Charting & Trading Platform
test(markets): verify compact snapshot contract, data provenance, validation and payload reduction
davoodmehraban89 committed
Oct 15, 2026 at 14:40 UTC
a5fee3c4994f419c3a7ddd64b94a810eeb2a916e
1 file changed
+76
packages/market-domain/test/market-catalog-summary.test.mjs
new
+76
@@ -0,0 +1,76 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import {marketCatalogSummary} from '../src/market-catalog-summary.mjs';
4
+const now=Date.parse('2026-10-15T11:00:00Z');
5
+function item(symbol='USD-IRT'){
6
+ const [base,quote]=symbol.split('-');
7
+ return {symbol,name:'دلار آمریکا',base,quote,category:'CURRENCY',
8
+ sellPrice:100000,buyPrice:100005,change:0.5,
9
+ updatedAt:'2026-10-15T10:55:00Z',isUpToDate:true,
10
+ history:Array.from({length:168},(_,i)=>100000+i)};
11
+}
12
+function catalog(markets=[item()]){
13
+ return {ok:true,source:'gheymat.online',fetchedAt:'2026-10-15T10:59:00Z',markets};
14
+}
15
+test('projects a valid catalog without carrying time series or private fields',()=>{
16
+ const full=catalog();
17
+ full.markets[0].providerToken='never-expose';
18
+ const s=marketCatalogSummary(full,now);
19
+ assert.equal(s.ok,true);assert.equal(s.markets[0].history,undefined);
20
+ assert.equal(s.markets[0].providerToken,undefined);
21
+ assert.equal(s.markets[0].historyCount,168);
22
+ assert.equal(s.markets[0].quote,'IRT');
23
+ assert.equal(s.markets[0].sellPrice,100000);
24
+ assert.equal(s.markets[0].buyPrice,100005);
25
+ assert.equal(s.markets[0].historyKind,'provider_price_points_not_ohlc');
26
+ assert.equal(s.dataKind,'provider_market_snapshots_not_executable_quotes');
27
+});
28
+test('verified input remains immutable across projection',()=>{
29
+ const orig=catalog();const json=JSON.stringify(orig);
30
+ const snapshot=marketCatalogSummary(orig,now);
31
+ snapshot.markets[0].sellPrice=1;
32
+ assert.equal(JSON.stringify(orig),json);
33
+ assert.equal(orig.markets[0].sellPrice,100000);
34
+});
35
+test('typical 58-market 168-observation payload loses at least 70% bytes',()=>{
36
+ const arr=Array.from({length:58},(_,i)=>item('S'+String(i+1).padStart(3,'0')+'-IRT'));
37
+ const full=catalog(arr),snapshot=marketCatalogSummary(full,now);
38
+ assert.equal(snapshot.markets.length,58);
39
+ assert.ok(JSON.stringify(snapshot).length<JSON.stringify(full).length*.3);
40
+});
41
+test('preserves upstream provider time and stale signal without pretending live ticks',()=>{
42
+ const s=marketCatalogSummary({...catalog(),fetchedAt:'2026-10-15T10:00:00Z'},now);
43
+ assert.equal(s.stale,true);assert.equal(s.quoteSide,'sell');
44
+ assert.equal(s.fetchedAt,'2026-10-15T10:00:00Z');
45
+});
46
+test('rejects unknown provider or missing original proof',()=>{
47
+ for(const raw of [null,{},catalog([]),{...catalog(),source:'made-up-feed'}, {...catalog(),ok:false}])
48
+ assert.deepEqual(marketCatalogSummary(raw,now),{ok:false,error:'market_data_not_collected'});
49
+});
50
+test('rejects duplicate symbols, no guessed units and malformed amounts',()=>{
51
+ const dup=catalog([item('USD-IRT'),item('USD-IRT')]);
52
+ const mismatch=catalog([{...item(),quote:'IRR'}]);
53
+ const negative=catalog([{...item(),sellPrice:-5}]);
54
+ const nan=catalog([{...item(),buyPrice:NaN}]);
55
+ for(const raw of [dup,mismatch,negative,nan])
56
+ assert.equal(marketCatalogSummary(raw,now).ok,false);
57
+});
58
+test('does not silently accept timestamps from the future',()=>{
59
+ assert.equal(marketCatalogSummary({...catalog(),fetchedAt:'2026-10-16T12:00:00Z'},now).ok,false);
60
+ assert.equal(marketCatalogSummary(catalog([{...item(),updatedAt:'2099-01-01T00:00:00Z'}]),now).ok,false);
61
+});
62
+test('rejects invalid, fabricated and oversized historical arrays',()=>{
63
+ for(const h of [['oops'],[null],[Infinity],Array.from({length:169},()=>100)])
64
+ assert.equal(marketCatalogSummary(catalog([{...item(),history:h}]),now).ok,false);
65
+});
66
+test('allows missing historical series only when provider explicitly reports zero points',()=>{
67
+ const full=catalog([{...item(),history:[]}]);
68
+ const s=marketCatalogSummary(full,now);
69
+ assert.equal(s.ok,true);
70
+ assert.equal(s.markets[0].historyCount,0);
71
+});
72
+test('rejects oversized catalog and invalid clock',()=>{
73
+ const big=catalog(Array.from({length:201},(_,i)=>item('S'+String(i+1).padStart(3,'0')+'-IRT')));
74
+ assert.equal(marketCatalogSummary(big,now).ok,false);
75
+ assert.equal(marketCatalogSummary(catalog(),NaN).ok,false);
76
+});