//! People's acts reach his WhatsApp itself (SENDERS.md, 24 Nov 2026): Block queues the block through //! the Mac, Delete everything queues the chat deleted for him there, and the act's state stands on the sender's row, //! with Retry, as the Mac writes it back (CONTRACT "Blocking a WhatsApp sender, deleting a WhatsApp chat"). Bimal is //! invented. use super::changes::served_without; use super::*; const KEY: &str = "+15550100222"; fn kind_rows(handle: &CottonHandle, kind: &str) -> Vec<(String, Map)> { queued(handle, BIMAL).into_iter().filter(|(_, r)| r.get("kind").and_then(Value::as_str) == Some(kind)).collect() } fn blocked_row(handle: &CottonHandle) -> Value { let page = answer(handle, "chat.senders.blocked", json!({})); page["rows"].as_array().unwrap().iter().find(|r| r["key"] == KEY).cloned().unwrap_or_else(|| panic!("{page}")) } fn sender_row(handle: &CottonHandle) -> Option { let page = answer(handle, "chat.senders", json!({})); page["rows"].as_array().unwrap().iter().find(|r| r["key"] == KEY).cloned() } /// RULE: Block from People files one `block` row for his WhatsApp; the Blocked row says it is on its way, then what /// the Mac wrote back: failed with its words and Retry (back to queued, the same row), then done. Unblock files /// `unblock`. #[test] fn people_block_goes_to_whatsapp_and_its_state_stands_on_the_blocked_row() { let (_dir, handle) = phone(); chats(&handle); answer(&handle, "chat.sender.act", json!({ "key": KEY, "act": "block" })); let rows = kind_rows(&handle, "block"); assert_eq!(rows.len(), 1, "{rows:?}"); assert_eq!(rows[0].1["to"], "15550100222@s.whatsapp.net"); assert_eq!(blocked_row(&handle)["source"]["line"], "Blocking on WhatsApp…"); mark(&handle, &rows[0].0, json!({ "status": "failed", "error": "WhatsApp is not linked on this Mac." })); let row = blocked_row(&handle); assert_eq!((row["source"]["line"].as_str(), row["source"]["retry"].as_str()), (Some("Not blocked on WhatsApp: WhatsApp is not linked on this Mac."), Some("Try again"))); assert_eq!(answer(&handle, "chat.sender.act", json!({ "key": KEY, "act": "retry" }))["said"], "Trying again."); let again = kind_rows(&handle, "block"); assert_eq!((again.len(), again[0].0.as_str(), again[0].1["status"].as_str()), (1, rows[0].0.as_str(), Some("queued")), "the same row, queued again"); mark(&handle, &rows[0].0, json!({ "status": "sent" })); let row = blocked_row(&handle); assert_eq!((row["source"]["line"].as_str(), row["source"]["retry"].is_null()), (Some("Blocked on WhatsApp"), true)); assert_eq!(refused(&handle, "chat.sender.act", json!({ "key": KEY, "act": "retry" })), "nothing_to_retry"); answer(&handle, "chat.sender.act", json!({ "key": KEY, "act": "unblock" })); assert_eq!(kind_rows(&handle, "unblock").len(), 1); assert_eq!(sender_row(&handle).unwrap()["source"]["line"], "Unblocking on WhatsApp…"); } /// RULE (ERASES): Delete everything erases his copy of what Bimal sent at once and files one `delete_chat` row /// naming the chat's newest WhatsApp message, whose wordless marker keeps its WhatsApp id and time for the Mac. The /// sender's row stands on, with nothing left here, while the chat is being deleted there, and with Retry if that /// fails; once done it goes. #[test] fn people_delete_everything_deletes_the_chat_on_whatsapp_and_its_state_stands_on_the_row() { let (_dir, handle) = phone(); chats(&handle); message(&handle, "m-later", BIMAL, "See you", "WA-NEWEST", 5); answer(&handle, "chat.sender.act", json!({ "key": KEY, "act": "delete", "count": 2 })); let rows = kind_rows(&handle, "delete_chat"); assert_eq!(rows.len(), 1, "{rows:?}"); assert_eq!(rows[0].1["target"], "WA-NEWEST"); assert!(kind_rows(&handle, "delete").is_empty(), "never a delete for everyone"); let marker = crate::phone::cotton_chat::read(&handle.client(), "test.marker", |ctx| { Ok(json!(one(ctx, "SELECT body, sent_at, json_extract(meta, '$.cotton.erased') AS erased FROM cotton_msg_messages WHERE external_id = 'WA-NEWEST'", &[])?)) }) .unwrap(); assert_eq!(marker["body"].as_str(), Some(""), "the words are gone, the id and time stay: {marker}"); assert!(marker["erased"].is_string(), "a marker: {marker}"); assert!(marker["sent_at"].as_str().is_some()); let row = sender_row(&handle).expect("the row stands while the chat is deleted there"); assert_eq!((row["sent"].as_u64(), row["source"]["line"].as_str()), (Some(0), Some("Deleting the chat on WhatsApp…"))); mark(&handle, &rows[0].0, json!({ "status": "failed", "error": "This chat's messages aren't in WhatsApp, so it can't be deleted there." })); assert_eq!(sender_row(&handle).unwrap()["source"]["retry"], "Try again"); mark(&handle, &rows[0].0, json!({ "status": "sent" })); assert!(sender_row(&handle).is_none(), "done there, nothing left here"); } /// RULE: where the Worker does not serve the source acts yet, People's Block and Delete everything still do their /// part in Cotton, and file nothing for the Mac. #[test] fn without_the_served_source_acts_people_acts_in_cotton_only() { let (_dir, handle) = phone(); chats(&handle); served_without(&handle, &["block", "unblock", "delete_chat"]); answer(&handle, "chat.sender.act", json!({ "key": KEY, "act": "block" })); answer(&handle, "chat.sender.act", json!({ "key": KEY, "act": "unblock" })); answer(&handle, "chat.sender.act", json!({ "key": KEY, "act": "delete", "count": 1 })); assert!(queued(&handle, BIMAL).is_empty()); assert!(sender_row(&handle).is_none()); }