//! What a WhatsApp connection brought is counted through the index of the//! account each linked message came from. Synthetic data only.usesuper::*;use cotton_store::driver::Driver;use serde_json::json;const OWNER: &str = "00000000-0000-4000-8000-0000000000bb";const WORKSPACE: &str = "00000000-0000-4000-8000-0000000000a1";const NOW: &str = "2026-10-11T07:00:00.000Z";const ACCOUNT: &str = "15550000001";fn t(value: &str) -> SqlValue {SqlValue::Text(value.to_string())}/// One thread holding `linked` live messages the helper brought for ACCOUNT,/// and `others` beside them: imported WhatsApp messages with no account, other/// accounts' and iMessage's, and deleted or hidden ones of ACCOUNT.fn database(linked: i64, others: i64) -> Driver {let db = cotton_store::test_template::create_workspace_in_memory_for_test(&cotton_store::meta::Meta { workspace_id: WORKSPACE.to_string(), owner_id: OWNER.to_string(), device_id: "device:words".to_string(), last_server_seq: 0, last_hlc: None, }) .expect("a fresh file"); db.run("INSERT INTO cotton_workspaces (id, name, owner_user_id, created_at, _hlc) VALUES (?, 'Invented', ?, ?, '')", &[t(WORKSPACE), t(OWNER), t(NOW)], ) .expect("the workspace row"); db.run("INSERT INTO cotton_msg_threads (id, workspace_id, kind, title, created_at, updated_at) VALUES ('thread', ?, 'group', 'Invented', ?, ?)", &[t(WORKSPACE), t(NOW), t(NOW)], ) .expect("a thread");let insert = |prefix: &str, n: i64, channel: &str, account: Option<&str>, deleted: bool, hidden: bool| {let meta = match account {Some(account) => json!({ "source": { "account": account, "chat": "15550000002@s.whatsapp.net" }, "padding": "x".repeat(400) }),None => json!({ "padding": "x".repeat(400) }), }; db.run("WITH RECURSIVE k(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM k WHERE i < ?) \ INSERT INTO cotton_msg_messages (id, thread_id, workspace_id, direction, channel, origin, body, sent_at, created_at, seq, meta, deleted_at, hidden_at) \ SELECT ? || '-' || i, 'thread', ?, 'in', ?, 'imported', 'invented words', ?, ?, i, ?, ?, ? FROM k", &[SqlValue::Integer(n), t(prefix), t(WORKSPACE), t(channel), t(NOW), t(NOW), t(&meta.to_string()),if deleted { t(NOW) } else { SqlValue::Null },if hidden { t(NOW) } else { SqlValue::Null }, ], ) .expect("messages"); }; insert("linked", linked, "whatsapp", Some(ACCOUNT), false, false); insert("deleted", 3, "whatsapp", Some(ACCOUNT), true, false); insert("hidden", 2, "whatsapp", Some(ACCOUNT), false, true); insert("other-account", 7, "whatsapp", Some("15550000009"), false, false); insert("imported", others, "whatsapp", None, false, false); insert("imessage", others, "imessage", None, false, false); db}/// The steps the WhatsApp line reads, and what it says.fn whatsapp_line(db: &Driver) -> (u64, Option<String>) {let before = db.steps();let said = brought(db, WORKSPACE, "whatsapp", ACCOUNT, &json!({ "account": ACCOUNT })).expect("the words"); (db.steps() - before, said)}/// RULE: A WhatsApp connection's "N messages in Messages" costs what its account holds, never the table: it counts through the index of the account a linked message came from, so it reads the same among 100,000 other messages as among 1,000./// On the owner's serving Mac (07:14:22Z, 11 Oct 2026) this count held the personal file 10.6 s — 1,288,830 steps, every message walked and its meta parsed.#[test]fn the_whatsapp_count_costs_what_its_account_holds() {let small = database(1_000, 1_000);let (few, said) = whatsapp_line(&small); assert_eq!(said.as_deref(), Some("1,000 messages in Messages"), "only the account's live messages count");let large = database(1_000, 100_000);let (many, said) = whatsapp_line(&large); assert_eq!(said.as_deref(), Some("1,000 messages in Messages")); assert_eq!(few, many, "the same steps among 100,000 other messages as among 1,000: {:?}", large.steps_by_statement());}const MAILBOX: &str = "owner@example.invalid";/// One thread holding `mine` live emails read from MAILBOX — half named by/// `account` (v76 on), half only by `gmail_account` (read before it), in a/// case Google may hand back — and `others` beside them: another mailbox's,/// and MAILBOX's binned ones. Each carries a body, as real mail does.fn mail_database(mine: i64, others: i64) -> Driver {let db = cotton_store::test_template::create_workspace_in_memory_for_test(&cotton_store::meta::Meta { workspace_id: WORKSPACE.to_string(), owner_id: OWNER.to_string(), device_id: "device:words".to_string(), last_server_seq: 0, last_hlc: None, }) .expect("a fresh file"); db.run("INSERT INTO cotton_workspaces (id, name, owner_user_id, created_at, _hlc) VALUES (?, 'Invented', ?, ?, '')", &[t(WORKSPACE), t(OWNER), t(NOW)], ) .expect("the workspace row"); db.run("INSERT INTO cotton_email_threads (id, workspace_id, external_key, created_at, updated_at) VALUES ('thread', ?, 'thread', ?, ?)", &[t(WORKSPACE), t(NOW), t(NOW)], ) .expect("a thread");let insert = |prefix: &str, n: i64, account: Option<&str>, gmail_account: Option<&str>, binned: bool| { db.run("WITH RECURSIVE k(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM k WHERE i < ?) \ INSERT INTO cotton_emails (id, workspace_id, thread_id, external_id, from_email, sent_at, created_at, body, account, gmail_account, trashed_at) \ SELECT ? || '-' || i, ?, 'thread', ? || '-' || i, 'someone@example.invalid', ?, ?, ?, ?, ?, ? FROM k", &[SqlValue::Integer(n), t(prefix), t(WORKSPACE), t(prefix), t(NOW), t(NOW), t(&"invented words ".repeat(60)), account.map_or(SqlValue::Null, t), gmail_account.map_or(SqlValue::Null, t),if binned { t(NOW) } else { SqlValue::Null }, ], ) .expect("emails"); }; insert("mine", mine / 2, Some(MAILBOX), None, false); insert("mine-before-v76", mine - mine / 2, None, Some("Owner@Example.invalid"), false); insert("binned", 3, Some(MAILBOX), None, true); insert("other-mailbox", others, Some("someone-else@example.invalid"), None, false); db}/// The steps a Google connection's words read, and what they say.fn google_line(db: &Driver) -> (u64, Option<String>) {let before = db.steps();let said = brought(db, WORKSPACE, "google", MAILBOX, &json!({})).expect("the words"); (db.steps() - before, said)}/// RULE: A Google connection's "N emails in Emails" costs what its mailbox holds, never the table: it counts the live mail through the index of the folded account (schema 111), so it reads the same among 50,000 other emails as among 500./// On the owner's serving Mac (14:19:36Z, 11 Oct 2026) this count held the personal file 10.7 s — every one of 68k emails, bodies and all, read to answer one line.#[test]fn the_google_mail_count_costs_what_its_mailbox_holds() {let small = mail_database(500, 500);let (few, said) = google_line(&small); assert_eq!(said.as_deref().map(|words| words.starts_with("500 emails in Emails")), Some(true), "{said:?}");let large = mail_database(500, 50_000);let (many, said) = google_line(&large); assert_eq!(said.as_deref().map(|words| words.starts_with("500 emails in Emails")), Some(true), "{said:?}"); assert_eq!(few, many, "the same steps among 50,000 other emails as among 500: {:?}", large.steps_by_statement());}/// The same count is read from the index alone: the rows of the mailbox's own/// mail — most of a one-account household's 68k — are never opened, so their/// bodies are never paged in. Steps alone cannot see that.#[test]fn the_google_mail_count_never_opens_an_emails_row() {let db = mail_database(10, 10);let detail: Vec<String> = db .all("EXPLAIN QUERY PLAN SELECT COUNT(*) AS n FROM cotton_emails WHERE workspace_id = ? AND lower(coalesce(account, gmail_account)) = lower(?) AND trashed_at IS NULL", &[t(WORKSPACE), t(MAILBOX)], ) .expect("the plan") .iter() .filter_map(|row| row.text("detail").map(str::to_string)) .collect(); assert!( detail.iter().any(|line| line.contains("USING COVERING INDEX idx_cotton_emails_account_live")),"{detail:?}" );}