Cotton code
962 lines · 57.8 KB · RustRaw
//! `shell.input`, `shell.draft` and an act's picks over synthetic phones://! what each write kind writes, the one create per draft, Pick a date and//! Pick a time, and a record pick narrowed by ink.use super::*;use crate::handle::CottonHandle;const TODAY: &str = "2026-11-05";const ZONE: &str = "Asia/Kolkata";fn phone() -> (tempfile::TempDir, CottonHandle) {    let dir = tempfile::tempdir().expect("a temp container");    let manifest: Value = serde_json::from_str(include_str!("../../../../apps/cotton/App/CottonManifest/flavour.json")).expect("a manifest");    let handle = CottonHandle::open(crate::handle::OpenConfig {        mode: crate::handle::OpenMode::Phone { container_path: dir.path().to_string_lossy().to_string(), previous_container_path: None },        app_version: None,        time_zone: Some(ZONE.to_string()),        device_secret: Some(crate::fixture::paired_secret()),        vault_secret: None,        flavor: cotton_core::flavor::Flavor::Real,        manifest: Some(manifest),    })    .expect("an open core");    crate::methods::dispatch(&handle, "workspace.startLocal", &json!({"deviceName": "An invented phone"})).expect("started");    (dir, handle)}fn ask(handle: &CottonHandle, method: &str, mut request: Value) -> Value {    request["timezone"] = json!(ZONE);    request["today"] = json!(TODAY);    crate::methods::dispatch(handle, method, &request).unwrap_or_else(|error| panic!("{method} {request}: {error:?}"))}fn make(handle: &CottonHandle, table: &str, data: Value) -> String {    let made = ask(handle, "records.create", json!({"table": table, "data": data}));    made["id"].as_str().or_else(|| made["record"]["id"].as_str()).unwrap_or_else(|| panic!("a made id: {made}")).to_string()}fn record(handle: &CottonHandle, table: &str, id: &str) -> Value {    let got = ask(handle, "records.get", json!({"table": table, "id": id}));    got.get("data").cloned().unwrap_or(got)}fn commit(handle: &CottonHandle, writes: &Value, words: &str) -> Value {    ask(handle, "shell.input", json!({"op": "commit", "writes": writes, "words": words}))}fn titled(handle: &CottonHandle, table: &str, title: &str) -> usize {    let listed = ask(handle, "records.list", json!({"table": table, "limit": 200}));    listed["records"].as_array().unwrap().iter().filter(|row| row["data"]["title"] == title).count()}/// RULE: a card's words are asked in the line (a title, which keeps its/// words) or on the whole screen (a longtext); committed, they are the/// column's patch, cleared to null when typed away, and words that change/// nothing write nothing.#[test]fn a_column_write_is_its_patch_and_a_title_keeps_its_words() {    let (_dir, handle) = phone();    let id = make(&handle, "todo", json!({"title": "Synthetic water the plants", "status": "open"}));    let title = ask(&handle, "shell.input", json!({"op": "ask", "kind": "column", "table": "todo", "id": id, "column": "title"}));    assert_eq!((title["editor"].as_str(), title["words"].as_str(), title["keeps_words"].as_bool()), (Some("line"), Some("Synthetic water the plants"), Some(true)), "{title}");    assert_eq!(title["writes"], json!({"kind": "column", "table": "todo", "id": id, "column": "title"}));    let written = commit(&handle, &title["writes"], "  Synthetic water the ferns ");    assert!(written["change"].is_object(), "the store's own write: {written}");    assert_eq!(record(&handle, "todo", &id)["title"], "Synthetic water the ferns");    assert_eq!(commit(&handle, &title["writes"], ""), json!({}), "a title typed away keeps its words");    assert_eq!(commit(&handle, &title["writes"], "Synthetic water the ferns"), json!({}), "the same words write nothing");    let notes = ask(&handle, "shell.input", json!({"op": "ask", "kind": "column", "table": "todo", "id": id, "column": "notes"}));    assert_eq!((notes["editor"].as_str(), notes["title"].as_str(), notes["keeps_words"].as_bool()), (Some("page"), Some("Synthetic water the ferns"), Some(false)), "{notes}");    commit(&handle, &notes["writes"], "Synthetic: the balcony ones");    assert_eq!(record(&handle, "todo", &id)["notes"], "Synthetic: the balcony ones");    commit(&handle, &notes["writes"], "   ");    assert!(record(&handle, "todo", &id)["notes"].is_null(), "typed away, a body is cleared to null");}/// RULE: a column the shell may not write on this record is refused at the ask.#[test]fn a_column_the_shell_may_not_write_is_refused() {    let (_dir, handle) = phone();    let id = make(&handle, "note", json!({"title": "Synthetic page", "body": "Words"}));    let refused = crate::methods::dispatch(&handle, "shell.input", &json!({"op": "ask", "kind": "column", "table": "note", "id": id, "column": "kind"})).unwrap_err();    assert_eq!(refused.code, "refused");}/// RULE: a held card's tap map says what each of its places writes: its/// date the days, its state the statuses, its group the lists, its words/// the line or the whole screen, an event's picture its kind; a new card's/// too, by its values.#[test]fn the_tap_map_says_what_each_place_writes() {    let (_dir, handle) = phone();    let todo = make(&handle, "todo", json!({"title": "Synthetic call the plumber", "status": "open"}));    let held = ask(&handle, "shell.opened", json!({"table": "todo", "id": todo}));    let parts = &held["parts"];    assert_eq!(parts["title"], json!({"does": "line", "column": "title"}), "{parts}");    assert_eq!(parts["body"], json!({"does": "page", "column": "notes"}));    assert_eq!(parts["date"], json!({"does": "days", "column": "due_at"}));    assert_eq!(parts["state"]["does"], "statuses");    assert_eq!(parts["group"], json!({"does": "groups"}));    assert!(parts.get("check").is_none() && parts.get("started").is_none(), "a place the shell writes nothing on is left out: {parts}");    let event = make(&handle, "event", json!({"title": "Synthetic standup", "starts_at": "2026-11-06T04:30:00.000Z"}));    let held = ask(&handle, "shell.opened", json!({"table": "event", "id": event}));    assert_eq!(held["parts"]["photo"], json!({"does": "options", "column": "kind"}));    let options = ask(&handle, "shell.opened", json!({"table": "event", "step": "options", "column": "kind"}));    let rows = options["step"]["rows"].as_array().unwrap();    assert!(!rows.is_empty() && rows.iter().all(|row| row["value"].is_string() && !row["words"].as_str().unwrap().is_empty()), "{options}");    let new = ask(&handle, "shell.opened", json!({"table": "note", "draft": {}}));    assert_eq!(new["parts"]["title"], json!({"does": "line", "column": "title"}), "{new}");    let closed = make(&handle, "note", json!({"title": "Synthetic kept page", "body": "Words"}));    let held = ask(&handle, "shell.opened", json!({"table": "note", "id": closed}));    assert!(held["parts"].get("kind").is_none());}/// RULE (one create per draft): a new todo's When, its time, its Repeat/// and an act chosen before its create lands make ONE record holding every/// one of them./// RULE: A new event is a whole day on its day until a time is set: a blank page stands on the day it was asked for, else today in the person's zone, as the day alone with no end and no `all_day`, and a table that is no event's stands on nothing.#[test]fn a_blank_event_stands_on_its_day_alone() {    let (_dir, handle) = phone();    let today_page = ask(&handle, "shell.draft", json!({"table": "event", "blank": true}));    assert_eq!(today_page, json!({"values": {"starts_at": TODAY}}));    let chosen = ask(&handle, "shell.draft", json!({"table": "event", "blank": true, "day": "2026-11-09"}));    assert_eq!(chosen, json!({"values": {"starts_at": "2026-11-09"}}));    let unread = ask(&handle, "shell.draft", json!({"table": "event", "blank": true, "day": "date-1"}));    assert_eq!(unread, json!({"values": {"starts_at": TODAY}}), "a day that does not read is today");    let note = ask(&handle, "shell.draft", json!({"table": "note", "blank": true}));    assert_eq!(note, json!({"values": {}}));}#[test]fn a_draft_takes_every_choice_made_before_its_create_lands() {    let (_dir, handle) = phone();    // A day a year out: a weekly series puts down its next occurrence once its    // first is due by the real clock, under the first title, so a day near now    // leaves a second "Synthetic weekly review" (it failed on 20 Nov 2026).    let draft = ask(&handle, "shell.draft", json!({"table": "todo", "values": {"status": "open"}}));    let id = draft["id"].as_str().expect("a draft's id").to_string();    assert!(draft["made"].is_null());    ask(&handle, "shell.draft", json!({"id": id, "stage": {"when": {"date": "2027-11-20"}}}));    let timed = ask(&handle, "shell.draft", json!({"id": id, "stage": {"when": {"time": "09:30"}}}));    assert_eq!(timed["values"]["due_at"], "2027-11-20T04:00:00.000Z", "the time joins its day in the zone: {timed}");    ask(&handle, "shell.draft", json!({"id": id, "stage": {"repeat": "weekly"}}));    let staged = ask(&handle, "shell.draft", json!({"id": id, "stage": {"act": "edit-notes", "answers": ["Synthetic agenda"]}}));    assert_eq!(staged["staged"], json!({"acts": ["edit-notes"], "repeat": "weekly"}));    let new = ask(&handle, "shell.input", json!({"op": "ask", "kind": "new", "table": "todo", "draft": id}));    assert_eq!((new["hint"].as_str(), new["keeps_words"].as_bool()), (Some("New todo"), Some(true)), "{new}");    let made = commit(&handle, &new["writes"], "Synthetic weekly review");    let record_id = made["made"].as_str().unwrap_or_else(|| panic!("a made record: {made}")).to_string();    assert!(made.get("refusal").is_none(), "{made}");    let held = record(&handle, "todo", &record_id);    assert_eq!(held["title"], "Synthetic weekly review");    assert_eq!(held["due_at"], "2027-11-20T04:00:00.000Z");    assert_eq!(held["notes"], "Synthetic agenda");    assert!(held["recurrence_id"].as_str().is_some_and(|series| !series.is_empty()), "{held}");    // Committed again once made: written onto the same record.    commit(&handle, &new["writes"], "Synthetic weekly review, renamed");    assert_eq!(titled(&handle, "todo", "Synthetic weekly review"), 0);    assert_eq!(titled(&handle, "todo", "Synthetic weekly review, renamed"), 1, "one create per draft");    // A choice put on the made draft writes the record at once.    ask(&handle, "shell.draft", json!({"id": id, "stage": {"values": {"notes": "Synthetic later"}}}));    assert_eq!(record(&handle, "todo", &record_id)["notes"], "Synthetic later");}// RULE (walk 361, step 04): a card not yet made is the core's card of its values — a new event's day said in words, never the stored `2026-11-06`, and its time once one is set.#[test]fn a_draft_wears_the_card_its_values_resolve_to() {    let (_dir, handle) = phone();    let draft = |values: Value| crate::methods::dispatch(&handle, "shell.draft", &json!({"table": "event", "values": values}))        .unwrap_or_else(|error| panic!("the draft's card: {error:?}"))["card"].clone();    let whole = draft(json!({"title": "Invented dentist", "starts_at": "2026-11-09"}));    assert_eq!(whole["face"], "event.rolodex");    assert_eq!(whole["fields"]["title"], "Invented dentist");    let day = whole["fields"]["date"].as_str().unwrap_or_else(|| panic!("a day in words: {whole}"));    assert!(day.contains("9 Nov") && !day.contains("2026-"), "the day in words: {day}");    let timed = draft(json!({"title": "Invented dentist", "starts_at": "2026-11-09T10:00:00+05:30", "ends_at": "2026-11-09T11:00:00+05:30"}));    let fields = timed["fields"].as_object().unwrap();    assert!(fields.values().filter_map(Value::as_str).all(|said| !said.contains("2026-") && !said.contains("T0")), "no stored value said: {timed}");    assert!(fields["date"].as_str().unwrap().contains("10:00"), "the clock set: {timed}");    assert!(!crate::methods::read_only("shell.draft"), "a stage writes once its record is made");    assert_eq!(draft(json!({}))["face"], "event.rolodex", "a blank draft still wears its card");}/// RULE: a new event's day is today until one is chosen, its card said in words.#[test]fn a_new_events_day_is_today() {    let (_dir, handle) = phone();    let draft = ask(&handle, "shell.draft", json!({"table": "event"}));    assert_eq!(draft["values"]["starts_at"], TODAY, "{draft}");}/// RULE: a new card of a table whose "+" holds today in a day it requires/// (`create.dated`, a journal page's day) is begun on today, by "+" and by a/// draft alike, and its title alone makes it; a day already chosen stands; an/// optional dated column (a todo's due) is left unset.#[test]fn a_new_journal_page_is_dated_today() {    let (_dir, handle) = phone();    let draft = ask(&handle, "shell.draft", json!({"table": "journal_entry"}));    assert_eq!(draft["values"]["day"], TODAY, "{draft}");    let chosen = ask(&handle, "shell.draft", json!({"table": "journal_entry", "values": {"day": "2026-10-01"}}));    assert_eq!(chosen["values"]["day"], "2026-10-01", "{chosen}");    let new = ask(&handle, "shell.input", json!({"op": "ask", "kind": "new", "table": "journal_entry"}));    let made = commit(&handle, &new["writes"], "Synthetic walk up the hill");    assert!(made["refusal"].is_null(), "{made}");    let todo = ask(&handle, "shell.draft", json!({"table": "todo"}));    assert!(todo["values"].get("due_at").is_none(), "{todo}");}/// RULE: a refused create keeps the draft as typed, still new, and says why.#[test]fn a_refused_create_keeps_the_draft() {    let (_dir, handle) = phone();    let draft = ask(&handle, "shell.draft", json!({"table": "todo", "values": {"status": "not_a_status"}}));    let id = draft["id"].as_str().unwrap().to_string();    let new = ask(&handle, "shell.input", json!({"op": "ask", "kind": "new", "table": "todo", "draft": id}));    let refused = commit(&handle, &new["writes"], "Synthetic refused todo");    assert!(refused["refusal"].as_str().is_some_and(|words| !words.is_empty()), "{refused}");    let kept = ask(&handle, "shell.draft", json!({"id": id}));    assert!(kept["made"].is_null());    assert_eq!(kept["values"]["title"], "Synthetic refused todo");    assert_eq!(titled(&handle, "todo", "Synthetic refused todo"), 0);}/// RULE: a new list's name is refused in the lists' words when empty or/// another list of the table has it, as Rename is; made, it holds the card.#[test]fn a_new_list_is_refused_empty_or_taken() {    let (_dir, handle) = phone();    let id = make(&handle, "todo", json!({"title": "Synthetic buy stamps", "status": "open"}));    let asked = ask(&handle, "shell.input", json!({"op": "ask", "kind": "group", "table": "todo", "id": id}));    assert!(asked["hint"].as_str().is_some_and(|hint| !hint.is_empty()), "{asked}");    assert_eq!(commit(&handle, &asked["writes"], "  ")["refusal"], "Give the list a name.");    let made = commit(&handle, &asked["writes"], "Synthetic errands");    assert!(made["change"].is_object(), "{made}");    assert_eq!(commit(&handle, &asked["writes"], "synthetic ERRANDS")["refusal"], "There is already a list called synthetic ERRANDS.");    let rows = ask(&handle, "shell.opened", json!({"table": "todo", "id": id, "step": "group"}));    assert!(rows["step"]["rows"].as_array().unwrap().iter().any(|row| row["words"] == "Synthetic errands" && row["holds"] == true), "{rows}");}/// RULE: a list's row in the Group step commits what it says: the card/// joins a list that does not hold it and leaves one that does.#[test]fn a_lists_row_joins_and_leaves() {    let (_dir, handle) = phone();    let id = make(&handle, "todo", json!({"title": "Synthetic post a card", "status": "open"}));    let other = make(&handle, "todo", json!({"title": "Synthetic book a cab", "status": "open"}));    ask(&handle, "groups.create", json!({"table": "todo", "name": "Synthetic trips", "records": [other]}));    let row = |handle: &_| {        let rows = ask(handle, "shell.opened", json!({"table": "todo", "id": id, "step": "group"}));        rows["step"]["rows"].as_array().unwrap().iter().find(|row| row["words"] == "Synthetic trips").cloned().expect("the list's row")    };    let out = row(&handle);    assert_eq!(out["holds"], false, "{out}");    assert_eq!(out["writes"]["kind"], "group");    commit(&handle, &out["writes"], "");    let joined = row(&handle);    assert_eq!(joined["holds"], true, "{joined}");    commit(&handle, &joined["writes"], "");    assert_eq!(row(&handle)["holds"], false);}/// RULE: a face row commits the table's one choice; the face already chosen/// writes nothing, and a face the table does not offer is refused.#[test]fn a_face_is_chosen_once_and_only_from_the_offers() {    let (_dir, handle) = phone();    let writes = json!({"kind": "face", "table": "person", "face": "person.passport"});    assert!(commit(&handle, &writes, "")["change"].is_object());    assert_eq!(commit(&handle, &writes, ""), json!({}), "the face already chosen writes nothing");    let step = ask(&handle, "shell.opened", json!({"table": "person", "step": "face"}));    assert_eq!(step["step"]["rows"][0]["value"], "person.passport", "{step}");    assert_eq!(step["step"]["rows"][0]["chosen"], true, "{step}");    let foreign = json!({"kind": "face", "table": "person", "face": "note.page"});    assert!(commit(&handle, &foreign, "")["refusal"].is_string());}/// RULE: a tool act's words (a list's Rename) run the act with them.#[test]fn a_tool_acts_words_run_it() {    let (_dir, handle) = phone();    let book = make(&handle, "book", json!({"title": "Synthetic Lantern", "status": "want_to_read"}));    let list = ask(&handle, "groups.create", json!({"table": "book", "name": "Synthetic shelf", "records": [book]}));    let group = list["group"]["id"].as_str().unwrap().to_string();    let acts = ask(&handle, "shell.opened", json!({"table": "book", "id": book, "view": format!("books/group:{group}")}));    let rename = acts["acts"].as_array().unwrap().iter().find(|act| act["id"] == crate::phone::tool_acts::LIST_RENAME).cloned().expect("Rename");    let mut asked = json!({"op": "ask", "kind": "tool_act"});    for key in ["tool", "group", "words"] {        asked[key] = rename[key].clone();    }    asked["act"] = rename["id"].clone();    let words = ask(&handle, "shell.input", asked);    assert_eq!(words["words"], "Synthetic shelf", "{words}");    let ran = commit(&handle, &words["writes"], "Synthetic tall shelf");    assert!(ran["said"].as_str().is_some() && ran["undo"].as_str().is_some(), "{ran}");    assert_eq!(commit(&handle, &words["writes"], "")["refusal"], "Give the list a name.");}/// RULE: a "+" that hands its words to an act at the object asks them under/// its door's label; empty words run nothing, and the object's answer (or/// its refusal, on a phone with no object) is the commit's.#[test]fn an_object_acts_words_are_its_argument() {    let (_dir, handle) = phone();    let asked = ask(&handle, "shell.input", json!({"op": "ask", "kind": "act", "table": "book", "door": "find"}));    assert_eq!(asked["hint"], "Find a book");    assert_eq!(asked["writes"], json!({"kind": "act", "table": "book", "name": "find_book", "argument": "query", "lands": "book"}));    assert_eq!(commit(&handle, &asked["writes"], " "), json!({}));    let ran = commit(&handle, &asked["writes"], "Synthetic atlas");    assert!(ran["refusal"].is_string() || ran["lands"]["id"].is_string(), "{ran}");}/// RULE (owner, 11 Nov 2026): Passwords' Plus offers New password, whose/// typed site runs on this device (`password.new`), never as an object act;/// a locked vault answers `lend`, so the shell reads this device's key once/// and commits again, and empty words run nothing.#[test]fn a_password_doors_words_run_on_this_device_and_a_locked_vault_asks_a_lend() {    let (_dir, handle) = phone();    let plus = ask(&handle, "shell.plus", json!({"table": "secret"}));    assert_eq!(plus["does"], "doors", "{plus}");    let doors: Vec<&str> = plus["doors"].as_array().unwrap().iter().map(|door| door["id"].as_str().unwrap()).collect();    assert_eq!(doors, ["new", "apple_passwords"]);    assert_eq!(plus["doors"][1]["accepts"]["files"]["types"], json!(["public.comma-separated-values-text"]));    let asked = ask(&handle, "shell.input", json!({"op": "ask", "kind": "act", "table": "secret", "door": "new"}));    assert_eq!(asked["hint"], "New password");    assert_eq!(asked["writes"], json!({"kind": "act", "table": "secret", "name": "password_new", "argument": "site", "lands": "id"}));    assert_eq!(commit(&handle, &asked["writes"], " "), json!({}));    let locked = commit(&handle, &asked["writes"], "mail.invented.test");    assert_eq!(locked["lend"], true, "{locked}");    assert_eq!(locked["refusal"], "The vault is locked. Unlock it to see this.");}/// RULE: Pick a date is every day from today for 120 days, Pick a time/// every half hour, each in the database's words.#[test]fn pick_a_date_and_a_time_are_every_day_and_half_hour() {    let (_dir, handle) = phone();    let days = ask(&handle, "shell.opened", json!({"table": "todo", "step": "when", "all": true}));    let rows = days["step"]["rows"].as_array().unwrap();    assert_eq!(rows.len(), PICKED_DAYS as usize);    assert_eq!(rows[0], json!({"value": "pick-2026-11-05", "words": "Thu 5 Nov", "kind": "day", "day": "2026-11-05", "picture": "edit-value-day"}), "from today");    assert_eq!(rows[119]["day"], "2027-03-04");    let times = ask(&handle, "shell.opened", json!({"table": "todo", "step": "time", "all": true}));    let rows = times["step"]["rows"].as_array().unwrap();    assert_eq!(rows.len(), 48);    assert_eq!(rows[19], json!({"value": "pick-09:30", "words": "9:30 am", "time": "09:30", "picture": "act-clock"}));    assert_eq!(rows[0]["words"], "12:00 am");    assert_eq!(rows[27]["words"], "1:30 pm");}/// RULE: a pick asking free words asks them in the writer, holding what its/// column holds; the same words run nothing, others run the act.#[test]fn a_pick_of_words_asks_them_in_the_writer() {    let (_dir, handle) = phone();    let id = make(&handle, "todo", json!({"title": "Synthetic tax forms", "status": "open", "notes": "Synthetic first"}));    let step = ask(&handle, "shell.opened", json!({"table": "todo", "id": id, "step": "pick", "act": "edit-notes"}));    let words = &step["step"]["words"];    assert_eq!((words["editor"].as_str(), words["words"].as_str(), words["title"].as_str()), (Some("page"), Some("Synthetic first"), Some("Synthetic tax forms")), "{step}");    assert_eq!(commit(&handle, &words["writes"], "Synthetic first"), json!({}));    let ran = commit(&handle, &words["writes"], "Synthetic second");    assert!(ran["ran"].is_object(), "{ran}");    assert_eq!(record(&handle, "todo", &id)["notes"], "Synthetic second");}/// RULE: a labelled number's pick is its values in order, then Skip; each/// tile says its value alone (★, ★★, …) and the question is the step's/// label, said once — never repeated under every tile (362 walk: every/// stars tile read "How many stars").#[test]fn a_finished_books_stars_are_a_labelled_row() {    let (_dir, handle) = phone();    let id = make(&handle, "book", json!({"title": "Synthetic atlas", "status": "reading"}));    let step = ask(&handle, "shell.opened", json!({"table": "book", "id": id, "step": "pick", "act": "finish"}));    let rows = step["step"]["rows"].as_array().unwrap();    let values: Vec<&str> = rows.iter().map(|row| row["value"].as_str().unwrap()).collect();    assert_eq!(values, ["1", "2", "3", "4", "5", ""], "{step}");    assert_eq!(rows[1]["words"], "★★", "{step}");    assert_eq!(step["step"]["label"], "How many stars", "the question is the step's, once");    assert!(rows.iter().all(|row| row.get("detail").is_none()), "no tile repeats the question: {step}");}/// RULE: an act of several picks asks them one after another — a record/// pick's rows narrowed by the ink letters by a word's start, a number's/// stepper resting on its count — and the last answer runs it.#[test]fn an_order_asks_each_pick_and_ink_narrows_its_records() {    let (_dir, handle) = phone();    for tool in ["kitchen", "kitchen_customers", "kitchen_orders", "kitchen_plans"] {        ask(&handle, "tools.add", json!({"tool_id": tool}));    }    let dish = make(&handle, "kitchen_dish", json!({"name": "Synthetic pork curry"}));    let day = make(&handle, "kitchen_menu_day", json!({"name": "Synthetic Friday", "dish": dish, "on": "2026-11-06", "quota": 10}));    make(&handle, "kitchen_customer", json!({"name": "Synthetic Asha Rao"}));    let bina = make(&handle, "kitchen_customer", json!({"name": "Synthetic Bina Das"}));    let first = ask(&handle, "shell.opened", json!({"table": "kitchen_dish", "id": dish, "step": "pick", "act": "order"}));    let step = &first["step"];    assert_eq!((step["label"].as_str(), step["table"].as_str()), (Some("Day"), Some("kitchen_menu_day")), "{first}");    let rows = step["rows"].as_array().unwrap();    assert_eq!(rows[0]["value"], day.as_str());    assert_eq!(rows.last().unwrap()["does"], "search");    let second = commit(&handle, &step["writes"], &day);    let step = &second["step"];    assert_eq!(step["label"], "Customer", "{second}");    let answers: Vec<&str> = step["rows"].as_array().unwrap().iter().filter(|row| row["does"] == "answer").map(|row| row["words"].as_str().unwrap()).collect();    assert_eq!(answers.len(), 2, "{second}");    let inked = ask(&handle, "shell.opened", json!({"table": "kitchen_dish", "id": dish, "step": "pick", "act": "order", "answers": [day], "ink": "bi"}));    let kept: Vec<&str> = inked["step"]["rows"].as_array().unwrap().iter().filter(|row| row["does"] == "answer").map(|row| row["value"].as_str().unwrap()).collect();    assert_eq!(kept, [bina.as_str()], "{inked}");    let none = ask(&handle, "shell.opened", json!({"table": "kitchen_dish", "id": dish, "step": "pick", "act": "order", "answers": [day], "ink": "zq"}));    assert!(none["step"]["rows"].as_array().unwrap().iter().all(|row| row["does"] != "answer"), "letters that leave nothing leave nothing: {none}");    let third = commit(&handle, &step["writes"], &bina);    let step = &third["step"];    assert_eq!(step["label"], "Quantity", "{third}");    let rows = step["rows"].as_array().unwrap();    let rest = step["rest"].as_u64().unwrap() as usize;    assert_eq!((rows[rest]["value"].as_str(), rows[rest]["does"].as_str()), (Some("1"), Some("answer")), "the stepper rests on its count: {third}");    assert!(rows.iter().all(|row| row["does"] != "count" || row["count"].as_f64().unwrap() >= 1.0), "never under the pick's min");    let ran = commit(&handle, &step["writes"], "2");    assert!(ran["ran"].is_object() || ran["refusal"].is_string(), "the last answer runs the act: {ran}");    let reread = crate::methods::dispatch(&handle, "shell.opened", &json!({"table": "kitchen_dish", "id": dish, "step": "pick", "act": "order", "answers": [day, bina, "2"]}));    assert_eq!(reread.unwrap_err().code, "bad_request", "the pick step is a read: it never runs the act");}fn shipped(handle: &CottonHandle, table: &str) -> TableDefinition {    definition_of(&handle.client(), table).unwrap_or_else(|error| panic!("{table} ships: {error:?}"))}/// RULE: a highlight's passage, place and moment are the book's: its tap/// map writes only its note; a thread's words are its engine's, so no part/// of its card writes words or a day, and their taps are the card's own.#[test]fn a_highlight_writes_its_note_and_a_thread_writes_nothing() {    let (_dir, handle) = phone();    let marked = json!({"book": "b-1", "text": "Every map leaves a coast out.", "colour": "red", "page_label": "37", "chapter": "The Coast"});    let highlight = parts(&shipped(&handle, "highlight"), marked.as_object().unwrap());    assert_eq!(highlight.get("note"), Some(&json!({"does": "page", "column": "note"})), "{highlight:?}");    for slot in ["quote", "place", "crumb", "source", "date"] {        assert!(highlight.get(slot).is_none(), "a highlight's {slot} is the book's: {highlight:?}");    }    let chat = json!({"name": "Invented Ada", "last_message_preview": "See you"});    let thread = parts(&shipped(&handle, "thread"), chat.as_object().unwrap());    assert!(thread.values().all(|part| part["does"] == "viewer"), "a thread's words route nowhere: {thread:?}");}/// RULE: a text pick that names a list column offers that column's words/// exactly, only from a read-only, non-secret column holding words alone;/// anything else offers nothing, never free typing.#[test]fn a_text_picks_choices_come_only_from_its_read_only_list() {    let definition = TableDefinition::from_json(r#"{"id":"sample","name":"Sample","plural":"Samples","builtin":false,"title":"name",        "columns":[{"key":"name","type":"text"},{"key":"offered","type":"longtext","readonly":true}],        "slip":{"title":"name"},"tile":{"face":"slip-list"},"page":[]}"#).expect("a sample");    let pick: CommandPick = serde_json::from_value(json!({"kind": "text", "argument": "ownerName", "choices_from": "offered"})).unwrap();    let row = json!({"offered": ["Asha", "  Chosen exactly  "]});    assert_eq!(choices_from(&definition, &pick, row.as_object().unwrap()), Some(vec!["Asha".to_string(), "  Chosen exactly  ".to_string()]));    for value in [json!(null), json!("Asha"), json!([]), json!([" "]), json!(["Asha", 2])] {        assert_eq!(choices_from(&definition, &pick, json!({"offered": value}).as_object().unwrap()), None, "{value}");    }    let mut open = definition.clone();    open.columns[1].readonly = None;    assert_eq!(choices_from(&open, &pick, row.as_object().unwrap()), None, "a column the person writes is no list to choose from");    let mut secret = definition.clone();    secret.columns[1].secret = Some(true);    assert_eq!(choices_from(&secret, &pick, row.as_object().unwrap()), None);}/// RULE: a held highlight's Recolour offers its colour column's values on/// the fan in their words, and Note asks its words on the whole screen,/// holding the note it has.#[test]fn a_highlights_recolour_chooses_and_its_note_asks_words() {    let (_dir, handle) = phone();    let book = make(&handle, "book", json!({"title": "Synthetic Lantern", "status": "reading"}));    let mark = make(&handle, "highlight", json!({"book": book, "text": "Synthetic passage", "note": "Synthetic thought", "colour": "red"}));    let recolour = ask(&handle, "shell.opened", json!({"table": "highlight", "id": mark, "step": "pick", "act": "recolour"}));    let rows = recolour["step"]["rows"].as_array().unwrap_or_else(|| panic!("{recolour}"));    let colour = shipped(&handle, "highlight").column("colour").cloned().unwrap();    let values: Vec<&str> = rows.iter().map(|row| row["value"].as_str().unwrap()).collect();    assert_eq!(values, colour.choices.iter().flatten().map(String::as_str).collect::<Vec<_>>(), "{recolour}");    assert!(recolour["step"]["words"].is_null(), "a colour is chosen on the fan, never written");    let note = ask(&handle, "shell.opened", json!({"table": "highlight", "id": mark, "step": "pick", "act": "note"}));    let words = &note["step"]["words"];    assert_eq!((words["editor"].as_str(), words["words"].as_str()), (Some("page"), Some("Synthetic thought")), "{note}");}// MARK: shell.input {op: line}const NOW: &str = "2026-11-05T09:00:00+05:30";fn line(handle: &CottonHandle, at: Value, words: &str) -> Value {    ask(handle, "shell.input", json!({"op": "line", "at": at, "words": words, "now": NOW}))}fn line_chosen(handle: &CottonHandle, at: Value, words: &str, record: &str) -> Value {    ask(handle, "shell.input", json!({"op": "line", "at": at, "words": words, "now": NOW, "record": record}))}fn spoken(handle: &CottonHandle, at: Value, words: &str) -> Value {    ask(handle, "shell.input", json!({"op": "line", "at": at, "words": words, "now": NOW, "spoken": true}))}fn todos() -> Value {    json!({"list": {"table": "todo"}})}fn only_write(answer: &Value) -> &Value {    let writes = answer["writes"].as_array().unwrap_or_else(|| panic!("writes: {answer}"));    assert_eq!(writes.len(), 1, "one write: {answer}");    &writes[0]}fn refused(answer: &Value) -> &str {    assert_eq!(answer["writes"], json!([]), "a refusal writes nothing: {answer}");    answer["refusal"].as_str().unwrap_or_else(|| panic!("a refusal: {answer}"))}/// RULE (board): a command has no record there and is refused; any other/// line is kept as a thought said nowhere in particular.#[test]fn a_line_on_the_board_is_a_thought_and_a_command_is_refused() {    let (_dir, handle) = phone();    let kept = line(&handle, json!({"board": {}}), "Synthetic idea for the garden");    let write = only_write(&kept);    assert_eq!((write["kind"].as_str(), write["words"].as_str()), (Some("thought"), Some("Synthetic idea for the garden")), "{kept}");    assert!(write.get("table").is_none());    // Committed, it is the store's capture: kept, or refused in its words    // where this phone's kit draws no thoughts.    let committed = commit(&handle, &kept["writes"], "");    assert!(committed.get("refusal").is_none_or(Value::is_string), "{committed}");    assert!(refused(&line(&handle, json!({"board": {}}), "done synthetic milk")).contains("done"));}/// RULE (list, create): a plain line is a new record of the list, its title/// the words; committed, the record is there.#[test]fn a_plain_line_on_a_list_is_a_new_record() {    let (_dir, handle) = phone();    let made = line(&handle, todos(), "Synthetic milk");    assert_eq!(only_write(&made), &json!({"kind": "create", "table": "todo", "data": {"title": "Synthetic milk"}}));    commit(&handle, &made["writes"], "");    assert_eq!(titled(&handle, "todo", "Synthetic milk"), 1);}/// RULE (add): "add" leaves the title, and so does a leading kind name.#[test]fn add_drops_the_verb_and_the_kind() {    let (_dir, handle) = phone();    assert_eq!(only_write(&line(&handle, todos(), "add Synthetic oats"))["data"]["title"], "Synthetic oats");    assert_eq!(only_write(&line(&handle, todos(), "add todo Synthetic rye"))["data"]["title"], "Synthetic rye");}/// RULE (column fill): the day goes to the date column and its joining word/// with it; a time with no time column goes onto the day as an instant.#[test]fn a_day_and_a_time_fill_the_date() {    let (_dir, handle) = phone();    let data = only_write(&line(&handle, todos(), "Synthetic buy milk tomorrow"))["data"].clone();    assert_eq!((data["title"].as_str(), data["due_at"].as_str()), (Some("Synthetic buy milk"), Some("2026-11-06")), "{data}");    let data = only_write(&line(&handle, todos(), "Synthetic call mum tomorrow at 6pm"))["data"].clone();    assert!(data["due_at"].as_str().is_some_and(|at| at.starts_with("2026-11-06T12:30")), "6pm in Kolkata as an instant: {data}");    assert_eq!(data["title"], "Synthetic call mum");}/// RULE (column fill): a project named fills the project column and a/// person the link to the people (read on a kit that draws them); with no/// project named a create takes the switcher's.#[test]fn a_project_named_fills_its_column_else_the_switchers() {    use super::line::{Names, Piece, Reading};    let (_dir, handle) = phone();    let todo = definition_of(&handle.client(), "todo").expect("the todo table");    let parsed = cotton_core::phrase::parse_phrase("Synthetic paint for Allotment", NOW, ZONE, &["Allotment".into()], &[]).expect("read");    let names = Names { projects: vec![("Allotment".into(), "synthetic-project".into())], people: Vec::new(), people_table: Some("person".into()) };    let reading = Reading { text: "Synthetic paint for Allotment".into(), parsed, names };    let fill = reading.fill(&todo, ZONE, NOW, None, &[Piece::Date, Piece::Time, Piece::Project, Piece::Person]);    assert_eq!(fill.values.get("project"), Some(&json!("synthetic-project")));    assert_eq!(reading.words_without(&fill.used), "Synthetic paint", "the joining word goes with the project");    let data = only_write(&line(&handle, json!({"list": {"table": "todo", "project": "synthetic-project"}}), "Synthetic seeds"))["data"].clone();    assert_eq!(data["project"], json!("synthetic-project"));}/// RULE (the session inbox is retired, Nov 2026): a created title, however/// long and wherever there is a project, asks no session anything.#[test]fn a_long_title_asks_no_session() {    let (_dir, handle) = phone();    let at = json!({"list": {"table": "todo", "project": "synthetic-project"}});    let long = line(&handle, at, "Synthetic water every bed");    assert!(long.get("asks").is_none(), "{long}");}/// RULE (record pick): an exact title is picked over a longer one.#[test]fn the_pick_takes_an_exact_title() {    let (_dir, handle) = phone();    make(&handle, "todo", json!({"title": "Synthetic milk powder", "status": "open"}));    let id = make(&handle, "todo", json!({"title": "Synthetic milk", "status": "open"}));    let done = line(&handle, todos(), "done synthetic milk");    assert_eq!(only_write(&done), &json!({"kind": "update", "table": "todo", "id": id, "patch": {"status": "done"}}), "{done}");    assert_eq!(done["picked"], json!({"table": "todo", "id": id, "title": "Synthetic milk"}));    commit(&handle, &done["writes"], "");    assert_eq!(record(&handle, "todo", &id)["status"], "done");}/// RULE (record pick): with no exact title, the one title the words begin.#[test]fn the_pick_takes_the_title_the_words_begin() {    let (_dir, handle) = phone();    let id = make(&handle, "todo", json!({"title": "Synthoat milk", "status": "open"}));    make(&handle, "todo", json!({"title": "Goat synthoat cheese", "status": "open"}));    assert_eq!(line(&handle, todos(), "done synthoat")["picked"]["id"], json!(id));}/// RULE (record pick, tie): two candidates with none exact ask which; the/// person's choice, sent back as `record`, is acted on.#[test]fn two_candidates_ask_which_and_the_choice_is_acted_on() {    let (_dir, handle) = phone();    let first = make(&handle, "todo", json!({"title": "Synthbran muffins", "status": "open"}));    make(&handle, "todo", json!({"title": "Synthbran flakes", "status": "open"}));    let asked = line(&handle, todos(), "done synthbran");    assert_eq!(asked["writes"], json!([]), "{asked}");    let candidates = asked["asks"][0]["candidates"].as_array().unwrap_or_else(|| panic!("which: {asked}"));    assert_eq!((asked["asks"][0]["kind"].as_str(), candidates.len()), (Some("which"), 2));    let chosen = line_chosen(&handle, todos(), "done synthbran", &first);    assert_eq!(only_write(&chosen)["id"], json!(first));}/// RULE (record pick): nothing matching is refused in words.#[test]fn no_candidate_is_refused() {    let (_dir, handle) = phone();    assert!(refused(&line(&handle, todos(), "done synthetic nothing")).contains("synthetic nothing"));    assert!(refused(&line(&handle, todos(), "done")).contains("todos"), "a verb with no words says which");}/// RULE (done): a record already done is refused.#[test]fn done_twice_is_refused() {    let (_dir, handle) = phone();    make(&handle, "todo", json!({"title": "Synthetic kept", "status": "done"}));    assert!(refused(&line(&handle, todos(), "done synthetic kept")).contains("already"));}/// RULE (delete): delete writes the store's delete under the table's rule.#[test]fn delete_writes_a_delete() {    let (_dir, handle) = phone();    let id = make(&handle, "todo", json!({"title": "Synthetic gone", "status": "open"}));    let deleting = line(&handle, todos(), "delete synthetic gone");    assert_eq!(only_write(&deleting), &json!({"kind": "delete", "table": "todo", "id": id}));    let committed = commit(&handle, &deleting["writes"], "");    assert!(committed.get("refusal").is_none(), "{committed}");}/// RULE (rename): on a list "rename X to Y"; without "to" it is refused.#[test]fn rename_on_a_list_splits_at_to() {    let (_dir, handle) = phone();    let id = make(&handle, "todo", json!({"title": "Synthetic milk", "status": "open"}));    let renamed = line(&handle, todos(), "rename synthetic milk to Synthetic oat milk");    assert_eq!(only_write(&renamed)["patch"], json!({"title": "Synthetic oat milk"}), "{renamed}");    assert_eq!(only_write(&renamed)["id"], json!(id));    refused(&line(&handle, todos(), "rename synthetic milk"));}/// RULE (move): on a list the record is what the day left; the day moves it.#[test]fn move_on_a_list_sets_the_day() {    let (_dir, handle) = phone();    let id = make(&handle, "todo", json!({"title": "Synthetic milk", "status": "open"}));    let moved = line(&handle, todos(), "move synthetic milk to friday");    assert_eq!(only_write(&moved), &json!({"kind": "update", "table": "todo", "id": id, "patch": {"due_at": "2026-11-06"}}), "{moved}");    assert!(refused(&line(&handle, todos(), "move synthetic milk")).contains("Synthetic milk"), "a move with nowhere to go says so");}/// RULE (page): a day fills the record's column; three words left over/// make it a thought on the record; a page rename needs no "to"; a page/// move with a word it did not use is refused.#[test]fn a_line_on_a_page_changes_or_is_a_thought() {    let (_dir, handle) = phone();    let id = make(&handle, "todo", json!({"title": "Synthetic milk", "status": "open"}));    let at = json!({"page": {"table": "todo", "id": id}});    assert_eq!(only_write(&line(&handle, at.clone(), "tomorrow")), &json!({"kind": "update", "table": "todo", "id": id, "patch": {"due_at": "2026-11-06"}}));    let thought = only_write(&line(&handle, at.clone(), "why did this move to tomorrow")).clone();    assert_eq!((thought["kind"].as_str(), thought["record"].as_str()), (Some("thought"), Some(id.as_str())), "{thought}");    assert_eq!(only_write(&line(&handle, at.clone(), "rename go to the gym"))["patch"], json!({"title": "go to the gym"}));    refused(&line(&handle, at, "move some other item tomorrow"));}/// RULE (blank page): a command is refused; words create with the preset.#[test]fn a_blank_page_creates_with_its_preset() {    let (_dir, handle) = phone();    let at = json!({"page": {"table": "todo", "preset": {"status": "open"}}});    assert_eq!(only_write(&line(&handle, at.clone(), "Synthetic first")), &json!({"kind": "create", "table": "todo", "data": {"title": "Synthetic first", "status": "open"}}));    refused(&line(&handle, at, "done synthetic first"));}/// RULE (about): a thought about a record is kept as said, never read.#[test]fn a_thought_about_a_record_is_kept_as_said() {    let (_dir, handle) = phone();    let id = make(&handle, "todo", json!({"title": "Synthetic milk", "status": "open"}));    let kept = line(&handle, json!({"about": {"table": "todo", "id": id}}), "done tomorrow");    let write = only_write(&kept);    assert_eq!((write["kind"].as_str(), write["words"].as_str(), write["record"].as_str()), (Some("thought"), Some("done tomorrow"), Some(id.as_str())));}/// RULE (spoken): a command runs only on an exact title; anything else is/// a thought kept with where it was said; "open" names a tool.#[test]fn a_spoken_line_is_a_command_or_a_thought() {    let (_dir, handle) = phone();    let id = make(&handle, "todo", json!({"title": "Synthetic milk", "status": "open"}));    make(&handle, "todo", json!({"title": "Synthetic milk powder", "status": "open"}));    assert_eq!(only_write(&spoken(&handle, todos(), "done the synthetic milk."))["id"], json!(id));    let kept = spoken(&handle, todos(), "done synthetic mil");    assert_eq!((only_write(&kept)["kind"].as_str(), only_write(&kept)["table"].as_str()), (Some("thought"), Some("todo")), "{kept}");    let kept = spoken(&handle, todos(), "Synthetic buy milk");    assert_eq!(only_write(&kept)["kind"], "thought", "speech never makes a record: {kept}");    let opened = spoken(&handle, json!({"board": {}}), "open notes");    assert_eq!(opened["opens"], json!({"table": "note"}), "{opened}");}/// RULE (reading): a time with more than one reading is refused before/// anything is written.#[test]fn an_ambiguous_time_is_refused() {    let (_dir, handle) = phone();    let parsed = crate::methods::dispatch(&handle, "phrase.parse", &json!({"text": "Synthetic call at 6", "now": NOW, "time_zone": ZONE, "projects": [], "people": []})).expect("read");    if parsed["time"]["candidates"].as_array().is_some_and(|candidates| candidates.len() > 1) {        assert!(refused(&line(&handle, todos(), "Synthetic call at 6")).contains("time"));    } else {        assert_eq!(only_write(&line(&handle, todos(), "Synthetic call at 6"))["kind"], "create");    }}/// RULE (create): one web address fills a required link column; the pure/// readers: the address, the kind, the target, the pick's order.#[test]fn the_line_readers() {    use super::line::{address, rank, target, Found};    assert_eq!(address(" https://example.com/a "), Some("https://example.com/a".into()));    assert_eq!(address("example.com"), None);    assert_eq!(address("https:// x"), None);    assert_eq!(target("  The Milk. "), "milk");    let found = |titles: &[&str]| titles.iter().map(|title| Found { id: title.to_string(), title: title.to_string(), data: Map::new() }).collect::<Vec<_>>();    let ids = |found: Vec<Found>| found.into_iter().map(|row| row.id).collect::<Vec<_>>();    assert_eq!(ids(rank(found(&["Milk powder", "milk"]), "Milk", false)), vec!["milk"]);    assert_eq!(ids(rank(found(&["Goat oat", "Oat milk"]), "oat", false)), vec!["Oat milk"]);    assert_eq!(ids(rank(found(&["Oat bran", "Oat milk"]), "oat", false)).len(), 2);    assert!(rank(found(&["Oat milk"]), "oat", true).is_empty(), "spoken, only exact");    assert!(rank(Vec::new(), "oat", false).is_empty());}/// RULE: Money's Add a spend is a door that writes a new money row in the/// shell's own inputs: Plus answers it as a draft door; asked, it begins the/// draft (out, actual, in rupees, dated today) and opens on the amount in the/// amount input with the rupee's mark; figures that are no amount are refused/// in the money set's words and nothing is staged; the amount committed asks/// the title next, and the title makes the row whole and lands on it.#[test]fn add_a_spend_asks_the_amount_then_the_title_and_lands_on_the_row() {    let (_dir, handle) = phone();    let plus = ask(&handle, "shell.plus", json!({"table": "transaction"}));    let doors: Vec<&str> = plus["doors"].as_array().unwrap().iter().map(|door| door["id"].as_str().unwrap()).collect();    assert_eq!(doors, ["spend", "bill", "money_in", "statement"], "{plus}");    assert_eq!(plus["doors"][0]["accepts"], json!({"draft": {"door": "spend"}}));    assert_eq!(plus["doors"][3]["accepts"]["files"]["mimes"], json!(["application/pdf", "text/csv", "text/comma-separated-values"]));    let amount = ask(&handle, "shell.input", json!({"op": "ask", "kind": "door", "table": "transaction", "door": "spend"}));    assert_eq!(amount["editor"], "amount", "{amount}");    assert_eq!(amount["mark"], "\u{20B9}");    assert_eq!(amount["words"], "");    assert_eq!(amount["writes"]["column"], "amount_minor");    let refused = commit(&handle, &amount["writes"], "a lot");    assert!(refused["refusal"].as_str().unwrap().contains("is not an amount"), "{refused}");    let staged = commit(&handle, &amount["writes"], "2,340.50");    let title = &staged["next"];    assert_eq!(title["editor"], "line", "{staged}");    assert_eq!(title["hint"], "Add a spend");    let made = commit(&handle, &title["writes"], "Invented grocer");    let id = made["lands"]["id"].as_str().unwrap_or_else(|| panic!("landed: {made}"));    let row = record(&handle, "transaction", id);    assert_eq!(row["description"], "Invented grocer");    assert_eq!(row["amount_minor"], 234_050);    assert_eq!(row["direction"], "out");    assert_eq!(row["status"], "actual");    assert_eq!(row["currency"], "INR");    assert_eq!(row["occurred_on"], TODAY);    // Written against the cash wallet the phone made, the one account it holds.    let accounts = ask(&handle, "records.list", json!({"table": "money_account", "limit": 10}));    let accounts = accounts["records"].as_array().unwrap();    assert_eq!(accounts.len(), 1, "{accounts:?}");    assert_eq!(accounts[0]["data"]["name"], "Cash");    assert_eq!(row["account_id"], accounts[0]["id"]);}/// RULE: a money row's amount is written in the amount input: it opens on the/// figures in the currency's places, and committed figures are its whole/// minor units.#[test]fn an_amount_is_asked_in_the_amount_input_and_written_in_minor_units() {    let (_dir, handle) = phone();    let account = crate::phone::money::usual_account(&handle.client(), "INR").expect("the cash wallet");    let id = make(&handle, "transaction", json!({"description": "Invented lunch", "amount_minor": 1_200_000, "currency": "INR", "direction": "out", "status": "actual", "occurred_on": TODAY, "account_id": account}));    let asked = ask(&handle, "shell.input", json!({"op": "ask", "kind": "column", "table": "transaction", "id": id, "column": "amount_minor"}));    assert_eq!(asked["editor"], "amount", "{asked}");    assert_eq!(asked["words"], "12000");    commit(&handle, &asked["writes"], "450.5");    assert_eq!(record(&handle, "transaction", &id)["amount_minor"], 45_050);}/// RULE (Android Todos, 12 Nov 2026): a made card's When, Time and Repeat/// rows say what a tap writes and the step it opens next, so a shell commits/// and asks and decides nothing: a day writes the day and opens its hours, No/// date clears both halves, Pick a date opens every day, an hour writes the/// day it was chosen on and itself, a preset repeats. The first write that/// gives the card a reminder on a phone that never asked leave to notify asks/// the platform to ask now, and once asked never again. A draft's rows are/// said alone.#[test]fn a_made_cards_when_time_and_repeat_rows_say_their_writes_and_the_step_after() {    let (_dir, handle) = phone();    let id = make(&handle, "todo", json!({"title": "Invented post the parcel"}));    // By the clock (no `today`): the days ahead are ahead of the reminder pass too.    let opened = |request: Value| {        let mut request = request;        request["timezone"] = json!(ZONE);        crate::methods::dispatch(&handle, "shell.opened", &request).unwrap_or_else(|error| panic!("{request}: {error:?}"))    };    let when = opened(json!({"table": "todo", "id": id, "step": "when"}));    let rows = when["step"]["rows"].as_array().unwrap().clone();    assert_eq!(rows[0]["writes"], json!({"kind": "when", "table": "todo", "id": id, "date": null, "time": null}));    assert_eq!(rows.last().unwrap()["then"], json!({"step": "when", "all": true}));    let day_row = rows.iter().find(|row| row["kind"] == "day").expect("a day ahead");    let day = day_row["day"].as_str().unwrap().to_string();    assert_eq!(day_row["writes"], json!({"kind": "when", "table": "todo", "id": id, "date": day}));    assert_eq!(day_row["then"], json!({"step": "time", "day": day}));    let landed = commit(&handle, &day_row["writes"], "");    assert_eq!(landed["answer"]["platform_action"], "request_notifications", "{landed}");    let dated = record(&handle, "todo", &id)["due_at"].as_str().expect("the day written").to_string();    // The same day again (Today on a card due today) writes nothing and refuses nothing: its hours still follow.    let again = commit(&handle, &day_row["writes"], "");    assert!(again.get("refusal").is_none(), "{again}");    let times = opened(json!({"table": "todo", "id": id, "step": "time", "day": day}));    let rows = times["step"]["rows"].as_array().unwrap().clone();    assert_eq!(rows[0]["writes"], json!({"kind": "when", "table": "todo", "id": id, "date": day, "time": null}));    assert_eq!(rows.last().unwrap()["then"], json!({"step": "time", "all": true, "day": day}));    let hour = rows.iter().find(|row| row["time"].is_string()).expect("an hour").clone();    assert_eq!(hour["writes"]["time"], hour["time"]);    commit(&handle, &hour["writes"], "");    assert_ne!(record(&handle, "todo", &id)["due_at"].as_str(), Some(dated.as_str()), "the hour written on the day");    ask(&handle, "device.notifications.asked", json!({"at": "2026-11-12T10:00:00Z"}));    let again = commit(&handle, &rows[0]["writes"], "");    assert!(again.get("answer").is_none(), "the one ask is spent: {again}");    let every = opened(json!({"table": "todo", "id": id, "step": "when", "all": true}));    assert_eq!(every["step"]["rows"][0]["writes"]["date"], every["step"]["rows"][0]["day"]);    let repeat = opened(json!({"table": "todo", "id": id, "step": "repeat"}));    let weekly = repeat["step"]["rows"].as_array().unwrap().iter().find(|row| row["value"] == "weekly").expect("weekly").clone();    assert_eq!(weekly["writes"], json!({"kind": "repeat", "table": "todo", "id": id, "preset": "weekly"}));    commit(&handle, &weekly["writes"], "");    assert!(record(&handle, "todo", &id)["recurrence_id"].as_str().is_some_and(|series| !series.is_empty()));    let draft = opened(json!({"table": "todo", "draft": {}, "step": "repeat"}));    assert!(draft["step"]["rows"].as_array().unwrap().iter().all(|row| row.get("writes").is_none()));}/// RULE (Android Todos, 12 Nov 2026): a "+" that makes a blank card asks its/// title with no draft of its own: the core begins the draft, and the card/// the words make is the one the deck lands on. Words typed away make none.#[test]fn a_blank_plus_asks_its_title_and_lands_on_the_card_it_makes() {    let (_dir, handle) = phone();    let asked = ask(&handle, "shell.input", json!({"op": "ask", "kind": "new", "table": "todo"}));    assert_eq!(asked["editor"], "line");    assert_eq!(asked["writes"]["kind"], "new");    assert!(asked["writes"]["draft"].as_str().is_some_and(|draft| !draft.is_empty()), "{asked}");    assert_eq!(commit(&handle, &asked["writes"], "  "), json!({}));    let made = commit(&handle, &asked["writes"], "Invented water the ferns");    let id = made["lands"]["id"].as_str().unwrap_or_else(|| panic!("lands on the card made: {made}"));    assert_eq!(record(&handle, "todo", id)["title"], "Invented water the ferns");    assert_eq!(titled(&handle, "todo", "Invented water the ferns"), 1);}/// RULE (Android Trips, 15 Nov 2026; a film's seasons since Trips retired,/// 22 Nov 2026): a blank "+" inside a whole (a series' seasons) begins its/// card in that whole: the whole's `where`, as the deck read its parts, seeds/// the draft, so the season made is the series' (its link is required). Only/// the table's own columns are seeded.#[test]fn a_blank_plus_inside_a_whole_makes_its_card_in_that_whole() {    let (_dir, handle) = phone();    let film = make(&handle, "film", json!({"title": "An invented series", "kind": "series"}));    let refused = ask(&handle, "shell.input", json!({"op": "ask", "kind": "new", "table": "film_season"}));    assert!(commit(&handle, &refused["writes"], "Invented loose season").get("refusal").is_some(), "a season of no film is refused");    let asked = ask(&handle, "shell.input", json!({"op": "ask", "kind": "new", "table": "film_season", "where": {"film": film, "number": 1, "no_such_column": "x"}}));    let made = commit(&handle, &asked["writes"], "Invented first season");    let id = made["lands"]["id"].as_str().unwrap_or_else(|| panic!("lands on the season made: {made}")).to_string();    let season = record(&handle, "film_season", &id);    assert_eq!((season["film"].as_str(), season["title"].as_str(), season["number"].as_i64()), (Some(film.as_str()), Some("Invented first season"), Some(1)), "{season}");    assert!(season.get("no_such_column").is_none(), "{season}");}/// RULE (Android Calendar, 12 Nov 2026): Calendar's New event is a route iOS/// draws (its event editor); a shell that draws no hand-built screen makes the/// card blank instead (`or: blank`): its title asked in the line, the card it/// makes landed on, its day, time and repeat then the held card's steps./// The phone has claimed its zone first, as every shell's launch pass does/// (`materialize_todo_recurrences {timezone}`): a new event is today's whole day.#[test]fn a_new_event_is_a_blank_card_where_no_editor_is_drawn() {    let (_dir, handle) = phone();    ask(&handle, "write", json!({"tool": "materialize_todo_recurrences", "args": {"timezone": ZONE}}));    let plus = ask(&handle, "shell.plus", json!({"table": "event"}));    assert_eq!((plus["does"].as_str(), plus["or"].as_str()), (Some("route"), Some("blank")), "{plus}");    let asked = ask(&handle, "shell.input", json!({"op": "ask", "kind": "new", "table": "event"}));    assert_eq!(asked["editor"], "line", "{asked}");    let made = commit(&handle, &asked["writes"], "Invented pottery class");    let id = made["lands"]["id"].as_str().unwrap_or_else(|| panic!("lands on the event made: {made}")).to_string();    assert_eq!(record(&handle, "event", &id)["title"], "Invented pottery class");    let opened = ask(&handle, "shell.opened", json!({"table": "event", "id": id}));    let steps: Vec<&str> = opened["acts"].as_array().unwrap().iter().filter_map(|act| act["step"].as_str()).collect();    for step in ["when", "repeat"] {        assert!(steps.contains(&step), "the held event offers {step}: {opened}");    }    // Weekly from its row: the event repeats, and its Recurring view holds it.    let repeat = ask(&handle, "shell.opened", json!({"table": "event", "id": id, "step": "repeat", "act": "repeat"}));    let weekly = repeat["step"]["rows"].as_array().unwrap().iter().find(|row| row["value"] == "weekly").unwrap_or_else(|| panic!("a weekly row: {repeat}")).clone();    let wrote = ask(&handle, "shell.input", json!({"op": "commit", "writes": weekly["writes"], "words": ""}));    assert!(wrote.get("refusal").is_none(), "{wrote}");    assert!(record(&handle, "event", &id)["rrule"].as_str().is_some_and(|rule| rule.contains("WEEKLY")), "{}", record(&handle, "event", &id));    // RULE: a repeating event's Bin asks only this one or every one; every one takes the series.    let held = ask(&handle, "shell.opened", json!({"table": "event", "id": id}));    let bin = held["acts"].as_array().unwrap().iter().find(|act| act["id"] == "bin").unwrap_or_else(|| panic!("a bin: {held}")).clone();    assert_eq!(bin["step"], "scope", "{bin}");    let scope = ask(&handle, "shell.opened", json!({"table": "event", "id": id, "step": "scope", "act": "bin"}));    let values: Vec<&str> = scope["step"]["rows"].as_array().unwrap().iter().filter_map(|row| row["value"].as_str()).collect();    assert_eq!(values, ["this", "all"], "{scope}");    let every = scope["step"]["rows"][1]["writes"].clone();    let gone = ask(&handle, "shell.input", json!({"op": "commit", "writes": every, "words": ""}));    assert!(gone.get("refusal").is_none(), "{gone}");    assert_eq!(titled(&handle, "event", "Invented pottery class"), 0, "every one is gone");}