//! `developer.inventPeopleWalk` — COTTON DEV ONLY: people who write to him on WhatsApp, by SMS and by mail, in one//! write, for the iPhone's walk of People (SENDERS.md; `ChatSendersWalkTests`) on a phone with no Mac. Android's walk//! has its own `developer.inventSenders` (WhatsApp only; SMS and mail land through their own paths there). The real Cotton refuses it by name.//!//! ```text//! developer.inventPeopleWalk { round? } → { threads: [<id>…], emails: <n> }//! ```//!//! Every name, number and address is invented://! - a bank's SMS under two headers, `VM-HDFCBK-S` and `AD-HDFCBK` (one sender, HDFCBK), never answered;//! - a courier's SMS from a number, never answered, one of them naming a parcel (a word to search for);//! - Kiran on WhatsApp, answered once;//! - a shop's mail, never answered, and Asha's mail, answered.//!//! People ranks them: HDFCBK (7, never answered), the shop (3, newer), the courier (3), Kiran, Asha. A walk deletes//! the shop's mail, and an erased id never comes back: `round` (a walk's own word) names the shop's mail afresh, so//! each walk has it to delete. Run again with the same round, it writes no row twice (each row's id is fixed).use serde_json::{json, Map, Value};usesuper::cotton_chat::write;usecrate::handle::CottonHandle;usecrate::methods::MethodError;use cotton_tools::mcp::handlers::common::insert_row;const BANK_1: &str = "11111111-1111-4111-8111-00000000df01";const BANK_2: &str = "11111111-1111-4111-8111-00000000df02";const COURIER: &str = "11111111-1111-4111-8111-00000000df03";const KIRAN: &str = "11111111-1111-4111-8111-00000000df04";const ME: &str = "walk@invented.example";pubfn invent(handle: &CottonHandle, request: &Value) -> Result<Value, MethodError> {if handle.flavor() != cotton_core::flavor::Flavor::Dev {returnErr(MethodError::new("not_dev", "developer.inventPeopleWalk answers only in Cotton Dev")); }let client = handle.client();let round: String = request.get("round").and_then(Value::as_str).unwrap_or("").chars().filter(char::is_ascii_alphanumeric).take(24).collect();let fresh = move |id: &str| if round.is_empty() { id.to_string() } else { format!("{id}-{round}") };let now = chrono::Utc::now();let at = move |minutes_ago: i64| super::cotton_chat::iso((now - chrono::Duration::minutes(minutes_ago)).timestamp_millis()); write(&client, "developer.inventPeopleWalk", move |ctx| {let put = |ctx: &mut cotton_tools::mcp::dispatch::Ctx<'_>, table: &str, id: &str, columns: Value| -> Result<(), String> {let held = super::cotton_chat::one(ctx, &format!("SELECT id FROM {table} WHERE workspace_id = ? AND id = ?"), &[super::cotton_chat::ws(ctx), rusqlite::types::Value::Text(id.to_string())])?;if held.is_some() {// An invented contact or chat already here is brought up to date (its number, its key); a message// or a mail already here is left as it is.returnif table == "cotton_msg_contacts" || table == "cotton_msg_threads" {let keep: Map<String, Value> = columns.as_object().cloned().unwrap_or_default().into_iter().filter(|(k, _)| k == "phone_e164" || k == "external_key").collect();if keep.is_empty() { Ok(()) } else { cotton_tools::mcp::handlers::common::update_row(ctx, table, id, keep).map_err(|f| f.to_string()) } } else {Ok(()) }; }let row: Map<String, Value> = columns.as_object().cloned().unwrap_or_default(); insert_row(ctx, table, id, row).map_err(|f| f.to_string()) }; put(ctx, "cotton_msg_contacts", "dev-senders-contact-courier", json!({ "phone_e164": "15550107411", "display_name": "Invented Courier" }))?; put(ctx, "cotton_msg_contacts", "dev-senders-contact-kiran", json!({ "phone_e164": "15550107412", "display_name": "Kiran Invented" }))?;let chats: [(&str, &str, &str, Option<&str>, Option<&str>, &[(&str, bool)]); 4] = [ (BANK_1, "sms", "sms:VM-HDFCBK-S", None, Some("VM-HDFCBK-S"), &[("Your invented OTP is 0000", false), ("Spend alert: 120.00 at an invented shop", false), ("Spend alert: 80.00 at an invented cafe", false), ("Your statement is ready", false), ("Invented offer: a card upgrade", false)]), (BANK_2, "sms", "sms:AD-HDFCBK", None, Some("AD-HDFCBK"), &[("Invented loan offer for you", false), ("Last day for the invented offer", false)]), (COURIER, "sms", "sms:+15550107411", Some("dev-senders-contact-courier"), None, &[("Your invented parcel is out for delivery", false), ("Delivered to the front desk", false), ("Rate your invented delivery", false)]), (KIRAN, "whatsapp", "15550107412@s.whatsapp.net", Some("dev-senders-contact-kiran"), None, &[("Are we still on for Saturday?", false), ("Yes, ten o'clock", true), ("Great, see you", false)]), ];for (i, (thread, channel, key, contact, title, lines)) in chats.iter().enumerate() { put(ctx, "cotton_msg_threads", thread, json!({ "kind": "dm", "external_key": key, "contact_id": contact, "title": title, "last_message_at": at(10 + i as i64), "read_seq": 0 }))?;for (n, (body, mine)) in lines.iter().enumerate() {let id = format!("dev-senders-m-{i}-{n}"); put(ctx, "cotton_msg_messages", &id, json!({ "thread_id": thread, "direction": if *mine { "out" } else { "in" }, "channel": channel, "origin": "imported", "body": body, "sent_at": at(90 - (i * 10 + n) as i64), "seq": n as i64 + 1, "external_id": format!("DEVSENDERS{i}{n}") }))?; } }let mails: [(&str, &str, &str, &str, &str, i64); 5] = [ ("dev-senders-e-shop-1", "dev-senders-et-shop-1", "deals@shop.invented", "Invented Shop", "Invented sale this weekend", 60), ("dev-senders-e-shop-2", "dev-senders-et-shop-2", "deals@shop.invented", "Invented Shop", "Invented sale ends tonight", 50), ("dev-senders-e-shop-3", "dev-senders-et-shop-3", "deals@shop.invented", "Invented Shop", "Invented new arrivals", 40), ("dev-senders-e-asha", "dev-senders-et-asha", "asha@invented.example", "Asha Invented", "The invented lake trip", 30), ("dev-senders-e-me", "dev-senders-et-asha", ME, "Walk Invented", "Re: The invented lake trip", 20), ];for (id, thread, from, name, subject, ago) in mails {let (id, thread) = if from == "deals@shop.invented" { (fresh(id), fresh(thread)) } else { (id.to_string(), thread.to_string()) };let (id, thread) = (id.as_str(), thread.as_str()); put(ctx, "cotton_email_threads", thread, json!({ "external_key": format!("key-{thread}"), "subject": subject, "first_at": at(ago), "last_at": at(ago), "email_count": 1 }))?;let to = if from == ME { "asha@invented.example" } else { ME }; put(ctx, "cotton_emails", id, json!({ "thread_id": thread, "external_id": format!("x-{id}"), "subject": subject, "from_email": from, "from_name": name, "recipients": json!([{ "email": to, "kind": "to" }]), "sent_at": at(ago), "body": subject, "snippet": subject, "unread": true, "account": ME }))?; }Ok(Value::Null) })?;Ok(json!({ "threads": [BANK_1, BANK_2, COURIER, KIRAN], "emails": 5 }))}#[cfg(test)]mod tests {use serde_json::json;/// The real Cotton refuses it by name. #[test]fn the_real_cotton_refuses_it() {let dir = tempfile::tempdir().unwrap();let handle = crate::phone::chat_app::mail::tests::comms(&dir, cotton_core::flavor::Flavor::Real); assert_eq!(super::invent(&handle, &json!({})).unwrap_err().code, "not_dev"); }/// The invented senders read as People draws them: the bank's two headers as one, the courier and Kiran by/// number, the shop and Asha by address; run twice, nothing doubles. #[test]fn the_invented_senders_stand_in_people_once() {let dir = tempfile::tempdir().unwrap();let handle = crate::phone::chat_app::mail::tests::comms(&dir, cotton_core::flavor::Flavor::Dev);for _ in0..2 {crate::methods::dispatch(&handle, "developer.inventPeopleWalk", &json!({})).expect("invented"); }let page = crate::phone::cotton_chat::over(&handle.client(), "chat.senders", &json!({})).unwrap();let keys: Vec<&str> = page["rows"].as_array().unwrap().iter().map(|r| r["key"].as_str().unwrap()).collect(); assert_eq!(keys, ["HDFCBK", "deals@shop.invented", "+15550107411", "+15550107412", "asha@invented.example"], "{page}"); assert_eq!(page["rows"][0]["sent"], 7); assert_eq!(page["rows"][3]["repliedLine"], "You replied"); }}