//! Which picture the paper screens draw a conversation and a call with. //! //! EVERY FIXTURE HERE IS SYNTHETIC: animals, invented keys, no real number. use rusqlite::types::Value as SqlValue; use serde_json::{json, Map, Value}; use crate::shell::client::phone_tests::{args, attached}; use crate::shell::client::ClientController; fn read(client: &ClientController, name: &str, args: &Map) -> Value { let entry = super::reads::read_for(&format!("read.{name}")) .unwrap_or_else(|| panic!("read.{name} is declared")); super::answer_read(client, entry, args) .unwrap_or_else(|reason| panic!("read.{name} answered: {reason}")) } fn run(client: &ClientController, sql: &str, values: &[&str]) { client .with_writer(|replica| { let bound: Vec = values .iter() .map(|value| SqlValue::Text((*value).to_string())) .collect(); replica .driver .run(sql, &bound) .map_err(|error| format!("{sql}: {error}")) }) .expect("the seed statement"); } const WS: &str = crate::fixture::WORKSPACE; const STAMP: &str = "2026-09-05T00:00:00.000Z"; const PERSON: &str = "55555555-5555-4555-8555-555555555511"; const DM: &str = "44444444-4444-4444-8444-444444444411"; const LOOSE: &str = "44444444-4444-4444-8444-444444444412"; const GROUP: &str = "44444444-4444-4444-8444-444444444413"; const AVATAR: &str = "ws/avatars/alpaca.jpg"; const GROUP_PHOTO: &str = "ws/threads/burrow.jpg"; /// A person with a photograph and their DM; a DM with a number nobody was /// matched to; and a group with its own photograph and three people in it, one /// of whom has left. fn seed(client: &ClientController) { run( client, "INSERT INTO cotton_people (id, workspace_id, full_name, avatar_key, created_at, updated_at) \ VALUES (?,?,'Alpaca Friend',?,?,?)", &[PERSON, WS, AVATAR, STAMP, STAMP], ); run( client, "INSERT INTO cotton_msg_contacts (id, workspace_id, person_id, display_name, created_at, \ updated_at) VALUES ('contact-a',?,?,'Alpaca',?,?)", &[WS, PERSON, STAMP, STAMP], ); run( client, "INSERT INTO cotton_msg_contacts (id, workspace_id, display_name, created_at, updated_at) \ VALUES ('contact-b',?,'Unknown Otter',?,?)", &[WS, STAMP, STAMP], ); for (thread, contact, kind, title, photo, last) in [ (DM, "contact-a", "dm", "", "", "2026-09-05T10:00:00.000Z"), (LOOSE, "contact-b", "dm", "", "", "2026-09-05T09:00:00.000Z"), (GROUP, "", "group", "The Burrow", GROUP_PHOTO, "2026-09-05T08:00:00.000Z"), ] { run( client, "INSERT INTO cotton_msg_threads (id, workspace_id, contact_id, kind, title, photo_key, \ archived, last_message_at, created_at, updated_at) \ VALUES (?,?,NULLIF(?,''),?,NULLIF(?,''),NULLIF(?,''),0,?,?,?)", &[thread, WS, contact, kind, title, photo, last, STAMP, STAMP], ); run( client, "INSERT INTO cotton_msg_messages (id, thread_id, workspace_id, direction, channel, \ origin, body, sent_at, created_at) VALUES (?,?,?,'in','whatsapp','imported', \ 'A synthetic line',?,?)", &[&format!("msg-{thread}"), thread, WS, last, last], ); } for (id, name, left) in [("p-1", "Badger", ""), ("p-2", "Mole", ""), ("p-3", "Vole", STAMP)] { run( client, "INSERT INTO cotton_msg_thread_participants (id, thread_id, workspace_id, display_name, \ left_at, created_at) VALUES (?,?,?,?,NULLIF(?,''),?)", &[id, GROUP, WS, name, left, STAMP], ); } } fn row_for<'a>(inbox: &'a Value, thread: &str) -> &'a Value { inbox["threads"] .as_array() .expect("threads") .iter() .find(|row| row["id"] == json!(thread)) .unwrap_or_else(|| panic!("no row opens {thread}: {inbox}")) } #[test] fn a_persons_conversation_wears_their_photograph_and_a_group_its_own() { let (_dir, client) = attached(); seed(&client); let inbox = read(&client, "threads", &Map::new()); assert_eq!(row_for(&inbox, DM)["pictureKey"], json!(AVATAR)); // ONE print for a group — its own photograph, never its members' faces. assert_eq!(row_for(&inbox, GROUP)["pictureKey"], json!(GROUP_PHOTO)); // A number nobody was matched to has no picture, and says so with a null // rather than an empty string, so the app's decision is one nil check. assert_eq!(row_for(&inbox, LOOSE)["pictureKey"], Value::Null); } #[test] fn a_conversations_header_carries_its_print_and_a_group_its_head_count() { let (_dir, client) = attached(); seed(&client); let dm = read(&client, "thread", &args(&[("threadId", json!(DM))])); assert_eq!(dm["thread"]["pictureKey"], json!(AVATAR)); assert!(dm["thread"].get("memberCount").is_none(), "a DM has no head count"); let group = read(&client, "thread", &args(&[("threadId", json!(GROUP))])); assert_eq!(group["thread"]["pictureKey"], json!(GROUP_PHOTO)); // The one who left is not counted. assert_eq!(group["thread"]["memberCount"], json!(2)); } #[test] fn a_call_from_a_person_carries_their_photograph_and_a_stranger_none() { let (_dir, client) = attached(); seed(&client); run( &client, "INSERT INTO cotton_call_log (id, workspace_id, source, external_id, provider, direction, \ outcome, started_at, duration_s, person_id, created_at, updated_at) VALUES \ ('call-known',?,'ios_call_history','ext-1','cellular','in','answered',?,720,?,?,?)", &[WS, STAMP, PERSON, STAMP, STAMP], ); run( &client, "INSERT INTO cotton_call_log (id, workspace_id, source, external_id, provider, direction, \ outcome, started_at, duration_s, created_at, updated_at) VALUES \ ('call-stranger',?,'ios_call_history','ext-2','cellular','in','missed',?,0,?,?)", &[WS, STAMP, STAMP, STAMP], ); let log = read(&client, "callLog", &Map::new()); let calls = log["calls"].as_array().expect("calls"); let by_id = |id: &str| calls.iter().find(|call| call["id"] == json!(id)).expect(id).clone(); assert_eq!(by_id("call-known")["avatarKey"], json!(AVATAR)); assert_eq!(by_id("call-stranger")["avatarKey"], Value::Null); }