usesuper::*;use serde_json::json;/// A copy on this build's own store schema (157), its 2,500 mails held from before it: no landing time.fn copy() -> cotton_store::Driver {let db = cotton_store::Driver::open_in_memory().unwrap(); cotton_store::create::apply_schema(&db).unwrap(); db.exec("INSERT INTO cotton_workspaces(id,name,owner_user_id,created_at) VALUES ('synthetic','Synthetic','person','2026-01-01T00:00:00.000Z')").unwrap(); db.exec("INSERT INTO cotton_email_threads(id,workspace_id,external_key,subject,last_at,created_at,updated_at) VALUES ('t-1','synthetic','k-1','Invented','2026-11-01T00:00:00.000Z','2026-11-01T00:00:00.000Z','2026-11-01T00:00:00.000Z')").unwrap(); db.exec("BEGIN").unwrap();for n in0..2_500 {let from = if n % 2 == 0 { " Ada@Example.test" } else { "bo@shop.example.test" }; db.run("INSERT INTO cotton_emails(id,workspace_id,thread_id,external_id,from_email,subject,body,sent_at,created_at) VALUES (?,'synthetic','t-1',?,?,'Invented','A body long enough to sit on its own pages.','2026-11-01T00:00:00.000Z','2026-11-01T00:00:00.000Z')", &[rusqlite::types::Value::Text(format!("e-{n:05}")), rusqlite::types::Value::Text(format!("<{n}@invented.example>")), rusqlite::types::Value::Text(from.into())], ) .unwrap(); } db.exec("COMMIT").unwrap(); db}fn landed(db: &cotton_store::Driver, id: &str) -> Option<String> { db.get("SELECT landed_at FROM _mail_landed WHERE id = ?", &[rusqlite::types::Value::Text(id.into())]).unwrap().and_then(|r| r.text("landed_at").map(str::to_string))}fn changes(db: &cotton_store::Driver) -> i64 { db.get("SELECT total_changes() AS n", &[]).unwrap().and_then(|r| r.int("n")).unwrap()}/// RULE (MAIL PREDICATE phase 2, the backfill; PHASE 2 PHONE 2, item 5): mail held before schema 157 takes its landing/// time from the object's pages into `_mail_landed`, slice by slice, resumable from the last id applied, and never/// writes the mail row; a time in another shape is pinned or left out; a mail pulled since (already landed, by the/// store's trigger) is never overwritten; once the object says no more follow, nothing is owed.#[test]fn held_mail_takes_its_landing_time_from_the_pages_beside_the_row_and_resumes() {let db = copy();let file = db.connection();// Pulled since 157: the row brings its own time, and the store's trigger keeps it beside the row. db.exec("UPDATE cotton_emails SET landed_at = '2026-11-27T09:00:00.000Z' WHERE id = 'e-00001'").unwrap(); assert_eq!(landed(&db, "e-00001").as_deref(), Some("2026-11-27T09:00:00.000Z")); assert_eq!(owed(file).unwrap().as_deref(), Some(""), "owed from the first");let all: Vec<(String, String)> = (0..2_500).map(|n| (format!("e-{n:05}"), if n == 7 { "2026-11-20T15:30:00+05:30".to_string() } elseif n == 8 { "yesterday".to_string() } else { "2026-11-20T10:00:00.000Z".to_string() })).collect();let (first, more) = page_of(&json!({ "rows": all[..1_000].iter().map(|(i, a)| json!([i, a])).collect::<Vec<_>>(), "after": "e-00999" })); assert!(more);let before = changes(&db);let filled = apply(file, &first, false).unwrap(); assert_eq!(filled, 998, "every held mail but the one already landed and the one whose time does not read");// 998 narrow rows and the place: no mail row written (its FTS and every other trigger would count too). assert_eq!(changes(&db) - before, 999, "the mail rows are never written");let rows_landed = db.get("SELECT count(*) AS n FROM cotton_emails WHERE landed_at IS NOT NULL", &[]).unwrap().and_then(|r| r.int("n")).unwrap(); assert_eq!(rows_landed, 1, "only the pulled one carries it on its row"); assert_eq!(owed(file).unwrap().as_deref(), Some("e-00999"), "resumes after the last id applied"); assert_eq!(landed(&db, "e-00001").as_deref(), Some("2026-11-27T09:00:00.000Z"), "a mail pulled since keeps its own"); assert_eq!(landed(&db, "e-00007").as_deref(), Some("2026-11-20T10:00:00.000Z"), "pinned to UTC milliseconds"); assert_eq!(landed(&db, "e-00008"), None, "a time that does not read is never written");let key = db.get("SELECT \"key\", domain FROM _mail_landed WHERE id = 'e-00000'", &[]).unwrap().unwrap(); assert_eq!((key.text("key"), key.text("domain")), (Some("ada@example.test"), Some("@example.test")), "the sender as every cut matches it"); apply(file, &all[1_000..2_000], false).unwrap(); apply(file, &all[2_000..], true).unwrap(); assert_eq!(owed(file).unwrap(), None, "nothing owed once the object said no more"); assert_eq!(missing(file, 10).unwrap(), ["e-00008"]);}/// RULE (PHASE 2 PHONE 2, item 5): once the pages are done, mail still without a time is asked for by id once a day;/// until one such round the copy is not caught up (a Delete says they will go), after it it is (a Delete says they/// are not covered). A mail erased goes from `_mail_landed` with it.#[test]fn mail_with_no_time_is_asked_for_once_a_day_then_said_plainly() {let db = copy();let file = db.connection();let all: Vec<(String, String)> = (0..2_500).filter(|n| *n != 8).map(|n| (format!("e-{n:05}"), "2026-11-20T10:00:00.000Z".to_string())).collect(); apply(file, &all, true).unwrap();let now = chrono::DateTime::parse_from_rfc3339("2026-11-27T10:00:00Z").unwrap().with_timezone(&chrono::Utc); assert!(!caught_up(file).unwrap(), "the pages are done, the re-ask is not"); assert!(reask_due(file, now).unwrap()); assert_eq!(missing(file, REASK_IDS).unwrap(), ["e-00008"]);// The object has no time for it either. asked(file, &[], now).unwrap(); assert!(caught_up(file).unwrap()); assert!(!reask_due(file, now + chrono::Duration::hours(23)).unwrap(), "once a day"); assert!(reask_due(file, now + chrono::Duration::hours(25)).unwrap()); asked(file, &[("e-00008".into(), "2026-11-20T10:00:00.000Z".into())], now + chrono::Duration::hours(25)).unwrap(); assert!(missing(file, 10).unwrap().is_empty()); db.exec("DELETE FROM cotton_emails WHERE id = 'e-00008'").unwrap(); assert_eq!(landed(&db, "e-00008"), None, "gone with its mail");}/// RULE: a copy whose mail all landed owes nothing.#[test]fn nothing_is_owed_when_every_mail_has_landed() {let db = copy(); db.exec("UPDATE cotton_emails SET landed_at = '2026-11-27T09:00:00.000Z'").unwrap(); assert_eq!(owed(db.connection()).unwrap(), None); assert!(caught_up(db.connection()).unwrap());}