//! The tidy's plan, pinned. Every person, number and address here is invented //! (555 numbers, example.invalid addresses). use std::collections::BTreeSet; use super::*; fn card(id: &str, organisation: bool, given: &str, org: Option<&str>, phones: &[&str], emails: &[&str]) -> PhoneCard { PhoneCard { id: id.into(), organisation, given: given.into(), family: String::new(), org: org.map(str::to_string), phones: phones.iter().map(|phone| phone.to_string()).collect(), emails: emails.iter().map(|email| email.to_string()).collect(), } } fn person(id: &str, name: &str, phones: &[&str], emails: &[&str]) -> CottonPerson { CottonPerson { id: id.into(), full_name: name.into(), org: None, phones: phones.iter().map(|phone| phone.to_string()).collect(), emails: emails.iter().map(|email| email.to_string()).collect(), } } fn every_tidy(plan: &TidyPlan) -> Vec<&PersonTidy> { plan.groups.iter().flat_map(|group| &group.people).collect() } fn plan_for_name(name: &str) -> PersonTidy { let people = [person("p1", name, &["5550000001"], &[])]; let cards = [card("c1", true, "", Some("TinMen"), &["5550000001"], &[])]; every_tidy(&plan(&cards, &people))[0].clone() } /// RULE: a planned card is never an organisation contact when the person has a name — the Expo-era mirror wrote every card as a company, and that is the thing this tidy repairs; the company stays as the card's company field. #[test] fn a_planned_card_is_a_person_card_named_from_cotton() { let people = [person("p1", "Asha Invented Rao", &["+91 55500 00001"], &[])]; let cards = [card("c1", true, "", Some("Zomato"), &["55500 00001"], &[])]; let plan = plan(&cards, &people); let tidy = every_tidy(&plan)[0]; assert!(!tidy.card.organisation); assert_eq!((tidy.card.given.as_str(), tidy.card.family.as_str()), ("Asha Invented", "Rao")); assert_eq!(tidy.card.org.as_deref(), Some("Zomato"), "the company is kept as the company field"); assert_eq!(tidy.reasons, [TidyReason::Organisation]); assert_eq!(tidy.keep_shows, "Zomato"); for name in ["Asha", "Asha Rao", "Asha Invented Rao"] { assert!(!plan_for_name(name).card.organisation, "{name}"); } } /// RULE: one person's several cards become ONE card — the plan never yields two target cards for one person (the later one-way mirror's "never a second card" begins here), and every card it deletes is listed by the name it shows. #[test] fn duplicates_of_one_person_fold_into_one_listed_card() { let people = [person("p1", "Ravi Invented", &["5550000002"], &["ravi@example.invalid"])]; let cards: Vec = (0..11) .map(|n| card(&format!("c{n:02}"), true, "", Some("TinMen"), &["555 000 0002"], &[])) .chain([card( "c99", false, "Ravi", Some("TinMen"), &["+1 555 000 0002", "5550000099"], &["RAVI@example.invalid", "ravi.other@example.invalid"], )]) .collect(); let plan = plan(&cards, &people); let tidies = every_tidy(&plan); assert_eq!(tidies.len(), 1, "one person, one tidy"); let tidy = tidies[0]; assert_eq!(tidy.keep, "c99", "the person card is kept"); assert_eq!(tidy.fold.len(), 11); assert!(tidy.fold.iter().all(|fold| fold.shows == "TinMen")); let mut every: BTreeSet<&str> = tidy.fold.iter().map(|fold| fold.id.as_str()).collect(); every.insert(&tidy.keep); assert_eq!(every.len(), 12, "every card is the kept one or a listed fold"); assert_eq!(tidy.reasons, [TidyReason::Organisation, TidyReason::Duplicates]); // Cotton's first, then the cards' extras, each once. assert_eq!(tidy.card.phones, ["5550000002", "5550000099"]); assert_eq!(tidy.card.emails, ["ravi@example.invalid", "ravi.other@example.invalid"]); assert_eq!(tidy.gain_phones, ["5550000099"]); assert_eq!(tidy.gain_emails, ["ravi.other@example.invalid"]); assert_eq!(plan.groups.len(), 1); assert_eq!((plan.groups[0].company.as_deref(), plan.groups[0].cards), (Some("TinMen"), 12)); assert_eq!((plan.read, plan.matched), (12, 12)); } /// RULE: a card matching two people is skipped and said, never planned; a person whose one card is already a person card is left alone; a card matching nobody is not touched. #[test] fn an_ambiguous_card_is_skipped_and_a_tidy_card_is_left_alone() { let people = [ person("p1", "Meera Invented", &["5550000003"], &[]), person("p2", "Kiran Invented", &[], &["kiran@example.invalid"]), person("p3", "Lata Invented", &["5550000004"], &[]), ]; let cards = [ card("both", true, "", Some("Zomato"), &["5550000003"], &["kiran@example.invalid"]), card("fine", false, "Lata", None, &["5550000004"], &[]), card("stranger", true, "", Some("Zomato"), &["5550000005"], &[]), ]; let plan = plan(&cards, &people); assert!(plan.groups.is_empty(), "{plan:?}"); assert_eq!(plan.skipped.len(), 1); assert_eq!(plan.skipped[0].id, "both"); assert!(plan.skipped[0].reason.contains("Meera Invented") && plan.skipped[0].reason.contains("Kiran Invented")); assert_eq!((plan.read, plan.matched), (3, 1)); } /// RULE: a phone or email another person holds is never a gain for this one — Cotton's handles belong to one person each, and a card carrying two people's numbers is skipped. #[test] fn a_handle_another_person_holds_is_not_a_gain() { let people = [person("p1", "Nila Invented", &["5550000006"], &[]), person("p2", "Om Invented", &["5550000007"], &[])]; let shared = plan(&[card("c1", true, "", Some("Zomato"), &["5550000006", "5550000007"], &[])], &people); assert_eq!(shared.skipped.len(), 1); assert!(shared.groups.is_empty()); // Her two cards, one carrying a new number: the new one is her gain. let cards = [card("c1", true, "", None, &["5550000006"], &[]), card("c2", true, "", None, &["5550000006", "5550000008"], &[])]; let tidy = every_tidy(&plan(&cards, &people))[0].clone(); assert_eq!(tidy.gain_phones, ["5550000008"]); assert_eq!(tidy.keep, "c2", "the fullest card is kept when none is a person card"); assert_eq!(tidy.card.org, None); } /// RULE: a company's own card — one organisation card whose company is the Cotton person's whole name — is left as it is; the tidy repairs people written as companies, not companies. #[test] fn a_companys_own_card_is_left_alone() { let people = [person("p1", "Zomato", &["5550000020"], &[])]; let one = plan(&[card("c1", true, "", Some("zomato"), &["5550000020"], &[])], &people); assert!(one.groups.is_empty(), "{one:?}"); // Two of them are still duplicates to fold. let two = plan( &[card("c1", true, "", Some("Zomato"), &["5550000020"], &[]), card("c2", true, "", Some("Zomato"), &["5550000020"], &[])], &people, ); assert_eq!(every_tidy(&two)[0].reasons, [TidyReason::Organisation, TidyReason::Duplicates]); } /// A person Cotton holds without a name keeps the card's own names. #[test] fn a_nameless_person_keeps_the_cards_names() { let people = [person("p1", " ", &["5550000021"], &[])]; let cards = [ card("c1", false, "Uma", Some("TinMen"), &["5550000021"], &[]), card("c2", true, "", Some("TinMen"), &["5550000021"], &[]), ]; let tidy = every_tidy(&plan(&cards, &people))[0].clone(); assert_eq!((tidy.card.organisation, tidy.card.given.as_str()), (false, "Uma")); } /// The grouping the owner reviews: by company, the biggest first, no company last. #[test] fn the_plan_is_grouped_by_company_biggest_first() { let people = [ person("p1", "A Invented", &["5550000011"], &[]), person("p2", "B Invented", &["5550000012"], &[]), person("p3", "C Invented", &["5550000013"], &[]), ]; let cards = [ card("a1", true, "", Some("Zomato"), &["5550000011"], &[]), card("b1", true, "", Some("tinmen"), &["5550000012"], &[]), card("b2", true, "", Some("TinMen"), &["5550000012"], &[]), card("c1", true, "", None, &["5550000013"], &[]), ]; let plan = plan(&cards, &people); let order: Vec<(Option<&str>, usize)> = plan.groups.iter().map(|group| (group.company.as_deref(), group.cards)).collect(); assert_eq!(order, [(Some("tinmen"), 2), (Some("Zomato"), 1), (None, 1)]); } #[test] fn a_name_splits_into_given_and_family() { assert_eq!(split_name(" Asha "), ("Asha".into(), String::new())); assert_eq!(split_name("Asha Rao"), ("Asha".into(), "Rao".into())); assert_eq!(split_name("Asha Invented Rao"), ("Asha Invented".into(), "Rao".into())); assert_eq!(split_name(""), (String::new(), String::new())); }