Cotton code
64 lines · 2.6 KB · RustRaw
use std::collections::BTreeMap;use super::*;use crate::messages::resolve_contact::MatchedPerson;fn member(name: &str, handles: &[(&str, &str)]) -> ChatMember {    ChatMember {        name: name.to_string(),        handles: handles.iter().map(|(kind, value)| (kind.to_string(), value.to_string())).collect(),    }}/// The database's people, by the handles they carry there.fn people(handles: &[(&str, &str)]) -> BTreeMap<String, MatchedPerson> {    handles        .iter()        .map(|(key, name)| {            (key.to_string(), MatchedPerson { id: format!("person-{name}"), full_name: Some(name.to_string()) })        })        .collect()}// RULE: A member is matched by a phone or a handle, never by a name. Two// people called Ravi are not one person; matching on the name would file one// Ravi's words under the other's place in the database.#[test]fn a_member_is_matched_by_a_phone_or_a_handle_and_never_by_a_name() {    let same_name = [member("Ravi Menon", &[("phone", "919820000099")])];    let there = people(&[("phone:919820000002", "Ravi Menon")]);    assert_eq!(outsiders(&same_name, &there), ["Ravi Menon"]);    // Any one of a member's handles is enough, spelled however the chat holds it.    let by_email = [member("R", &[("phone", "919820000099"), ("email", " Ravi@Example.com ")])];    assert!(outsiders(&by_email, &people(&[("email:ravi@example.com", "Ravi Menon")])).is_empty());    let by_instagram = [member("ravi.m", &[("instagram", "@Ravi.M")])];    assert!(outsiders(&by_instagram, &people(&[("instagram:ravi.m", "Ravi Menon")])).is_empty());}// RULE: A member with no phone and no handle is an outsider. A seat a source// gave only a name cannot be matched to anybody, and a member the check// cannot place must stop the filing rather than slip through it.#[test]fn a_member_with_no_phone_and_no_handle_is_an_outsider() {    let members = [        member("Asha Rao", &[("phone", "919820000001")]),        member("~ hidden number", &[]),        member("Someone", &[("cotton", "u-123")]),    ];    let there = people(&[("phone:919820000001", "Asha Rao")]);    assert_eq!(outsiders(&members, &there), ["~ hidden number", "Someone"]);}// RULE: Every outsider is named, each once, in the chat's own order.#[test]fn every_outsider_is_named_once_in_the_chats_order() {    let members = [        member("Ravi", &[("phone", "919820000002")]),        member("Meera", &[]),        member("Ravi", &[("phone", "919820000002")]),        member("Kabir", &[("email", "kabir@example.com")]),    ];    let out = outsiders(&members, &BTreeMap::new());    assert_eq!(out, ["Ravi", "Meera", "Kabir"]);}