Files
AtreidesAssetsFree/tests/price/fiat-herkomst.test.mjs
T
2026-10-29 11:57:29 +01:00

68 lines
3.5 KiB
JavaScript

// Generated, do not edit. sha256:332bac6d5d6fd8fc6489b8ba93b57adc4f35577ee5444684f9169fb9444c227e
import { describe, it, beforeEach, mock } from 'node:test';
import assert from 'node:assert/strict';
import { createDbHarness } from '../helpers/db-node.mjs';
import { rekenvalutaUitDatabase } from '../helpers/rekenvaluta.mjs';
const harness = createDbHarness();
mock.module('../../server/db.js', { namedExports: harness.exports });
for (const mod of ['providers', 'price-source', 'price-history', 'api-metrics', 'assets', 'tx-links']) {
mock.module(`../../client/data/${mod}.js`, { namedExports: await import(`../../server/data/${mod}.js`) });
}
await rekenvalutaUitDatabase(mock);
const facade = await import('../../client/price/providers.js');
const fiat = await import('../../client/ui/fiat.js');
let fetchCalls = 0;
globalThis.fetch = async () => { fetchCalls++; return { ok: true, status: 200, json: async () => ({ price: 95000 }) }; };
const { run, lastId, one } = harness.exports;
let btc, bron;
beforeEach(() => {
harness.recreate();
facade.clearSpotCache();
fetchCalls = 0;
run(`INSERT INTO Asset (symbol, name, searchName, kind, unit, subunit, decimals, displayUnit)
VALUES ('BTC','Bitcoin','bitcoin','crypto','BTC','sat',8,'unit')`);
btc = lastId();
run(`INSERT INTO Provider (key, name, searchName, baseUrl, authenticationKind, authenticationRequired,
isActive, isSeed, isSystem, createdAt)
VALUES ('bitvavo','Bitvavo','bitvavo','https://voorbeeld.test','none',0,1,1,0,'2026-01-01T00:00:00Z')`);
bron = lastId();
run(`INSERT INTO ProviderCoverage (providerId, symbol) VALUES (?, 'BTC')`, [bron]);
for (const cap of ['spot', 'daily_history']) {
run(`INSERT INTO ProviderEndpoint (providerId, capability, method, pathTemplate) VALUES (?, ?, 'GET', '/x')`, [bron, cap]);
run(`INSERT INTO PriceSource (assetId, capability, providerId, rank, status)
VALUES (?, ?, ?, 1, 'confirmed')`, [btc, cap === 'spot' ? 'spot' : 'history', bron]);
}
});
const asset = () => one(`SELECT * FROM Asset WHERE id = ?`, [btc]);
describe('fiatHistoricalOrigin: de dagkoers onder een verplaatsing', () => {
it('beschrijft de rij waarmee de waarde gerekend is, met zijn bron', async () => {
run(`INSERT INTO PriceHistory (assetId, providerId, fiatPrice, resolution, observedAt, createdAt)
VALUES (?, ?, 80000, 'day', '2026-03-03T00:00:00Z', '2026-03-04T00:00:00Z')`, [btc, bron]);
await fiat.loadHistorical([{ assetId: btc, dateKey: '2026-03-03' }]);
assert.notEqual(fiat.fiatHistorical(1e8, asset(), '2026-03-03'), '', 'er is een waarde');
const o = fiat.fiatHistoricalOrigin(asset(), '2026-03-03');
assert.deepEqual({ kind: o.kind, fiatPrice: o.fiatPrice, source: o.source, day: o.day }, { kind: 'close', fiatPrice: 80000, source: 'Bitvavo', day: '2026-03-03' });
assert.equal(fetchCalls, 0, 'de koers stond er al, dus er ging niets naar buiten');
});
it('heeft geen herkomst zonder koers, en ook niet voor de euro zelf', () => {
assert.equal(fiat.fiatHistoricalOrigin(asset(), '2020-01-01'), null);
assert.equal(fiat.fiatHistoricalOrigin({ id: 99, symbol: 'EUR' }, '2026-03-03'), null);
});
});
describe('fiatValueOrigin: de spot onder een bedrag op Tracking', () => {
it('is live en noemt de bron die de spot gaf', async () => {
await fiat.loadFiat([asset()]);
const o = fiat.fiatValueOrigin(asset());
assert.deepEqual({ kind: o.kind, fiatPrice: o.fiatPrice, source: o.source }, { kind: 'live', fiatPrice: 95000, source: 'Bitvavo' });
});
});