62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { describe, it } from "node:test";
|
|
import { maskPhoneNumber, normalizePhoneNumber } from "../../src/util/smsPhoneNumber.js";
|
|
|
|
describe("normalizePhoneNumber", () => {
|
|
const accepted: [unknown, string][] = [
|
|
["+15555550123", "+15555550123"],
|
|
[" +1 555 555 0123 ", "+15555550123"],
|
|
["(555) 555-0123", "+15555550123"],
|
|
["555.555.0123", "+15555550123"],
|
|
["5555550123", "+15555550123"],
|
|
["15555550123", "+15555550123"],
|
|
["0015555550123", "+15555550123"],
|
|
["+442071838750", "+442071838750"],
|
|
["+81312345678", "+81312345678"],
|
|
];
|
|
|
|
for (const [input, expected] of accepted) {
|
|
it(`normalizes ${JSON.stringify(input)}`, () => {
|
|
assert.equal(normalizePhoneNumber(input), expected);
|
|
});
|
|
}
|
|
|
|
const rejected: unknown[] = [
|
|
undefined,
|
|
null,
|
|
42,
|
|
"",
|
|
" ",
|
|
"not a phone",
|
|
"555-0123",
|
|
"+0155555501",
|
|
"+1555555012345678",
|
|
"+1555",
|
|
"25555550123",
|
|
"+1555555o123",
|
|
"+",
|
|
];
|
|
|
|
for (const input of rejected) {
|
|
it(`rejects ${JSON.stringify(input)}`, () => {
|
|
assert.equal(normalizePhoneNumber(input), undefined);
|
|
});
|
|
}
|
|
});
|
|
|
|
describe("maskPhoneNumber", () => {
|
|
it("keeps the country code and the last two digits", () => {
|
|
assert.equal(maskPhoneNumber("+15555550123"), "+1555*****23");
|
|
});
|
|
|
|
it("masks a shorter international number without leaking the middle", () => {
|
|
const masked = maskPhoneNumber("+442071838750");
|
|
assert.equal(masked, "+4420******50");
|
|
assert.equal(masked.length, "+442071838750".length);
|
|
});
|
|
|
|
it("never returns more than the last two digits of a short number", () => {
|
|
assert.equal(maskPhoneNumber("+12345"), "****45");
|
|
});
|
|
});
|