//! A deleted photo or video waits in the bin (architect, 5 Oct 2026: the bin, //! one rule — every record a person deletes waits there). Its Delete is //! "Moved to the bin" (the Bin's tile on Home, the default) with Undo; the binned picture leaves the photo //! and video lists, the library, its album, search, the person it shows and //! the faces waiting for a name; the bin's Restore brings it back as it was, //! and only the bin's erase removes it for good — and only then does its //! file's key die. 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"; const AT: &str = "2026-09-02T09:00:00.000Z"; const ALBUM: &str = "album:event:2026-09-02T09"; fn t(value: &str) -> SqlValue { SqlValue::Text(value.to_string()) } 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() } struct Seeded { h: Harness, person: String, photo: String, video: String, } impl Seeded { fn key(&self, id: &str) -> String { let table = if id.starts_with("video:") { "cotton_videos" } else { "cotton_photos" }; let row = self.h.one(&format!("SELECT key FROM {table} WHERE id = ?"), &[t(id)]).expect("the row"); row["key"].as_str().expect("a key").to_string() } } /// A photo and a video of the same afternoon, each with a person confirmed in /// it, in one kept album; the photo also has a face waiting for a name. fn seeded() -> Seeded { let h = Harness::new(); let ws = h.workspace_id.clone(); let person = synthetic_uuid("a person in the pictures"); h.run( "INSERT INTO cotton_people (id, workspace_id, full_name, created_at, updated_at) VALUES (?, ?, 'Kiran Example', ?, ?)", &[t(&person), t(&ws), t(AT), t(AT)], ); let sha_photo = "a".repeat(64); let photo = format!("photo:{sha_photo}"); h.run( "INSERT INTO cotton_photos (id, workspace_id, key, sha256, content_type, size_bytes, width, height, \ taken_at, caption, created_at, updated_at) \ VALUES (?, ?, ?, ?, 'image/jpeg', 240000, 1200, 900, ?, 'Kite over the dunes', ?, ?)", &[t(&photo), t(&ws), t(&format!("{ws}/photos/{sha_photo}.jpg")), t(&sha_photo), t(AT), t(AT), t(AT)], ); let sha_video = "b".repeat(64); let video = format!("video:{sha_video}"); h.run( "INSERT INTO cotton_videos (id, workspace_id, key, sha256, content_type, size_bytes, width, height, \ duration_ms, taken_at, poster_key, caption, created_at, updated_at) \ VALUES (?, ?, ?, ?, 'video/mp4', 9000000, 1920, 1080, 42000, ?, ?, 'Kite festival clip', ?, ?)", &[ t(&video), t(&ws), t(&format!("{ws}/videos/{sha_video}.mp4")), t(&sha_video), t(AT), t(&format!("{ws}/videos/{sha_video}.poster.jpg")), t(AT), t(AT), ], ); let face = |serial: u8, kind: &str, record: &str, named: bool| { h.run( "INSERT INTO cotton_media_faces (id, workspace_id, record_kind, record_id, source, source_id, x, y, w, h, \ person_id, suggested_person_id, suggested_distance, created_at, updated_at) \ VALUES (?, ?, ?, ?, 'apple', ?, 0, 0, 0.1, 0.1, ?, ?, ?, ?, ?)", &[ t(&format!("face:{serial}")), t(&ws), t(kind), t(record), t(&format!("source:{serial}")), if named { t(&person) } else { SqlValue::Null }, if named { SqlValue::Null } else { t(&person) }, if named { SqlValue::Null } else { SqlValue::Real(0.2) }, t(AT), t(AT), ], ); }; face(1, "photo", &photo, true); face(2, "video", &video, true); face(3, "photo", &photo, false); h.run( "INSERT INTO cotton_media_albums (id, workspace_id, kind, state, decided_at, title, title_by, starts_at, \ ends_at, home_label, cover_id, created_at, updated_at) \ VALUES (?, ?, 'event', 'kept', ?, 'Kite day', 'user', '2026-09-02', '2026-09-02', 'Example City', ?, ?, ?)", &[t(ALBUM), t(&ws), t(AT), t(&photo), t(AT), t(AT)], ); for record in [&photo, &video] { let kind = record.split(':').next().unwrap(); h.run( "INSERT INTO cotton_media_album_items (id, workspace_id, album_id, record_kind, record_id, added_by, \ sort_at, created_at, updated_at) VALUES (?, ?, ?, ?, ?, 'user', ?, ?, ?)", &[ t(&cotton_store::protocol::derived_id::media_album_item_id(ALBUM, record)), t(&ws), t(ALBUM), t(kind), t(record), t(AT), t(AT), t(AT), ], ); } Seeded { h, person, photo, video } } fn ids_in(answer: &Value, list: &str) -> Vec { answer[list] .as_array() .unwrap_or_else(|| panic!("no {list} in {answer}")) .iter() .map(|row| row["id"].as_str().expect("an id").to_string()) .collect() } /// Every place a picture is read from, and whether this one is found there. fn found_in(s: &Seeded, id: &str) -> Vec<(&'static str, bool)> { let kind = if id.starts_with("video:") { "video" } else { "photo" }; let has = |ids: Vec| ids.iter().any(|found| found == id); let mut out = vec![ ("its own list", has(ids_in(&owner(&s.h, "list_records", json!({ "table": kind })), "records"))), ("the library", has(ids_in(&owner(&s.h, "list_records", json!({ "table": "media" })), "records"))), ("the assistant's list", has(ids_in(&owner(&s.h, &format!("list_{kind}s"), json!({})), &format!("{kind}s")))), ("its album", has(ids_in(&owner(&s.h, "get_album", json!({ "id": ALBUM })), "items"))), ("search", has(ids_in(&owner(&s.h, "search_records", json!({ "query": "kite" })), "records"))), ( "its person's pictures", has(ids_in(&owner(&s.h, "list_records", json!({ "table": kind, "where": { "people": s.person } })), "records")), ), ]; if kind == "photo" { let faces = owner(&s.h, "list_records", json!({ "table": "face" })); let waiting = faces["records"].as_array().expect("faces").iter().any(|face| face["data"]["photo_id"] == id); out.push(("the faces waiting for a name", waiting)); } out } fn everywhere(s: &Seeded, id: &str) -> bool { found_in(s, id).iter().all(|(_, found)| *found) } fn nowhere(s: &Seeded, id: &str) -> Vec<&'static str> { found_in(s, id).into_iter().filter(|(_, found)| *found).map(|(place, _)| place).collect() } fn binned(s: &Seeded, id: &str) -> bool { let bin = owner(&s.h, "list_bin", json!({})); bin["entries"].as_array().expect("entries").iter().any(|entry| entry["id"] == id) } fn stamps(s: &Seeded, id: &str) -> serde_json::Map { let table = if id.starts_with("video:") { "cotton_videos" } else { "cotton_photos" }; s.h.one(&format!("SELECT trashed_at, updated_at FROM {table} WHERE id = ?"), &[t(id)]).expect("the row is in the file") } /// RULE: A photo's or a video's Delete moves it to the bin — "Moved to the bin" (the Bin's tile on Home, the default) with Undo — and it leaves its list, the library, its album, search, its person and the faces waiting for a name; that Undo brings it back everywhere. #[test] fn deleting_a_picture_moves_it_to_the_bin_and_undo_brings_it_back() { let s = seeded(); for (table, id, name) in [("photo", &s.photo, "Kite over the dunes"), ("video", &s.video, "Kite festival clip")] { assert!(everywhere(&s, id), "the premise: {id} is found everywhere — {:?}", found_in(&s, id)); let gone = owner(&s.h, "delete_record", json!({ "table": table, "id": id })); assert_eq!(gone["change"]["receipt"]["said"], format!("Moved {name} to the bin"), "{gone}"); assert_eq!(gone["change"]["undoable"], true, "{gone}"); assert_eq!(nowhere(&s, id), Vec::<&str>::new(), "a binned {table} is still read there"); assert!(binned(&s, id), "the bin does not list the deleted {table}"); assert!(stamps(&s, id)["trashed_at"].is_string(), "a deleted {table} was erased, not binned"); owner(&s.h, "undo", json!({ "change_id": gone["change"]["id"] })); assert!(everywhere(&s, id), "Undo did not bring {id} back — {:?}", found_in(&s, id)); assert!(!binned(&s, id), "{id} is still in the bin after Undo"); } } /// RULE: A binned photo or video comes back by the bin's Restore with its edited time unchanged, and only the bin's erase removes it — and only then does its file's key die. #[test] fn a_binned_picture_is_restored_as_it_was_or_erased_from_the_bin() { let s = seeded(); for (kind, id) in [("photo", &s.photo), ("video", &s.video)] { let key = s.key(id); let edited = stamps(&s, id)["updated_at"].clone(); owner(&s.h, "delete_record", json!({ "table": "media", "id": id })); assert_eq!(stamps(&s, id)["updated_at"], edited, "binning {id} changed its edited time"); let back = owner(&s.h, "restore_from_bin", json!({ "kind": kind, "id": id })); assert_eq!(back["restored"], true, "{back}"); assert!(everywhere(&s, id), "a restored {kind} is not back — {:?}", found_in(&s, id)); assert_eq!(stamps(&s, id)["updated_at"], edited, "a restored {kind} came back edited now"); owner(&s.h, "delete_record", json!({ "table": kind, "id": id })); let dead = |key: &str| s.h.with(|db| cotton_store::media_erasure::is_dead(db, key)).expect("the rule reads"); assert!(!dead(&key), "a binned {kind}'s key died before the bin was emptied"); owner(&s.h, "delete_record", json!({ "table": "bin", "id": id })); assert!(s.h.one(&format!("SELECT id FROM cotton_{kind}s WHERE id = ?"), &[t(id)]).is_none(), "an erased {kind} is still in the file"); assert!(!binned(&s, id), "an erased {kind} is still in the bin"); assert!(dead(&key), "an erased {kind}'s key is still alive"); } } /// RULE: remove_photo and remove_video move the picture to the bin rather than erasing it: the answer says so with bytesRemoved false, the row waits with its faces, and restore_from_bin brings it back. #[test] fn remove_photo_and_remove_video_bin_rather_than_erase() { let s = seeded(); for (tool, kind, id) in [("remove_photo", "photo", &s.photo), ("remove_video", "video", &s.video)] { let answer = owner(&s.h, tool, json!({ "id": id })); assert_eq!((answer["removed"].clone(), answer["trashed"].clone()), (json!(true), json!(true)), "{answer}"); assert_eq!(answer["bytesRemoved"], false, "{answer}"); assert!(binned(&s, id) && stamps(&s, id)["trashed_at"].is_string(), "{tool} erased instead of binning"); assert!(s.h.count("SELECT COUNT(*) AS n FROM cotton_media_faces WHERE record_id = ?", &[t(id)]) > 0, "{tool} took the faces"); let refused = call_tool_as_owner(&s.h, PHONE, &format!("get_{kind}"), json!({ "id": id })); assert!(refused.is_error, "get_{kind} still reads a binned {kind}"); // Again: it is already in the bin, and stays there as it is. let again = owner(&s.h, tool, json!({ "id": id })); assert_eq!(again["removed"], true, "{again}"); owner(&s.h, "restore_from_bin", json!({ "kind": kind, "id": id })); assert!(everywhere(&s, id), "a restored {kind} is not back — {:?}", found_in(&s, id)); } }