Intelligence, Advanced Charting & Trading Platform

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 });