/** * Phone normalisation, ported from `crates/cotton-rules/src/phone_tests.rs` * and the `normalize_phone` half of `crates/cotton-core/src/import/phone_tests.rs` * (with its `vectors/import.json` `phone` table), run against the rules module: * the Worker reads a number with cotton-rules' own `phonenumber` crate now. */ import { describe, expect, it } from "vitest"; import vectors from "../../../crates/cotton-rules/vectors/import.json"; import { normalizePhone, normalizePhoneIn, regionOf, stripPresentation } from "../src/import/phone.ts"; describe("cotton-store protocol::phone", () => { it("a number with its country code is E.164 without the trunk zero", () => { for (const [raw, want] of [ ["+91 98765 43210", "919876543210"], ["+91 098765 43210", "919876543210"], ["0091 98765 43210", "919876543210"], ["+44 20 7946 0958", "442079460958"], ["+44 (0)20 7946 0958", "442079460958"], ["+1 (202) 555-0143", "12025550143"], [" +1.202.555.0143 ", "12025550143"], ]) { expect(normalizePhone(raw!), raw).toBe(want); } }); it("a number without a country code stays as typed", () => { expect(normalizePhone("098765 43210")).toBe("09876543210"); expect(normalizePhone("919876543210")).toBe("919876543210"); expect(normalizePhone("020 7946 0958")).toBe("02079460958"); }); it("what is not a number is null", () => { for (const raw of ["", "100", "x12", "++919876543210", "+91 98765 4321O", "1234567890123456"]) expect(normalizePhone(raw), raw).toBeNull(); }); /** The normaliser before libphonenumber: presentation out, one leading `+` off, 8–15 digits. */ const before = (raw: string): string | null => { const stripped = stripPresentation(raw); const digits = stripped.startsWith("+") ? stripped.slice(1) : stripped; return /^[0-9]{8,15}$/.test(digits) ? digits : null; }; const SPELLINGS: string[][] = [ ["+919876543210", "+91 98765 43210", "+91-98765-43210", "+91 (98765) 43210", "919876543210", "+91 098765 43210", "0091 98765 43210", "+91 0 98765 43210", "00 91 98765 43210"], ["+44 20 7946 0958", "+44 (0)20 7946 0958", "+44 020 7946 0958", "0044 20 7946 0958", "442079460958", "+44.20.7946.0958", "00 44 (0) 20 7946 0958"], ["+1 202 555 0143", "+1 (202) 555-0143", "12025550143", "001 202 555 0143", "+1.202.555.0143"], ["+49 30 12345678", "+49 (0)30 12345678", "0049 30 12345678", "+49 030 12345678"], ["+61 2 9876 5432", "+61 (0)2 9876 5432", "0061 2 9876 5432", "61298765432"], ["+33 1 23 45 67 89", "+33 (0)1 23 45 67 89", "0033 1 23 45 67 89", "33123456789"], ["+44 7911 123456", "+44 (0) 7911 123456", "447911123456", "+44 07911 123456"], ["098765 43210", "09876543210", "0987-654-3210"], ]; it("every spelling of a number matches and nothing that matched stops", () => { const all = SPELLINGS.flatMap((spellings, group) => spellings.map((s) => [group, s] as const)); expect(all.length).toBeGreaterThanOrEqual(40); for (const [groupA, a] of all) { const nowA = normalizePhone(a); expect(nowA, `${a} is a number`).not.toBeNull(); for (const [groupB, b] of all) { const nowB = normalizePhone(b); if (before(a) !== null && before(a) === before(b)) expect(nowA, `${a} and ${b} matched before`).toBe(nowB); expect(groupA === groupB, `${a} (${nowA}) and ${b} (${nowB})`).toBe(nowA === nowB); } } }); it("a national number stored without its code matches neither before nor now", () => { const incoming = "+91 98765 43210"; for (const stored of ["09876543210", "9876543210"]) { expect(normalizePhone(incoming)).not.toBe(normalizePhone(stored)); expect(before(incoming)).not.toBe(before(stored)); expect(normalizePhone(stored)).toBe(stored); } }); // normalize_phone_in / region_of carry no Rust test of their own; these pin // the Rust's reading (the coordinator's row 4: a code-less number in the owner's country). it("reads a code-less number in the owner's country, and never one written with a code", () => { expect(regionOf("919876543210")).toBe("IN"); expect(normalizePhoneIn("098765 43210", "IN")).toBe("919876543210"); expect(normalizePhoneIn("98765 43210", "IN")).toBe("919876543210"); expect(normalizePhoneIn("+44 20 7946 0958", "IN")).toBe("442079460958"); expect(normalizePhoneIn("098765 43210", null)).toBe("09876543210"); }); }); describe("cotton-core import::phone normalize", () => { it("normalizes every spelling of the +91 form", () => { for (const input of ["+919876543210", "+91 98765 43210", "+91-98765-43210", "+91 (98765) 43210", "+91 098765 43210", "0091 98765 43210", "919876543210"]) expect(normalizePhone(input), input).toBe("919876543210"); }); it("does not infer a country code: a zero-prefixed local number keeps its zero", () => { expect(normalizePhone("09876543210")).toBe("09876543210"); expect(normalizePhone("098765 43210")).toBe("09876543210"); expect(normalizePhone("9876543210")).toBe("9876543210"); }); it("rejects anything that is not eight to fifteen digits", () => { for (const input of ["12345", "1234567890123456", "not a phone", ""]) expect(normalizePhone(input), input).toBeNull(); }); it("a non-ASCII digit is not a digit", () => { expect(normalizePhone("٩٨٧٦٥٤٣٢")).toBeNull(); }); it("the promotion rules are arithmetic, not telephony (the normalize half)", () => { expect(normalizePhone("0091 98765 43210")).toBe("919876543210"); expect(normalizePhone("9112")).toBeNull(); }); it("every normalizePhone reading matches the vector table", () => { const cases = vectors.phone as { input: string; normalizePhone: string | null; promote: { cc: string | null; promoted: string; canonical: string | null }[] }[]; expect(cases.length).toBeGreaterThanOrEqual(19); for (const c of cases) { expect(normalizePhone(c.input), `normalizePhone(${JSON.stringify(c.input)})`).toBe(c.normalizePhone); // The table's canonical column is normalize_phone over the promoted spelling. for (const p of c.promote) expect(normalizePhone(p.promoted), `canonical(${JSON.stringify(c.input)}, ${JSON.stringify(p.cc)})`).toBe(p.canonical); } }); });