Relocate the existing Astro website and preserve its assets and hosting configuration. Add the consumer/backend scaffold and themed Flutter package with consumer tests. Related to #1.
46 lines
1.4 KiB
JavaScript
46 lines
1.4 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { calculateQuote, parseAmount } from '../src/lib/quote.ts';
|
|
test('demo calculation deducts the example fee in currency cents', () => {
|
|
assert.deepEqual(calculateQuote(1, 'BTC', 'EUR'), {
|
|
rate: 57500,
|
|
fee: 575,
|
|
receive: 56925,
|
|
demo: true,
|
|
});
|
|
assert.deepEqual(calculateQuote(2, 'ETH', 'USD'), {
|
|
rate: 2500,
|
|
fee: 50,
|
|
receive: 4950,
|
|
demo: true,
|
|
});
|
|
const q = calculateQuote(123.45, 'USDT', 'EUR');
|
|
assert.equal(q.fee, 1.14);
|
|
assert.equal(q.receive, 112.43);
|
|
});
|
|
test('accepts decimal comma and eight crypto decimal places', () => {
|
|
assert.equal(parseAmount(' 0,25 '), 0.25);
|
|
assert.equal(parseAmount('0.00000001'), 0.00000001);
|
|
assert.equal(
|
|
calculateQuote(parseAmount('0,25'), 'BTC', 'EUR').receive,
|
|
14231.25,
|
|
);
|
|
});
|
|
test('rejects ambiguous, malformed, excessive or invalid input', () => {
|
|
for (const value of [
|
|
'',
|
|
'-1',
|
|
'1e3',
|
|
'1,2,3',
|
|
'1.2.3',
|
|
'<script>',
|
|
'0.000000001',
|
|
'1 000',
|
|
])
|
|
assert.ok(Number.isNaN(parseAmount(value)));
|
|
for (const amount of [0, -1, NaN, Infinity, 1000001])
|
|
assert.throws(() => calculateQuote(amount, 'BTC', 'EUR'), RangeError);
|
|
assert.throws(() => calculateQuote(1, 'BOGUS', 'EUR'), RangeError);
|
|
assert.throws(() => calculateQuote(1, 'BTC', 'BOGUS'), RangeError);
|
|
});
|