Cotton code
56 lines · 2.9 KB · RustRaw
use super::*;use cotton_store::file_identity::Namespace;/// RULE: a social handle is one handle however it was pasted — `@name`, the profile URL and the bare name normalise alike, and a normalised handle normalises to itself.#[test]fn a_social_handle_is_one_handle_however_it_was_pasted() {    for raw in ["@Zara.Quill", "zara.quill", "https://www.instagram.com/zara.quill/", "instagram.com/Zara.Quill?igsh=abc", " @zara.quill "] {        assert_eq!(normalise_handle(HandleKind::Instagram, raw).as_deref(), Some("zara.quill"), "{raw}");    }    assert_eq!(normalise_handle(HandleKind::Linkedin, "https://www.linkedin.com/in/zara-quill-42/?trk=x").as_deref(), Some("zara-quill-42"));    assert_eq!(normalise_handle(HandleKind::Facebook, "https://facebook.com/profile.php?id=100012345").as_deref(), Some("100012345"));    assert_eq!(normalise_handle(HandleKind::Facebook, "fb.com/Zara.Quill").as_deref(), Some("zara.quill"));    for kind in [HandleKind::Instagram, HandleKind::Facebook, HandleKind::Linkedin] {        for raw in ["@Zara.Quill", "zara.quill", "Zara-Quill"] {            if let Some(once) = normalise_handle(kind, raw) {                assert_eq!(normalise_handle(kind, &once), Some(once.clone()), "{kind} {raw}");            }        }    }}/// RULE: a social handle that is not one is refused, never guessed — a guessed handle could link someone else's messages.#[test]fn a_social_handle_that_is_not_one_is_refused() {    for raw in ["", "   ", "@", "zara quill", "zara!quill", "https://instagram.com/", &"a".repeat(31)] {        assert_eq!(normalise_handle(HandleKind::Instagram, raw), None, "{raw:?}");    }}/// RULE: a phone is matched by the platform's digits, and a number that is not one (a short code) is kept as written, never lost and never matching a counterpart.#[test]fn a_phone_normalises_to_digits_and_a_short_code_is_kept() {    assert_eq!(normalise_handle(HandleKind::Phone, "+91 98765-43210").as_deref(), Some("919876543210"));    assert_eq!(normalise_handle(HandleKind::Phone, "100").as_deref(), Some("100"));    assert_eq!(normalise_handle(HandleKind::Email, " Zara@Example.TEST ").as_deref(), Some("zara@example.test"));    assert_eq!(normalise_handle(HandleKind::Phone, "  "), None);}/// RULE: a handle row's id is derived from (workspace, kind, normalised), so two devices adding one handle write one row.#[test]fn a_handle_row_is_derived_from_its_unique_key() {    let namespace = Namespace::for_tests("ns-handles");    let write = |person: &'static str, value: &'static str| HandleWrite {        namespace: &namespace,        workspace_id: "ws",        person_id: person,        kind: HandleKind::Instagram,        value,        normalised: "zara.quill",        source: HandleSource::Manual,        verified_at: None,        position: 0,        now: "t",    };    assert_eq!(handle_change(&write("p-1", "@Zara.Quill")).row_id, handle_change(&write("p-1", "zara.quill")).row_id);}