//! A deleted note waits in the bin (architect, 5 Oct 2026): every record a //! person deletes waits there, and a note that left without reaching it was //! a fault. Delete moves the note in with the receipt "Moved to the //! bin" while the Bin's tile is on Home (the default), "Deleted " while //! it is not; its Undo, the bin's Restore and the bin's erase each do their one //! thing; Archive stays its own act beside Delete. Driven end to end through //! the dispatcher. //! //! Every value is invented ([nothing-real-ever-goes-in-cotton-s-public-tree]). mod support; use rusqlite::types::Value as SqlValue; use serde_json::{json, Value}; use support::harness::Harness; use support::mcp::call_tool_as_owner; const PHONE: &str = "device:phone"; fn owner(h: &Harness, tool: &str, args: Value) -> Value { let answer = call_tool_as_owner(h, PHONE, tool, args); assert!(!answer.is_error, "{tool} refused: {}", answer.text); answer.json() } fn add_note(h: &Harness) -> String { let added = owner(h, "create_record", json!({ "table": "note", "data": { "title": "Kite flying", "body": "Bring the long string." } })); added["record"]["id"].as_str().expect("the note's id").to_string() } fn read(h: &Harness, id: &str) -> Value { owner(h, "get_record_by_table", json!({ "table": "note", "id": id })) } fn listed(h: &Harness, id: &str) -> bool { let page = owner(h, "list_records", json!({ "table": "note" })); page["records"].as_array().expect("records").iter().any(|record| record["id"] == id) } fn binned(h: &Harness, id: &str) -> bool { let bin = owner(h, "list_bin", json!({})); bin["entries"].as_array().expect("entries").iter().any(|entry| entry["kind"] == "note" && entry["id"] == id) } fn rows(h: &Harness, id: &str) -> i64 { h.count("SELECT count(*) AS c FROM cotton_notes WHERE id = ?", &[SqlValue::Text(id.to_string())]) } fn delete(h: &Harness, id: &str) -> Value { owner(h, "delete_record", json!({ "table": "note", "id": id })) } /// RULE: A note's Delete moves it to the bin — listed there, gone from Notes, the receipt "Moved to the bin" (the Bin's tile on Home, the default) with Undo — and that Undo brings it back exactly as it was. #[test] fn deleting_a_note_moves_it_to_the_bin_and_undo_brings_it_back_exactly() { let h = Harness::new(); let id = add_note(&h); let before = read(&h, &id); let gone = delete(&h, &id); assert_eq!(gone["change"]["receipt"]["said"], "Moved Kite flying to the bin", "{gone}"); assert_eq!(gone["change"]["undoable"], true, "{gone}"); assert!(!listed(&h, &id), "a binned note is still in Notes"); assert!(binned(&h, &id), "the bin does not list the deleted note"); assert_eq!(rows(&h, &id), 1, "a deleted note was erased, not binned"); owner(&h, "undo", json!({ "change_id": gone["change"]["id"] })); assert!(listed(&h, &id), "Undo did not bring the note back to Notes"); assert!(!binned(&h, &id), "the note is still in the bin after Undo"); assert_eq!(read(&h, &id)["data"], before["data"], "the note came back changed"); } /// RULE: A delete names the bin only while the Bin's tile is on Home: there (the default) its receipt is "Moved to the bin"; with the card taken off Home — the Bin still one of the database's tools — it is "Deleted ", because "the bin" would name nothing the person can see. #[test] fn a_deletes_receipt_names_the_bin_only_while_the_bins_card_is_on_home() { let h = Harness::new(); let id = add_note(&h); assert_eq!(delete(&h, &id)["change"]["receipt"]["said"], "Moved Kite flying to the bin"); // The person takes the Bin's tile off Home: still a tool, no longer a card. h.run( "INSERT INTO cotton_home_arrangement (workspace_id, project_slugs, tools, view_ids, created_at, updated_at) \ VALUES (?, '[]', '[\"notes\",\"bin\"]', '[\"tool/notes\"]', '2026-10-06T00:00:00.000Z', '2026-10-06T00:00:00.000Z')", &[SqlValue::Text(h.workspace_id.clone())], ); let other = add_note(&h); assert_eq!(delete(&h, &other)["change"]["receipt"]["said"], "Deleted Kite flying"); } /// RULE: A deleted note comes back by the bin's Restore, and the bin's erase removes it for good. #[test] fn a_binned_note_is_restored_or_erased_from_the_bin() { let h = Harness::new(); let id = add_note(&h); delete(&h, &id); let back = owner(&h, "restore_from_bin", json!({ "kind": "note", "id": id })); assert_eq!(back["restored"], true, "{back}"); assert!(listed(&h, &id), "a restored note is not in Notes"); delete(&h, &id); owner(&h, "delete_record", json!({ "table": "bin", "id": id })); assert_eq!(rows(&h, &id), 0, "an erased note is still in the file"); assert!(!binned(&h, &id), "an erased note is still in the bin"); } /// RULE: Archive stays a note's own act beside Delete: an archived note leaves Notes without entering the bin, and its receipt says Archived. #[test] fn archiving_a_note_is_not_binning_it() { let h = Harness::new(); let id = add_note(&h); let archived = owner(&h, "update_record", json!({ "table": "note", "id": id, "patch": { "archived_at": "2026-09-30T09:00:00.000Z" } })); assert_eq!(archived["change"]["receipt"]["said"], "Archived Kite flying", "{archived}"); assert!(!listed(&h, &id)); assert!(!binned(&h, &id), "an archived note went to the bin"); let archived_at = h.one("SELECT archived_at, trashed_at FROM cotton_notes WHERE id = ?", &[SqlValue::Text(id.clone())]).expect("the note"); assert!(archived_at["archived_at"].is_string() && archived_at["trashed_at"].is_null(), "{archived_at:?}"); }