@davoodmehraban89 / VELYQOR / commits / e8625eb
Fork 0

Intelligence, Advanced Charting & Trading Platform

test: define provider normalization and health contracts

davoodmehraban89 committed Oct 5, 2026 at 16:17 UTC e8625eba3192cfdf08c61131452aa6cf08e67739
1 file changed +49
packages/market-domain/test/provider-contract.test.mjs new
+49
@@ -0,0 +1,49 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import {
4 + normalizeIranianQuote,
5 + canonicalObservation,
6 + providerHealth,
7 +} from '../src/provider-contract.mjs';
8 +
9 +test('normalizes provider rial values to canonical toman', () => {
10 + assert.equal(normalizeIranianQuote({ value: 1_000_000, unit: 'IRR_PER_USD' }), 100_000);
11 +});
12 +
13 +test('keeps canonical toman values unchanged', () => {
14 + assert.equal(normalizeIranianQuote({ value: 100_000, unit: 'IRT_PER_USD' }), 100_000);
15 +});
16 +
17 +test('rejects unsupported units instead of guessing', () => {
18 + assert.throws(() => normalizeIranianQuote({ value: 100, unit: 'UNKNOWN' }), /unsupported unit/i);
19 +});
20 +
21 +test('creates an auditable canonical observation with distinct source and receive timestamps', () => {
22 + const observation = canonicalObservation({
23 + providerCode: 'example',
24 + canonicalSymbol: 'USD_IRT_FREE',
25 + providerTimestamp: new Date('2026-10-05T12:00:00Z'),
26 + receivedAt: new Date('2026-10-05T12:00:02Z'),
27 + price: 100_000,
28 + rawUnit: 'IRT_PER_USD',
29 + });
30 + assert.deepEqual(observation, {
31 + providerCode: 'example',
32 + canonicalSymbol: 'USD_IRT_FREE',
33 + providerTimestamp: '2026-10-05T12:00:00.000Z',
34 + receivedAt: '2026-10-05T12:00:02.000Z',
35 + price: 100_000,
36 + canonicalUnit: 'IRT_PER_USD',
37 + rawUnit: 'IRT_PER_USD',
38 + });
39 +});
40 +
41 +test('marks provider stale when last successful observation exceeds threshold', () => {
42 + const result = providerHealth({
43 + now: new Date('2026-10-05T12:05:00Z'),
44 + lastSuccessAt: new Date('2026-10-05T12:00:00Z'),
45 + staleAfterMs: 60_000,
46 + });
47 + assert.equal(result.status, 'STALE');
48 + assert.equal(result.ageMs, 300_000);
49 +});