//! Putting mail in the bin hides it; emptying the bin is what erases it. //! //! `mcp_emails.rs` already holds the two halves a caller can see — //! `keeps_the_bin_out_of_every_folder_but_the_bin` and //! `empties_the_bin_says_what_it_erased_and_says_what_gmail_still_holds`. Both //! read through the tools, so both would go on passing if the bin started //! deleting rows outright, or if emptying it started marking rows instead of //! removing them: a row that is hidden from every read looks exactly like a row //! that is gone. This file looks at the database itself. //! //! Every fixture is synthetic. mod support; use serde_json::{json, Value}; use support::harness::Harness; use support::mcp::{call_tool_as_owner, call_tool, mint_token}; fn writer_harness() -> (Harness, String) { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); (h, secret) } fn count(h: &Harness, table: &str) -> i64 { h.count(&format!("SELECT COUNT(*) AS n FROM {table}"), &[]) } fn trashed_at(h: &Harness, table: &str) -> Option { h.one(&format!("SELECT trashed_at FROM {table} LIMIT 1"), &[]) .and_then(|row| { row.get("trashed_at") .and_then(Value::as_str) .map(str::to_string) }) } /// One conversation of one message, and the conversation's id. fn seeded(h: &Harness, secret: &str) -> String { call_tool( h, secret, "ingest_emails", json!({ "emails": [{ "threadKey": "gmail:newsletter", "subject": "This week at Acme", "sentAt": "2026-08-21T10:00:00.000Z", "body": "Nine things you must know.", "messageId": "", "from": { "email": "news@acme.com", "name": "Acme" }, "to": [{ "email": "owner@example.com" }], }], }), ); let list = call_tool(h, secret, "list_email_threads", json!({})); list["threads"][0]["id"] .as_str() .expect("the conversation is listed") .to_string() } /// RULE: Moving mail to the bin keeps it in the database, and only emptying the bin erases it. /// /// WHY: the bin is the one place a person can change their mind. Mail that was /// deleted outright when it went in would look identical from every screen — /// hidden either way — and the difference would only show up the day somebody /// tried to restore something and found there was nothing left to restore. #[test] fn binning_mail_keeps_the_rows_and_emptying_the_bin_is_what_removes_them() { let (h, secret) = writer_harness(); let thread_id = seeded(&h, &secret); call_tool( &h, &secret, "update_email", json!({ "threadId": thread_id, "bin": true }), ); // Still in the file — marked, not gone. assert_eq!(count(&h, "cotton_emails"), 1, "binning erased the message"); assert_eq!( count(&h, "cotton_email_threads"), 1, "binning erased the conversation" ); assert!( trashed_at(&h, "cotton_emails").is_some(), "the message went to the bin without being marked as binned" ); assert!( trashed_at(&h, "cotton_email_threads").is_some(), "every message of the conversation is binned, so the conversation should be too" ); // Restoring it is therefore possible, which is the whole point of the above. call_tool( &h, &secret, "update_email", json!({ "threadId": thread_id, "bin": false }), ); assert_eq!(trashed_at(&h, "cotton_emails"), None); assert_eq!(count(&h, "cotton_emails"), 1); // And emptying the bin really does remove the rows. call_tool( &h, &secret, "update_email", json!({ "threadId": thread_id, "bin": true }), ); let emptied = call_tool_as_owner(&h, "device:phone", "empty_bin", json!({})).json(); assert_eq!(emptied["total"], 2); assert_eq!( count(&h, "cotton_emails"), 0, "emptying the bin left the message in the database" ); assert_eq!( count(&h, "cotton_email_threads"), 0, "emptying the bin left the conversation in the database" ); } /// RULE: Mail erased from the bin is never written again at its id, so the same message ingested anew takes the next generation of its derived id rather than a write the store would refuse. #[test] fn reingested_mail_after_bin_erase_takes_the_next_generation() { let (h, secret) = writer_harness(); let thread_id = seeded(&h, &secret); let old_email = h .one("SELECT id FROM cotton_emails LIMIT 1", &[]) .and_then(|row| row.get("id").and_then(Value::as_str).map(str::to_string)) .expect("the message is there"); call_tool( &h, &secret, "update_email", json!({ "threadId": thread_id, "bin": true }), ); let emptied = call_tool_as_owner(&h, "device:phone", "empty_bin", json!({})).json(); assert_eq!(emptied["total"], 2); assert_eq!(count(&h, "cotton_emails"), 0); let again = seeded(&h, &secret); assert_ne!(again, thread_id, "the erased conversation's id is never written again"); let new_email = h .one("SELECT id FROM cotton_emails LIMIT 1", &[]) .and_then(|row| row.get("id").and_then(Value::as_str).map(str::to_string)) .expect("the message is filed again"); assert_ne!(new_email, old_email, "the erased message's id is never written again"); assert_eq!(count(&h, "cotton_emails"), 1); assert_eq!(count(&h, "cotton_email_threads"), 1); assert_eq!( h.count( "SELECT COUNT(*) AS n FROM cotton_emails WHERE id = ?", &[rusqlite::types::Value::Text(old_email.clone())] ), 0, "the erased message stays absent" ); } /// RULE: Mail that is not in the bin is untouched when the bin is emptied. /// /// WHY: emptying the bin is the one call that destroys anything, so it must /// reach nothing but what was put there. #[test] fn emptying_the_bin_leaves_every_conversation_that_was_not_in_it() { let (h, secret) = writer_harness(); seeded(&h, &secret); let emptied = call_tool_as_owner(&h, "device:phone", "empty_bin", json!({})).json(); assert_eq!(emptied["total"], 0); assert_eq!(count(&h, "cotton_emails"), 1); assert_eq!(count(&h, "cotton_email_threads"), 1); }