← back to Costa Rica
test/contacts.test.js
33 lines
'use strict';
// Zero-dependency tests (node:test) for E.164 phone normalization used by the
// contacts + account features. Costa Rica default (+506) for bare 8-digit locals.
// Run: node --test
const { test } = require('node:test');
const assert = require('node:assert');
const { normalizePhone } = require('../routes/app');
test('normalizePhone: bare 8-digit Costa Rica local gets +506', () => {
assert.equal(normalizePhone('8888 8888'), '+50688888888');
assert.equal(normalizePhone('2222-3333'), '+50622223333');
});
test('normalizePhone: 11-digit 506-prefixed number gets a leading +', () => {
assert.equal(normalizePhone('50688887777'), '+50688887777');
});
test('normalizePhone: an already-E.164 (+) number is preserved', () => {
assert.equal(normalizePhone('+50688887777'), '+50688887777');
assert.equal(normalizePhone('+1 (818) 373-4564'), '+18183734564');
});
test('normalizePhone: a 10+ digit foreign number gets a leading +', () => {
assert.equal(normalizePhone('18183734564'), '+18183734564');
});
test('normalizePhone: unparseable / too-short / empty input is null (no bogus number stored)', () => {
assert.equal(normalizePhone(''), null);
assert.equal(normalizePhone(null), null);
assert.equal(normalizePhone('123'), null); // too short, not CR-local length
assert.equal(normalizePhone('abc'), null); // no digits
});