Intelligence, Advanced Charting & Trading Platform
phase-1/gold-api-fallback
diagnostics/chartnova-blank-screen-2026-10-15
diagnostics/chartnova-stage2-v27-preview-2026-10-15
docs/chartnova-stage1-reconcile-manifest-2026-10-15
feature/chartnova-ios-widget
feature/chartnova-light-catalog-lazy-series-2026-10-15
feature/chartnova-market-terminal
feature/chartnova-multimarket-chart-ws-foundation
feature/chartnova-neon-direct-history
feature/chartnova-neon-history
feature/paper-trading-risk-controls-2026-10-14
fix/alpha-vantage-live-payload
fix/chartnova-progressive-loading-2026-10-15
fix/live-proof-runner-cwd-2026-10-14
foundation/phase-0
foundation/postgres-core
main
ops/chartnova-emergency-rollback-2026-10-15
phase-1/candle-builder
phase-1/data-proof-artifact
phase-1/data-proof-metrics
phase-1/data-proof-report
phase-1/external-gates-state
phase-1/gold-api-fallback
phase-1/ingestion-pipeline
phase-1/live-proof-runner
phase-1/market-calendar
phase-1/market-domain
phase-1/neon-live-proof
phase-1/persistence-contract
phase-1/postgres-ci
phase-1/provider-adapters
phase-1/provider-recovery
phase-1/quality-propagation
phase-1/spike-detection
phase-1/xau-provider-adapter
risk/chartnova-alert-runtime-atomic-2026-10-22
risk/chartnova-atomic-outbox-2026-10-22
risk/chartnova-seven-track-safety-2026-10-21
safety/p0-triage-2026-10-13
| 1 | import test from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { theoreticalGold18Irt, goldPremium, alignSources } from '../src/gold-pricing.mjs'; |
| 4 | |
| 5 | test('calculates theoretical 18K gold in IRT per gram', () => { |
| 6 | const result = theoreticalGold18Irt({ xauUsdPerTroyOunce: 4000, usdIrt: 100000 }); |
| 7 | const expected = (4000 / 31.1034768) * 0.75 * 100000; |
| 8 | assert.ok(Math.abs(result - expected) < 1e-8); |
| 9 | }); |
| 10 | |
| 11 | test('calculates absolute and percentage premium', () => { |
| 12 | const result = goldPremium({ marketIrtPerGram: 10_500_000, theoreticalIrtPerGram: 10_000_000 }); |
| 13 | assert.equal(result.absoluteIrt, 500_000); |
| 14 | assert.ok(Math.abs(result.percent - 5) < 1e-12); |
| 15 | }); |
| 16 | |
| 17 | test('rejects zero or negative market inputs', () => { |
| 18 | assert.throws(() => theoreticalGold18Irt({ xauUsdPerTroyOunce: 0, usdIrt: 100000 }), /positive/); |
| 19 | assert.throws(() => goldPremium({ marketIrtPerGram: -1, theoreticalIrtPerGram: 10 }), /positive/); |
| 20 | }); |
| 21 | |
| 22 | test('marks aligned sources stale when either source exceeds threshold', () => { |
| 23 | const target = new Date('2026-10-05T12:00:00Z'); |
| 24 | const aligned = alignSources({ |
| 25 | target, |
| 26 | xau: { value: 4000, timestamp: new Date('2026-10-05T11:59:40Z') }, |
| 27 | usdIrt: { value: 100000, timestamp: new Date('2026-10-05T11:57:00Z') }, |
| 28 | maxAgeMs: 60_000, |
| 29 | }); |
| 30 | assert.equal(aligned.quality, 'STALE'); |
| 31 | assert.equal(aligned.xauAgeMs, 20_000); |
| 32 | assert.equal(aligned.usdIrtAgeMs, 180_000); |
| 33 | }); |
| 34 | |
| 35 | test('rejects future source observations for a target timestamp', () => { |
| 36 | assert.throws(() => alignSources({ |
| 37 | target: new Date('2026-10-05T12:00:00Z'), |
| 38 | xau: { value: 4000, timestamp: new Date('2026-10-05T12:00:01Z') }, |
| 39 | usdIrt: { value: 100000, timestamp: new Date('2026-10-05T11:59:59Z') }, |
| 40 | maxAgeMs: 60_000, |
| 41 | }), /after target/); |
| 42 | }); |