//! Your people over a scripted object: what each act asks of the object, and //! the Account part drawn from its answer. Every number and name is invented. use std::sync::Arc; use cotton_client::transport::object::ObjectAddress; use cotton_client::transport::scripted::{Reply, ScriptedExchange, Seen}; use serde_json::{json, Value}; use super::{over, part, pick}; use crate::handle::CottonHandle; use crate::phone::passwords::tests::phone; use crate::phone::personal_object::ObjectDoor; const CREDENTIAL: &str = "ctd_11111111-1111-4111-8111-111111111111_test-credential"; const ASKER: &str = "+15550100001"; const FRIEND: &str = "+15550100003"; const LATER: &str = "+15550100004"; fn with_door(handle: &CottonHandle, replies: impl IntoIterator) -> Arc { let exchange = ScriptedExchange::new(replies); let door = ObjectDoor { address: ObjectAddress::open("https://cottondb.example.test").expect("an address"), exchange: exchange.clone(), credential: CREDENTIAL.to_string() }; handle.client().set_object_door(Some(Arc::new(door))); exchange } fn body(seen: &Seen) -> Value { serde_json::from_slice(&seen.body).expect("a JSON body") } // RULE: Your people is asking, connected and waiting in that order, empty ones left out; an ask reads " wants to connect" with Accept (primary), Refuse and Block; a connected person Remove and Block; a waiting one says they have not added you yet and offers Remove; each act carries the method and request it runs; Add a person opens the people_add ask. #[test] fn your_people_draws_asking_connected_and_waiting_from_the_object() { let (_dir, handle) = phone(); let exchange = with_door(&handle, [Reply::ok(&json!({ "people": [{ "number": FRIEND, "name": "Test Friend", "waiting": false }, { "number": LATER, "name": null, "waiting": true }], "asking": [{ "number": ASKER, "name": "Test Asker", "at": "2026-11-14T08:00:00.000Z" }], }).to_string())]); let drawn = part(&handle.client()); assert_eq!(exchange.seen()[0].path, "/v1/chat/people"); assert_eq!(drawn["title"], "Your people"); assert_eq!(drawn["add"], json!({ "words": "Add a person", "hint": "Their phone number, starting with +", "ask": { "kind": "people_add" } })); let ids: Vec<&str> = drawn["sections"].as_array().unwrap().iter().map(|s| s["id"].as_str().unwrap()).collect(); assert_eq!(ids, ["asking", "people", "waiting"]); let asking = &drawn["sections"][0]; assert_eq!(asking["title"], "Asking you"); assert_eq!(asking["rows"][0], json!({ "number": ASKER, "words": format!("{ASKER} wants to connect"), "detail": "calls themselves Test Asker · not in your contacts", "inContacts": false, "initials": "#", "acts": [ { "id": "accept", "words": "Accept", "primary": true, "call": { "method": "people.accept", "request": { "number": ASKER } } }, { "id": "refuse", "words": "Refuse", "call": { "method": "people.refuse", "request": { "number": ASKER } } }, { "id": "block", "words": "Block", "red": true, "call": { "method": "people.block", "request": { "number": ASKER } } }, ], })); let connected = &drawn["sections"][1]["rows"][0]; assert_eq!((connected["words"].as_str(), connected["detail"].as_str()), (Some(FRIEND), Some("calls themselves Test Friend · not in your contacts"))); assert_eq!(connected["inContacts"], false); assert_eq!(connected["acts"].as_array().unwrap().iter().map(|a| a["id"].as_str().unwrap()).collect::>(), ["remove", "block"]); let waiting = &drawn["sections"][2]["rows"][0]; assert_eq!(waiting, &json!({ "number": LATER, "words": LATER, "detail": "They have not added you yet", "inContacts": false, "initials": "#", "acts": [{ "id": "remove", "words": "Remove", "red": true, "call": { "method": "people.remove", "request": { "number": LATER } } }] })); assert!(drawn.get("empty").is_none()); } // RULE (PEOPLE BUILD 1, C7; 1b, M3): a number his contacts hold is named by them, whatever name the server carries, // with the number second; one they do not hold is titled by its NUMBER, the name they gave only the second line. #[test] fn your_people_names_a_number_by_his_contacts_and_marks_one_they_do_not_hold() { let (_dir, handle) = phone(); handle .client() .with_writer(|replica| { let ws: String = replica.driver.get("SELECT workspace_id FROM _meta", &[]).map_err(|e| e.to_string())?.and_then(|r| r.text("workspace_id").map(str::to_string)).unwrap(); let at = "2026-11-27T10:00:00.000Z".to_string(); replica.driver.run("INSERT INTO cotton_people (id, workspace_id, full_name, created_at, updated_at) VALUES ('p-friend', ?, 'Asha Menon', ?, ?)", &[ws.clone().into(), at.clone().into(), at.clone().into()]).map_err(|e| e.to_string())?; replica .driver .run("INSERT INTO cotton_person_handles (id, workspace_id, person_id, kind, value, normalised, source, created_at, updated_at) VALUES ('h-friend', ?, 'p-friend', 'phone', '+1 (555) 010-0003', '15550100003', 'contacts', ?, ?)", &[ws.into(), at.clone().into(), at.into()]) .map_err(|e| e.to_string())?; Ok(()) }) .unwrap(); with_door(&handle, [Reply::ok(&json!({ "people": [{ "number": FRIEND, "name": "Ash", "waiting": false }, { "number": LATER, "name": "Kabir P.", "waiting": false }], "asking": [], }).to_string())]); let drawn = part(&handle.client()); let rows = drawn["sections"][0]["rows"].as_array().unwrap(); assert_eq!((rows[0]["words"].as_str(), rows[0]["detail"].as_str(), rows[0]["inContacts"].as_bool()), (Some("Asha Menon"), Some(FRIEND), Some(true))); assert_eq!((rows[1]["words"].as_str(), rows[1]["detail"].as_str(), rows[1]["inContacts"].as_bool()), (Some(LATER), Some("calls themselves Kabir P. · not in your contacts"), Some(false))); } // RULE: with nobody the part says so; with the object out of reach it says that and draws no section, Add a person still standing. #[test] fn your_people_says_when_there_is_nobody_or_the_object_cannot_be_reached() { let (_dir, handle) = phone(); with_door(&handle, [Reply::ok(r#"{"people":[],"asking":[]}"#), Reply::json(503, r#"{"code":"down","message":"no"}"#)]); let none = part(&handle.client()); assert_eq!(none["empty"], "No one yet. Add a person by their phone number to send them things."); assert_eq!(none["sections"], json!([])); let away = part(&handle.client()); assert_eq!(away["unreachable"], "Your people show when Cotton can be reached."); assert_eq!(away["sections"], json!([])); assert_eq!(away["add"]["words"], "Add a person"); } // RULE: each act asks the object's one relationship (chat's routes) and says what it did by the person's name. #[test] fn each_act_asks_the_objects_route_and_says_it() { let (_dir, handle) = phone(); let exchange = with_door(&handle, (0..5).map(|_| Reply::ok(r#"{"ok":true}"#))); let said = |method: &str| over(&handle.client(), method, &json!({ "number": ASKER, "name": "Test Asker" })).unwrap()["said"].as_str().unwrap().to_string(); assert_eq!(said("people.accept"), "Test Asker is one of your people now"); assert_eq!(said("people.refuse"), "Refused Test Asker"); assert_eq!(said("people.block"), "Blocked Test Asker"); assert_eq!(said("people.remove"), "Removed Test Asker"); assert_eq!(said("people.unblock"), "Unblocked Test Asker"); let paths: Vec = exchange.seen().iter().map(|s| s.path.clone()).collect(); assert_eq!(paths, ["/v1/chat/accept", "/v1/chat/refuse", "/v1/chat/block", "/v1/chat/remove", "/v1/chat/unblock"]); assert_eq!(body(&exchange.seen()[0]), json!({ "number": ASKER })); } // RULE: Add a person asks a number on the phone keyboard; its commit asks the object to accept the number as typed and says the same for any number; the owner's own number is refused in the object's words. #[test] fn add_a_person_asks_a_number_and_says_the_objects_answer() { let (_dir, handle) = phone(); let exchange = with_door(&handle, [ Reply::ok(r#"{"ok":true}"#), Reply::json(400, r#"{"code":"own_number","message":"That is your own number. Add someone else's."}"#), Reply::ok(r#"{"ok":true}"#), ]); let asked = crate::methods::shell_input::call(&handle, &json!({ "op": "ask", "kind": "people_add" })).unwrap(); assert_eq!(asked, json!({ "editor": "line", "title": "Add a person", "words": "", "hint": "Their phone number, starting with +", "keyboard": "phone", "keeps_words": false, "writes": { "kind": "people_add" } })); let commit = |words: &str| crate::methods::shell_input::call(&handle, &json!({ "op": "commit", "writes": asked["writes"], "words": words })).unwrap(); assert_eq!(commit(" +1 555 010 0001 "), json!({ "said": "Added +15550100001. Once they add you too, you are connected." })); assert_eq!((exchange.seen()[0].method.as_str(), exchange.seen()[0].path.as_str()), ("POST", "/v1/chat/accept")); assert_eq!(body(&exchange.seen()[0]), json!({ "number": "+1 555 010 0001" }), "the object keeps the number as sign-in does"); assert_eq!(commit("+15550100002"), json!({ "refusal": "That is your own number. Add someone else's." })); let added = over(&handle.client(), "people.add", &json!({ "number": LATER })).unwrap(); assert_eq!(added, json!({ "said": "Added +15550100004. Once they add you too, you are connected.", "number": LATER })); } // RULE (owner, 15 Nov 2026): New chat opens Your people, the same list Account reads: the connected then the waiting, each to pick, no acts on them and nobody who is only asking; its title is "New chat"; someone not yet among them is added by number, and adding answers the same for any number. #[test] fn new_chat_picks_from_your_people() { let (_dir, handle) = phone(); with_door(&handle, [Reply::ok(&json!({ "people": [{ "number": LATER, "name": null, "waiting": true }, { "number": FRIEND, "name": "Test Friend", "waiting": false }], "asking": [{ "number": ASKER, "name": "Test Asker", "at": "2026-11-14T08:00:00.000Z" }], }).to_string())]); let page = over(&handle.client(), "people.pick", &json!({})).expect("the page"); assert_eq!(page, json!({ "title": "New chat", "heading": "Your people", "rows": [ { "number": FRIEND, "words": FRIEND, "detail": "calls themselves Test Friend · not in your contacts", "inContacts": false, "initials": "#" }, { "number": LATER, "words": LATER, "detail": "They have not added you yet", "inContacts": false, "initials": "#" }, ], "add": { "words": "Add a person", "hint": "Their phone number, starting with +" }, })); // Nobody yet: the page says so and still offers the add. with_door(&handle, [Reply::ok(r#"{"people":[],"asking":[]}"#)]); let empty = pick(&handle.client()); assert_eq!(empty["rows"], json!([])); assert_eq!(empty["empty"], "No one yet. Add a person by their phone number to start a chat."); assert_eq!(empty["add"]["words"], "Add a person"); // The object out of reach: said, never a list that looks empty. with_door(&handle, []); let dark = pick(&handle.client()); assert_eq!(dark["unreachable"], "Your people show when Cotton can be reached."); assert!(dark.get("empty").is_none()); } // RULE (15 Nov 2026): Your people answers a Blocked section after the others, from the object's `blocked`: each row the person's name (or number), "They cannot reach you" and one act, Unblock, which runs people.unblock; an object that answers no `blocked` (before it carried one) draws no such section; New chat never offers a blocked person. #[test] fn your_people_draws_blocked_last_with_unblock() { let (_dir, handle) = phone(); let listed = json!({ "people": [{ "number": FRIEND, "name": "Test Friend", "waiting": false }], "asking": [], "blocked": [{ "number": ASKER, "name": "Test Asker", "at": "2026-11-15T08:00:00.000Z" }, { "number": LATER, "name": null, "at": "2026-11-15T07:00:00.000Z" }], }) .to_string(); with_door(&handle, [Reply::ok(&listed), Reply::ok(&listed), Reply::ok(r#"{"people":[],"asking":[]}"#)]); let drawn = part(&handle.client()); let ids: Vec<&str> = drawn["sections"].as_array().unwrap().iter().map(|s| s["id"].as_str().unwrap()).collect(); assert_eq!(ids, ["people", "blocked"]); let blocked = &drawn["sections"][1]; assert_eq!(blocked["title"], "Blocked"); assert_eq!(blocked["rows"], json!([ { "number": ASKER, "words": ASKER, "detail": "They cannot reach you", "inContacts": false, "initials": "#", "acts": [{ "id": "unblock", "words": "Unblock", "call": { "method": "people.unblock", "request": { "number": ASKER } } }] }, { "number": LATER, "words": LATER, "detail": "They cannot reach you", "inContacts": false, "initials": "#", "acts": [{ "id": "unblock", "words": "Unblock", "call": { "method": "people.unblock", "request": { "number": LATER } } }] }, ])); let page = pick(&handle.client()); assert_eq!(page["rows"].as_array().unwrap().iter().map(|r| r["number"].as_str().unwrap()).collect::>(), [FRIEND], "New chat never offers a blocked person"); assert_eq!(part(&handle.client())["empty"], "No one yet. Add a person by their phone number to send them things.", "an older object, no blocked key"); }