← back to Costa Rica
test/marketplace.test.js
53 lines
'use strict';
// Zero-dependency tests (node:test) for the marketplace money + auth core.
// Run: node --test
const { test } = require('node:test');
const assert = require('node:assert');
const { computeSplit, nights } = require('../lib/money');
const { signToken, verifyToken, hashPassword, verifyPassword } = require('../lib/auth');
test('nights counts calendar nights', () => {
assert.equal(nights('2026-09-10', '2026-09-13'), 3);
assert.equal(nights('2026-09-10', '2026-09-10'), 0);
});
test('computeSplit: 3 nights @ $120 + $40 cleaning, 10% fee', () => {
const s = computeSplit({ subtotal: 12000 * 3, cleaningFee: 4000, currency: 'USD', platformFeeBps: 1000 });
assert.equal(s.total, 40000); // 360 + 40 cleaning = 400.00
assert.equal(s.platformFee, 4000); // 10% of 400
assert.equal(s.hostPayout, 36000); // total - platformFee
assert.equal(s.hostPayout + s.platformFee, s.total); // conservation: no cents lost
});
test('computeSplit: host + platform always reconstruct total (no rounding drift)', () => {
for (const [sub, clean, bps] of [[3333, 777, 1000], [9999, 0, 1250], [10001, 501, 999]]) {
const s = computeSplit({ subtotal: sub, cleaningFee: clean, currency: 'CRC', platformFeeBps: bps });
assert.equal(s.hostPayout + s.platformFee + s.processorFee, s.total);
}
});
test('JWT round-trips and rejects tampering', () => {
const t = signToken({ sub: 42, role: 'traveler' });
const claims = verifyToken(t);
assert.equal(claims.sub, 42);
assert.equal(verifyToken(t.slice(0, -3) + 'xxx'), null); // bad signature
assert.equal(verifyToken('not.a.jwt'), null);
});
test('JWT honors expiry', () => {
const expired = signToken({ sub: 1 }, -10); // already expired
assert.equal(verifyToken(expired), null);
});
test('scrypt password hash verifies and rejects wrong password', () => {
const h = hashPassword('correct horse');
assert.ok(verifyPassword('correct horse', h));
assert.ok(!verifyPassword('wrong horse', h));
assert.ok(!verifyPassword('correct horse', 'garbage'));
});
test('payment provider is sandbox by default (no live creds)', () => {
const { getProvider } = require('../lib/payments');
assert.equal(getProvider().liveMode, false);
});