@davoodmehraban89 / VELYQOR / commits / 41bffa3
Fork 0

Intelligence, Advanced Charting & Trading Platform

test: define ingestion and derived-gold pipeline behavior

davoodmehraban89 committed Oct 5, 2026 at 16:20 UTC 41bffa37044d6d9568204dd334d08c9611f3687c
1 file changed +70
packages/market-domain/test/pipeline.test.mjs new
+70
@@ -0,0 +1,70 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { normalizeProviderObservation, deriveGoldMetrics } from '../src/pipeline.mjs';
4 +
5 +test('normalizes an Iranian rial provider observation and preserves audit input', () => {
6 + const result = normalizeProviderObservation({
7 + providerCode: 'navasan',
8 + canonicalSymbol: 'USD_IRT_FREE',
9 + providerTimestamp: new Date('2026-10-05T12:00:00Z'),
10 + receivedAt: new Date('2026-10-05T12:00:02Z'),
11 + price: 1_000_000,
12 + rawUnit: 'IRR_PER_USD',
13 + rawPayload: { value: '1000000' },
14 + referencePrice: 100_100,
15 + maxDivergencePct: 1,
16 + maxAgeMs: 60_000,
17 + });
18 +
19 + assert.equal(result.tick.price, 100_000);
20 + assert.equal(result.tick.quality.state, 'GOOD');
21 + assert.equal(result.raw.price, 1_000_000);
22 + assert.equal(result.raw.rawUnit, 'IRR_PER_USD');
23 + assert.deepEqual(result.raw.rawPayload, { value: '1000000' });
24 +});
25 +
26 +test('derives theoretical gold and premium with complete source provenance', () => {
27 + const target = new Date('2026-10-05T12:00:00Z');
28 + const result = deriveGoldMetrics({
29 + target,
30 + xau: { value: 4_000, timestamp: new Date('2026-10-05T11:59:50Z') },
31 + usdIrt: { value: 100_000, timestamp: new Date('2026-10-05T11:59:45Z') },
32 + marketGold: { value: 10_000_000, timestamp: new Date('2026-10-05T11:59:40Z') },
33 + maxAgeMs: 60_000,
34 + formulaVersion: 'gold18-v1',
35 + });
36 +
37 + assert.equal(result.quality, 'GOOD');
38 + assert.equal(result.formulaVersion, 'gold18-v1');
39 + assert.equal(result.sourceSnapshot.targetTimestamp, '2026-10-05T12:00:00.000Z');
40 + assert.equal(result.sourceSnapshot.xau.timestamp, '2026-10-05T11:59:50.000Z');
41 + assert.equal(result.sourceSnapshot.usdIrt.timestamp, '2026-10-05T11:59:45.000Z');
42 + assert.equal(result.sourceSnapshot.marketGold.timestamp, '2026-10-05T11:59:40.000Z');
43 + assert.ok(result.theoreticalIrtPerGram > 0);
44 + assert.ok(Number.isFinite(result.premium.absoluteIrt));
45 + assert.ok(Number.isFinite(result.premium.percent));
46 +});
47 +
48 +test('marks derived gold metrics stale when any required source is stale', () => {
49 + const target = new Date('2026-10-05T12:00:00Z');
50 + const result = deriveGoldMetrics({
51 + target,
52 + xau: { value: 4_000, timestamp: new Date('2026-10-05T11:59:50Z') },
53 + usdIrt: { value: 100_000, timestamp: new Date('2026-10-05T11:55:00Z') },
54 + marketGold: { value: 10_000_000, timestamp: new Date('2026-10-05T11:59:40Z') },
55 + maxAgeMs: 60_000,
56 + formulaVersion: 'gold18-v1',
57 + });
58 + assert.equal(result.quality, 'STALE');
59 +});
60 +
61 +test('rejects a market-gold observation from the future', () => {
62 + assert.throws(() => deriveGoldMetrics({
63 + target: new Date('2026-10-05T12:00:00Z'),
64 + xau: { value: 4_000, timestamp: new Date('2026-10-05T11:59:50Z') },
65 + usdIrt: { value: 100_000, timestamp: new Date('2026-10-05T11:59:45Z') },
66 + marketGold: { value: 10_000_000, timestamp: new Date('2026-10-05T12:00:01Z') },
67 + maxAgeMs: 60_000,
68 + formulaVersion: 'gold18-v1',
69 + }), /after target/);
70 +});