//! The phone fold on a synthetic Mac-shaped copy: the synthetic household //! (every value invented), with a share of its phones aged into what the //! earlier normaliser stored — the three trunk-0 shapes T8 found, one person //! holding one number twice, and two people holding one number — then folded //! as the Mac folds its files at start (`cotton_store::phone_fold`). mod support; use cotton_store::file_identity::read_file_identity; use cotton_store::phone_fold::fold_phones; use cotton_store::protocol::derived_id::{contact_handle_id, person_handle_id}; use cotton_store::protocol::person_handles::{normalise_handle, HandleKind}; use cotton_store::protocol::phone::refold_stored_phone; use cotton_store::Driver; use cotton_tools::synthetic::{seed_household, Plan}; use rusqlite::types::Value; use serde_json::json; use support::harness::{synthetic_uuid, Harness}; fn text(value: &str) -> Value { Value::Text(value.to_string()) } fn household() -> Harness { let h = Harness::new(); h.seed(&[json!({ "opId": format!("01J{:023}", 0), "deviceId": "device:seed", "kind": "upsert", "table": "cotton_collections", "rowId": synthetic_uuid("calendar"), "patch": { "kind": "calendar", "name": "Personal", "is_default": true, "workspace_id": h.workspace_id, "created_at": "2026-09-08T09:00:00.000Z", }, "hlc": "1700000000000:0000:device:seed", "schemaVersion": cotton_store::schema::ddl::SCHEMA_VERSION, })]); // A small household with enough people and chats to age. let mut plan = Plan::scaled(400); plan.people = 120; plan.message_threads = 30; h.with(|db| seed_household(db, &h.owner_id, &plan, 7)).expect("a household"); h } /// The earlier normaliser: presentation out, one `+` off, 8–15 digits. fn before(raw: &str) -> String { let digits: String = raw.chars().filter(|ch| ch.is_ascii_digit()).collect(); digits } #[derive(Debug, Default, PartialEq)] struct Counts { phone_handles: i64, handles_in_the_old_form: usize, contacts_in_the_old_form: usize, contact_handles_in_the_old_form: usize, people: i64, suggestions: i64, } fn counts(db: &Driver) -> Counts { let count = |sql: &str| db.get(sql, &[]).unwrap().unwrap().int("n").unwrap(); let handles = db.all("SELECT value, normalised FROM cotton_person_handles WHERE kind = 'phone'", &[]).unwrap(); let contacts = db.all("SELECT phone_e164 AS p FROM cotton_msg_contacts WHERE phone_e164 IS NOT NULL", &[]).unwrap(); let contact_handles = db.all("SELECT value AS p FROM cotton_msg_contact_handles WHERE kind = 'phone'", &[]).unwrap(); Counts { phone_handles: count("SELECT COUNT(*) AS n FROM cotton_person_handles WHERE kind = 'phone'"), handles_in_the_old_form: handles .iter() .filter(|row| normalise_handle(HandleKind::Phone, row.text("value").unwrap()).as_deref() != row.text("normalised")) .count(), contacts_in_the_old_form: contacts.iter().filter(|row| refold_stored_phone(row.text("p").unwrap()).is_some()).count(), contact_handles_in_the_old_form: contact_handles .iter() .filter(|row| refold_stored_phone(row.text("p").unwrap()).is_some()) .count(), people: count("SELECT COUNT(*) AS n FROM cotton_people"), suggestions: count("SELECT COUNT(*) AS n FROM cotton_person_suggestions WHERE reason = 'handle_name' AND contact_id IS NULL"), } } /// Age the household: what an older build left in a Mac's file. fn age(db: &Driver, workspace: &str) -> usize { let identity = read_file_identity(db).unwrap().unwrap(); let namespace = identity.namespace(); // People whose phone is in London's 020 7946 range: E.164 442079460nnn. let people: Vec<(String, String, String)> = db .all( "SELECT id, person_id, normalised FROM cotton_person_handles WHERE kind = 'phone' AND normalised LIKE '442079460%' ORDER BY normalised LIMIT 60", &[], ) .unwrap() .iter() .map(|row| { (row.text("id").unwrap().to_string(), row.text("person_id").unwrap().to_string(), row.text("normalised").unwrap().to_string()) }) .collect(); assert!(people.len() >= 40, "the household holds {} London numbers", people.len()); let insert = |person: &str, value: &str, created: &str| { let old = before(value); db.run( "INSERT INTO cotton_person_handles (id, workspace_id, person_id, kind, value, normalised, source, created_at, updated_at, _hlc) \ VALUES (?, ?, ?, 'phone', ?, ?, 'contacts', ?, ?, '1700000000000:0000:device:old')", &[text(&person_handle_id(namespace, "phone", &old)), text(workspace), text(person), text(value), text(&old), text(created), text(created)], ) .unwrap(); }; let mut aged = 0; for (n, (id, person, e164)) in people.iter().enumerate() { let tail = &e164[9..]; match n % 6 { // The three trunk-0 shapes, in place of the handle. 0..=2 => { let value = match n % 6 { 0 => format!("+44 (0)20 7946 0{tail}"), 1 => format!("+44 020 7946 0{tail}"), _ => format!("0044 020 7946 0{tail}"), }; db.run("DELETE FROM cotton_person_handles WHERE id = ?", &[text(id)]).unwrap(); insert(person, &value, "2026-01-01T00:00:00.000Z"); } // One person, one number twice: the handle and its trunk-0 spelling. 3 => insert(person, &format!("+44 (0)20 7946 0{tail}"), "2026-02-01T00:00:00.000Z"), // Two people, one number: the next person also holds this one, older. 4 => { let (_, other, _) = &people[(n + 1) % people.len()]; insert(other, &format!("+44 020 7946 0{tail}"), "2025-01-01T00:00:00.000Z"); } // India's shape, a new number. _ => insert(person, &format!("+91 098765 4{:04}", n), "2026-03-01T00:00:00.000Z"), } aged += 1; } // Contacts the way the earlier normaliser stored them, with their phone handle. let contacts: Vec<(String, String)> = db .all("SELECT id, phone_e164 FROM cotton_msg_contacts WHERE phone_e164 LIKE '442079460%' ORDER BY phone_e164 LIMIT 20", &[]) .unwrap() .iter() .map(|row| (row.text("id").unwrap().to_string(), row.text("phone_e164").unwrap().to_string())) .collect(); for (id, e164) in &contacts { let old = format!("440{}", &e164[2..]); db.run("UPDATE cotton_msg_contacts SET phone_e164 = ? WHERE id = ?", &[text(&old), text(id)]).unwrap(); db.run("DELETE FROM cotton_msg_contact_handles WHERE kind = 'phone' AND value = ?", &[text(e164)]).unwrap(); db.run( "INSERT INTO cotton_msg_contact_handles (id, workspace_id, contact_id, kind, value, created_at) VALUES (?, ?, ?, 'phone', ?, '2026-01-01T00:00:00.000Z')", &[text(&contact_handle_id(namespace, "phone", &old)), text(workspace), text(id), text(&old)], ) .unwrap(); } aged + contacts.len() } #[test] fn the_fold_on_a_synthetic_mac_shaped_copy() { let h = household(); h.with(|db| { age(db, &h.workspace_id); let ahead = counts(db); let report = fold_phones(db).expect("the fold"); let after = counts(db); eprintln!("before: {ahead:?}\n{report}\nafter: {after:?}"); assert!(ahead.handles_in_the_old_form >= 40, "{ahead:?}"); assert!(ahead.contacts_in_the_old_form >= 10, "{ahead:?}"); // Every aged handle is folded but the ones another person holds. assert_eq!(after.handles_in_the_old_form, report.handles_kept, "{after:?}"); assert!(report.handles_kept >= 1 && report.handles_collapsed >= 1, "{report}"); assert_eq!(after.phone_handles, ahead.phone_handles - report.handles_collapsed as i64); assert_eq!(after.contacts_in_the_old_form, report.contacts_kept); assert_eq!(after.contact_handles_in_the_old_form, report.contact_handles_kept); // Nobody merged, and no question is raised (person_suggestion left with TABLE DROP 1). assert_eq!(after.people, ahead.people); assert_eq!(after.suggestions, ahead.suggestions); assert!(fold_phones(db).unwrap().wrote_nothing()); }); }