Intelligence, Advanced Charting & Trading Platform
Phase 1: add live provider proof runner
Phase 1: add live provider proof runner
Davood Mehraban committed
Oct 7, 2026 at 01:37 UTC
f2f11c0a9ea206f56db6d86a4cdb7a6b0f6be205
3 files changed
+21
-1
package.json
+2
-1
@@ -8,6 +8,7 @@
8
},
9
"scripts": {
10
"test": "node --test packages/market-domain/test/*.test.mjs",
11
- "verify": "pnpm --if-present lint && pnpm --if-present typecheck && pnpm --if-present test && pnpm --if-present build"
11
+ "verify": "pnpm --if-present lint && pnpm --if-present typecheck && pnpm --if-present test && pnpm --if-present build",
12
+ "phase1:live-proof": "node scripts/phase1-live-data-proof.mjs"
13
}
14
}
packages/market-domain/test/live-data-proof-runner.test.mjs
new
+2
@@ -0,0 +1,2 @@
1
+import test from 'node:test';import assert from 'node:assert/strict';import {spawnSync} from 'node:child_process';
2
+test('live proof runner fails closed when provider credentials are absent',()=>{const env={...process.env};delete env.NAVASAN_API_KEY;delete env.ALPHAVANTAGE_API_KEY;const run=spawnSync(process.execPath,['scripts/phase1-live-data-proof.mjs'],{cwd:process.cwd(),env,encoding:'utf8'});assert.notEqual(run.status,0);assert.match(run.stderr+run.stdout,/NAVASAN_API_KEY is required/)});
scripts/phase1-live-data-proof.mjs
new
+17
@@ -0,0 +1,17 @@
1
+import fs from 'node:fs/promises';
2
+import path from 'node:path';
3
+import { buildAlphaVantageGoldSpotUrl, parseAlphaVantageGoldSpot } from '../packages/market-domain/src/adapters/alpha-vantage.mjs';
4
+import { parseNavasanLatest } from '../packages/market-domain/src/adapters/navasan.mjs';
5
+import { buildDataProofReport } from '../packages/market-domain/src/data-proof-report.mjs';
6
+import { buildDataProofArtifact } from '../packages/market-domain/src/data-proof-artifact.mjs';
7
+const sleep=ms=>new Promise(r=>setTimeout(r,ms));
8
+const int=(n,d)=>{const v=process.env[n]==null?d:Number(process.env[n]);if(!Number.isInteger(v)||v<0)throw new RangeError(n+' must be a non-negative integer');return v};
9
+const required=n=>{const v=process.env[n]?.trim();if(!v)throw new Error(n+' is required');return v};
10
+const safeJson=async r=>{const t=await r.text();let p;try{p=JSON.parse(t)}catch{throw new Error('provider returned non-JSON HTTP '+r.status)}if(!r.ok)throw new Error('provider HTTP '+r.status);return p};
11
+const navasanUrl=k=>{const u=new URL('https://api.navasan.tech/latest/');u.searchParams.set('api_key',k);return u};
12
+const observations=new Map([['USD_IRT_FREE',[]],['GOLD18_IRT',[]],['XAU_USD',[]]]),providerErrors=[];
13
+const sample=async()=>{const nk=required('NAVASAN_API_KEY'),ak=required('ALPHAVANTAGE_API_KEY');try{const receivedAt=new Date(),p=await safeJson(await fetch(navasanUrl(nk),{signal:AbortSignal.timeout(15000)}));for(const x of parseNavasanLatest({payload:p,receivedAt}))observations.get(x.canonicalSymbol)?.push({timestamp:x.providerTimestamp,price:x.price})}catch(e){providerErrors.push({provider:'navasan',at:new Date().toISOString(),message:e.message})}try{const receivedAt=new Date(),p=await safeJson(await fetch(buildAlphaVantageGoldSpotUrl(ak),{signal:AbortSignal.timeout(15000)}));if(p.Note||p.Information||p['Error Message'])throw new Error(p.Note||p.Information||p['Error Message']);const x=parseAlphaVantageGoldSpot({payload:p,receivedAt});observations.get('XAU_USD').push({timestamp:x.providerTimestamp,price:x.price})}catch(e){providerErrors.push({provider:'alpha_vantage',at:new Date().toISOString(),message:e.message})}};
14
+const samples=Math.max(1,int('DATA_PROOF_SAMPLES',3)),intervalMs=int('DATA_PROOF_INTERVAL_MS',60000);for(let i=0;i<samples;i++){await sample();if(i+1<samples)await sleep(intervalMs)}
15
+const generatedAt=new Date(),configs={USD_IRT_FREE:{providerCode:'navasan',thresholds:{maxGapMs:14400000,maxFreshnessMs:14400000,maxDuplicateCount:Math.max(0,samples-1),maxOutOfOrderCount:0}},GOLD18_IRT:{providerCode:'navasan',thresholds:{maxGapMs:14400000,maxFreshnessMs:14400000,maxDuplicateCount:Math.max(0,samples-1),maxOutOfOrderCount:0}},XAU_USD:{providerCode:'alpha_vantage',thresholds:{maxGapMs:3600000,maxFreshnessMs:3600000,maxDuplicateCount:Math.max(0,samples-1),maxOutOfOrderCount:0}}},reports=[];
16
+for(const [symbol,c] of Object.entries(configs)){const items=observations.get(symbol);if(!items.length){reports.push({providerCode:c.providerCode,canonicalSymbol:symbol,checkedAt:generatedAt.toISOString(),status:'FAIL',failures:['NO_LIVE_OBSERVATIONS'],thresholds:c.thresholds,metrics:{count:0}});continue}reports.push(buildDataProofReport({providerCode:c.providerCode,canonicalSymbol:symbol,observations:items,checkedAt:generatedAt,thresholds:c.thresholds}))}
17
+const artifact=buildDataProofArtifact({generatedAt,reports}),output={...artifact,samplePlan:{samples,intervalMs},providerErrors},outPath=process.env.DATA_PROOF_OUTPUT||path.join('artifacts','phase1-live-data-proof.json');await fs.mkdir(path.dirname(outPath),{recursive:true});await fs.writeFile(outPath,JSON.stringify(output,null,2)+'\n','utf8');console.log('DATA_PROOF_STATUS='+artifact.status);for(const r of reports)console.log(r.canonicalSymbol+'='+r.status+';count='+(r.metrics.count??0)+';failures='+(r.failures.join(',')||'none'));console.log('PROVIDER_ERRORS='+providerErrors.length);console.log('ARTIFACT='+outPath);process.exitCode=artifact.status==='PASS'?0:2;