//! Chat's notification rules. Every name and word is invented. use super::*; use crate::messages::app::list::lines; fn pic() -> Picture { Picture { photo: Some("file/people/asha.jpg".into()), initials: "AE".into(), tint: 2 } } fn said(id: &str, at: i64, mine: bool, text: &str) -> MessageFacts { MessageFacts { id: id.into(), at, mine, sender: None, text: text.into(), mentions_me: false } } fn chat(messages: Vec) -> ThreadFacts { ThreadFacts { thread: "t-asha".into(), name: "Asha Example".into(), picture: pic(), photo_path: Some("/data/asha.jpg".into()), group: false, request: false, muted: false, blocked: false, messages } } // RULE: a chat with something of theirs unread is one notice: its name, its picture, their unread lines oldest first, and the person's own after the first of them said as "You"; Reply and Mark as read are offered. #[test] fn an_unread_chat_is_one_notice_with_its_lines() { let l = lines(); let n = notice(&l, &chat(vec![said("m1", 10, false, "are you up?"), said("m2", 20, false, "Photo"), said("m3", 30, true, "yes")])).expect("a notice"); assert_eq!((n.title.as_str(), n.unread, n.at, n.actions, n.request), ("Asha Example", 2, 30, true, false)); assert_eq!(n.photo_path.as_deref(), Some("/data/asha.jpg")); let shown: Vec<(Option<&str>, &str)> = n.lines.iter().map(|x| (x.from.as_deref(), x.text.as_str())).collect(); assert_eq!(shown, [(None, "are you up?"), (None, "Photo"), (Some("You"), "yes")]); } // RULE: nothing of theirs unread, a muted chat, or a blocked person: no notice (so a notice already posted is taken away). #[test] fn read_muted_or_blocked_chats_notice_nothing() { let l = lines(); assert!(notice(&l, &chat(vec![])).is_none()); assert!(notice(&l, &chat(vec![said("m1", 10, true, "only mine")])).is_none()); let mut muted = chat(vec![said("m1", 10, false, "hi")]); muted.muted = true; assert!(notice(&l, &muted).is_none()); let mut blocked = chat(vec![said("m1", 10, false, "hi")]); blocked.blocked = true; assert!(notice(&l, &blocked).is_none()); } // RULE (owner, 24 Nov 2026; chat DECISIONS.md): a mention of him overrides mute, as WhatsApp does; never a block. #[test] fn a_mention_of_him_rings_a_muted_group() { use super::mentions_me; let l = lines(); let mut group = chat(vec![said("g1", 5, false, "dinner at eight"), MessageFacts { mentions_me: true, sender: Some("Ravi".into()), ..said("g2", 6, false, "@Asha are you coming?") }]); group.group = true; group.muted = true; let rung = notice(&l, &group).expect("a mention rings through mute"); assert_eq!(rung.lines.last().unwrap().text, "@Asha are you coming?"); group.messages[1].mentions_me = false; assert!(notice(&l, &group).is_none(), "without one, muted stays quiet"); group.messages[1].mentions_me = true; group.blocked = true; assert!(notice(&l, &group).is_none(), "a block is never overridden"); // His own message mentioning himself rings nothing. let mut own = chat(vec![MessageFacts { mentions_me: true, ..said("g3", 7, true, "@Asha note to self") }]); own.muted = true; assert!(notice(&l, &own).is_none()); // What counts as a mention of him: his number in the message's mentions, or @ and his number or name in its words. assert!(mentions_me("hi all", &["919800000001".into()], "+919800000001", None)); assert!(mentions_me("ask @+919800000001 first", &[], "+919800000001", None)); assert!(mentions_me("ask @919800000001", &[], "919800000001", None)); assert!(mentions_me("@Asha Invented, look", &[], "+919800000001", Some("Asha Invented"))); assert!(!mentions_me("mail asha@invented.example", &[], "+919800000001", Some("Asha")), "an @ inside a word is no mention"); assert!(!mentions_me("@Ravi look", &["919800000002".into()], "+919800000001", Some("Asha"))); assert!(!mentions_me("@Asha", &[], "", Some("Asha")), "no number known, no mention"); } // RULE: a stranger's request says "New message" alone: no name, no picture, no words, no actions. #[test] fn a_request_says_new_message_alone() { let l = lines(); let mut asking = chat(vec![said("m1", 10, false, "we met at the station")]); asking.request = true; let n = notice(&l, &asking).expect("a notice"); assert_eq!((n.title.as_str(), n.lines.len(), n.actions, n.request), ("New message", 0, false, true)); assert_eq!((n.picture.photo.clone(), n.photo_path.clone(), n.picture.initials.as_str()), (None, None, "")); assert!(!serde_json::to_string(&n).unwrap().contains("station")); } // RULE: at most LINES_MAX lines, the newest; a group's lines name their sender. #[test] fn lines_are_the_newest_and_a_group_names_its_senders() { let l = lines(); let many: Vec = (0..10).map(|i| said(&format!("m{i}"), i, false, &format!("line {i}"))).collect(); let n = notice(&l, &chat(many)).unwrap(); assert_eq!(n.lines.len(), LINES_MAX); assert_eq!((n.lines[0].text.as_str(), n.unread), ("line 4", 10)); let mut group = chat(vec![MessageFacts { sender: Some("Ravi".into()), ..said("g1", 5, false, "hello all") }]); group.group = true; assert_eq!(notice(&l, &group).unwrap().lines[0].from.as_deref(), Some("Ravi")); } // RULE: the answer lists notices and missed calls newest first, with the channels' and actions' words. #[test] fn the_answer_is_newest_first_with_its_words() { let l = lines(); let older = chat(vec![said("a", 10, false, "older")]); let newer = ThreadFacts { thread: "t-ravi".into(), name: "Ravi Example".into(), ..chat(vec![said("b", 50, false, "newer")]) }; let missed = |call: &str, at: i64, video: bool| MissedFacts { call: call.into(), thread: None, name: "Ravi Example".into(), picture: pic(), photo_path: None, video, at }; let answer = notices(&l, &[older, newer], &[missed("c1", 5, false), missed("c2", 9, true)]); assert_eq!(answer.notices.iter().map(|n| n.thread.as_str()).collect::>(), ["t-ravi", "t-asha"]); assert_eq!(answer.missed.iter().map(|m| (m.call.as_str(), m.title.as_str())).collect::>(), [("c2", "Missed video call"), ("c1", "Missed voice call")]); assert_eq!(answer.missed[0].back, "Call back"); let w = &answer.words; assert_eq!((w.channel.as_str(), w.reply.as_str(), w.mark_read.as_str(), w.missed_channel.as_str(), w.you.as_str()), ("Comms", "Reply", "Mark as read", "Missed calls", "You")); assert!(!w.reply_hint.is_empty()); }