Cotton code
94 lines · 6.1 KB · RustRaw
//! The senders list's rules (SENDERS.md), on invented senders.use super::*;use crate::messages::app::list::lines;fn tally(key: &str, channel: &str, name: Option<&str>, sent: u32, replied: bool, last: &str) -> Tally {    Tally { key: key_of_sender(key).expect("a key"), channel: channel.into(), name: name.map(str::to_string), sent, replied, last_at: Some(last.into()) }}/// RULE: a sender is a number, an address, or an SMS short code by its name without its two-letter prefix and its/// one-letter suffix.#[test]fn a_sender_is_a_number_an_address_or_a_short_code_by_its_name() {    assert_eq!(key_of_sender("+91 98450 12345"), Some(SenderKey { key: "+919845012345".into(), kind: KeyKind::Number }));    assert_eq!(key_of_sender("00919845012345").unwrap().key, "+919845012345");    assert_eq!(key_of_sender(" Asha@Example.COM ").unwrap(), SenderKey { key: "asha@example.com".into(), kind: KeyKind::Address });    for raw in ["VM-HDFCBK-S", "AD-HDFCBK", "HDFCBK", "jd-hdfcbk-t"] {        assert_eq!(key_of_sender(raw), Some(SenderKey { key: "HDFCBK".into(), kind: KeyKind::Name }), "{raw}");    }    assert_eq!(short_code("VK-AMAZON-P").as_deref(), Some("AMAZON"));    // A name with no prefix keeps its own dashes; a five-digit short code is its digits.    assert_eq!(short_code("BIG-SALE").as_deref(), Some("BIG-SALE"));    assert_eq!(key_of_sender("57575").unwrap(), SenderKey { key: "57575".into(), kind: KeyKind::Name });    assert_eq!(key_of_sender("  "), None);}/// RULE: one row per sender across channels, the counts added; those he never replied to come first, the most sent/// first among them.#[test]fn one_ranked_row_per_sender_never_replied_and_most_sent_first() {    let senders = senders_of(vec![        tally("+919845012345", "whatsapp", Some("Ravi"), 12, true, "2026-11-20T10:00:00Z"),        tally("VM-HDFCBK-S", "sms", None, 214, false, "2026-11-23T09:00:00Z"),        tally("AD-HDFCBK", "sms", None, 6, false, "2026-11-22T09:00:00Z"),        tally("offers@shop.example", "mail", Some("Shop"), 40, false, "2026-11-21T09:00:00Z"),        tally("+919845012345", "sms", None, 3, false, "2026-11-23T11:00:00Z"),        tally("asha@example.com", "mail", Some("Asha"), 2, true, "2026-11-19T09:00:00Z"),    ]);    let keys: Vec<&str> = senders.iter().map(|s| s.key.key.as_str()).collect();    assert_eq!(keys, ["HDFCBK", "offers@shop.example", "+919845012345", "asha@example.com"]);    assert_eq!(senders[0].sent, 220, "both short-code forms are one sender");    let ravi = &senders[2];    assert_eq!((ravi.sent, ravi.replied, ravi.channels.clone()), (15, true, vec!["whatsapp".to_string(), "sms".into()]));    assert_eq!(ravi.name.as_deref(), Some("Ravi"));    assert_eq!(ravi.last_at.as_deref(), Some("2026-11-23T11:00:00Z"));}/// GOLDEN: a row's words, its acts, the chips, and each act's ask with its count.#[test]fn a_row_says_how_much_and_whether_he_replied_and_each_act_asks_with_its_count() {    let lines = lines();    let senders = senders_of(vec![tally("VM-HDFCBK-S", "sms", None, 214, false, "2026-11-23T09:00:00Z"), tally("+919845012345", "whatsapp", Some("Ravi"), 1, true, "2026-11-20T10:00:00Z")]);    let first = row(&lines, &senders[0]);    assert_eq!((first.name.as_str(), first.handle.as_deref(), first.sent_line.as_str(), first.replied_line.as_str(), first.channels_line.as_str()), ("HDFCBK", None, "214 messages", "Never replied", "SMS"));    let ravi = row(&lines, &senders[1]);    assert_eq!((ravi.name.as_str(), ravi.handle.as_deref(), ravi.sent_line.as_str(), ravi.replied_line.as_str()), ("Ravi", Some("+919845012345"), "1 message", "You replied"));    assert_eq!(first.hold.iter().map(|h| h.label.as_str()).collect::<Vec<_>>(), ["Block", "Delete everything"]);    let chips = filters(&lines, &senders, "sms");    assert_eq!(chips.iter().map(|c| (c.id.as_str(), c.label.as_str(), c.count, c.on)).collect::<Vec<_>>(), [("all", "All", None, false), ("whatsapp", "WhatsApp", Some(1), false), ("sms", "SMS", Some(1), true)]);    assert!(keeps("all", &senders[1]) && keeps("whatsapp", &senders[1]) && !keeps("mail", &senders[1]));    let block = ask(&lines, "block", "HDFCBK", "HDFCBK", 214, false);    assert_eq!(block.title, "Block HDFCBK?");    assert!(block.line.starts_with("214 messages from HDFCBK will go to Blocked"), "{}", block.line);    let delete = ask(&lines, "delete", "HDFCBK", "HDFCBK", 214, true);    assert_eq!((delete.title.as_str(), delete.confirm.as_str(), delete.count), ("Delete everything from HDFCBK?", "Delete 214", 214));    assert!(delete.line.ends_with("Cotton cannot block or delete them there."), "on iPhone SMS nothing is done at the source: {}", delete.line);}/// RULE: what sits in Blocked is erased 30 days after it came there: from the block, or its own arrival if later.#[test]fn what_is_in_blocked_is_erased_thirty_days_after_it_came_there() {    let day = 86_400_000;    assert_eq!(erased_at(10 * day, 2 * day), 40 * day);    assert_eq!(erased_at(10 * day, 15 * day), 45 * day);}/// RULE (SENDERS.md "Acts", step 5): a whole sender's long-press is Block and Delete everything, and for one who sends/// mail Archive everything and Unsubscribe; a domain's the same. Archive and/// Unsubscribe ask first, the count in the words.#[test]fn a_mail_sender_and_a_domain_hold_archive_and_unsubscribe_each_asked_first() {    let lines = lines();    let ids = |acts: Vec<HoldAct>| acts.into_iter().map(|a| a.id).collect::<Vec<_>>();    assert_eq!(ids(sender_acts(&lines, true, false)), ["block", "delete", "archive", "unsubscribe"]);    assert_eq!(ids(sender_acts(&lines, false, false)), ["block", "delete"]);    assert_eq!(ids(sender_acts(&lines, true, true)), ["block", "delete", "archive", "unsubscribe"]);    let archive = ask(&lines, "archive", "@shop.invented", "shop.invented", 214, false);    assert_eq!((archive.title.as_str(), archive.confirm.as_str()), ("Archive everything from shop.invented?", "Archive 214"));    assert_eq!(archive.line, "214 messages from shop.invented will leave Comms.");    let unsubscribe = ask(&lines, "unsubscribe", "deals@shop.invented", "Invented Shop", 3, false);    assert_eq!((unsubscribe.title.as_str(), unsubscribe.confirm.as_str()), ("Unsubscribe from Invented Shop?", "Unsubscribe"));}