Cotton code
206 lines · 11.0 KB · RustRaw
//! Contacts' groups and archive on a real replica: people made through `write` as the app makes them, then every//! method called as the shell calls it, its answer read in the published shape. Every person here is invented.use serde_json::{json, Value};use super::{call, initials};use crate::shell::client::phone_tests::attached;use crate::shell::client::ClientController;fn person(client: &ClientController, name: &str, phone: &str) -> String {    let answer = client        .write("create_person", json!({ "fullName": name, "phones": [phone] }).as_object().unwrap())        .expect("create_person");    answer.pointer("/answer/id").and_then(Value::as_str).expect("an id").to_string()}fn ask(client: &ClientController, method: &str, request: Value) -> Value {    call(client, method, &request).unwrap_or_else(|e| panic!("{method} refused: {e:?}"))}/// How many living people `list_people` answers (the database's own person, You, among them).fn living(client: &ClientController) -> u64 {    client.write("list_people", &serde_json::Map::new()).unwrap().pointer("/answer/total").and_then(Value::as_u64).unwrap()}fn words_of(rows: &Value) -> Vec<String> {    rows.as_array().unwrap().iter().map(|r| r["words"].as_str().unwrap().to_string()).collect()}/// RULE: with no group yet, Groups says so and offers New group; nothing is listed.#[test]fn no_groups_answers_the_empty_foot_and_new_group() {    let (_dir, client) = attached();    let groups = ask(&client, "contacts.groups", json!({}));    assert_eq!(groups["title"], "Groups");    assert_eq!(groups["sections"], json!([]));    assert_eq!(groups["foot"], "No groups yet. Make one with New group.");    assert_eq!(groups["add"]["words"], "New group");    assert_eq!(groups["add"]["call"]["method"], "contacts.group.make");}/// RULE: a group made with people answers the group: its name, count, its people as the home's rows with Take out,/// and its acts (add, rename, and delete, red with its confirm); Groups then lists it under Made by you.#[test]fn a_group_is_made_listed_and_answers_its_people_and_acts() {    let (_dir, client) = attached();    let asha = person(&client, "Asha Menon", "+91 98765 00001");    let ravi = person(&client, "Ravi Shah", "+91 98765 00002");    let made = ask(&client, "contacts.group.make", json!({ "name": "Family", "people": [asha, ravi] }));    assert_eq!(made["title"], "Family");    assert_eq!(made["detail"], "2");    assert_eq!(made["madeBy"], "you");    assert_eq!(made["said"], "Made Family with 2 people");    assert_eq!(words_of(&made["rows"]), ["Asha Menon", "Ravi Shah"]);    let first = &made["rows"][0];    assert_eq!(first["initials"], "AM");    assert_eq!(first["detail"], "+91 98765 00001");    assert_eq!(first["call"], json!({ "method": "contacts.person", "request": { "id": asha } }));    assert_eq!(first["acts"][0]["call"]["method"], "contacts.group.remove");    assert_eq!(first["acts"][0]["red"], true);    let acts: Vec<&str> = made["acts"].as_array().unwrap().iter().map(|a| a["id"].as_str().unwrap()).collect();    assert_eq!(acts, ["add", "rename", "delete"]);    assert_eq!(made["acts"][2]["red"], true);    assert_eq!(made["acts"][2]["confirm"], "Delete Family? The people in it stay in your contacts.");    let groups = ask(&client, "contacts.groups", json!({}));    assert_eq!(groups["sections"][0]["id"], "you");    assert_eq!(groups["sections"][0]["title"], "Made by you");    assert_eq!(words_of(&groups["sections"][0]["rows"]), ["Family"]);    assert_eq!(groups["sections"][0]["rows"][0]["detail"], "2");    assert_eq!(groups["sections"][0]["rows"][0]["call"]["method"], "contacts.group");    assert_eq!(groups["foot"], "Each group says why it was made. Change or delete any of them.");}/// RULE: rename, add, take out and delete each change the group and answer it (delete answers its sentence), and/// the people stay in the contacts whatever happens to the group.#[test]fn rename_add_take_out_and_delete_change_the_group_and_never_the_people() {    let (_dir, client) = attached();    let asha = person(&client, "Asha Menon", "+91 98765 00001");    let ravi = person(&client, "Ravi Shah", "+91 98765 00002");    let dev = person(&client, "Dev Nair", "+91 98765 00003");    let before = living(&client);    let made = ask(&client, "contacts.group.make", json!({ "name": "Family", "people": [asha] }));    let id = made["id"].as_str().unwrap().to_string();    let renamed = ask(&client, "contacts.group.rename", json!({ "id": id, "name": "Home" }));    assert_eq!(renamed["title"], "Home");    let added = ask(&client, "contacts.group.add", json!({ "id": id, "people": [ravi, dev] }));    assert_eq!(words_of(&added["rows"]), ["Asha Menon", "Ravi Shah", "Dev Nair"]);    let taken = ask(&client, "contacts.group.remove", json!({ "id": id, "person": ravi }));    assert_eq!(words_of(&taken["rows"]), ["Asha Menon", "Dev Nair"]);    assert_eq!(taken["detail"], "2");    let deleted = ask(&client, "contacts.group.delete", json!({ "id": id }));    assert_eq!(deleted["said"], "Group deleted");    assert_eq!(ask(&client, "contacts.groups", json!({}))["sections"], json!([]));    assert_eq!(living(&client), before, "deleting a group deletes no one");    assert!(call(&client, "contacts.group", &json!({ "id": id })).is_err(), "a deleted group is gone");}/// RULE: a group is made with at least one person and a name; asking for one with neither is refused, not guessed.#[test]fn a_group_needs_a_name_and_people() {    let (_dir, client) = attached();    let asha = person(&client, "Asha Menon", "+91 98765 00001");    assert!(call(&client, "contacts.group.make", &json!({ "name": "Family", "people": [] })).is_err());    assert!(call(&client, "contacts.group.make", &json!({ "name": " ", "people": [asha] })).is_err());}/// RULE: archive takes a person out of the list (and out of a group's rows) and unarchive brings them back; each/// answers `{id, said}`.#[test]fn archive_and_unarchive_answer_id_and_said_and_move_the_person() {    let (_dir, client) = attached();    let asha = person(&client, "Asha Menon", "+91 98765 00001");    let ravi = person(&client, "Ravi Shah", "+91 98765 00002");    let made = ask(&client, "contacts.group.make", json!({ "name": "Family", "people": [asha, ravi] }));    let id = made["id"].as_str().unwrap().to_string();    let before = living(&client);    let archived = ask(&client, "contacts.archive", json!({ "id": asha }));    assert_eq!(archived, json!({ "id": asha, "said": "Archived" }));    assert_eq!(living(&client), before - 1);    assert_eq!(words_of(&ask(&client, "contacts.group", json!({ "id": id }))["rows"]), ["Ravi Shah"]);    let back = ask(&client, "contacts.unarchive", json!({ "id": asha }));    assert_eq!(back, json!({ "id": asha, "said": "Back in your contacts" }));    assert_eq!(living(&client), before);}/// RULE: the mark's letters: first and last word, upper case; `#` when the name holds no letter.#[test]fn initials_are_first_and_last_letters_or_a_hash() {    assert_eq!(initials("Asha Menon"), "AM");    assert_eq!(initials("asha"), "A");    assert_eq!(initials("Mary-Jane Watson Rao"), "MR");    assert_eq!(initials("+91 98765 00001"), "#");    assert_eq!(initials(""), "#");}/// RULE: pin keeps a person in Close and answers `{id, said}`; the person page then offers Unpin, and unpin takes/// them back out.#[test]fn pin_keeps_a_person_in_close_and_unpin_lets_them_go() {    let (_dir, client) = attached();    let asha = person(&client, "Asha Menon", "+91 98765 00001");    let pinned = ask(&client, "contacts.pin", json!({ "id": asha, "on": true }));    assert_eq!(pinned, json!({ "id": asha, "said": "Kept in Close" }));    let home = crate::phone::contacts_app::call(&client, "contacts.home", &json!({})).unwrap();    let close = home["sections"].as_array().unwrap().iter().find(|s| s["id"] == "close").expect("a Close section");    assert_eq!(words_of(&close["rows"]), ["Asha Menon"]);    let page = crate::phone::contacts_app::call(&client, "contacts.person", &json!({ "id": asha })).unwrap();    assert!(page["acts"].as_array().unwrap().iter().any(|a| a["id"] == "unpin"), "the page offers Unpin: {}", page["acts"]);    let unpinned = ask(&client, "contacts.pin", json!({ "id": asha, "on": false }));    assert_eq!(unpinned["said"], "No longer kept in Close");    let home = crate::phone::contacts_app::call(&client, "contacts.home", &json!({})).unwrap();    assert!(!home["sections"].as_array().unwrap().iter().any(|s| s["id"] == "close"));    assert!(call(&client, "contacts.pin", &json!({ "id": asha })).is_err(), "pin says which way");}/// RULE: a group Contacts made (its `group_origin` says app, with a reason) stands under Made by Contacts with its/// reason; deleting it marks its origin dismissed, so it is not made again.#[test]fn a_group_contacts_made_says_why_and_deleting_it_dismisses_it() {    let (_dir, client) = attached();    let asha = person(&client, "Asha Menon", "+91 98765 00001");    let ravi = person(&client, "Ravi Shah", "+91 98765 00002");    let work = ask(&client, "contacts.group.make", json!({ "name": "Work", "people": [asha, ravi] }));    let work_id = work["id"].as_str().unwrap().to_string();    ask(&client, "contacts.group.make", json!({ "name": "Book club", "people": [ravi] }));    let origin = work_id.clone();    crate::phone::cotton_chat::write(&client, "test.origin", |ctx| {        let mut row = serde_json::Map::new();        row.insert("workspace_id".into(), json!(ctx.workspace_id.to_string()));        row.insert("group_id".into(), json!(origin));        row.insert("made_by".into(), json!("app"));        row.insert("reason".into(), json!("people at acme.example"));        let id = ctx.new_uuid();        cotton_tools::mcp::handlers::common::insert_row(ctx, super::ORIGIN, &id, row).map_err(|e| e.to_string())?;        Ok(json!({}))    })    .unwrap();    let groups = ask(&client, "contacts.groups", json!({}));    let ids: Vec<&str> = groups["sections"].as_array().unwrap().iter().map(|s| s["id"].as_str().unwrap()).collect();    assert_eq!(ids, ["app", "you"]);    assert_eq!(groups["sections"][0]["title"], "Made by Contacts");    assert_eq!(words_of(&groups["sections"][0]["rows"]), ["Work"]);    assert_eq!(groups["sections"][0]["rows"][0]["detail"], "2 · people at acme.example");    assert_eq!(words_of(&groups["sections"][1]["rows"]), ["Book club"]);    let page = ask(&client, "contacts.group", json!({ "id": work_id }));    assert_eq!(page["madeBy"], "app");    assert_eq!(page["detail"], "2 · people at acme.example");    ask(&client, "contacts.group.delete", json!({ "id": work_id }));    let dismissed = crate::phone::cotton_chat::read(&client, "test.origin", |ctx| {        let rows = crate::phone::cotton_chat::rows(ctx, &format!("SELECT dismissed_at FROM {}", super::ORIGIN), &[])?;        Ok(json!(rows.iter().filter(|r| r.get("dismissed_at").is_some_and(|v| !v.is_null())).count()))    })    .unwrap();    assert_eq!(dismissed, json!(1), "the origin is kept, dismissed");    let groups = ask(&client, "contacts.groups", json!({}));    assert_eq!(groups["sections"].as_array().unwrap().len(), 1);}