Cotton code
232 lines · 7.8 KB · RustRaw
//! A vector table (`cotton-rules/vectors/import.json`, key `phone`) of 19 inputs × 5//! country codes.//!//! The TypeScript's first case is a byte-equality guard: it reads//! `packages/core/src/models/message/constants.ts` and asserts that three//! literal lines appear in BOTH it and `src/phone.ts`. That guard belongs to//! the TypeScript lane — it proves two TypeScript files did not drift — and it//! has no Rust analogue, because the Rust is not the file it reads. What the//! Rust owes instead is a proof that its BEHAVIOUR matches, and that is the//! vector table: every reading below came out of Node.use super::*;use serde::Deserialize;#[derive(Deserialize)]#[serde(rename_all = "camelCase")]struct Promotion {    cc: Option<String>,    promoted: String,    canonical: Option<String>,}#[derive(Deserialize)]#[serde(rename_all = "camelCase")]struct PhoneVector {    input: String,    normalize_phone: Option<String>,    sender_looks_like_phone: bool,    looks_national_only: bool,    promote: Vec<Promotion>,}#[derive(Deserialize)]struct Vectors {    phone: Vec<PhoneVector>,}fn vectors() -> Vec<PhoneVector> {    let parsed: Vectors =        serde_json::from_str(include_str!("../../../cotton-rules/vectors/import.json"))            .expect("cotton-rules/vectors/import.json");    parsed.phone}// ── normalizePhone ───────────────────────────────────────────────────────────#[test]fn normalizes_every_spelling_of_the_plus_91_form() {    for input in [        "+919876543210",        "+91 98765 43210",        "+91-98765-43210",        "+91 (98765) 43210",        "+91 098765 43210",        "0091 98765 43210",        "919876543210",    ] {        assert_eq!(normalize_phone(input).as_deref(), Some("919876543210"));    }}/// Deliberate parity with the platform's normalizer: guessing a country code/// would file messages under a person who does not exist. The CLI warns/// instead.#[test]fn does_not_infer_a_country_code_a_zero_prefixed_local_number_keeps_its_zero() {    assert_eq!(        normalize_phone("09876543210").as_deref(),        Some("09876543210")    );    assert_eq!(        normalize_phone("098765 43210").as_deref(),        Some("09876543210")    );    assert_eq!(normalize_phone("9876543210").as_deref(), Some("9876543210"));}#[test]fn rejects_anything_that_is_not_eight_to_fifteen_digits() {    for input in ["12345", "1234567890123456", "not a phone", ""] {        assert_eq!(normalize_phone(input), None, "{input:?}");    }}/// `\d` in a JavaScript regex without the `u` flag is `[0-9]`, so an/// Arabic-Indic numeral is not a phone number on either side.#[test]fn a_non_ascii_digit_is_not_a_digit() {    assert_eq!(normalize_phone("٩٨٧٦٥٤٣٢"), None);}// ── senderLooksLikePhone ─────────────────────────────────────────────────────#[test]fn recognises_an_unsaved_contact_written_as_a_number() {    assert!(sender_looks_like_phone("+91 98765 43210"));    assert!(sender_looks_like_phone("919876543210"));}#[test]fn does_not_mistake_a_saved_contact_name_for_a_number() {    assert!(!sender_looks_like_phone("Anita Rao"));    assert!(!sender_looks_like_phone("Mum"));}// ── promoteToCountry ─────────────────────────────────────────────────────────#[test]fn is_a_no_op_without_a_country_code() {    assert_eq!(promote_to_country("09876543210", None), "09876543210");    // The TypeScript's guard is `if (!countryCode)`, and the empty string is    // falsy there — so `""` is "no country code", not "prepend nothing".    assert_eq!(promote_to_country("09876543210", Some("")), "09876543210");}#[test]fn strips_a_trunk_zero_and_prepends_the_country_code() {    assert_eq!(        normalize_phone(&promote_to_country("09876543210", Some("91"))).as_deref(),        Some("919876543210")    );}#[test]fn prepends_to_a_bare_national_number() {    assert_eq!(        normalize_phone(&promote_to_country("9876543210", Some("91"))).as_deref(),        Some("919876543210")    );}#[test]fn leaves_an_already_international_number_alone() {    assert_eq!(        normalize_phone(&promote_to_country("+919876543210", Some("91"))).as_deref(),        Some("919876543210")    );    assert_eq!(        normalize_phone(&promote_to_country("919876543210", Some("91"))).as_deref(),        Some("919876543210")    );}#[test]fn accepts_the_country_code_with_or_without_a_leading_plus() {    assert_eq!(        normalize_phone(&promote_to_country("09876543210", Some("+91"))).as_deref(),        Some("919876543210")    );}// ── looksNationalOnly ────────────────────────────────────────────────────────#[test]fn flags_exactly_the_numbers_that_will_not_match_an_app_sign_in() {    assert!(looks_national_only("09876543210"));    assert!(looks_national_only("9876543210"));    assert!(!looks_national_only("919876543210"));}// ── the Node vector table ────────────────────────────────────────────────────#[test]fn every_reading_matches_the_typescript() {    let cases = vectors();    assert!(cases.len() >= 19, "the table should not shrink");    for case in &cases {        assert_eq!(            normalize_phone(&case.input),            case.normalize_phone,            "normalizePhone({:?})",            case.input        );        assert_eq!(            sender_looks_like_phone(&case.input),            case.sender_looks_like_phone,            "senderLooksLikePhone({:?})",            case.input        );        assert_eq!(            looks_national_only(&case.input),            case.looks_national_only,            "looksNationalOnly({:?})",            case.input        );        for promotion in &case.promote {            let cc = promotion.cc.as_deref();            assert_eq!(                promote_to_country(&case.input, cc),                promotion.promoted,                "promoteToCountry({:?}, {:?})",                case.input,                cc            );            assert_eq!(                normalize_phone(&promote_to_country(&case.input, cc)),                promotion.canonical,                "canonical({:?}, {:?})",                case.input,                cc            );        }    }}/// `"0091 98765 43210"` is the international prefix and the country code, so/// it is the same number as `+91 98765 43210`; promoted with `91` it becomes/// `91919876543210`, a number belonging to nobody — which is exactly why/// promotion is opt-in and the report shouts.#[test]fn the_promotion_rules_are_arithmetic_not_telephony() {    assert_eq!(        normalize_phone("0091 98765 43210").as_deref(),        Some("919876543210")    );    assert_eq!(        promote_to_country("0091 98765 43210", Some("91")),        "91919876543210"    );    // A number too short to be a phone is still promoted — and still refused    // by the canonicaliser afterwards.    assert_eq!(promote_to_country("12", Some("91")), "9112");    assert_eq!(normalize_phone("9112"), None);}#[test]fn a_number_reads_grouped_the_way_its_country_writes_it() {    assert_eq!(phone_words("+44 20 7946 0958").as_deref(), Some("+44 20 7946 0958"));    assert_eq!(phone_words("919876543210").as_deref(), Some("+91 98765 43210"));    // No country code: said as written by the caller.    assert_eq!(phone_words("098765 43210"), None);}