Cotton code
58 lines · 3.4 KB · RustRaw
//! A voice message between phones on one relay (the voice player, 23 Nov//! 2026): its bars and length reach the other person's phone, it is not played//! there until played, and playing it tells the sender nothing. Invented//! numbers and recordings.use super::*;impl Phone {    fn chat_with(&self, number: &str) -> String {        cotton_core::chat::ids::thread_id(&crate::phone::cotton_chat::namespace(&self.handle.client()).unwrap(), number)    }    /// The voice bubble in the chat with `number`.    fn voice_with(&self, number: &str) -> Value {        let page = self.call("chat.thread", json!({ "thread": self.chat_with(number) }));        page["items"].as_array().unwrap().iter().find(|i| i["item"] == "bubble" && i["kind"] == "voice").cloned().unwrap_or_else(|| panic!("{}: no voice message in {page}", self.number))    }}// RULE: A voice message carries the sender's waveform to the other person's phone, which draws the core's 36 bars// and its length; it is not played there until played a second, and playing it sends nothing to the sender: no// message, no receipt, nothing held for them on the relay.#[test]fn a_voice_message_is_played_on_the_listeners_copy_and_the_sender_is_never_told() {    let (relay, p) = world(&[ASHA, BEN], &[(ASHA, BEN)]);    let (asha, ben) = (&p[0], &p[1]);    ben.write(ASHA, "Listen to this when you can");    asha.sync();    let recording = ben._dir.path().join("invented-voice.m4a");    std::fs::write(&recording, vec![7u8; 2_048]).unwrap();    let wave: Vec<f64> = (0..64).map(|i| ((i as f64) * 0.4).sin().abs() * 0.6 + 0.1).collect();    ben.call("chat.act", json!({ "act": "send", "thread": ben.chat_with(ASHA), "attachments": [{ "path": recording.to_string_lossy(), "mime": "audio/mp4", "kind": "voice", "durationMs": 42_000, "waveform": wave }] }));    ben.call("chat.flush", json!({}));    asha.sync();    let heard = asha.voice_with(BEN);    let part = &heard["media"][0];    assert_eq!((part["durationMs"].as_u64(), part["length"].as_str(), part["played"].as_bool()), (Some(42_000), Some("0:42"), Some(false)), "{heard}");    assert_eq!(part["bars"].as_array().map(Vec::len), Some(36));    let own = ben.voice_with(ASHA);    assert!(own["media"][0].get("played").is_none(), "the sender's own is played");    assert_eq!(own["media"][0]["bars"], part["bars"], "both phones draw the same bars from the same note");    // Everything the relay holds for Ben now, then Asha plays his note.    let held_for_ben = |relay: &Relay| relay.0.lock().unwrap().held.get(&ben.credential).map(Vec::len).unwrap_or(0);    let before = held_for_ben(&relay);    let played = asha.call("chat.act", json!({ "act": "played", "thread": asha.chat_with(BEN), "message": heard["message"] }));    assert!(played["bubble"]["media"][0].get("played").is_none(), "{played}");    asha.call("chat.flush", json!({}));    asha.sync();    assert_eq!(held_for_ben(&relay), before, "nothing was sent to the sender");    assert!(asha.voice_with(BEN)["media"][0].get("played").is_none(), "played for good on her copy");    let row = asha.message(heard["message"].as_str().unwrap());    assert!(crate::phone::cotton_chat::meta_value(&row["meta"])["cotton"]["played_at"].is_string(), "{row:?}");    // His next words still land, and her note stays played.    ben.write(ASHA, "Did it play?");    asha.sync();    assert!(asha.voice_with(BEN)["media"][0].get("played").is_none());}