usesuper::*;usecrate::shell::client::phone_tests::attached;use serde_json::json;fn todo(client: &ClientController, data: Value) -> String {let made = crate::phone::records::call(client, "records.create", &json!({ "table": "todo", "data": data })).unwrap(); ["/record/id", "/id", "/after/id", "/change/after/id"] .iter() .find_map(|pointer| made.pointer(pointer).and_then(Value::as_str)) .unwrap_or_else(|| panic!("the made todo's id: {made}")) .to_string()}fn ids(answer: &Value) -> Vec<String> { answer["acts"].as_array().unwrap().iter().map(|act| act["id"].as_str().unwrap().to_string()).collect()}#[test]fn the_held_card_is_a_read() { assert!(crate::methods::read_only("shell.opened"));for gone in ["cards.actions", "cards.dateChoices", "cards.timeChoices", "cards.repeatChoices"] { assert!(!crate::methods::ALL_METHODS.contains(&gone), "{gone} is shell.opened's now"); }}/// RULE: an open todo held answers its acts whole: Mark done primary and/// first, Set date standing for When (its step), the repeat rule's Repeat/// (its step), Status (its step), Drop and Delete red and last — each act/// in plain JSON a Kotlin shell decodes.#[test]fn an_open_todo_answers_its_acts_ranked_and_flagged() {let (_dir, client) = attached();let id = todo(&client, json!({ "title": "Synthetic water the plants", "due_at": "2026-10-22T09:00:00Z" }));let answer = call(&client, &json!({ "table": "todo", "id": id })).unwrap();// Send (the kit's Send to a person of yours) stands before Drop and Delete. assert_eq!(ids(&answer), ["done", "set-date", "open-repeat-rule", "invite", "edit-notes", "group", "status", "send", "drop", "delete"]);let acts = answer["acts"].as_array().unwrap(); assert_eq!(acts[0], json!({ "id": "done", "label": "Mark done", "kind": "command", "rank": "main", "primary": true, "red": false, "icon": "act-stamp", "mark": "check" })); assert_eq!((acts[1]["step"].as_str(), acts[2]["step"].as_str(), acts[6]["step"].as_str()), (Some("when"), Some("repeat"), Some("status"))); assert_eq!(acts[6]["column"], "status"); assert_eq!(acts[6]["kind"], "chooser");for red in &acts[8..] { assert_eq!((red["rank"].as_str(), red["red"].as_bool()), (Some("away"), Some(true)), "{red}"); } assert_eq!((acts[7]["kind"].as_str(), acts[7]["step"].as_str()), (Some("chooser"), Some("send"))); assert!(acts[1..8].iter().all(|act| act["rank"] == "more" && act["primary"] == false && act["red"] == false));}/// RULE: a held card says which act each mark at rest runs (a todo's check/// runs Mark done, while it holds) and the column its When writes.#[test]fn a_held_card_names_its_marks_acts_and_its_date() {let (_dir, client) = attached();let marks = super::super::card_actions::read_definition(&client, "todo", METHOD).unwrap().marks.unwrap_or_default();let (mark, runs) = marks.iter().next().expect("a todo's card carries a mark");let id = todo(&client, json!({ "title": "Synthetic check", "due_at": "2026-10-22T09:00:00Z" }));let answer = call(&client, &json!({ "table": "todo", "id": id })).unwrap();let marked: Vec<&Value> = answer["acts"].as_array().unwrap().iter().filter(|act| act["mark"].is_string()).collect(); assert_eq!(marked.len(), 1, "{answer}"); assert_eq!((marked[0]["mark"].as_str(), marked[0]["id"].as_str()), (Some(mark.as_str()), Some(runs.as_str()))); assert_eq!(answer["date"], "due_at");}#[test]fn a_held_card_asks_for_its_table_and_its_id_or_its_draft() {let (_dir, client) = attached(); assert_eq!(call(&client, &json!({ "id": "x" })).unwrap_err().code, "bad_request"); assert_eq!(call(&client, &json!({ "table": "todo" })).unwrap_err().code, "bad_request"); assert_eq!(call(&client, &json!({ "table": "todo", "id": "x", "step": "mood" })).unwrap_err().code, "bad_request");}/// RULE: a card not yet made is asked by its values: it offers only what/// supplies its first row — the acts that ask, and Repeat.#[test]fn a_new_cards_draft_offers_what_asks_and_repeat() {let (_dir, client) = attached();let answer = call(&client, &json!({ "table": "todo", "draft": { "status": "open" } })).unwrap(); assert_eq!(ids(&answer), ["set-date", "open-repeat-rule", "invite", "edit-notes"]);}/// RULE: the When step is No date, then the days for the asked day, Pick a/// date last; the Repeat step Never then the presets; each row a value and/// its words.#[test]fn the_when_and_repeat_steps_are_their_rows() {let (_dir, client) = attached();let when = call(&client, &json!({ "table": "todo", "step": "when", "today": "2026-10-22", "timezone": "UTC" })).unwrap(); assert_eq!(when["step"]["kind"], "when");let rows = when["step"]["rows"].as_array().unwrap(); assert_eq!(rows[0], json!({ "value": "none", "words": "No date", "kind": "none", "picture": "act-cross" })); assert_eq!((rows[1]["words"].as_str(), rows[1]["day"].as_str(), rows[1]["kind"].as_str()), (Some("Today"), Some("2026-10-22"), Some("today"))); assert_eq!(rows.last().unwrap()["value"], "pick");let repeat = call(&client, &json!({ "table": "todo", "step": "repeat" })).unwrap();let words: Vec<&str> = repeat["step"]["rows"].as_array().unwrap().iter().map(|row| row["words"].as_str().unwrap()).collect(); assert_eq!(words.len(), 7); assert_eq!(words[0], "Never"); assert!(words.iter().all(|word| !word.is_empty() && !word.contains("FREQ")), "{words:?}");}/// RULE: every row of the When, Time, Repeat and Group steps names the/// picture it is drawn with, and each is one Loom ships — no tile is words/// alone (362 walk: No date, the weeks and every time and repeat were blank).#[test]fn every_step_row_names_a_shipped_picture() {let (_dir, client) = attached();let id = todo(&client, json!({ "title": "Synthetic water the ferns", "due_at": "2026-10-24T09:30:00Z" }));crate::phone::records::call(&client, "groups.create", &json!({ "table": "todo", "name": "Garden", "records": [id.clone()] })).expect("a list");// Loom's catalogue by imageset name, as `pnpm gen:loom-pictures` writes it into cotton-core.let list = cotton_store::manifest_dir().join("../cotton-core/src/definitions/loom_pictures.json");let shipped: Vec<String> = serde_json::from_str(&std::fs::read_to_string(&list).expect("loom_pictures.json")).expect("loom_pictures.json is a list of imageset names");for picture insuper::STEP_PICTURES { assert!(shipped.iter().any(|name| name == picture), "{picture} is not in Loom's catalogue"); }for ask in [ json!({ "table": "todo", "step": "when", "today": "2026-10-22", "timezone": "UTC" }), json!({ "table": "todo", "step": "when", "all": true }), json!({ "table": "todo", "id": id, "step": "time", "timezone": "Asia/Kolkata" }), json!({ "table": "todo", "step": "time", "all": true }), json!({ "table": "todo", "step": "repeat" }), json!({ "table": "todo", "id": id, "step": "group" }), json!({ "table": "todo", "id": id, "step": "status" }), ] {let answer = call(&client, &ask).unwrap();let rows = answer["step"]["rows"].as_array().unwrap(); assert!(!rows.is_empty(), "{ask}");for row in rows {let picture = row["picture"].as_str().unwrap_or_else(|| panic!("{ask}: {row} names no picture")); assert!(shipped.iter().any(|name| name == picture), "{ask}: {picture} is not in Loom's catalogue"); } }}/// RULE: the Time step chooses the record's own hour, said in its zone; a/// new card's chooses No time.#[test]fn the_time_step_chooses_the_records_own_hour() {let (_dir, client) = attached();let id = todo(&client, json!({ "title": "Synthetic post the letter", "due_at": "2026-10-24T09:30:00Z" }));let answer = call(&client, &json!({ "table": "todo", "id": id, "step": "time", "timezone": "Asia/Kolkata" })).unwrap();let rows = answer["step"]["rows"].as_array().unwrap();let words: Vec<&str> = rows.iter().map(|row| row["words"].as_str().unwrap()).collect(); assert_eq!(words, ["No time", "Morning", "9:30", "Noon", "15:00", "Evening", "Pick a time"]);let chosen: Vec<&Value> = rows.iter().filter(|row| row["chosen"] == true).collect(); assert_eq!(chosen.len(), 1); assert_eq!(chosen[0]["time"], "15:00");let blank = call(&client, &json!({ "table": "todo", "draft": {}, "step": "time" })).unwrap(); assert_eq!(blank["step"]["rows"][0], json!({ "value": "none", "words": "No time", "chosen": true, "picture": "act-cross" }));}/// RULE: the Status step is the verbs that move the record's status now,/// by command id: an open todo's Mark done and Drop, a done one's Reopen.#[test]fn the_status_step_is_the_verbs_that_hold_now() {let (_dir, client) = attached();let id = todo(&client, json!({ "title": "Synthetic" }));let open = call(&client, &json!({ "table": "todo", "id": id, "step": "status" })).unwrap(); assert_eq!(open["step"]["rows"], json!([{ "value": "done", "words": "Mark done", "red": false, "picture": "act-stamp" }, { "value": "drop", "words": "Drop", "red": true, "picture": "act-drop" }]));}/// RULE (owner, 5 Nov 2026): Add and Remove sample records stand on this/// device's own card in every development build (Cotton Dev, Books, Music,/// Cinemas Dev) and in an app that keeps no personal object (Rigby), signed/// in or not; a live Cotton's card never offers them.#[test]fn sample_records_stand_on_a_dev_build_and_rigby_never_on_live_cotton() {let samples = |client: &ClientController| -> Vec<String> {let answer = call(client, &json!({ "table": "device", "id": crate::fixture::SELF_DEVICE_ID })).unwrap(); ids(&answer).into_iter().filter(|id| id.ends_with("_samples")).collect() };let (_dev_dir, dev) = attached(); dev.set_self_device_id(Some(crate::fixture::SELF_DEVICE_ID.into())); dev.declare_flavor(cotton_core::flavor::Flavor::Dev).unwrap(); assert_eq!(samples(&dev), ["add_samples", "remove_samples"], "a development build offers the samples");let (_live_dir, live) = attached(); live.set_self_device_id(Some(crate::fixture::SELF_DEVICE_ID.into())); live.declare_flavor(cotton_core::flavor::Flavor::Real).unwrap(); assert!(samples(&live).is_empty(), "a live Cotton never offers them");let (_rigby_dir, rigby) = attached(); rigby.set_self_device_id(Some(crate::fixture::SELF_DEVICE_ID.into())); rigby.declare_flavor(cotton_core::flavor::Flavor::Real).unwrap(); rigby.without_sync().unwrap(); assert_eq!(samples(&rigby), ["add_samples", "remove_samples"], "an app with no object to sign in to offers them");}/// RULE: This device held as a card is read as `records.get` reads it, so/// it offers what only this device does. On a Cotton copy signed in to its/// object that is Sign out, Sync now, Share ink and Erase (owner, 2 Nov/// 2026), Erase red and last, and never another device's Erase.#[test]fn this_devices_card_offers_its_own_acts_and_never_another_devices_erase() {let (_dir, client) = attached(); client.set_self_device_id(Some(crate::fixture::SELF_DEVICE_ID.into()));let answer = call(&client, &json!({ "table": "device", "id": crate::fixture::SELF_DEVICE_ID })).unwrap(); assert_eq!(ids(&answer), ["sign_out", "sync_now", "share_ink", "erase_here"], "{answer}"); assert_eq!(answer["acts"][3]["red"], true);}/// RULE: an act whose one pick names its values (Finished's stars, Rate)/// carries the pick step; its rows are answered on the device/// (`methods::shell_input_tests`).#[test]fn a_named_pick_is_answered_as_its_row() {let (_dir, client) = attached();let made = crate::phone::records::call(&client, "records.create", &json!({ "table": "book", "data": { "title": "Synthetic Lantern", "status": "reading" } })).unwrap();let id = ["/record/id", "/id", "/after/id", "/change/after/id"].iter().find_map(|pointer| made.pointer(pointer).and_then(Value::as_str)).unwrap().to_string();let acts = call(&client, &json!({ "table": "book", "id": id })).unwrap();let step_of = |act: &str| acts["acts"].as_array().unwrap().iter().find(|found| found["id"] == act).map(|found| found["step"].clone()); assert_eq!(step_of("finish"), Some(json!("pick")), "{acts}"); assert_eq!(step_of("rate"), Some(json!("pick")), "{acts}"); assert_eq!(step_of("review"), Some(Value::Null), "words are typed, never a row: {acts}");// The rows are the device's (`methods::shell_input::pick`): a client alone refuses them. assert_eq!(call(&client, &json!({ "table": "book", "id": id, "step": "pick", "act": "finish" })).unwrap_err().code, "bad_request");}/// RULE (books-4-walk, 6 Nov 2026: a Books list's deck held offered no Set/// picture once the held card became the core's): a card held on a list's/// deck offers the list's own acts after its own — Set picture from Photos,/// naming the list it sets — and a card held on any other deck offers none.#[test]fn a_card_held_on_a_lists_deck_offers_the_lists_own_acts() {let (_dir, client) = attached();let made = crate::phone::records::call(&client, "records.create", &json!({ "table": "book", "data": { "title": "Synthetic Lantern", "status": "want_to_read" } })).unwrap();let id = ["/record/id", "/id", "/after/id", "/change/after/id"].iter().find_map(|pointer| made.pointer(pointer).and_then(Value::as_str)).unwrap().to_string();let list = crate::phone::records::call(&client, "groups.create", &json!({ "table": "book", "name": "Synthetic shelf", "records": [id] })).unwrap();let group = list["group"]["id"].as_str().expect("the list's id").to_string();let on_list = call(&client, &json!({ "table": "book", "id": id, "view": format!("books/group:{group}") })).unwrap();let set = on_list["acts"].as_array().unwrap().iter().find(|act| act["id"] == crate::phone::tool_acts::LIST_SET_PICTURE).cloned();let set = set.unwrap_or_else(|| panic!("the list's Set picture: {on_list}")); assert_eq!((set["label"].as_str(), set["group"].as_str()), (Some("Set picture from Photos"), Some(group.as_str())), "{set}");let elsewhere = call(&client, &json!({ "table": "book", "id": id, "view": "books/want_to_read" })).unwrap(); assert!(!ids(&elsewhere).iter().any(|act| act == crate::phone::tool_acts::LIST_SET_PICTURE), "{elsewhere}");}/// RULE (list-acts, 7 Nov 2026): a card held on a list's deck moves along the/// list — Move earlier and Move later, each absent at its end — beside/// Remove from the list; the list's own moves are its tile's and its empty/// deck's, never the card's; Remove list stands red and last. The deck/// deals the list in its order, and a book added lands last.#[test]fn a_card_held_on_a_lists_deck_moves_along_the_list() {let (_dir, client) = attached();let book = |title: &str| {let made = crate::phone::records::call(&client, "records.create", &json!({ "table": "book", "data": { "title": title, "status": "want_to_read" } })).unwrap(); ["/record/id", "/id", "/after/id", "/change/after/id"].iter().find_map(|pointer| made.pointer(pointer).and_then(Value::as_str)).unwrap().to_string() };let (one, two, three) = (book("Synthetic One"), book("Synthetic Two"), book("Synthetic Three"));let list = crate::phone::records::call(&client, "groups.create", &json!({ "table": "book", "name": "Synthetic shelf", "records": [one, two] })).unwrap();let group = list["group"]["id"].as_str().unwrap().to_string();crate::phone::records::call(&client, "groups.update", &json!({ "id": group, "add": [three] })).unwrap();let view = format!("books/group:{group}");let dealt = || -> Vec<String> {let page = crate::phone::records::call(&client, "records.list", &json!({ "table": "book", "where": { "group": group } })).unwrap(); page["records"].as_array().unwrap().iter().map(|record| record["id"].as_str().unwrap().to_string()).collect() }; assert_eq!(dealt(), [one.clone(), two.clone(), three.clone()], "a book added lands last");let list_acts = |id: &str| -> Vec<Value> {let held = call(&client, &json!({ "table": "book", "id": id, "view": view })).unwrap(); held["acts"].as_array().unwrap().iter().filter(|act| act["group"] == json!(group)).cloned().collect() };let words = |acts: &[Value]| acts.iter().map(|act| act["label"].as_str().unwrap().to_string()).collect::<Vec<_>>();let last = list_acts(&three); assert_eq!(words(&last), ["Rename", "Set picture from Photos", "Move earlier", "Remove from Synthetic shelf", "Remove list"]); assert_eq!(last.last().unwrap()["red"], json!(true), "Remove list is red: {last:?}"); assert_eq!(words(&list_acts(&one)), ["Rename", "Set picture from Photos", "Move later", "Remove from Synthetic shelf", "Remove list"]);let earlier = last.iter().find(|act| act["id"] == crate::phone::tool_acts::LIST_MEMBER_EARLIER).unwrap().clone();let run = |act: &Value| crate::phone::tool_acts::call(&client, &json!({ "tool": act["tool"], "act": act["id"], "group": act["group"], "record": act["record"] })).unwrap(); assert_eq!(run(&earlier)["said"], "Reordered Synthetic shelf"); assert_eq!(run(&earlier)["said"], "Reordered Synthetic shelf"); assert_eq!(dealt(), [three.clone(), one.clone(), two.clone()], "the last book is now first");let off = list_acts(&one).into_iter().find(|act| act["id"] == crate::phone::tool_acts::LIST_MEMBER_REMOVE).unwrap();let removed = run(&off); assert_eq!(removed["said"], "Removed Synthetic One from Synthetic shelf"); assert_eq!(dealt(), [three.clone(), two.clone()]);crate::phone::records::call(&client, "records.undo", &json!({ "change_id": removed["undo"] })).unwrap(); assert_eq!(dealt(), [three, one, two], "undo puts the book back in its place");}/// RULE (Music 5 walk, 7 Nov 2026): the group step is the core's row, for/// every app: New list stands first, then the table's lists in their own/// order, each saying how many it holds and whether the held card is in it,/// each with its tool's tile pictogram — so New list is on the screen/// however many lists there are.#[test]fn the_group_step_is_new_list_first_then_the_lists() {let (_dir, client) = attached();let id = todo(&client, json!({ "title": "Synthetic water the plants" }));let other = todo(&client, json!({ "title": "Synthetic feed the cat" }));for (name, records) in [("Garden", vec![id.clone(), other.clone()]), ("House", vec![other.clone()]), ("Errands", vec![])] {letmut ask = json!({ "table": "todo", "name": name });if !records.is_empty() { ask["records"] = json!(records); }crate::phone::records::call(&client, "groups.create", &ask).expect("a list"); }let answer = call(&client, &json!({ "table": "todo", "id": id, "step": "group" })).unwrap(); assert_eq!(answer["step"]["kind"], "group");let rows = answer["step"]["rows"].as_array().unwrap(); assert_eq!(rows[0], json!({ "value": "new", "words": "New group", "new": true, "picture": "act-plus", "asks": { "op": "ask", "kind": "group", "table": "todo", "id": id } }), "New list first: {answer}");let said: Vec<_> = rows[1..].iter().map(|row| (row["words"].as_str().unwrap(), row["detail"].as_str(), row["holds"].as_bool().unwrap())).collect(); assert_eq!(said, [("Garden", Some("2"), true), ("House", Some("1"), false), ("Errands", Some("0"), false)]); assert!(rows[1..].iter().all(|row| row["value"].as_str().is_some_and(|value| !value.is_empty() && value != "new")));// A tool's tile dresses the row: New list in its new_list picture, each list in its list picture.let film = crate::phone::records::call(&client, "records.create", &json!({ "table": "film", "data": { "title": "Synthetic Night Ferry" } })).unwrap();let film = ["/record/id", "/id", "/after/id", "/change/after/id"].iter().find_map(|pointer| film.pointer(pointer).and_then(Value::as_str)).unwrap().to_string();crate::phone::records::call(&client, "groups.create", &json!({ "table": "film", "name": "Weekend", "records": [film] })).expect("a list");let definition = crate::phone::card_actions::read_definition(&client, "film", "test").unwrap();let tile = crate::phone::fan_tile::of_table(&client, &definition, "test").unwrap().expect("Cinemas names its tile");let rows = call(&client, &json!({ "table": "film", "id": film, "step": "group" })).unwrap()["step"]["rows"].clone(); assert_eq!(rows[0]["pictogram"].as_str(), tile.role("new_list"), "{rows}"); assert_eq!(rows[1]["pictogram"].as_str(), tile.role("list"), "{rows}"); assert_eq!(call(&client, &json!({ "table": "todo", "draft": {}, "step": "group" })).unwrap_err().code, "bad_request", "a card not yet made is in no list");}/// RULE: the Face step is the table's offers, the face the card wears/// first and chosen, then the others in the table's order, each said by/// its kit title; none for a table offering one face, or a face outside/// the table's family (a view naming its own face).#[test]fn the_face_step_is_the_tables_offers_worn_first() {let (_dir, client) = attached();let answer = call(&client, &json!({ "table": "song", "step": "face", "face": "song.tray" })).unwrap(); assert_eq!(answer["step"]["kind"], "face");let rows = answer["step"]["rows"].as_array().unwrap();let values: Vec<&str> = rows.iter().map(|row| row["value"].as_str().unwrap()).collect(); assert_eq!(values, ["song.tray", "song.single", "song.cassette"]); assert_eq!(rows.iter().map(|row| row["chosen"] == true).collect::<Vec<_>>(), [true, false, false]); assert!(rows.iter().all(|row| !row["words"].as_str().unwrap().is_empty() && row["picture"] == FACE_PICTURE), "{rows:?}");for (table, face) in [("film", "film.poster"), ("song", "todo.card")] {let none = call(&client, &json!({ "table": table, "step": "face", "face": face })).unwrap(); assert_eq!(none["step"]["rows"], json!([]), "{table} worn as {face}"); }let unworn = call(&client, &json!({ "table": "song", "step": "face" })).unwrap(); assert_eq!(unworn["step"]["rows"][0]["value"], "song.single", "asked with no face, the one chosen stands first"); assert_eq!(rows[1]["writes"], json!({ "kind": "face", "table": "song", "face": "song.single" }), "a row says what it commits");}