Intelligence, Advanced Charting & Trading Platform
test(rest): verify bounded paginated history, licensing and strict source type separation
davoodmehraban89 committed
Oct 12, 2026 at 00:23 UTC
a208a4602f4fdf41639e77855f19c7c3b09cfbba
1 file changed
+71
packages/market-feed/test/history-api.test.mjs
new
+71
@@ -0,0 +1,71 @@
1
+import test from "node:test";
2
+import assert from "node:assert/strict";
3
+import {parseHistoryRequest,serveHistoryV2} from "../src/history-api.mjs";
4
+const root="https://app.chartnova.ir/api/v2/history";
5
+const req=(query,method="GET")=>new Request(root+query,{method});
6
+const common="?symbol=BTC_USD&from=2025-10-10&to=2026-10-10&dataKind=source_ohlc&timeframe=1h&limit=2";
7
+const row=(t)=>({
8
+ symbol:"BTC_USD",unit:"USD_PER_BTC",timeframe:"1h",openTime:t,closeTime:new Date(Date.parse(t)+3600000).toISOString(),
9
+ open:"100.0",high:"101.0",low:"99.0",close:"100.5",volume:"0",quality:"DEGRADED",secretProviderToken:"NOT_FOR_PUBLIC"
10
+});
11
+const response=(rows)=>({source:"binance",unit:"USD_PER_BTC",dataKind:"source_ohlc",rights:"approved_public",attribution:"Binance Exchange test fixture only",rows});
12
+test("date, symbol, kind, limit, unknown parameter and duplicate input validation",()=>{
13
+ const valid=parseHistoryRequest(new URL(root+common));assert.equal(valid.limit,2);
14
+ const malformed=[
15
+ "?symbol=BTC_USD&from=2025-10-10&to=2026-10-10&dataKind=source_ohlc&timeframe=1h&limit=6000",
16
+ common+"&symbol=ETH_USD",common+"&extra=SQL",common.replace("BTC_USD","../../root"),
17
+ common.replace("source_ohlc","made_up"),common.replace("from=2025-10-10","from=2020-01-01"),
18
+ common+"&cursor=garbage",common.replace("2025-10-10","2025-13-10")];
19
+ for(const q of malformed)assert.throws(()=>parseHistoryRequest(new URL(root+q)),undefined,q);
20
+});
21
+test("unconfigured provider-neutral history never claims live data",async()=>{
22
+ const r=await serveHistoryV2(req(common));
23
+ assert.equal(r.status,503);assert.equal((await r.json()).error,"storage_not_configured");
24
+});
25
+test("read-only repository port returns descending page in ascending presentation with cursor",async()=>{
26
+ const dates=["2025-10-12T01:00:00Z","2025-10-12T00:00:00Z","2025-10-11T23:00:00Z"];
27
+ let called;
28
+ const r=await serveHistoryV2(req(common),{lookup:async(query)=>{called=query;return response(dates.map(row))}});
29
+ assert.equal(r.status,200);
30
+ const json=await r.json();
31
+ assert.equal(json.series.length,2);assert.equal(json.page.hasMore,true);
32
+ assert.equal(json.page.nextCursor,dates[1]);
33
+ assert.equal(json.series[0].time,dates[1]);assert.equal(json.series[1].time,dates[0]);
34
+ assert.equal(json.series[0].volume,0);
35
+ assert(!JSON.stringify(json).includes("NOT_FOR_PUBLIC"));
36
+ assert.equal(called.symbol,"BTC_USD");assert.equal(called.limit,2);
37
+});
38
+test("unknown license rights or empty data rejected instead of silently rendered",async()=>{
39
+ const a=await serveHistoryV2(req(common),{lookup:async()=>({...response([row("2025-10-11T00:00:00Z")]),rights:"research_only"})});
40
+ assert.equal(a.status,503);
41
+ const b=await serveHistoryV2(req(common),{lookup:async()=>response([])});
42
+ assert.equal(b.status,404);
43
+});
44
+test("cross-symbol, reversed order, excessive rows and invalid OHLC rejected",async()=>{
45
+ const t="2025-10-12T00:00:00Z";
46
+ for(const rows of [
47
+ [{...row(t),symbol:"ETH_USD"}],
48
+ [row("2025-10-11T00:00:00Z"),row(t)],
49
+ [{...row(t),low:"105"}],
50
+ [row(t),row(t)],
51
+ [row(t),row(t),row(t),row(t)]]
52
+ ){
53
+ const result=await serveHistoryV2(req(common),{lookup:async()=>response(rows)});
54
+ assert.equal(result.status,502);
55
+ }
56
+});
57
+test("CoinGecko-like point data cannot acquire OHLC fields",async()=>{
58
+ const q="?symbol=BTC_USD&from=2025-10-10&to=2026-10-10&dataKind=reference_point&timeframe=1d";
59
+ const ref={symbol:"BTC_USD",unit:"USD_PER_BTC",sourceTimestamp:"2025-10-10T00:00:00Z",price:120,quality:"DEGRADED"};
60
+ const source={source:"coingecko",unit:"USD_PER_BTC",dataKind:"reference_point",rights:"approved_public",attribution:"research fixture",rows:[ref]};
61
+ const a=await serveHistoryV2(req(q),{lookup:async()=>source});
62
+ assert.equal(a.status,200);
63
+ const json=await a.json();assert.equal(json.series[0].price,120);
64
+ assert.equal(json.series[0].open,undefined);
65
+ const b=await serveHistoryV2(req(q),{lookup:async()=>({...source,rows:[{...ref,open:110}]})});
66
+ assert.equal(b.status,502);
67
+});
68
+test("method not allowed is explicit",async()=>{
69
+ const r=await serveHistoryV2(req(common,"POST"));
70
+ assert.equal(r.status,405);
71
+});