use super::*; use crate::messages::{MessageMedia, MessageMeta, MessagePollOption, MessageSystem}; fn photo() -> MessageMedia { MessageMedia { key: "w/messages/t/a.jpg".into(), content_type: "image/jpeg".into(), ..MessageMedia::default() } } /// RULE: Recognized importer markers are classified while ordinary written bodies are left untouched. #[test] fn markers_are_classified_and_written_bodies_pass_through() { for marker in ["[unsupported message]", "[attachment: Asha Rao.vcf]", "[attachment: IMG-20240102-WA0001.JPG]", "[attachment: VID-1.mp4]", "[attachment: PTT-1.opus]", "[attachment: lease.pdf]", "[attachment: notes]", "[attachment]", "[image omitted]", " [media omitted] "] { assert!(marker_words(marker).is_some(), "{marker}"); } assert!(marker_words("[attachment: Asha Rao.vcf]").is_some_and(|words| words.contains("Asha Rao"))); assert!(marker_words("[attachment: lease.pdf]").is_some_and(|words| words.contains("lease.pdf"))); for body in ["see [attachment: a.pdf]", "[ok]", "[a] and [b omitted]", "hello", ""] { assert_eq!(marker_words(body), None, "{body}"); } } /// RULE: A chat list line is nonblank for missing or blank content, maps markers through their classifier, and preserves a written body. #[test] fn a_chats_last_line_is_classified_or_passed_through() { assert!(!list_line(None).trim().is_empty()); assert!(!list_line(Some(" ")).trim().is_empty()); let marker = "[unsupported message]"; assert_eq!(list_line(Some(marker)), marker_words(marker).expect("marker")); assert_eq!(list_line(Some("See you at six")), "See you at six"); } /// RULE: A shared-contact attachment yields its supplied contact name, while other bodies do not become contact cards. #[test] fn a_shared_contact_is_known_by_its_name() { assert_eq!(shared_contact("[attachment: Hanna.vcf]"), Some("Hanna".to_string())); assert_eq!(shared_contact(" [attachment: Example Chen.VCF] "), Some("Example Chen".to_string())); assert_eq!(shared_contact("[attachment: a.jpg]"), None); assert_eq!(shared_contact("call Hanna.vcf"), None); } /// RULE: Only deleted or omitted content without richer message data is an aside. #[test] fn an_aside_is_a_line_about_the_conversation() { let media = [photo()]; let aside = |body: Option<&str>, media: &[MessageMedia], deleted: Option<&str>, meta: Option<&MessageMeta>| { is_aside(&PreviewSource { body, media, deleted_at: deleted, meta }) }; assert!(aside(Some("secret"), &media, Some("2026-01-01T00:00:00.000Z"), None)); assert!(aside(Some("[unsupported message]"), &[], None, None)); assert!(aside(Some("[image omitted]"), &[], None, None)); assert!(!aside(Some("[image omitted]"), &media, None, None)); assert!(!aside(Some("[attachment: Asha.vcf]"), &[], None, None)); assert!(!aside(Some("hello"), &[], None, None)); assert!(!aside(None, &media, None, None)); let system = MessageMeta { system: Some(MessageSystem::Flag(true)), ..MessageMeta::default() }; assert!(!aside(Some("[unsupported message]"), &[], None, Some(&system))); } /// RULE: Deleted content never leaks, media takes precedence over marker copy, and written/system/poll data remains available to the renderer. #[test] fn note_words_preserves_content_without_leaking_deleted_bodies() { let media = [photo()]; let words = |body: Option<&str>, media: &[MessageMedia], deleted: Option<&str>, meta: Option<&MessageMeta>| { note_words(&PreviewSource { body, media, deleted_at: deleted, meta }) }; assert_eq!(words(Some("hello\nthere"), &[], None, None), "hello\nthere"); let deleted = words(Some("secret"), &media, Some("2026-01-01T00:00:00.000Z"), None); assert!(!deleted.contains("secret")); // A marker beside media that is drawn says nothing; alone it is its words. assert_eq!(words(Some("[attachment: a.jpg]"), &media, None, None), ""); assert_eq!(words(Some("[attachment: a.jpg]"), &[], None, None), marker_words("[attachment: a.jpg]").expect("marker")); assert_eq!(words(Some("caption"), &media, None, None), "caption"); assert_eq!(words(None, &media, None, None), ""); let system = MessageMeta { system: Some(MessageSystem::Flag(true)), ..MessageMeta::default() }; assert_eq!(words(Some("Asha joined"), &[], None, Some(&system)), "Asha joined"); let poll = MessageMeta { poll: Some(MessagePoll { question: "Dinner?".into(), options: vec![ MessagePollOption { text: "Yes".into(), votes: 2.0 }, MessagePollOption { text: "No".into(), votes: 1.0 }, ], ..Default::default() }), ..MessageMeta::default() }; let rendered_poll = words(Some("POLL:\nDinner?"), &[], None, Some(&poll)); for source in ["Dinner?", "Yes", "No"] { assert!(rendered_poll.contains(source), "{rendered_poll}"); } } /// RULE: Message lines select media or marker semantics without leaking a deleted body. #[test] fn message_lines_apply_media_and_marker_precedence() { let media = [photo()]; assert!(!message_line(&PreviewSource { body: None, media: &media, ..PreviewSource::default() }).is_empty()); let marker = "[attachment: Asha.vcf]"; assert_eq!(message_line(&PreviewSource { body: Some(marker), media: &[], ..PreviewSource::default() }), marker_words(marker).expect("marker")); } /// RULE: Direct and group chat names use their available supplied identity in precedence order and never return blank. #[test] fn a_chat_is_always_named() { assert_eq!(direct_name(Some("person"), Some("contact"), Some("title"), Some("number"), Some("address")), "person"); assert_eq!(direct_name(None, Some(" contact "), Some("title"), Some("number"), None), "contact"); assert_eq!(direct_name(Some(" "), None, Some("title"), Some("number"), None), "title"); assert_eq!(direct_name(None, None, Some(""), Some("number"), None), "+number"); assert_eq!(direct_name(None, None, None, Some("+number"), None), "+number"); assert_eq!(direct_name(None, None, None, None, Some("address")), "address"); assert!(!direct_name(None, None, None, None, None).trim().is_empty()); assert_eq!(group_name(Some("group"), &[]), "group"); let names: Vec = ["Asha", "Ben", "Chen", "Dee", "Eli"].iter().map(|n| n.to_string()).collect(); assert!(group_name(None, &names[..2]).contains("Asha")); assert!(group_name(Some(" "), &names).contains("Asha")); assert!(!group_name(None, &[]).trim().is_empty()); } /// RULE: Reactions discard blank values, deduplicate by emoji, and distinguish repeated from single reactions. #[test] fn reactions_aggregate_without_blank_values() { let line = |emojis: &[&str]| reaction_line(&emojis.iter().map(|e| e.to_string()).collect::>()); assert_eq!(line(&[]), None); assert!(line(&["❤️"]).is_some_and(|value| value.contains("❤️"))); assert_eq!(line(&["❤️"]), line(&["❤️", " "])); let repeated = line(&["❤️", "👍", "❤️", " "]).expect("reactions"); assert!(repeated.contains("❤️") && repeated.contains("👍")); assert_ne!(repeated, line(&["❤️", "👍"]).expect("reactions")); } /// RULE: Ordinary Markdown is reduced to readable prose, while media, system, and marker semantics retain their precedence. #[test] fn a_markdown_message_reads_as_prose_on_a_slip() { let body = "Plan for **Saturday**:\n\n- pick up the *trailer* at nine\n- lunch at ours\n\nHours on [example.com](https://example.com/tip)."; let line = message_line(&PreviewSource { body: Some(body), media: &[], ..PreviewSource::default() }); for source in ["Saturday", "trailer", "example.com"] { assert!(line.contains(source), "{line}"); } assert!(!line.contains("**") && !line.contains("*trailer*") && !line.contains("https://")); let media = [photo()]; let media_line = message_line(&PreviewSource { body: Some("the **new** shelf"), media: &media, ..PreviewSource::default() }); assert!(media_line.contains("new shelf") && !media_line.contains("**")); let list = list_line(Some("Plan for **Saturday**: - pick up the *trailer*")); assert!(list.contains("Saturday") && list.contains("trailer") && !list.contains("**")); let marker = "[unsupported message]"; assert_eq!(message_line(&PreviewSource { body: Some(marker), media: &[], ..PreviewSource::default() }), marker_words(marker).expect("marker")); } /// RULE: A preview never puts stripped syntax back when Markdown leaves no words, including a clipped code span. #[test] fn stripped_preview_syntax_is_never_restored() { assert!(!list_line(Some("`")).contains('`')); assert!(!list_line(Some("[example.com")).contains('[')); } /// RULE: A media-only message's note and line say the kit's word for its mark when no media is drawn, nothing beside drawn media, and an old row's left-behind label reads as an empty body. #[test] fn a_media_only_message_reads_from_its_mark() { let mut meta = MessageMeta::default(); meta.extra.insert(crate::messages::media_only::MEDIA_ONLY_META.into(), serde_json::Value::from("video")); let words = crate::messages::media_only::media_only_words("video"); for body in ["", "Video", "conversation.preview.video"] { let bare = PreviewSource { body: Some(body), meta: Some(&meta), ..PreviewSource::default() }; assert_eq!(note_words(&bare), words, "{body:?}"); assert_eq!(message_line(&bare), words, "{body:?}"); } let drawn = [photo()]; let with_media = PreviewSource { body: Some("Video"), media: &drawn, meta: Some(&meta), ..PreviewSource::default() }; assert_eq!(note_words(&with_media), ""); }