48 lines
4 KiB
JavaScript
48 lines
4 KiB
JavaScript
// node --test site/lib/money.test.mjs (the pre-push gate runs it beside the explorer and emission tests)
|
|
// site/money.js is the app's currency rules for the site: its tables must equal the app's, its formatter must place the
|
|
// symbol as the locale does, and the picker's choice must follow navigator.language with a saved override winning.
|
|
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { readFileSync } from 'node:fs';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { dirname, join } from 'node:path';
|
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
const load = (p) => { const mod = { exports: {} }; new Function('module', 'window', readFileSync(p, 'utf8'))(mod, undefined); return mod.exports; };
|
|
const M = load(join(here, '../money.js'));
|
|
const V = load(join(here, '../../app/igneum-app/ui/app.js')).View;
|
|
|
|
test('the site tables equal the app tables (regenerate with node site/lib/money-regen.mjs)', (t) => {
|
|
// the app side rides release-0.3.21 first; on a tree whose app has no ISO_CURRENCY yet the drift check waits for it
|
|
if (!V.ISO_CURRENCY) { t.skip('the app on this tree carries no currency tables yet (currency-21 app side not merged here)'); return; }
|
|
assert.deepEqual(M.REGIONS, V.REGIONS.map(r => ({ code: r.code, name: r.name, cur: r.cur, price: r.price, source: r.source || '', restricted: !!r.restricted })));
|
|
assert.deepEqual(M.ISO_CURRENCY, V.ISO_CURRENCY); assert.deepEqual(M.EU_MEMBERS, V.EU_MEMBERS); assert.deepEqual(M.CURRENCY_CODES, V.CURRENCY_CODES);
|
|
for (const c of ['GBP', 'EUR', 'JPY', 'CHF']) assert.deepEqual(M.minorOf(c), V.minorOf(c), c);
|
|
});
|
|
|
|
test('the picker follows navigator.language, a saved choice wins, no row falls back to USD and asks', () => {
|
|
assert.equal(M.currencyChoice('', 'de-DE').code, 'EUR'); assert.equal(M.currencyChoice('', 'en-GB').code, 'GBP'); assert.equal(M.currencyChoice('', 'fr-CA').code, 'CAD'); assert.equal(M.currencyChoice('', 'en-AU').code, 'AUD');
|
|
assert.equal(M.currencyChoice('USD', 'de-DE').code, 'USD'); assert.equal(M.currencyChoice('USD', 'de-DE').source, 'override');
|
|
const eo = M.currencyChoice('', 'eo'); assert.equal(eo.code, 'USD'); assert.equal(eo.prompt, true);
|
|
assert.equal(M.currencyChoice('zzz', 'eo').prompt, true, 'an unknown saved code is ignored');
|
|
});
|
|
|
|
test('the default tariff is the region row, else the first table row in the currency, and the cost line uses one formatter', () => {
|
|
const de = M.tariffFor(M.currencyChoice('', 'de-DE')); assert.equal(de.price, 38.69); assert.match(de.source, /Eurostat/); assert.equal(de.region, 'Germany');
|
|
const gbp = M.tariffFor(M.currencyChoice('GBP', 'de-DE')); assert.equal(gbp.price, 26.32); assert.equal(gbp.region, 'United Kingdom');
|
|
const fr = M.tariffFor(M.currencyChoice('', 'fr-FR')); assert.equal(fr.price, 28.96, 'an EU member on the euro takes the EU average');
|
|
assert.equal(M.tariffFor(M.currencyChoice('', 'sv-SE')), null, 'no table tariff in kronor: the picker says so');
|
|
assert.equal(M.fmtMoney(M.costPerDay(227, 38.69), 'EUR', 'de-DE'), '2,11 €');
|
|
assert.equal(M.fmtMoney(M.costPerDay(227, 26.32), 'GBP', 'en-GB'), '£1.43');
|
|
assert.equal(M.fmtMoney(M.costPerDay(227, 17.7), 'USD', 'en-US'), '$0.96');
|
|
assert.equal(M.costPerDay(null, 26.32), null, 'no watts, no cost');
|
|
assert.equal(M.fmtMoney(1234.5, 'USD', 'en-US'), '$1,235'); assert.equal(M.fmtMoney(1.5, 'JPY', 'en-US'), '¥2');
|
|
});
|
|
|
|
test('yourcard.js loads money.js and shows the electricity line with the currency switch; no hand-built money string', () => {
|
|
const yc = readFileSync(join(here, '../yourcard.js'), 'utf8');
|
|
assert.ok(yc.includes("'/money.js'"), 'loads the module when the page did not');
|
|
assert.ok(yc.includes('window.IgneumMoney') && yc.includes('.fmtMoney(') && yc.includes('.currencyChoice(') && yc.includes('.tariffFor('), 'the module is the only money path');
|
|
assert.ok(yc.includes('igneum-currency'), 'the saved choice');
|
|
assert.ok(yc.includes('class="cur"'), 'the switch');
|
|
assert.equal(/'£'|"£"/.test(yc), false);
|
|
});
|