Intelligence, Advanced Charting & Trading Platform
test: define Alpha Vantage XAU adapter contract
davoodmehraban89 committed
Oct 5, 2026 at 16:26 UTC
a63e287e70e686e4bbce14a2600c9faa669e07a1
1 file changed
+65
packages/market-domain/test/alpha-vantage-adapter.test.mjs
new
+65
@@ -0,0 +1,65 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import {
4
+ buildAlphaVantageGoldSpotUrl,
5
+ parseAlphaVantageGoldSpot,
6
+} from '../src/adapters/alpha-vantage.mjs';
7
+
8
+test('builds Alpha Vantage GOLD_SILVER_SPOT request without mutating key', () => {
9
+ const url = buildAlphaVantageGoldSpotUrl('secret-key');
10
+ assert.equal(
11
+ url,
12
+ 'https://www.alphavantage.co/query?function=GOLD_SILVER_SPOT&symbol=GOLD&apikey=secret-key',
13
+ );
14
+});
15
+
16
+test('parses Alpha Vantage gold spot payload into canonical XAU_USD observation', () => {
17
+ const result = parseAlphaVantageGoldSpot({
18
+ payload: {
19
+ price: '4001.25',
20
+ unit: 'USD per troy ounce',
21
+ timestamp: '2026-10-05T12:00:00Z',
22
+ },
23
+ receivedAt: new Date('2026-10-05T12:00:02Z'),
24
+ });
25
+
26
+ assert.equal(result.providerCode, 'alpha_vantage');
27
+ assert.equal(result.canonicalSymbol, 'XAU_USD');
28
+ assert.equal(result.price, 4001.25);
29
+ assert.equal(result.canonicalUnit, 'USD_PER_TROY_OUNCE');
30
+ assert.equal(result.providerTimestamp, '2026-10-05T12:00:00.000Z');
31
+});
32
+
33
+test('accepts equivalent documented unit spelling', () => {
34
+ const result = parseAlphaVantageGoldSpot({
35
+ payload: {
36
+ price: 3999.5,
37
+ unit: 'USD/troy ounce',
38
+ timestamp: '2026-10-05T12:00:00Z',
39
+ },
40
+ receivedAt: new Date('2026-10-05T12:00:01Z'),
41
+ });
42
+ assert.equal(result.price, 3999.5);
43
+});
44
+
45
+test('rejects non-USD-per-troy-ounce units', () => {
46
+ assert.throws(() => parseAlphaVantageGoldSpot({
47
+ payload: {
48
+ price: '4000',
49
+ unit: 'USD per gram',
50
+ timestamp: '2026-10-05T12:00:00Z',
51
+ },
52
+ receivedAt: new Date('2026-10-05T12:00:01Z'),
53
+ }), /unit/i);
54
+});
55
+
56
+test('rejects provider timestamps after receive time', () => {
57
+ assert.throws(() => parseAlphaVantageGoldSpot({
58
+ payload: {
59
+ price: '4000',
60
+ unit: 'USD per troy ounce',
61
+ timestamp: '2026-10-05T12:00:03Z',
62
+ },
63
+ receivedAt: new Date('2026-10-05T12:00:01Z'),
64
+ }), /before providerTimestamp/i);
65
+});