//! A reply between phones on one relay (23 Nov 2026: on //! the owner's iPhone a sent reply showed no quote). The quote is the core's //! answer on every phone that holds the reply: the writer's, the writer's own //! other phone, and the other person's. Invented numbers and words. use super::*; impl Phone { /// The one-to-one chat with `number` on this phone, as `chat.thread` answers it. fn page_with(&self, number: &str) -> Value { let thread = cotton_core::chat::ids::thread_id(&crate::phone::cotton_chat::namespace(&self.handle.client()).unwrap(), number); self.call("chat.thread", json!({ "thread": thread })) } /// The bubble whose own words are `words`, in the chat with `number`. fn bubble_with(&self, number: &str, words: &str) -> Value { let page = self.page_with(number); page["items"].as_array().unwrap().iter().find(|i| i["item"] == "bubble" && i["text"] == json!(words)).cloned().unwrap_or_else(|| panic!("{}: no bubble {words:?} in {page}", self.number)) } /// Replies to `message` in the chat with `number` as a phone does: the composer's strip, then Send with it. fn reply_with(&self, number: &str, message: &str, words: &str) -> Value { let thread = cotton_core::chat::ids::thread_id(&crate::phone::cotton_chat::namespace(&self.handle.client()).unwrap(), number); let strip = self.call("chat.compose", json!({ "thread": thread, "replyTo": message })); assert_eq!(strip["strip"]["kind"], "reply", "{strip}"); let sent = self.call("chat.act", json!({ "act": "send", "thread": thread, "text": words, "replyTo": message })); self.call("chat.flush", json!({})); sent } } /// Who and what a bubble quotes. fn quoted(bubble: &Value) -> (String, String) { let reply = &bubble["reply"]; assert!(reply.is_object(), "a quote on {bubble}"); (reply["sender"].as_str().unwrap_or_default().to_string(), reply["line"].as_str().unwrap_or_default().to_string()) } // RULE: A reply carries its quote to every phone that holds it: the writer's bubble quotes at once; the other // person's phone and the writer's own other phone draw the same words, each naming who wrote them as that phone // knows them; another sync round changes nothing, and a tap on the quote goes to the message it quotes. #[test] fn a_reply_carries_its_quote_to_every_phone() { let (_relay, p) = world(&[ASHA, ASHA, BEN], &[(ASHA, BEN)]); let (asha, tablet, ben) = (&p[0], &p[1], &p[2]); ben.write(ASHA, "Keep the umbrella, it suits you"); asha.sync(); tablet.sync(); let original = asha.bubble_with(BEN, "Keep the umbrella, it suits you"); let sent = asha.reply_with(BEN, original["message"].as_str().unwrap(), "I will, thank you"); let ben_name = quoted(&sent["bubble"]).0; assert_eq!(quoted(&sent["bubble"]).1, "Keep the umbrella, it suits you", "the writer's bubble quotes at once"); assert!(!ben_name.is_empty()); for round in 0..2 { for phone in &p { phone.sync(); } let on_ben = ben.bubble_with(ASHA, "I will, thank you"); assert_eq!(quoted(&on_ben), ("You".to_string(), "Keep the umbrella, it suits you".to_string()), "round {round}: Ben's own words, quoted on his phone"); let his = ben.bubble_with(ASHA, "Keep the umbrella, it suits you"); assert_eq!(on_ben["reply"]["message"], his["message"], "round {round}: the quote goes to his message"); let on_tablet = tablet.bubble_with(BEN, "I will, thank you"); assert_eq!(quoted(&on_tablet), (ben_name.clone(), "Keep the umbrella, it suits you".to_string()), "round {round}: Asha's other phone"); let mine = asha.bubble_with(BEN, "I will, thank you"); assert_eq!(quoted(&mine), (ben_name.clone(), "Keep the umbrella, it suits you".to_string()), "round {round}: the writer's phone after the flush"); // The sealed message names the message it answers by its writer's id, nothing more. let row = ben.message(on_ben["message"].as_str().unwrap()); assert!(row["reply_to_external_id"].is_string() && row["reply_to_id"] == his["message"], "{row:?}"); } } // RULE: A reply that lands before the message it quotes (the object's order is its clock; a re-send can come late) // still quotes it once both are here. #[test] fn a_reply_that_lands_before_its_quote_still_quotes_it() { let (relay, p) = world(&[ASHA, BEN], &[(ASHA, BEN)]); let (asha, ben) = (&p[0], &p[1]); // Ben writes, then answers his own words, before Asha's phone takes either. ben.write(ASHA, "The bookshop at ten"); let his = ben.bubble_with(ASHA, "The bookshop at ten"); ben.reply_with(ASHA, his["message"].as_str().unwrap(), "Or eleven, if it rains"); relay.0.lock().unwrap().held.get_mut(&asha.credential).unwrap().reverse(); asha.sync(); let reply = asha.bubble_with(BEN, "Or eleven, if it rains"); assert_eq!(quoted(&reply).1, "The bookshop at ten", "{reply}"); let original = asha.bubble_with(BEN, "The bookshop at ten"); assert_eq!(reply["reply"]["message"], original["message"], "the quote goes to it"); } // RULE: A quote of a message deleted for everyone says so in the core's words, on the other person's phone too, // and keeps no word of what it was. #[test] fn a_quote_of_a_deleted_message_says_so() { let (_relay, p) = world(&[ASHA, BEN], &[(ASHA, BEN)]); let (asha, ben) = (&p[0], &p[1]); ben.write(ASHA, "The spare key is under the pot"); asha.sync(); let original = asha.bubble_with(BEN, "The spare key is under the pot"); asha.reply_with(BEN, original["message"].as_str().unwrap(), "Found it"); ben.sync(); let his = ben.bubble_with(ASHA, "The spare key is under the pot"); let thread = cotton_core::chat::ids::thread_id(&crate::phone::cotton_chat::namespace(&ben.handle.client()).unwrap(), ASHA); ben.call("chat.act", json!({ "act": "delete", "thread": thread, "message": his["message"] })); ben.call("chat.flush", json!({})); asha.sync(); for phone in [asha, ben] { let peer = if phone.number == ASHA { BEN } else { ASHA }; let reply = phone.bubble_with(peer, "Found it"); assert_eq!(quoted(&reply).1, cotton_core::messages::display::DELETED_WORDS, "{}: {reply}", phone.number); assert!(!reply.to_string().contains("spare key"), "{}: no word of it left: {reply}", phone.number); } }