//! People ordered by how much the owner reaches them (peoplerank, 21 Oct 2026). mod support; use chrono::{Duration, SecondsFormat, Utc}; use cotton_store::derived::Derive; use rusqlite::types::Value as SqlValue; use serde_json::{json, Value}; use support::harness::Harness; use support::mcp::{call_tool, mint_token}; /// RULE: a person's `reached_at` is the newest message, call or mail with any /// of their handles and `reach_90d` how many fell in the last 90 days; the /// engine fills both when a fold brings the events (an import, then one new /// message), and People's All view reads the people reached most recently /// first, the unreached after them by name A to Z. The owner: alphabetical makes /// no sense on All people when Cotton holds the WhatsApp and call data — and a /// column the fold forgets, or an order the read ignores, compiles fine. #[test] fn all_people_reads_the_most_recently_reached_first() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Synthetic reach agent"); let person = |name: &str| call_tool(&h, &token, "create_person", json!({"fullName": name}))["id"].as_str().unwrap().to_owned(); let handle = |person: &str, kind: &str, value: &str| { let made = call_tool(&h, &token, "create_record", json!({"table": "person_handle", "data": {"person_id": person, "kind": kind, "value": value}})); made["record"]["data"]["normalised"].as_str().unwrap().to_owned() }; let asha = person("Invented Asha"); let bina = person("Invented Bina"); let chitra = person("Invented Chitra"); // Never reached, and made out of name order: the tie is by name. person("Invented Esha"); person("Invented Dev"); let asha_phone = handle(&asha, "phone", "+1 202 555 0101"); handle(&bina, "email", "bina@example.test"); let chitra_phone = handle(&chitra, "phone", "+1 202 555 0103"); let now = Utc::now(); let ago = |days: i64| (now - Duration::days(days)).to_rfc3339_opts(SecondsFormat::Millis, true); let ws = || SqlValue::Text(h.workspace_id.clone()); let t = |value: &str| SqlValue::Text(value.to_string()); // The import: a chat with Asha (one text past 90 days, one inside), a mail // from Bina, a call with Chitra. h.run("INSERT INTO cotton_msg_contacts (id, workspace_id, display_name, created_at, updated_at) VALUES ('contact-asha', ?1, 'Invented Asha', ?2, ?2)", &[ws(), t(&ago(200))]); h.run("INSERT INTO cotton_msg_contact_handles (id, workspace_id, contact_id, kind, value, created_at) VALUES ('handle-asha', ?1, 'contact-asha', 'phone', ?2, ?3)", &[ws(), t(&asha_phone), t(&ago(200))]); h.run("INSERT INTO cotton_msg_threads (id, workspace_id, contact_id, kind, created_at, updated_at) VALUES ('thread-asha', ?1, 'contact-asha', 'dm', ?2, ?2)", &[ws(), t(&ago(200))]); let text = |id: &str, at: &str| { h.run( "INSERT INTO cotton_msg_messages (id, thread_id, workspace_id, direction, channel, origin, body, sent_at, created_at) VALUES (?1, 'thread-asha', ?2, 'in', 'whatsapp', 'imported', 'An invented text.', ?3, ?3)", &[t(id), ws(), t(at)], ) }; text("text-old", &ago(100)); text("text-recent", &ago(10)); h.run("INSERT INTO cotton_email_threads (id, workspace_id, external_key, subject, first_at, last_at, email_count, created_at, updated_at) VALUES ('mail-thread', ?1, 'key-bina', 'An invented subject', ?2, ?2, 1, ?2, ?2)", &[ws(), t(&ago(3))]); h.run("INSERT INTO cotton_emails (id, workspace_id, thread_id, external_id, subject, from_name, from_email, sent_at, body, created_at) VALUES ('mail-bina', ?1, 'mail-thread', '', 'An invented subject', 'Invented Bina', 'Bina@example.test', ?2, 'An invented body.', ?2)", &[ws(), t(&ago(3))]); h.run( "INSERT INTO cotton_call_log (id, workspace_id, source, external_id, provider, direction, outcome, started_at, duration_s, handle, created_at, updated_at) VALUES ('call-chitra', ?1, 'ios_call_history', 'ext-chitra', 'com.apple.telephony', 'out', 'answered', ?2, 60, ?3, ?2, ?2)", &[ws(), t(&ago(20)), t(&chitra_phone)], ); let fold = |rows: &[(&str, &str)]| { let rows: Vec<(String, String)> = rows.iter().map(|(table, id)| (table.to_string(), id.to_string())).collect(); h.with(|db| cotton_tools::derive::CoreDerive.derive(db, &rows)).expect("the derive pass runs"); }; fold(&[ ("cotton_msg_contacts", "contact-asha"), ("cotton_msg_contact_handles", "handle-asha"), ("cotton_msg_threads", "thread-asha"), ("cotton_msg_messages", "text-old"), ("cotton_msg_messages", "text-recent"), ("cotton_emails", "mail-bina"), ("cotton_call_log", "call-chitra"), ]); let all = || { let read = call_tool(&h, &token, "list_records", json!({"table": "person", "view": "people/all"})); read["records"].as_array().unwrap().iter().map(|record| { let data = &record["data"]; (data["full_name"].as_str().unwrap().trim_start_matches("Invented ").to_owned(), data["reached_at"].clone(), data["reach_90d"].clone()) }).collect::>() }; let reached = |name: &str, at: Value, count: Value| (name.to_owned(), at, count); assert_eq!(all(), vec![ reached("Bina", json!(ago(3)), json!(1)), reached("Asha", json!(ago(10)), json!(1)), reached("Chitra", json!(ago(20)), json!(1)), reached("Dev", Value::Null, Value::Null), reached("Esha", Value::Null, Value::Null), ]); // One new text from Asha: the fold that brings it moves her alone. let hour_ago = (now - Duration::hours(1)).to_rfc3339_opts(SecondsFormat::Millis, true); text("text-new", &hour_ago); fold(&[("cotton_msg_messages", "text-new")]); assert_eq!(all()[..3], [ reached("Asha", json!(hour_ago), json!(2)), reached("Bina", json!(ago(3)), json!(1)), reached("Chitra", json!(ago(20)), json!(1)), ]); let listed = call_tool(&h, &token, "list_people", json!({})); let names: Vec<&str> = listed["people"].as_array().unwrap().iter().map(|row| row["name"].as_str().unwrap()).collect(); assert_eq!(names, ["Invented Asha", "Invented Bina", "Invented Chitra", "Invented Dev", "Invented Esha"]); }