//! ## Where the inputs come from //! //! Every case in the TypeScript starts by running `parseChatTxt` over a //! synthetic transcript, and the adapter is not ported yet. So the transcripts //! were parsed by the RUNNING TypeScript and their `ChatTxtParseResult`s //! captured into `tests/fixtures/import_parse.json`; the Rust reads those as //! its input. That is the stronger arrangement anyway — `identity` is a pure //! function of the parse result, and the fixture pins the exact value the //! TypeScript hands it, invisible characters and sender-count ORDER included. //! //! The transcripts themselves are SYNTHETIC — invented people, never real chat //! data. `identity_dm` is an iOS 1:1 export whose encryption notice is //! attributed to the OTHER person (the chat's title); `identity_group` is an //! iOS group export whose notice and events are attributed to the group's //! name; `identity_group_you` is the same with one sender renamed to the //! literal `You`. use super::*; use serde::Deserialize; use std::collections::BTreeMap; #[derive(Deserialize)] struct Parses { #[serde(rename = "chatTxt")] chat_txt: BTreeMap, } fn parses() -> BTreeMap { let parsed: Parses = serde_json::from_str(include_str!("../../tests/fixtures/import_parse.json")) .expect("import_parse.json"); parsed.chat_txt } fn parse_named(name: &str) -> ChatTxtParseResult { parses() .remove(name) .unwrap_or_else(|| panic!("no parse fixture named {name}")) } // ── chat titles ────────────────────────────────────────────────────────────── #[test] fn reads_the_title_from_the_exports_file_name_ios_and_android_staged_or_not() { assert_eq!( chat_title_from_file_name(Some("WhatsApp Chat - Asha Rao.zip")).as_deref(), Some("Asha Rao") ); assert_eq!( chat_title_from_file_name(Some( "file:///cache/1724569200000-WhatsApp%20Chat%20-%20Asha%20Rao.zip" )) .as_deref(), Some("Asha Rao") ); assert_eq!( chat_title_from_file_name(Some("WhatsApp Chat with Asha Rao.txt")).as_deref(), Some("Asha Rao") ); assert_eq!(chat_title_from_file_name(Some("random.zip")), None); assert_eq!(chat_title_from_file_name(None), None); assert_eq!(chat_title_from_file_name(Some("")), None); } #[test] fn reads_the_title_from_the_encryption_notices_attribution() { assert_eq!( chat_title_from_notice(&parse_named("identity_dm")).as_deref(), Some("Asha") ); assert_eq!( chat_title_from_notice(&parse_named("identity_group")).as_deref(), Some("Weekend Plans") ); assert_eq!( chat_title_from_notice(&parse_named("identity_dm_unattributed")), None ); } /// `decodeURIComponent` raises a `URIError` on a malformed escape and the /// TypeScript catches it — a literal `%` that is not an escape keeps the name /// as it is. Rust answers `None` from the decoder and falls back the same way, /// because a pure library may not panic on a name a stranger's file system /// produced. #[test] fn a_lone_percent_keeps_the_name_rather_than_throwing() { assert_eq!( chat_title_from_file_name(Some("WhatsApp Chat - 100% Club.zip")).as_deref(), Some("100% Club") ); assert_eq!( chat_title_from_file_name(Some("WhatsApp%20Chat%20-%20100%25%20Club.zip")).as_deref(), Some("100% Club") ); } // ── humanSenders ───────────────────────────────────────────────────────────── #[test] fn drops_the_groups_own_name() { let mut senders = human_senders(&parse_named("identity_group")); senders.sort(); assert_eq!(senders, ["+91 98765 43210", "Asha", "Rohan", "Vikram"]); } #[test] fn keeps_both_people_of_a_dm_the_notices_attribution_is_a_person_there() { let mut senders = human_senders(&parse_named("identity_dm")); senders.sort(); assert_eq!(senders, ["Asha", "Rohan"]); } // ── resolveOwner ───────────────────────────────────────────────────────────── fn owner(name: &str, hints: OwnerHints) -> Option { resolve_owner(&parse_named(name), &hints) } #[test] fn a_one_to_one_export_you_are_whoever_is_not_the_title() { assert_eq!( owner("identity_dm", OwnerHints::default()), Some(OwnerResolution { name: "Rohan".to_string(), how: OwnerHow::DmTitle, may_silent: false, }) ); } #[test] fn uses_a_title_from_outside_the_transcript_when_given() { assert_eq!( owner("identity_dm_unattributed", OwnerHints::default()), None ); assert_eq!( owner( "identity_dm_unattributed", OwnerHints { chat_title: Some("asha".to_string()), ..OwnerHints::default() } ), Some(OwnerResolution { name: "Rohan".to_string(), how: OwnerHow::DmTitle, may_silent: false, }) ); } #[test] fn prefers_the_remembered_name_in_a_group_and_allows_it_to_be_silent() { assert_eq!( owner( "identity_group", OwnerHints { remembered_names: vec!["rohan".to_string()], ..OwnerHints::default() } ), Some(OwnerResolution { name: "Rohan".to_string(), how: OwnerHow::Remembered, may_silent: false, }) ); assert_eq!( owner( "identity_group", OwnerHints { remembered_names: vec!["Someone Quiet".to_string()], ..OwnerHints::default() } ), Some(OwnerResolution { name: "Someone Quiet".to_string(), how: OwnerHow::Remembered, may_silent: true, }) ); } /// **Every remembered spelling is tried before any of them is taken as the /// silent answer.** The owner is one person with several names — a contact /// name, a number, a nickname — and an export naming them by the spelling they /// answered with two exports ago is still an export that names them. Holding /// only the latest is how finding d of the 15 Sep 2026 walk asked "which one is /// you?" on every export in a row. #[test] fn any_remembered_spelling_finds_the_owner_before_one_is_taken_as_silent() { // `+91 98765 43210` is in this group and `Someone Quiet` is not: the // OLDER remembered name is the one that matches, and it still wins. assert_eq!( owner( "identity_group", OwnerHints { remembered_names: vec![ "Someone Quiet".to_string(), "+91 98765 43210".to_string(), ], ..OwnerHints::default() } ), Some(OwnerResolution { name: "+91 98765 43210".to_string(), how: OwnerHow::Remembered, may_silent: false, }) ); // With none of them present the MOST RECENT is the silent answer. assert_eq!( owner( "identity_group", OwnerHints { remembered_names: vec!["Someone Quiet".to_string(), "Nobody Here".to_string()], ..OwnerHints::default() } ), Some(OwnerResolution { name: "Someone Quiet".to_string(), how: OwnerHow::Remembered, may_silent: true, }) ); // A list of nothing but whitespace decides nothing at all. assert_eq!( owner( "identity_group", OwnerHints { remembered_names: vec![" ".to_string()], ..OwnerHints::default() } ), None ); } #[test] fn never_lets_a_remembered_name_be_silent_in_a_dm() { // Two people spoke; one of them is you. assert_eq!( owner( "identity_dm_unattributed", OwnerHints { remembered_names: vec!["Someone Quiet".to_string()], ..OwnerHints::default() } ), None ); } #[test] fn falls_through_literal_you_and_the_display_name_else_gives_up() { assert_eq!( owner("identity_group_you", OwnerHints::default()).map(|o| o.how), Some(OwnerHow::LiteralYou) ); assert_eq!( owner( "identity_group", OwnerHints { owner_display_name: Some("rohan".to_string()), ..OwnerHints::default() } ), Some(OwnerResolution { name: "Rohan".to_string(), how: OwnerHow::DisplayName, may_silent: false, }) ); assert_eq!(owner("identity_group", OwnerHints::default()), None); } // ── planParticipants ───────────────────────────────────────────────────────── fn sources() -> IdentitySources { IdentitySources { thread_members: vec![ThreadMember { label: "Vikram".to_string(), phone: Some("918123456780".to_string()), }], contacts: vec![IdentityContact { name: "Priya From Stores".to_string(), phone: "919000000001".to_string(), }], } } fn labels(values: &[&str]) -> Vec { values.iter().map(|v| v.to_string()).collect() } fn hint_of<'a>(plan: &'a IdentityPlan, label: &str) -> Option<&'a ParticipantHint> { plan.hints .iter() .find(|entry| entry.label == label) .map(|entry| &entry.hint) } #[test] /// RULE: a label is matched by a number, a seat or a contact WhatsApp showed /// under it; a person is never found by name (PEOPLE BUILD 2). fn matches_numbers_thread_members_and_contacts_and_never_a_person_by_name() { let plan = plan_participants( &labels(&[ "+91 98765 43210", "Vikram", "priya from stores", "Asha", "Ajay", "Nobody", "Old Ajay", "No Number", ]), &sources(), Some("91"), ); assert_eq!( plan.hints .iter() .map(|entry| (entry.label.as_str(), entry.hint.clone())) .collect::>(), vec![ ("+91 98765 43210", phone_hint("919876543210")), ("Vikram", phone_hint("918123456780")), ("priya from stores", phone_hint("919000000001")), ] ); assert_eq!( plan.matched, labels(&["+91 98765 43210", "Vikram", "priya from stores"]) ); assert_eq!( plan.unidentified, labels(&["Asha", "Ajay", "Nobody", "Old Ajay", "No Number"]) ); } fn phone_hint(phone: &str) -> ParticipantHint { ParticipantHint { phone: Some(phone.to_string()), person_id: None, } } #[test] fn a_seated_member_wins_over_a_contact_of_the_same_name() { // The thread already decided. let mut sources = sources(); sources.contacts = vec![IdentityContact { name: "Vikram".to_string(), phone: "910000000000".to_string(), }]; let plan = plan_participants(&labels(&["Vikram"]), &sources, None); assert_eq!(hint_of(&plan, "Vikram"), Some(&phone_hint("918123456780"))); } // ── the Node vector tables ─────────────────────────────────────────────────── #[derive(Deserialize)] struct FileNameVector { name: String, title: Option, } #[derive(Deserialize)] struct PlanVector { name: String, labels: Vec, sources: IdentitySources, #[serde(rename = "countryCode")] country_code: Option, plan: PlanGolden, } #[derive(Deserialize)] struct PlanGolden { hints: BTreeMap, matched: Vec, unidentified: Vec, } #[derive(Deserialize)] struct OwnerVector { name: String, source: String, hints: OwnerHints, owner: Option, } #[derive(Deserialize)] struct NoticeVector { name: String, title: Option, humans: Vec, } #[derive(Deserialize)] struct Vectors { #[serde(rename = "fileNames")] file_names: Vec, #[serde(rename = "identityPlans")] identity_plans: Vec, owners: Vec, notices: Vec, } fn vectors() -> Vectors { serde_json::from_str(include_str!("../../../cotton-rules/vectors/import.json")) .expect("cotton-rules/vectors/import.json") } #[test] fn every_file_name_reading_matches_the_typescript() { let vectors = vectors(); assert!( vectors.file_names.len() >= 14, "the table should not shrink" ); for case in &vectors.file_names { assert_eq!( chat_title_from_file_name(Some(&case.name)), case.title, "{:?}", case.name ); } } /// A staging prefix needs TEN digits: nine leave the name unrecognised, and a /// path whose last segment is empty answers nothing at all. #[test] fn the_file_name_rules_are_narrower_than_they_look() { assert_eq!( chat_title_from_file_name(Some("123456789-WhatsApp Chat - Short Prefix.zip")), None ); assert_eq!( chat_title_from_file_name(Some("WhatsApp Chat - Trailing.zip/")), None ); assert_eq!( chat_title_from_file_name(Some("WhatsApp Chat - Padded Name .zip")).as_deref(), Some("Padded Name") ); } #[test] fn every_notice_title_and_sender_list_matches_the_typescript() { let parses = parses(); let vectors = vectors(); assert!(vectors.notices.len() >= 14, "the table should not shrink"); for case in &vectors.notices { let parse = parses .get(&case.name) .unwrap_or_else(|| panic!("no parse fixture named {}", case.name)); assert_eq!(chat_title_from_notice(parse), case.title, "{}", case.name); // The ORDER is the sender map's insertion order, and it is observable. assert_eq!(human_senders(parse), case.humans, "{}", case.name); } } #[test] fn every_owner_resolution_matches_the_typescript() { let parses = parses(); let vectors = vectors(); assert!(vectors.owners.len() >= 9, "the table should not shrink"); for case in &vectors.owners { let parse = parses .get(&case.source) .unwrap_or_else(|| panic!("no parse fixture named {}", case.source)); assert_eq!( resolve_owner(parse, &case.hints), case.owner, "{}", case.name ); } } #[test] fn every_participant_plan_matches_the_typescript() { for case in &vectors().identity_plans { let plan = plan_participants(&case.labels, &case.sources, case.country_code.as_deref()); let hints: BTreeMap = plan .hints .iter() .map(|entry| (entry.label.clone(), entry.hint.clone())) .collect(); assert_eq!(hints, case.plan.hints, "{}", case.name); assert_eq!(plan.matched, case.plan.matched, "{}", case.name); assert_eq!(plan.unidentified, case.plan.unidentified, "{}", case.name); } } // ── nameKey / sameName ─────────────────────────────────────────────────────── #[test] fn a_name_key_folds_layout_and_case_but_not_identity() { assert_eq!(name_key(" Anita Rao "), "anita rao"); assert!(same_name("ANITA RAO", "anita rao")); assert!(!same_name("Anita Rao", "Anita Rao Jr")); // A bidi control is NOT whitespace, so it survives the key — two labels // that differ only by an invisible mark are two people here, which is the // safe direction to be wrong in. assert_ne!(name_key("\u{200e}Anita"), name_key("Anita")); } /// The `text` vector table's `nameKey` column: the identity key of every /// shape that breaks a text pass (the passes themselves are cotton-rules'). #[test] fn every_text_vector_keys_as_the_typescript_did() { #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct TextKey { input: String, name_key: String, } #[derive(Deserialize)] struct Texts { text: Vec, } let texts: Texts = serde_json::from_str(include_str!("../../../cotton-rules/vectors/import.json")).expect("vectors/import.json"); assert!(texts.text.len() >= 17, "the table should not shrink"); for case in &texts.text { assert_eq!(name_key(&case.input), case.name_key, "{:?}", case.input); } }