//! The mail list's per-thread tests are answered from an index, never from the//! emails' rows (schema 111).usesuper::*;/// The plan SQLite chooses for one statement, as its detail lines.fn plan(db: &cotton_store::driver::Driver, sql: &str) -> Vec<String> { db.all(&format!("EXPLAIN QUERY PLAN {sql}"), &[SqlValue::Text("w".into())]) .expect("the plan") .iter() .filter_map(|row| row.text("detail").map(str::to_string)) .collect()}/// RULE: The mail list's count and page test each thread's mail through the covering index on (thread_id, trashed_at, in_inbox, starred, unread) — never by reading an email's row, bodies and all./// On the owner's serving Mac (14:19:45Z, 11 Oct 2026) this count held the personal file 7.5 s, one email row read per thread.#[test]fn the_lists_per_thread_tests_read_the_live_mail_index_alone() {let db = cotton_store::test_template::create_workspace_in_memory_for_test(&cotton_store::meta::Meta { workspace_id: "w".to_string(), owner_id: "o".to_string(), device_id: "device:plan".to_string(), last_server_seq: 0, last_hlc: None, }) .expect("a fresh file");for folder in [EmailFolder::All, EmailFolder::Inbox, EmailFolder::Archived, EmailFolder::Bin] {let members = folder_members(folder);for flag in ["", " AND e.starred = 1", " AND e.unread = 1"] {let sql = format!("SELECT count(*) FROM cotton_email_threads t WHERE t.workspace_id = ?1 AND EXISTS ({members}{flag})");let detail = plan(&db, &sql).join(" | "); assert!( detail.contains("USING COVERING INDEX idx_cotton_emails_thread_live"),"{folder:?}{flag}: {detail}" ); } }}/// RULE (MAIL PREDICATE phase 1, the 9b review): the reads that take `(unread = 1) AS unread` and/// `sum(CASE WHEN (unread = 1) ...)` as a value are right only while `unread` holds 0 or 1. It does: the mail table's/// codec for it is a boolean, which is written 1 or 0 and refuses anything else.#[test]fn unread_is_written_zero_or_one() {let spec = cotton_store::schema::table_specs::TABLE_SPECS.iter().find(|spec| spec.name == "cotton_emails").expect("the mail table");let codec = spec.columns.iter().find(|(name, _)| *name == "unread").map(|(_, codec)| *codec).expect("its unread column"); assert!(matches!(codec, cotton_store::schema::decl::Codec::Bool), "unread is a boolean column"); assert_eq!(cotton_store::schema::codecs::encode_value(codec, &serde_json::json!(true)).expect("true"), SqlValue::Integer(1)); assert_eq!(cotton_store::schema::codecs::encode_value(codec, &serde_json::json!(false)).expect("false"), SqlValue::Integer(0)); assert!(cotton_store::schema::codecs::encode_value(codec, &serde_json::json!(2)).is_err(), "never another number");}