Intelligence, Advanced Charting & Trading Platform

1 import test from "node:test";
2 import assert from "node:assert/strict";
3 import {preflightOhlcHistory} from "../src/licensed-ohlc-history.mjs";
4 // Synthetic values below are UNIT TEST FIXTURES ONLY; never ingest these prices.
5 const base={
6 providerCode:"test_provider",symbol:"USD_IRT_FREE",timeframe:"1d",
7 rawUnit:"IRR_PER_USD",canonicalUnit:"IRT_PER_USD",timeZone:"Asia/Tehran",
8 intendedUse:"public_api",from:"2025-10-01",to:"2026-10-11",receivedAt:"2026-10-11T20:00:00Z",
9 license:{providerCode:"test_provider",status:"approved",evidenceRef:"test/evidence/only",
10 reviewedBy:"fixture-reviewer",reviewedAt:"2025-10-01T10:00:00Z",
11 rights:{commercialStorage:true,publicRedistribution:true}}
12 };
13 const sample=(date,override={})=>({providerCode:"test_provider",symbol:"USD_IRT_FREE",dataKind:"source_ohlc",
14 sessionDate:date,openTime:date+"T00:00:00+03:30",closeTime:date+"T23:00:00+03:30",
15 open:"1200000.0",high:"1250000.0",low:"1190000.0",close:"1210000.0",...override});
16 const run=(rows,other={})=>preflightOhlcHistory({...base,rows,...other});
17 test("approved commercially distributable OHLC normalizes IRR to IRT without invented volume",()=>{
18 const r=run([sample("2025-10-13"),sample("2025-10-14")],{isSessionDay:()=>true});
19 assert.deepEqual(r.candles.map(x=>[x.open,x.high,x.low,x.close,x.volume]),[
20 ["120000","125000","119000","121000",null],
21 ["120000","125000","119000","121000",null]]);
22 assert.equal(r.candles[0].unit,"IRT_PER_USD");
23 assert.equal(r.audit.gapsFilled,0);assert.equal(r.audit.annualCoverageMet,false);
24 assert.equal(r.audit.readyForAnnualPublication,false);
25 });
26 test("no license, unpublished or unauthorized commercial reuse fails closed",()=>{
27 for(const license of [null,{...base.license,status:"pending"},{...base.license,rights:{commercialStorage:true,publicRedistribution:false}},{...base.license,rights:{commercialStorage:false,publicRedistribution:true}}]){
28 assert.throws(()=>run([sample("2025-10-13")],{license}),/Licensed|authorized/);
29 }
30 });
31 test("internal-only license can stage but cannot publish",()=>{
32 const license={...base.license,rights:{commercialStorage:true,publicRedistribution:false}};
33 const result=run([sample("2025-10-13")],{license,intendedUse:"internal_storage"});
34 assert.equal(result.candles.length,1);
35 assert.equal(result.intendedUse,"internal_storage");
36 });
37 test("expired license rejected, future review rejected",()=>{
38 assert.throws(()=>run([],{license:{...base.license,expiresAt:"2026-10-01T00:00:00Z"}}),/Expired/);
39 assert.throws(()=>run([],{license:{...base.license,reviewedAt:"2027-10-01T00:00:00Z"}}),/Future/);
40 });
41 test("cannot relabel daily reference observation as a traded candle",()=>{
42 assert.throws(()=>run([sample("2025-10-13",{dataKind:"daily_reference_point_not_ohlc"})]),/Reject reference/);
43 assert.throws(()=>run([sample("2025-10-13",{symbol:"EUR_USD_ECB"})]),/mismatched/);
44 });
45 test("OHLC ordering, time zone, future intervals and naive times are rejected",()=>{
46 assert.throws(()=>run([sample("2025-10-13",{low:"1260000"})]),/OHLC ordering/);
47 assert.throws(()=>run([sample("2025-10-13",{openTime:"2025-10-12T19:00:00Z"})]),/Timezone\/session mismatch/);
48 assert.throws(()=>run([sample("2025-10-13",{openTime:"2025-10-13T00:00:00"})]),/timezone offset/);
49 assert.throws(()=>run([sample("2025-10-13",{closeTime:"2027-10-13T23:00:00+03:30"})]),/future bar/);
50 });
51 test("deduplicates exact OHLC and rejects conflicting provider revision",()=>{
52 const row=sample("2025-10-13");
53 const result=run([row,{...row,sourceRecordId:"different-audit-row"}]);
54 assert.equal(result.candles.length,1);assert.equal(result.audit.duplicates,1);
55 assert.throws(()=>run([row,{...row,close:"1230000"}]),/Conflicting duplicate/);
56 });
57 test("sorts shuffled provider records but records arrival-order defect",()=>{
58 const result=run([sample("2025-10-15"),sample("2025-10-13"),sample("2025-10-14")]);
59 assert.deepEqual(result.candles.map(x=>x.sessionDate),["2025-10-13","2025-10-14","2025-10-15"]);
60 assert.equal(result.audit.outOfOrder,1);
61 });
62 test("does not manufacture missing sessions and distinguishes calendar availability",()=>{
63 const a=sample("2025-10-13"),b=sample("2025-10-16");
64 const unknown=run([a,b]);
65 assert.equal(unknown.audit.maxCalendarGapDays,3);
66 assert.equal(unknown.audit.calendarVerified,false);
67 const known=run([a,b],{isSessionDay:()=>true});
68 assert.equal(known.audit.missingExpectedSessions,2);
69 assert.equal(known.audit.gapsFilled,0);
70 assert.equal(known.candles.length,2);
71 });
72 test("full-year coverage is not implied by 254 observations unless dates and calendar support it",()=>{
73 const two=run([sample("2025-10-13"),sample("2026-10-09")],{isSessionDay:()=>false});
74 assert.equal(two.audit.coverageDays,362);
75 assert.equal(two.audit.annualCoverageMet,false);
76 assert.equal(two.audit.readyForAnnualPublication,false);
77 });
78 test("rejects undocumented unit pairs and excessive precision before persistence",()=>{
79 assert.throws(()=>run([],{canonicalUnit:"USD_PER_BTC"}),/Undocumented/);
80 assert.throws(()=>run([sample("2025-10-13",{close:"1210000.1234567890"})]),/precision/);
81 assert.equal(run([sample("2025-10-13",{volume:"0"})]).candles[0].volume,"0");
82 });