Cotton code
94 lines · 6.2 KB · RustRaw
//! Comms' You, grouped (`chat.you`). Every name and number is invented.use rusqlite::types::Value as SqlValue;use serde_json::{json, Value};use crate::handle::CottonHandle;use crate::phone::passwords::tests::phone;fn sql(handle: &CottonHandle, statement: &str, params: &[SqlValue]) {    handle.client().with_writer(|replica| replica.driver.run(statement, params).map(|_| ()).map_err(|e| e.to_string())).expect(statement);}fn t(s: &str) -> SqlValue {    SqlValue::Text(s.to_string())}fn chat(handle: &CottonHandle, id: &str, title: &str, muted_until: Option<&str>) {    let ws = crate::phone::cotton_chat::read(&handle.client(), "test.ws", |ctx| Ok(json!(ctx.workspace_id))).unwrap();    let at = "2026-11-22T08:00:00.000Z";    sql(        handle,        "INSERT INTO cotton_msg_threads (id, workspace_id, kind, external_key, title, last_message_at, read_seq, muted_until, created_at, updated_at) VALUES (?, ?, 'group', ?, ?, ?, 0, ?, ?, ?)",        &[t(id), t(ws.as_str().unwrap()), t(&format!("group:{id}")), t(title), t(at), muted_until.map_or(SqlValue::Null, t), t(at), t(at)],    );}fn you(handle: &CottonHandle, request: Value) -> Value {    crate::methods::dispatch(handle, "chat.you", &request).unwrap_or_else(|error| panic!("chat.you: {} {}", error.code, error.message))}/// The top level is seven rows in the drawing's order, each saying how it stands in a few words (you-grouped.html):/// how sync stands, the read receipts (and the blocked people when Cotton answers them), notifications with the chats/// muted, what Comms keeps on this phone and the version. Notifications' page lists each muted chat and until when.#[test]fn you_is_eight_rows_that_each_say_how_they_stand() {    let (_dir, handle) = phone();    chat(&handle, "11111111-1111-4111-8111-0000000000f1", "Family", Some(cotton_core::constants::MUTE_ALWAYS_UNTIL));    chat(&handle, "11111111-1111-4111-8111-0000000000f2", "Book club", Some("2099-01-01T00:00:00.000Z"));    chat(&handle, "11111111-1111-4111-8111-0000000000f3", "Lake trip", None);    chat(&handle, "11111111-1111-4111-8111-0000000000f4", "Muted once", Some("2020-01-01T00:00:00.000Z"));    let answer = you(&handle, json!({ "notifications": true }));    let rows = answer["rows"].as_array().unwrap();    let ids: Vec<&str> = rows.iter().map(|r| r["id"].as_str().unwrap()).collect();    assert_eq!(ids, ["export", "sync", "claude", "privacy", "notifications", "storage", "help", "about"]);    let labels: Vec<&str> = rows.iter().map(|r| r["label"].as_str().unwrap()).collect();    assert_eq!(labels, ["Export", "Sync", "Your Claude", "Privacy", "Notifications", "Storage", "Help and feedback", "About"]);    assert_eq!(rows[0]["line"], "Your chats as text, with their pictures and files.", "Comms' own row first, after the card");    let line = |id: &str| rows.iter().find(|r| r["id"] == id).and_then(|r| r["line"].as_str()).unwrap_or_default().to_string();    assert_eq!(line("sync"), "Signed out");    // Signed out, Cotton answers no blocked people: the row says only what this phone holds.    assert_eq!(line("privacy"), "Read receipts off");    assert_eq!(line("notifications"), "On · 2 chats muted", "a mute that ran out is not counted: {answer}");    assert!(line("storage").ends_with(" on this phone"), "{answer}");    assert_eq!(line("help"), "Share a report");    assert!(line("about").starts_with("Version "), "{answer}");    let muted: Vec<(&str, &str)> = answer["muted"].as_array().unwrap().iter().map(|m| (m["name"].as_str().unwrap(), m["until"].as_str().unwrap())).collect();    assert_eq!(muted.len(), 2, "{answer}");    assert!(muted.contains(&("Family", "Until you turn it back on")), "{muted:?}");    assert!(muted.iter().any(|(name, until)| *name == "Book club" && until.starts_with("Until ") && !until.contains("2099-")), "said in words, never a raw date: {muted:?}");    assert_eq!(answer["sync"]["title"], "Signed out");    assert_eq!(answer["sync"]["trouble"], false);    // The phone's notifications off: the row says so, whatever is muted.    let off = you(&handle, json!({ "notifications": false }));    assert_eq!(off["rows"].as_array().unwrap().iter().find(|r| r["id"] == "notifications").unwrap()["line"], "Off");}/// RULE (mail/DECISIONS.md step 9, the coordinator 27 Nov 2026): Comms lets go of Mail behind one line pointing to/// Post. A phone that says whether Post is on it gets You's Mail row right after Export: "Your mail is in Post now"/// with Post's own address on each phone (from=comms), or, without Post, "Post is not on this phone yet". Post has no/// App Store listing yet, so no store link: the row then offers nothing to tap. A phone that says nothing (one that/// cannot open Post yet) gets no row.#[test]fn you_points_to_post_for_the_mail() {    let (_dir, handle) = phone();    let post_of = |answer: &Value| answer["rows"].as_array().unwrap().iter().find(|r| r["id"] == "post").cloned();    assert!(post_of(&you(&handle, json!({}))).is_none(), "no row for a phone that says nothing");    let answer = you(&handle, json!({ "post": true }));    let ids: Vec<&str> = answer["rows"].as_array().unwrap().iter().map(|r| r["id"].as_str().unwrap()).collect();    assert_eq!(&ids[..2], ["export", "post"], "right after Export: {ids:?}");    let post = post_of(&answer).expect("a Mail row");    assert_eq!(post["label"], "Mail");    assert_eq!(post["line"], "Your mail is in Post now");    assert_eq!(post["post"]["ios"]["live"], "cottonmail://open?from=comms", "{post}");    assert_eq!(post["post"]["ios"]["dev"], "cottonmaildev://open?from=comms");    assert_eq!(post["post"]["android"]["package"], "com.cottondb.mail");    assert_eq!(post["post"]["android"]["dev_package"], "com.cottondb.mail.dev");    assert_eq!(post["post"]["absent"], "Post is not on this phone yet.");    assert!(post["post"]["store"].is_null(), "no listing yet: {post}");    let absent = post_of(&you(&handle, json!({ "post": false }))).expect("a Mail row without Post");    assert_eq!(absent["line"], "Post is not on this phone yet.");    let quick = crate::methods::dispatch(&handle, "chat.you.now", &json!({ "post": true })).expect("chat.you.now");    assert_eq!(post_of(&quick).map(|r| r["line"].clone()), Some(json!("Your mail is in Post now")), "the first frame too");}