Cotton code
71 lines · 4.2 KB · RustRaw
//! Acts on his WhatsApp itself, through the Mac (owner, 24 Nov 2026; CONTRACT "Blocking a WhatsApp sender, deleting a//! WhatsApp chat"): a person blocked there, and a person's chat deleted there for him alone. Invented people.use super::changes::served_without;use super::*;fn header_acts(handle: &CottonHandle, thread: &str) -> Vec<String> {    let page = answer(handle, "chat.thread", json!({ "thread": thread }));    page["header"]["acts"].as_array().map(|a| a.iter().map(|x| x["act"].as_str().unwrap().to_string()).collect()).unwrap_or_default()}fn kind_rows(handle: &CottonHandle, kind: &str) -> Vec<Map<String, Value>> {    queued(handle, BIMAL).into_iter().map(|(_, r)| r).filter(|r| r.get("kind").and_then(Value::as_str) == Some(kind)).collect()}// RULE: a person's WhatsApp chat offers Block on WhatsApp and Delete on WhatsApp too where the served outbox takes// them; a Worker without them offers neither and refuses both, queuing nothing.#[test]fn a_whatsapp_chat_offers_its_source_acts_only_where_the_outbox_takes_them() {    let (_dir, handle) = phone();    chats(&handle);    assert_eq!(header_acts(&handle, BIMAL), vec!["blockOnWhatsApp".to_string(), "deleteOnWhatsApp".to_string()]);    served_without(&handle, &["block", "unblock", "delete_chat"]);    assert!(header_acts(&handle, BIMAL).is_empty());    assert_eq!(refused(&handle, "chat.act", json!({ "act": "blockOnWhatsApp", "thread": BIMAL })), "cannot");    assert_eq!(refused(&handle, "chat.act", json!({ "act": "deleteOnWhatsApp", "thread": BIMAL, "count": 1 })), "cannot");    assert!(queued(&handle, BIMAL).is_empty());}// RULE: Block on WhatsApp queues one `block` row for the chat's address, naming no message, and marks the person// blocked here at once; chat.block on that chat does the same, and chat.unblock queues `unblock`.#[test]fn a_block_on_whatsapp_is_one_row_for_the_mac() {    let (_dir, handle) = phone();    chats(&handle);    answer(&handle, "chat.act", json!({ "act": "blockOnWhatsApp", "thread": BIMAL }));    let rows = kind_rows(&handle, "block");    assert_eq!(rows.len(), 1, "{rows:?}");    assert_eq!((rows[0]["to"].as_str(), rows[0]["status"].as_str(), rows[0].get("target")), (Some("15550100222@s.whatsapp.net"), Some("queued"), None));    let blocked = crate::phone::cotton_chat::read(&handle.client(), "test.blocked", |ctx| {        Ok(json!(one(ctx, "SELECT blocked FROM cotton_msg_contacts WHERE id = 'c-bimal'", &[])?.map(|r| r.get("blocked").cloned()).unwrap_or_default()))    })    .unwrap();    assert!(blocked == json!(1) || blocked == json!(true), "{blocked}");    answer(&handle, "chat.unblock", json!({ "thread": BIMAL }));    assert_eq!(kind_rows(&handle, "unblock").len(), 1);}// RULE (ERASES): Delete on WhatsApp too says the count first and does nothing; only that count sent back deletes:// the chat goes from Cotton as Delete does, and one `delete_chat` row names the chat's newest WhatsApp message. It// is never a delete for everyone: no `delete` row.#[test]fn a_chat_deleted_on_whatsapp_asks_the_count_first() {    let (_dir, handle) = phone();    chats(&handle);    message(&handle, "m-later", BIMAL, "See you", "WA-NEWEST", 5);    let asked = over(&handle.client(), "chat.act", &json!({ "act": "deleteOnWhatsApp", "thread": BIMAL })).unwrap_err();    assert_eq!(asked.code, "count_first");    assert_eq!(asked.details["count"], 2);    assert!(asked.message.contains("Its 2 messages"), "{}", asked.message);    assert!(queued(&handle, BIMAL).is_empty(), "the count alone queues nothing");    assert_eq!(refused(&handle, "chat.act", json!({ "act": "deleteOnWhatsApp", "thread": BIMAL, "count": 1 })), "count_first");    assert!(queued(&handle, BIMAL).is_empty());    answer(&handle, "chat.act", json!({ "act": "deleteOnWhatsApp", "thread": BIMAL, "count": 2 }));    let rows = kind_rows(&handle, "delete_chat");    assert_eq!(rows.len(), 1, "{rows:?}");    assert_eq!((rows[0]["target"].as_str(), rows[0]["to"].as_str()), (Some("WA-NEWEST"), Some("15550100222@s.whatsapp.net")));    assert!(kind_rows(&handle, "delete").is_empty(), "never a delete for everyone");    let list = answer(&handle, "chat.list", json!({}));    assert!(list["rows"].as_array().unwrap().iter().all(|r| r["thread"] != BIMAL), "{list}");}