//! A deleted calendar event waits in the bin (architect, 5 Oct 2026, sev 2: //! "a deleted Calendar event never reaches the Bin"). Every event a person //! deletes — Cotton's own or one synced from Google — is trashed, listed by //! the bin, brought back by Undo or the bin's Restore, and erased only from //! the bin. A synced one also goes `pending`, so the push cancels it at //! Google; an erase asked before then is accepted and carried out by the push //! once it has. 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::{synthetic_uuid, 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_event(h: &Harness) -> String { // An event is filed in the workspace's default calendar. h.run( "INSERT OR IGNORE INTO cotton_collections (id, workspace_id, kind, name, is_default, created_at) \ VALUES (?, ?, 'calendar', 'Personal', 1, '2026-10-01T09:00:00.000Z')", &[SqlValue::Text(synthetic_uuid("calendar")), SqlValue::Text(h.workspace_id.clone())], ); let added = owner( h, "create_record", json!({ "table": "event", "data": { "title": "Kite festival", "starts_at": "2026-10-15T09:30:00.000Z" } }), ); added["record"]["id"].as_str().expect("the event's id").to_string() } /// The event as Google's sync left it: a resource up there, nothing in flight. fn synced(h: &Harness, id: &str) { h.run( "UPDATE cotton_events SET gcal_id = 'g-kite', gcal_etag = '\"1\"', sync_state = 'synced' WHERE id = ?", &[SqlValue::Text(id.to_string())], ); } fn read(h: &Harness, id: &str) -> Value { owner(h, "get_record_by_table", json!({ "table": "event", "id": id })) } fn listed(h: &Harness, id: &str) -> bool { let page = owner(h, "list_records", json!({ "table": "event" })); 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"] == "event" && entry["id"] == id) } fn row(h: &Harness, id: &str) -> Option> { h.one("SELECT trashed_at, sync_state, gcal_id FROM cotton_events WHERE id = ?", &[SqlValue::Text(id.to_string())]) } fn delete(h: &Harness, id: &str) -> Value { owner(h, "delete_record", json!({ "table": "event", "id": id, "scope": "all" })) } /// RULE: A Cotton-only event's Delete moves it to the bin — listed there, gone from the Calendar, 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_local_event_moves_it_to_the_bin_and_undo_brings_it_back_exactly() { let h = Harness::new(); let id = add_event(&h); let before = read(&h, &id); let gone = delete(&h, &id); assert_eq!(gone["change"]["receipt"]["said"], "Moved Kite festival to the bin", "{gone}"); assert_eq!(gone["change"]["undoable"], true, "{gone}"); assert!(!listed(&h, &id), "a binned event is still in the Calendar"); assert!(binned(&h, &id), "the bin does not list the deleted event"); assert!(row(&h, &id).is_some_and(|row| row["trashed_at"].is_string()), "a deleted event was dropped, not binned"); owner(&h, "undo", json!({ "change_id": gone["change"]["id"] })); assert!(listed(&h, &id), "Undo did not bring the event back to the Calendar"); assert!(!binned(&h, &id), "the event is still in the bin after Undo"); assert_eq!(read(&h, &id)["data"], before["data"], "the event came back changed"); } /// RULE: A deleted Cotton-only event comes back by the bin's Restore, and the bin's erase removes it for good. #[test] fn a_binned_local_event_is_restored_or_erased_from_the_bin() { let h = Harness::new(); let id = add_event(&h); delete(&h, &id); let back = owner(&h, "restore_from_bin", json!({ "kind": "event", "id": id })); assert_eq!(back["restored"], true, "{back}"); assert!(listed(&h, &id), "a restored event is not in the Calendar"); delete(&h, &id); owner(&h, "delete_record", json!({ "table": "bin", "id": id })); assert!(row(&h, &id).is_none(), "an erased event is still in the file"); assert!(!binned(&h, &id), "an erased event is still in the bin"); } /// RULE (Cotton Days, 20 Nov 2026): a Google-synced event's Delete moves it to the bin and the bin's Restore brings it back, and neither marks it pending: nothing writes to Google, so no mark claims a push. #[test] fn a_synced_event_goes_to_the_bin_and_comes_back_unmarked() { let h = Harness::new(); let id = add_event(&h); synced(&h, &id); let gone = delete(&h, &id); assert_eq!(gone["change"]["receipt"]["said"], "Moved Kite festival to the bin", "{gone}"); assert!(binned(&h, &id), "the bin does not list the deleted synced event"); let trashed = row(&h, &id).expect("the row"); assert!(trashed["trashed_at"].is_string() && trashed["sync_state"] != "pending", "{trashed:?}"); let back = owner(&h, "restore_from_bin", json!({ "kind": "event", "id": id })); assert_eq!(back["restored"], true, "{back}"); let restored = row(&h, &id).expect("the row"); assert!(restored["trashed_at"].is_null() && restored["sync_state"] != "pending", "{restored:?}"); assert!(listed(&h, &id)); } /// Was this row's erase asked, the row still in the file for the push? fn erase_asked(h: &Harness, id: &str) -> bool { h.one("SELECT erase_requested_at FROM cotton_events WHERE id = ?", &[SqlValue::Text(id.to_string())]) .is_some_and(|row| row["erase_requested_at"].is_string()) } /// RULE: Erasing a binned event Google still holds is accepted, never refused: it leaves the bin and its count at once, and the row waits only for the push to cancel it at Google, which then erases it. #[test] fn erasing_a_binned_event_google_still_holds_is_accepted_and_leaves_the_bin_at_once() { let h = Harness::new(); let id = add_event(&h); synced(&h, &id); delete(&h, &id); let erased = call_tool_as_owner(&h, PHONE, "delete_record", json!({ "table": "bin", "id": id })); assert!(!erased.is_error, "an erase Google still holds was refused: {}", erased.text); assert!(!binned(&h, &id), "an erased event is still listed in the bin"); assert_eq!(bin_events(&h), (Vec::new(), 0), "an erased event is still counted"); assert!(bin_table(&h).is_empty(), "the bin table still lists the erased event"); assert!(erase_asked(&h, &id), "the erase was not left for the push to carry out"); assert!(!listed(&h, &id), "an erased event came back into the Calendar"); // Emptying the bin is the same erase. let other = add_event(&h); h.run( "UPDATE cotton_events SET gcal_id = 'g-other', sync_state = 'synced' WHERE id = ?", &[SqlValue::Text(other.clone())], ); delete(&h, &other); let emptied = owner(&h, "empty_bin", json!({ "kind": "event" })); assert_eq!(emptied["erased"]["event"], 1, "{emptied}"); assert_eq!(emptied["atGoogle"], 1, "{emptied}"); assert!(emptied.get("kept").is_none(), "{emptied}"); assert!(erase_asked(&h, &other) && !binned(&h, &other)); } /// A weekly "Kite festival" whose 22 Oct occurrence was moved to Fri 23 Oct: /// (series id, moved occurrence's id). fn series_with_a_moved_occurrence(h: &Harness) -> (String, String) { let id = add_event(h); owner(h, "update_event", json!({ "id": id, "rrule": "FREQ=WEEKLY" })); let moved = owner( h, "update_event", json!({ "id": id, "occurrenceAt": "2026-10-22T09:30:00.000Z", "scope": "this", "startsAt": "2026-10-23T11:00:00.000Z" }), ); (id, moved["id"].as_str().expect("the moved occurrence's id").to_string()) } /// The days the Calendar draws the series on, 14 to 31 Oct. fn drawn(h: &Harness) -> Vec<String> { let page = owner( h, "list_events", json!({ "from": "2026-10-14T00:00:00.000Z", "to": "2026-10-31T00:00:00.000Z", "expand": true }), ); page["events"].as_array().expect("events").iter().map(|event| event["startsAt"].as_str().unwrap().to_string()).collect() } /// The bin's event entries as (id, title), and the event count it shows. fn bin_events(h: &Harness) -> (Vec<(String, String)>, i64) { let bin = owner(h, "list_bin", json!({})); let entries = bin["entries"] .as_array() .expect("entries") .iter() .filter(|entry| entry["kind"] == "event") .map(|entry| (entry["id"].as_str().unwrap().to_string(), entry["title"].as_str().unwrap().to_string())) .collect(); (entries, bin["counts"]["event"].as_i64().expect("the event count")) } /// The bin table's rows as (id, title). fn bin_table(h: &Harness) -> Vec<(String, String)> { let page = owner(h, "list_records", json!({ "table": "bin" })); page["records"] .as_array() .expect("records") .iter() .map(|record| (record["id"].as_str().unwrap().to_string(), record["data"]["title"].as_str().unwrap().to_string())) .collect() } /// RULE: A moved occurrence deleted on its own waits in the bin as "<title> on <day>", counted as listed; Restore brings it back, and erasing it leaves that day cancelled. #[test] fn a_moved_occurrence_deleted_on_its_own_waits_in_the_bin_by_its_day() { let h = Harness::new(); let (_, moved) = series_with_a_moved_occurrence(&h); assert!(drawn(&h).contains(&"2026-10-23T11:00:00.000Z".to_string())); let gone = delete(&h, &moved); assert_eq!(gone["change"]["receipt"]["said"], "Moved Kite festival on Fri 23 Oct to the bin", "the receipt names the day (architect 2005): {gone}"); let named = vec![(moved.clone(), "Kite festival on Fri 23 Oct".to_string())]; assert_eq!(bin_events(&h), (named.clone(), 1), "the bin does not list the moved occurrence by its day"); assert_eq!(bin_table(&h), named, "the bin table does not list what list_bin lists"); let days = drawn(&h); assert!(!days.iter().any(|day| day.starts_with("2026-10-22") || day.starts_with("2026-10-23")), "{days:?}"); let back = owner(&h, "restore_from_bin", json!({ "kind": "event", "id": moved })); assert_eq!(back["restored"], true, "{back}"); assert!(drawn(&h).contains(&"2026-10-23T11:00:00.000Z".to_string()), "a restored occurrence is not drawn"); assert_eq!(bin_events(&h), (Vec::new(), 0)); delete(&h, &moved); owner(&h, "delete_record", json!({ "table": "bin", "id": moved })); assert!(row(&h, &moved).is_none(), "an erased occurrence is still in the file"); assert_eq!(bin_events(&h), (Vec::new(), 0)); let days = drawn(&h); assert!(!days.iter().any(|day| day.starts_with("2026-10-22") || day.starts_with("2026-10-23")), "the erase put the day back: {days:?}"); } /// RULE: A binned series stands for its moved occurrences — one deleted on its own before is not listed or counted again, and the series' erase takes it. #[test] fn a_binned_series_stands_for_its_binned_moved_occurrence() { let h = Harness::new(); let (series, moved) = series_with_a_moved_occurrence(&h); delete(&h, &moved); delete(&h, &series); assert_eq!(bin_events(&h), (vec![(series.clone(), "Kite festival".to_string())], 1)); assert_eq!(bin_table(&h).len(), 1); let emptied = owner(&h, "empty_bin", json!({ "kind": "event" })); assert_eq!(emptied["erased"]["event"], 1, "{emptied}"); assert!(row(&h, &series).is_none() && row(&h, &moved).is_none(), "the erase left part of the series"); } /// RULE: "This event" on a delete of a repeating event cancels that occurrence on its series and says so — it never claims the bin. #[test] fn cancelling_one_occurrence_of_a_series_says_so_and_claims_no_bin() { let h = Harness::new(); let (series, moved) = series_with_a_moved_occurrence(&h); let cancelled = owner( &h, "delete_event", json!({ "id": series, "occurrenceAt": "2026-10-22T09:30:00.000Z", "scope": "this" }), ); assert_eq!(cancelled["cancelled"], true, "{cancelled}"); assert!(!cancelled.to_string().contains("bin"), "{cancelled}"); assert!(row(&h, &moved).is_none(), "a local moved occurrence outlived its cancel"); assert_eq!(bin_events(&h), (Vec::new(), 0)); assert!(!drawn(&h).iter().any(|day| day.starts_with("2026-10-22") || day.starts_with("2026-10-23"))); } /// The moved occurrence as the push leaves it once Google has cancelled it: /// its Google link gone, and its instant skipped by its series. fn let_go_at_google(h: &Harness, series: &str, moved: &str) { h.run( "UPDATE cotton_events SET gcal_id = NULL, gcal_etag = NULL, sync_state = NULL WHERE id = ?", &[SqlValue::Text(moved.to_string())], ); h.run( "UPDATE cotton_events SET exdates = json_array('2026-10-22T09:30:00.000Z') WHERE id = ?", &[SqlValue::Text(series.to_string())], ); } fn restore_in_bin(h: &Harness, id: &str) -> Value { owner(h, "update_record", json!({ "table": "bin", "id": id, "patch": { "state": "restored" } })) } /// RULE: A binned moved occurrence Google has cancelled comes back by Restore as its own event on its day — no longer of the series, which keeps the day cancelled, pending so the push makes it — said "Restored <title>, <day>, as its own event". #[test] fn a_moved_occurrence_google_let_go_is_restored_as_its_own_event_on_its_day() { let h = Harness::new(); let (series, moved) = series_with_a_moved_occurrence(&h); delete(&h, &moved); let_go_at_google(&h, &series, &moved); assert_eq!(bin_events(&h), (vec![(moved.clone(), "Kite festival on Fri 23 Oct".to_string())], 1)); let back = restore_in_bin(&h, &moved); assert_eq!(back["change"]["receipt"]["said"], "Restored Kite festival, Fri 23 Oct, as its own event", "{back}"); let row = h .one( "SELECT trashed_at, series_id, original_starts_at, sync_state, starts_at FROM cotton_events WHERE id = ?", &[SqlValue::Text(moved.clone())], ) .expect("the row"); assert!(row["trashed_at"].is_null() && row["series_id"].is_null() && row["original_starts_at"].is_null(), "{row:?}"); assert_ne!(row["sync_state"], "pending", "nothing writes to Google, so nothing is marked for a push: {row:?}"); assert_eq!(row["starts_at"], "2026-10-23T11:00:00.000Z"); let days = drawn(&h); assert_eq!(days.iter().filter(|day| day.starts_with("2026-10-23")).count(), 1, "{days:?}"); assert!(!days.iter().any(|day| day.starts_with("2026-10-22")), "the series drew the day again: {days:?}"); assert_eq!(bin_events(&h), (Vec::new(), 0)); } /// RULE: A restore's receipt speaks the database's own words: a database that rewords `bin.occurrence` and `bin.restored_own_event` lists the occurrence in its words, and its Restore reads the day back out of those words and says it in its own — never the kit's sentence, nor a plain "Changed". #[test] fn a_restore_as_its_own_event_is_said_in_the_databases_own_lines() { let h = Harness::new(); let (series, moved) = series_with_a_moved_occurrence(&h); for (line, words) in [("occurrence", "{day} — {title}"), ("restored_own_event", "{title} is back on {day}, on its own")] { h.run( "UPDATE cotton_lines SET words = ? WHERE workspace_id = ? AND \"set\" = 'bin' AND line = ?", &[SqlValue::Text(words.into()), SqlValue::Text(h.workspace_id.clone()), SqlValue::Text(line.into())], ); } delete(&h, &moved); let_go_at_google(&h, &series, &moved); assert_eq!(bin_events(&h), (vec![(moved.clone(), "Fri 23 Oct — Kite festival".to_string())], 1)); let back = restore_in_bin(&h, &moved); assert_eq!(back["change"]["receipt"]["said"], "Kite festival is back on Fri 23 Oct, on its own", "{back}"); } /// RULE: A binned moved occurrence restored before the push has cancelled it at Google comes back as it was, an occurrence of its series, with the plain "Restored <name>". #[test] fn a_moved_occurrence_restored_before_the_push_comes_back_as_it_was() { let h = Harness::new(); let (series, moved) = series_with_a_moved_occurrence(&h); h.run( "UPDATE cotton_events SET gcal_id = 'g-kite_20261022T093000Z', sync_state = 'synced' WHERE id = ?", &[SqlValue::Text(moved.clone())], ); delete(&h, &moved); let back = restore_in_bin(&h, &moved); assert_eq!(back["change"]["receipt"]["said"], "Restored Kite festival on Fri 23 Oct", "{back}"); let row = h .one("SELECT series_id, gcal_id FROM cotton_events WHERE id = ?", &[SqlValue::Text(moved.clone())]) .expect("the row"); assert_eq!(row["series_id"], series.as_str(), "{row:?}"); assert_eq!(row["gcal_id"], "g-kite_20261022T093000Z"); }