Cotton code
436 lines · 25.9 KB · RustRaw
//! SMS on Android (DECISIONS "SMS on Android"): the texts Comms reads and receives land in his copy as chats, the ones//! he writes wait for the phone holding the role, and a blocked sender stands only in the Blocked place. Every number,//! name and word is invented.use serde_json::{json, Value};use super::key_of;use crate::handle::CottonHandle;use crate::phone::cotton_chat::over;use crate::phone::passwords::tests::phone;const ASHA: &str = "+919800000101";fn answer(handle: &CottonHandle, method: &str, request: Value) -> Value {    over(&handle.client(), method, &request).unwrap_or_else(|error| panic!("{method}: {} {}", error.code, error.message))}fn text(from: &str, number: Option<&str>, body: &str, at: i64, out: bool) -> Value {    json!({ "from": from, "number": number, "body": body, "at": at, "out": out })}fn row<'a>(list: &'a Value, name: &str) -> Option<&'a Value> {    list["rows"].as_array().unwrap().iter().find(|r| r["title"] == name || r["name"] == name || r.to_string().contains(&format!("\"{name}\"")))}fn thread_of(handle: &CottonHandle, name: &str) -> String {    let list = answer(handle, "chat.list", json!({}));    row(&list, name).and_then(|r| r["thread"].as_str()).unwrap_or_else(|| panic!("{name} in {list}")).to_string()}fn history(handle: &CottonHandle) -> Value {    answer(        handle,        "chat.sms.land",        json!({ "texts": [            text("09800000101", Some(ASHA), "Reached the station", 1_763_900_000_000, false),            text("+919800000101", Some(ASHA), "Coming to pick you up", 1_763_900_060_000, true),            text("JD-HDFCBK", None, "Rs 500 debited from a/c XX01", 1_763_900_120_000, false),            text("VM-HDFCBK-S", None, "Rs 200 credited to a/c XX01", 1_763_900_180_000, false),        ] }),    )}/// RULE: who a text is with. A number is its E.164 and a contact's digits; a sender with a name is one chat by the name/// after the operator's two-letter prefix, whatever the prefix; a short code keeps its digits.#[test]fn a_sender_is_one_chat_by_its_number_or_its_name() {    assert_eq!(key_of("09800000101", Some(ASHA)), Some((ASHA.into(), Some("919800000101".into()), None)));    assert_eq!(key_of("JD-HDFCBK", None), Some(("HDFCBK".into(), None, Some("HDFCBK".into()))));    assert_eq!(key_of("VM-HDFCBK-S", None), Some(("HDFCBK".into(), None, Some("HDFCBK".into()))));    assert_eq!(key_of("AMAZON", None), Some(("AMAZON".into(), None, Some("AMAZON".into()))));    assert_eq!(key_of("56767", None), Some(("56767".into(), None, Some("56767".into()))));    assert_eq!(key_of("  ", None), None);}/// RULE: the history lands once, read, as one chat a sender, each saying it is SMS; read again it lands nothing.#[test]fn the_history_lands_once_as_read_sms_chats() {    let (_dir, handle) = phone();    let first = history(&handle);    assert_eq!(first["landed"], 4, "{first}");    let again = history(&handle);    assert_eq!((again["landed"].clone(), again["already"].clone()), (json!(0), json!(4)), "read twice, landed once");    let list = answer(&handle, "chat.list", json!({}));    assert_eq!(list["rows"].as_array().unwrap().iter().filter(|r| r["unread"].as_u64().unwrap_or(0) > 0).count(), 0, "history is read: {list}");    let bank = thread_of(&handle, "HDFCBK");    let page = answer(&handle, "chat.thread", json!({ "thread": bank }));    let bubbles: Vec<&Value> = page["items"].as_array().unwrap().iter().filter(|i| i["item"] == "bubble").collect();    assert_eq!(bubbles.len(), 2, "both prefixes in one chat: {page}");    assert_eq!(page["header"]["place"]["mark"], "via SMS", "{page}");    assert_eq!(page["readOnly"], "This sender doesn't take replies.");    assert!(answer(&handle, "chat.notices", json!({})).to_string().find(&bank).is_none(), "history never notifies");    // Not the phone holding the role: Asha's chat reads only and says where texts go from.    let asha = answer(&handle, "chat.list", json!({}))["rows"].as_array().unwrap().iter().find(|r| r["thread"] != bank).unwrap()["thread"].as_str().unwrap().to_string();    let page = answer(&handle, "chat.thread", json!({ "thread": asha }));    assert!(page.get("composer").is_none() || page["composer"].is_null(), "{page}");    assert_eq!(page["readOnly"], "Texts go from your Android phone where Comms is the SMS app.");}/// RULE: a text arriving now is unread and notifies.#[test]fn a_text_arriving_now_is_unread_and_notifies() {    let (_dir, handle) = phone();    history(&handle);    let mut live = text("+919800000101", Some(ASHA), "Where are you?", 1_763_990_000_000, false);    live["live"] = json!(true);    assert_eq!(answer(&handle, "chat.sms.land", json!({ "texts": [live] }))["landed"], 1);    let list = answer(&handle, "chat.list", json!({}));    let asha = list["rows"].as_array().unwrap().iter().find(|r| r.to_string().contains("Where are you?")).unwrap_or_else(|| panic!("{list}"));    assert!(asha["unread"].as_u64().unwrap_or(0) > 0, "{asha}");    let thread = asha["thread"].as_str().unwrap();    assert!(answer(&handle, "chat.notices", json!({})).to_string().contains(thread), "it notifies");}/// RULE: on the phone holding the role he writes in an SMS chat as in any; the text waits for the phone, is handed out/// once, says how it went, and the same text read back from Android's store is not landed twice. A failed one says why/// and goes again on Retry.#[test]fn a_text_written_here_goes_once_from_the_phone_holding_the_role() {    let (_dir, handle) = phone();    history(&handle);    let bank = thread_of(&handle, "HDFCBK");    let asha = answer(&handle, "chat.list", json!({}))["rows"].as_array().unwrap().iter().find(|r| r["thread"] != bank).unwrap()["thread"].as_str().unwrap().to_string();    answer(&handle, "chat.sms.here", json!({ "held": true }));    let page = answer(&handle, "chat.thread", json!({ "thread": asha }));    assert_eq!(page["composer"]["place"]["mark"], "via SMS", "{page}");    assert_eq!(answer(&handle, "chat.thread", json!({ "thread": bank }))["readOnly"], "This sender doesn't take replies.");    let sent = answer(&handle, "chat.act", json!({ "act": "send", "thread": asha, "text": "On my way" }));    let id = sent["message"].as_str().unwrap().to_string();    assert_eq!(sent["bubble"]["state"], "Sending…", "{sent}");    let out = answer(&handle, "chat.sms.outbox", json!({}));    assert_eq!(out["texts"].as_array().unwrap().len(), 1, "{out}");    assert_eq!((out["texts"][0]["to"].clone(), out["texts"][0]["body"].clone()), (json!(ASHA), json!("On my way")));    assert_eq!(answer(&handle, "chat.sms.outbox", json!({}))["texts"], json!([]), "handed out once");    assert_eq!(answer(&handle, "chat.sms.sent", json!({ "id": id, "ok": false, "why": "no_service" }))["state"], "failed");    let page = answer(&handle, "chat.thread", json!({ "thread": asha }));    let bubble = page["items"].as_array().unwrap().iter().find(|i| i["message"] == id.as_str()).unwrap();    assert_eq!(bubble["state"], "Not sent: No mobile signal", "{bubble}");    answer(&handle, "chat.act", json!({ "act": "retry", "thread": asha, "message": id }));    let again = answer(&handle, "chat.sms.outbox", json!({}));    assert_eq!(again["texts"][0]["id"], id.as_str(), "Retry queues it again: {again}");    assert_eq!(answer(&handle, "chat.sms.sent", json!({ "id": id, "ok": true }))["state"], "sent");    // Android's store holds it now, at the time the phone was given: read back, it is the same text.    let read_back = text(ASHA, Some(ASHA), "On my way", again["texts"][0]["at"].as_i64().unwrap(), true);    assert_eq!(answer(&handle, "chat.sms.land", json!({ "texts": [read_back] }))["already"], 1);    // Given up: no longer the SMS app, the chat reads only again.    answer(&handle, "chat.sms.here", json!({ "held": false }));    assert_eq!(answer(&handle, "chat.thread", json!({ "thread": asha }))["readOnly"], "Texts go from your Android phone where Comms is the SMS app.");    assert!(over(&handle.client(), "chat.act", &json!({ "act": "send", "thread": asha, "text": "x" })).is_err());}/// RULE: blocking an SMS chat's sender is a sender block (SENDERS.md): their chat leaves the list, is never notified,/// its texts still land and stand in Blocked; unblocked, it comes back. Nothing asks Cotton's server.#[test]fn a_blocked_sender_stands_only_in_the_blocked_place() {    let (_dir, handle) = phone();    history(&handle);    let bank = thread_of(&handle, "HDFCBK");    let page = answer(&handle, "chat.contact", json!({ "thread": bank }));    assert_eq!((page["sms"].clone(), page["blocked"].clone()), (json!(true), json!(false)), "its page offers Block: {page}");    assert!(page["mutes"].as_array().is_some_and(|m| !m.is_empty()), "and Mute");    answer(&handle, "chat.block", json!({ "thread": bank }));    assert_eq!(answer(&handle, "chat.contact", json!({ "thread": bank }))["blocked"], true);    let list = answer(&handle, "chat.list", json!({}));    assert!(row(&list, "HDFCBK").is_none(), "{list}");    let mut live = text("AX-HDFCBK", None, "OTP is 000000", 1_763_990_000_000, false);    live["live"] = json!(true);    assert_eq!(answer(&handle, "chat.sms.land", json!({ "texts": [live] }))["landed"], 1, "kept, not lost");    assert!(row(&answer(&handle, "chat.list", json!({ "filter": "unread" })), "HDFCBK").is_none());    assert!(!answer(&handle, "chat.notices", json!({})).to_string().contains(&bank), "never notified");    let blocked = answer(&handle, "chat.senders.blocked", json!({}));    assert!(blocked["rows"].as_array().unwrap().iter().any(|r| r["name"] == "HDFCBK"), "in Blocked: {blocked}");    answer(&handle, "chat.unblock", json!({ "thread": bank }));    assert!(row(&answer(&handle, "chat.list", json!({})), "HDFCBK").is_some(), "back in the list");}/// RULE: a text handed out whose answer never came (the app closed mid-send) is told failed at the next start, never/// sent again by itself.#[test]fn a_send_whose_answer_never_came_is_failed_not_resent() {    let (_dir, handle) = phone();    history(&handle);    let bank = thread_of(&handle, "HDFCBK");    let asha = answer(&handle, "chat.list", json!({}))["rows"].as_array().unwrap().iter().find(|r| r["thread"] != bank).unwrap()["thread"].as_str().unwrap().to_string();    answer(&handle, "chat.sms.here", json!({ "held": true }));    let id = answer(&handle, "chat.act", json!({ "act": "send", "thread": asha, "text": "Hello" }))["message"].as_str().unwrap().to_string();    assert_eq!(answer(&handle, "chat.sms.outbox", json!({}))["texts"].as_array().unwrap().len(), 1);    handle        .client()        .with_writer(|replica| replica.driver.run("UPDATE cotton_msg_messages SET meta = '{\"sms\":{\"handed\":1}}' WHERE id = ?", &[rusqlite::types::Value::Text(id.clone())]).map(|_| ()).map_err(|e| e.to_string()))        .unwrap();    assert_eq!(answer(&handle, "chat.sms.here", json!({ "held": true }))["stale"], 1);    assert_eq!(answer(&handle, "chat.sms.outbox", json!({}))["texts"], json!([]), "never resent by itself");}/// RULE: what Delete everything erased is not read back from Android's store: a text from the history, read again/// (Comms reinstalled, another of his Android phones), stays erased.#[test]fn an_erased_text_is_not_read_back() {    let (_dir, handle) = phone();    history(&handle);    let ask = answer(&handle, "chat.sender.ask", json!({ "key": "HDFCBK", "act": "delete" }));    let done = answer(&handle, "chat.sender.act", json!({ "key": "HDFCBK", "act": "delete", "count": ask["count"] }));    assert_eq!(done["erased"], 2, "{done}");    let again = history(&handle);    assert_eq!((again["landed"].clone(), again["already"].clone()), (json!(0), json!(4)), "{again}");    assert!(row(&answer(&handle, "chat.list", json!({})), "HDFCBK").is_none());}mod discovery_off_2 {    //! DISCOVERY OFF 2 (27 Nov 2026): on the phone holding the SMS role, a chat with a number not known on Cotton here    //! asks them and never texts by default; the person may send one message as SMS, knowingly, and it says so; when    //! they accept, the chat is on Cotton and what waited goes sealed. An SMS chat already here moves the same way.    use std::sync::Arc;    use cotton_client::transport::object::ObjectAddress;    use cotton_client::transport::scripted::{Reply, ScriptedExchange};    use cotton_core::chat::seal;    use cotton_core::vault::wrap::public_of;    use serde_json::{json, Value};    use super::{answer, history, ASHA};    use crate::handle::CottonHandle;    use crate::phone::passwords::tests::phone;    use crate::phone::personal_object::ObjectDoor;    const NEW: &str = "+919800000177";    fn scalar(seed: u8) -> [u8; 32] {        let mut bytes = [seed; 32];        bytes[0] = seed.wrapping_mul(7).wrapping_add(1);        bytes    }    fn with_door(handle: &CottonHandle, replies: impl IntoIterator<Item = Reply>) -> Arc<ScriptedExchange> {        let exchange = ScriptedExchange::new(replies);        let door = ObjectDoor { address: ObjectAddress::open("https://cottondb.example.test").expect("an address"), exchange: exchange.clone(), credential: "ctd_11111111-1111-4111-8111-111111111111_test-credential".to_string() };        handle.client().set_object_door(Some(Arc::new(door)));        exchange    }    fn hold_definitions(handle: &CottonHandle) {        let thread: Value = serde_json::from_str(include_str!("../../../cotton-core/kits/personal/tables/thread.json")).unwrap();        let contact: Value = serde_json::from_str(include_str!("../../../cotton-core/kits/personal/tables/message_contact.json")).unwrap();        handle.client().with_writer(|replica| cotton_client::transport::object::hold_defined_columns(&replica.driver, [&thread, &contact]).map(|_| ())).unwrap();    }    fn waiting() -> Reply {        Reply::ok(r#"{"state":"waiting"}"#)    }    // RULE: a new chat with a number not known here asks them at once, is a Cotton chat, and texts nothing by itself.    #[test]    fn a_new_chat_asks_and_never_texts_by_default() {        let (_dir, handle) = phone();        hold_definitions(&handle);        answer(&handle, "chat.sms.here", json!({ "held": true }));        let exchange = with_door(&handle, [waiting()]);        let started = answer(&handle, "chat.start", json!({ "number": NEW }));        assert_eq!(started["reach"], "notYet", "{started}");        assert_eq!(exchange.seen().iter().map(|s| (s.method.clone(), s.path.clone())).collect::<Vec<_>>(), vec![("POST".to_string(), "/v1/chat/wait".to_string())]);        let thread = started["thread"].as_str().unwrap().to_string();        let page = answer(&handle, "chat.thread", json!({ "thread": thread }));        assert_eq!(page["reach"]["state"], "notYet");        assert_eq!((page["reach"]["sms"]["instead"].as_str(), page["reach"]["sms"]["mark"].as_str()), (Some("Send as SMS instead"), Some("SMS · not private")), "{}", page["reach"]);        assert_ne!(page["composer"]["place"]["mark"], "via SMS", "a Cotton chat");        // Written as usual, it waits for them: nothing for the phone to text. (The pass first asks who accepted, since        // this phone asked.)        with_door(&handle, [Reply::ok(r#"{"reached":[],"waiting":[]}"#), waiting()]);        let sent = answer(&handle, "chat.send", json!({ "thread": thread, "body": "hello there", "privateKey": seal::key_text(&scalar(2)) }));        assert_eq!(sent["state"], "queued", "{sent}");        assert_eq!(answer(&handle, "chat.sms.outbox", json!({}))["texts"], json!([]), "nothing goes as SMS by default");        // Chosen, one message goes as a text, marked not private.        let chosen = answer(&handle, "chat.act", json!({ "act": "send", "thread": thread, "text": "by text", "asSms": true }));        assert_eq!(chosen["bubble"]["place"]["mark"], "SMS · not private", "{chosen}");        let out = answer(&handle, "chat.sms.outbox", json!({}));        assert_eq!(out["texts"].as_array().unwrap().len(), 1, "{out}");        assert_eq!((out["texts"][0]["to"].clone(), out["texts"][0]["body"].clone()), (json!(NEW), json!("by text")));        // They accept: the chat is on Cotton, and what waited goes sealed for their phone.        let theirs = seal::key_text(&public_of(&scalar(1)));        let exchange = with_door(&handle, [            Reply::ok(&json!({ "reached": [NEW], "waiting": [] }).to_string()),            Reply::ok(&json!({ "me": "+919800000100", "devices": [{ "device": "theirhandle00000000000", "key": theirs }], "own": [] }).to_string()),            Reply::ok(r#"{"ok":true,"id":"x","at":1763000000000}"#),        ]);        let pass = answer(&handle, "chat.flush", json!({ "privateKey": seal::key_text(&scalar(2)) }));        assert_eq!(pass["sent"].as_array().unwrap().len(), 1, "{pass} {:?}", exchange.seen().iter().map(|s| s.path.clone()).collect::<Vec<_>>());        assert!(exchange.seen().iter().any(|s| s.path == "/v1/chat/send"));        let page = answer(&handle, "chat.thread", json!({ "thread": thread }));        assert_eq!(page["reach"]["state"], "on");        assert!(page["reach"].get("sms").is_none(), "no SMS offer once they are on Cotton");    }    // RULE: an SMS chat already here opens as it was, asks them, and becomes their Cotton chat once they accept: its    // texts stand in it, and what is written next goes sealed.    #[test]    fn an_sms_chat_moves_to_cotton_when_they_accept() {        let (_dir, handle) = phone();        hold_definitions(&handle);        history(&handle);        answer(&handle, "chat.sms.here", json!({ "held": true }));        let exchange = with_door(&handle, [waiting()]);        let started = answer(&handle, "chat.start", json!({ "number": ASHA }));        let sms = started["thread"].as_str().unwrap().to_string();        assert_eq!(started["place"]["mark"], "via SMS", "{started}");        assert_eq!(exchange.only().path, "/v1/chat/wait", "asked");        // Nothing waits, yet the pass asks who accepted, since this phone asked.        let exchange = with_door(&handle, [Reply::ok(&json!({ "reached": [ASHA], "waiting": [] }).to_string())]);        answer(&handle, "chat.flush", json!({ "privateKey": seal::key_text(&scalar(2)) }));        assert_eq!(exchange.seen()[0].path, "/v1/chat/reach");        let page = answer(&handle, "chat.thread", json!({ "thread": sms }));        assert_eq!(page["reach"]["state"], "on", "{}", page["reach"]);        assert_ne!(page["composer"]["place"]["mark"], "via SMS", "a Cotton chat now");        assert!(page.to_string().contains("Reached the station"), "its texts stand in it");    }    // RULE: on a phone without the SMS role, nothing changes: no ask at the start, and no SMS offer.    #[test]    fn without_the_role_nothing_is_offered() {        let (_dir, handle) = phone();        hold_definitions(&handle);        let exchange = with_door(&handle, []);        let started = answer(&handle, "chat.start", json!({ "number": NEW }));        assert_eq!(exchange.calls(), 0);        let page = answer(&handle, "chat.thread", json!({ "thread": started["thread"].as_str().unwrap() }));        assert!(page["reach"].get("sms").is_none(), "{}", page["reach"]);        assert!(over_err(&handle, json!({ "act": "send", "thread": started["thread"].as_str().unwrap(), "text": "x", "asSms": true })));    }    fn over_err(handle: &CottonHandle, request: Value) -> bool {        crate::phone::cotton_chat::over(&handle.client(), "chat.act", &request).is_err()    }}mod discovery_off_3 {    //! DISCOVERY OFF 3 (27 Nov 2026), on the phone holding the SMS role: a text back from someone this phone asked lands    //! in their chat (one chat per contact) and never fails anyone else's texts; each text sent in an SMS chat with    //! someone asked says it is not private until they accept; a chosen text left waiting when the role goes fails    //! with its reason; an ask that cannot go at a chat's start is said, never dropped.    use std::sync::Arc;    use cotton_client::transport::object::ObjectAddress;    use cotton_client::transport::scripted::{Reply, ScriptedExchange};    use serde_json::{json, Value};    use super::{answer, history, text, ASHA};    use crate::handle::CottonHandle;    use crate::phone::passwords::tests::phone;    use crate::phone::personal_object::ObjectDoor;    const NEW: &str = "+919800000177";    fn with_door(handle: &CottonHandle, replies: impl IntoIterator<Item = Reply>) -> Arc<ScriptedExchange> {        let exchange = ScriptedExchange::new(replies);        let door = ObjectDoor { address: ObjectAddress::open("https://cottondb.example.test").expect("an address"), exchange: exchange.clone(), credential: "ctd_11111111-1111-4111-8111-111111111111_test-credential".to_string() };        handle.client().set_object_door(Some(Arc::new(door)));        exchange    }    fn hold_definitions(handle: &CottonHandle) {        let thread: Value = serde_json::from_str(include_str!("../../../cotton-core/kits/personal/tables/thread.json")).unwrap();        let contact: Value = serde_json::from_str(include_str!("../../../cotton-core/kits/personal/tables/message_contact.json")).unwrap();        handle.client().with_writer(|replica| cotton_client::transport::object::hold_defined_columns(&replica.driver, [&thread, &contact]).map(|_| ())).unwrap();    }    fn waiting() -> Reply {        Reply::ok(r#"{"state":"waiting"}"#)    }    // RULE (H1): asked from a new Cotton chat, they text back: the text lands in that chat, never a second chat (the    // store refuses two one-to-one chats with a contact), and another sender's text in the same batch lands as well.    #[test]    fn a_text_back_from_someone_asked_lands_in_their_chat() {        let (_dir, handle) = phone();        hold_definitions(&handle);        answer(&handle, "chat.sms.here", json!({ "held": true }));        with_door(&handle, [waiting()]);        let thread = answer(&handle, "chat.start", json!({ "number": NEW }))["thread"].as_str().unwrap().to_string();        let landed = answer(&handle, "chat.sms.land", json!({ "texts": [            text(NEW, Some(NEW), "Got your message", 1_764_000_000_000, false),            text("+919800000101", Some(ASHA), "Someone else entirely", 1_764_000_060_000, false),        ] }));        assert_eq!((landed["landed"].as_u64(), landed["skipped"].as_u64()), (Some(2), Some(0)), "{landed}");        assert!(landed["threads"].as_array().unwrap().iter().any(|t| t == &json!(thread)), "into the asked chat: {landed}");        let page = answer(&handle, "chat.thread", json!({ "thread": thread }));        assert!(page.to_string().contains("Got your message"), "{page}");    }    // RULE (MEDIUM): in an SMS chat with someone this phone asked, each text he sends says "SMS · not private" until    // they accept; texting there stays his default.    #[test]    fn texts_to_someone_asked_say_not_private() {        let (_dir, handle) = phone();        hold_definitions(&handle);        history(&handle);        answer(&handle, "chat.sms.here", json!({ "held": true }));        with_door(&handle, [waiting()]);        let sms = answer(&handle, "chat.start", json!({ "number": ASHA }))["thread"].as_str().unwrap().to_string();        let sent = answer(&handle, "chat.act", json!({ "act": "send", "thread": sms, "text": "On my way" }));        assert_eq!(sent["bubble"]["place"]["mark"], "SMS · not private", "{sent}");        let out = answer(&handle, "chat.sms.outbox", json!({}));        assert_eq!(out["texts"].as_array().map(Vec::len), Some(1), "still sent as a text: {out}");    }    // RULE (LOW): a text he chose, still waiting when Comms stops being the SMS app, is not sent later: it fails, saying why.    #[test]    fn a_waiting_text_fails_when_the_role_goes() {        let (_dir, handle) = phone();        hold_definitions(&handle);        history(&handle);        answer(&handle, "chat.sms.here", json!({ "held": true }));        with_door(&handle, [waiting()]);        let sms = answer(&handle, "chat.start", json!({ "number": ASHA }))["thread"].as_str().unwrap().to_string();        answer(&handle, "chat.act", json!({ "act": "send", "thread": sms, "text": "Later please" }));        answer(&handle, "chat.sms.here", json!({ "held": false }));        answer(&handle, "chat.sms.here", json!({ "held": true }));        assert_eq!(answer(&handle, "chat.sms.outbox", json!({}))["texts"], json!([]), "never sent later");        let page = answer(&handle, "chat.thread", json!({ "thread": sms }));        assert!(page.to_string().contains("Comms is no longer your SMS app"), "{page}");    }    // RULE (MEDIUM): over the new-chats limit, the chat is not made and the server's words are said; offline, the chat    // is made, says it will ask when back, and the next pass that reaches the object asks once.    #[test]    fn an_ask_that_cannot_go_is_said() {        let (_dir, handle) = phone();        hold_definitions(&handle);        answer(&handle, "chat.sms.here", json!({ "held": true }));        with_door(&handle, [Reply::json(429, r#"{"code":"too_many_numbers","message":"You can start 5 new chats an hour. Try again later."}"#)]);        let over = crate::phone::cotton_chat::over(&handle.client(), "chat.start", &json!({ "number": NEW })).expect_err("refused");        assert_eq!((over.code.as_str(), over.message.as_str()), ("too_many_numbers", "You can start 5 new chats an hour. Try again later."));        const LATER: &str = "+919800000188";        with_door(&handle, []);        let started = answer(&handle, "chat.start", json!({ "number": LATER }));        assert_eq!(started["said"], "You're offline. They'll be asked when you're back.", "{started}");        let exchange = with_door(&handle, [waiting(), Reply::ok(r#"{"reached":[],"waiting":[]}"#)]);        answer(&handle, "chat.flush", json!({ "privateKey": cotton_core::chat::seal::key_text(&[9u8; 32]) }));        assert!(exchange.seen().iter().any(|s| s.path == "/v1/chat/wait"), "asked once back: {:?}", exchange.seen().iter().map(|s| s.path.clone()).collect::<Vec<_>>());        let exchange = with_door(&handle, [Reply::ok(r#"{"reached":[],"waiting":[]}"#)]);        answer(&handle, "chat.flush", json!({ "privateKey": cotton_core::chat::seal::key_text(&[9u8; 32]) }));        assert!(!exchange.seen().iter().any(|s| s.path == "/v1/chat/wait"), "asked only once");    }}