use super::*; use crate::ids::derived_id; use crate::mail::mime::{decode, GmailFacts, MailFlags}; const DB: &str = "00000000-0000-4000-8000-000000000001"; const MESSAGE: &[u8] = b"Authentication-Results: mx.cloudflare.net; dkim=pass header.d=example.org; spf=pass smtp.mailfrom=example.org; dmarc=pass header.from=example.org\r\n\ Authentication-Results: evil.example; dkim=pass; spf=pass; dmarc=pass\r\n\ From: Ada Example \r\n\ To: me@owner.example, \"Bo\" \r\n\ Subject: Re: Lunch\r\n\ Date: Mon, 5 Oct 2026 09:30:00 +0000\r\n\ Message-ID: \r\n\ In-Reply-To: \r\n\ References: \r\n\ MIME-Version: 1.0\r\n\ Content-Type: multipart/mixed; boundary=\"b\"\r\n\ \r\n\ --b\r\n\ Content-Type: text/plain; charset=utf-8\r\n\ \r\n\ Noon works.\r\n\ --b\r\n\ Content-Type: application/pdf; name=\"menu.pdf\"\r\n\ Content-Disposition: attachment; filename=\"menu.pdf\"\r\n\ Content-Transfer-Encoding: base64\r\n\ \r\n\ UERGREFUQQ==\r\n\ --b--\r\n"; fn at() -> Arriving<'static> { Arriving { database: DB, account: "Me@Owner.example", received: Some(1_790_000_000_000), fetched: None } } #[test] fn an_arrival_lands_on_the_row_the_mac_would_write() { let out = arrive(MESSAGE, &at()).unwrap(); // The Mac's road: the same bytes decoded and mapped as its IMAP reader does. let mut decoded = decode(MESSAGE, "me@owner.example", &GmailFacts::default()).unwrap(); decoded.flags = Some(MailFlags { unread: true, starred: false }); let mac = Page::new().email(&Ids::new(DB), &decoded).unwrap(); assert_eq!(out["email"]["id"], mac.id); assert_eq!(out["thread"]["id"], mac.thread_id); assert_eq!(out["thread"]["key"], "imap:root@example.org"); assert_eq!(out["thread"]["id"], derived_id("cotton_email_threads", &[DB, "imap:root@example.org"])); let data = &out["email"]["data"]; assert_eq!(data["account"], "me@owner.example"); assert_eq!(data["direction"], "in"); assert_eq!(data["unread"], true); assert_eq!(data["in_inbox"], true); assert_eq!(data["body"], "Noon works."); assert_eq!(data["from_email"], "ada@example.org"); assert_eq!(data["message_id"], ""); assert_eq!(data["message_refs"], " "); assert_eq!(out["thread"]["data"]["subject"], "Lunch"); assert_eq!(out["thread"]["data"]["email_count"], 1); assert_eq!(out["messageId"], ""); assert_eq!(out["inReplyTo"], ""); assert_eq!(out["references"], json!(["", ""])); assert_eq!(out["attachments"], json!([{ "index": 0, "name": "menu.pdf", "mime": "application/pdf", "size": 7 }])); let people: Vec<&str> = out["participants"].as_array().unwrap().iter().map(|p| p["data"]["email"].as_str().unwrap()).collect(); assert_eq!(people, vec!["ada@example.org", "me@owner.example", "bo@example.net"]); assert_eq!(out["participants"][0]["data"]["role"], "from"); } #[test] fn only_cloudflares_first_verdict_is_believed() { assert_eq!(cloudflare_auth(MESSAGE), Some(json!({ "spf": "pass", "dkim": "pass", "dmarc": "pass" }))); let forged = b"Authentication-Results: evil.example; dkim=pass; spf=pass; dmarc=pass\r\nFrom: a@b.c\r\n\r\nhi\r\n"; assert_eq!(cloudflare_auth(forged), None, "a sender's own header is never Cloudflare's"); let failing = b"Authentication-Results: mx.cloudflare.net; spf=softfail smtp.mailfrom=b.c; dmarc=none\r\nFrom: a@b.c\r\n\r\nhi\r\n"; assert_eq!(cloudflare_auth(failing), Some(json!({ "spf": "softfail", "dkim": "none", "dmarc": "none" }))); } #[test] fn an_attachments_bytes_come_decoded_by_its_index() { assert_eq!(attachment(MESSAGE, 0).as_deref(), Some(&b"PDFDATA"[..])); assert_eq!(attachment(MESSAGE, 1), None); } #[test] fn what_is_not_mail_is_refused_in_words() { assert!(arrive(b"", &at()).unwrap_err().contains("not_mail")); assert!(arrive(b"Subject: hi\r\nDate: Mon, 5 Oct 2026 09:30:00 +0000\r\n\r\nx\r\n", &at()).unwrap_err().contains("no_sender")); } #[test] fn a_message_with_no_believable_date_is_dated_by_its_arrival() { let raw = b"From: a@example.org\r\nDate: Mon, 16 Sep 2611 10:00:00 +0000\r\nSubject: Odd\r\n\r\nx\r\n"; let out = arrive(raw, &at()).unwrap(); assert_eq!(out["sentMs"], 1_790_000_000_000_i64); } #[test] fn files_that_run_code_are_named() { assert!(runs_code("invoice.PDF.exe")); assert!(runs_code("macro.docm")); assert!(!runs_code("menu.pdf")); assert!(!runs_code("README")); } #[test] fn a_fetched_gmail_message_lands_on_the_row_the_mac_would_write_from_its_labels() { let fetched = Fetched { msgid: Some(1_790_000_000_123_456_789), thrid: Some(1_790_000_000_000_000_001), gm_labels: vec!["\\Sent".into(), "Receipts".into()], flags: vec!["\\Seen".into()], in_inbox: false }; let read = Arriving { fetched: Some(fetched.clone()), ..at() }; let out = arrive(MESSAGE, &read).unwrap(); // The Mac's road for the same read: its labels mapped, its flags its state. let facts = GmailFacts { msgid: fetched.msgid, thrid: fetched.thrid, labels: crate::mail::labels::api_labels(&fetched.gm_labels, &fetched.flags), received: read.received }; let mut decoded = decode(MESSAGE, "me@owner.example", &facts).unwrap(); decoded.flags = Some(MailFlags::of(&fetched.flags)); let mac = Page::new().email(&Ids::new(DB), &decoded).unwrap(); assert_eq!(out["email"]["id"], mac.id); assert_eq!(out["thread"]["id"], mac.thread_id); let data = &out["email"]["data"]; assert_eq!(data["direction"], "out"); assert_eq!(data["unread"], false); assert_eq!(data["in_inbox"], false); } #[test] fn a_fetched_message_from_a_plain_server_says_its_state_by_its_flags_and_its_folder() { let plain = Arriving { fetched: Some(Fetched { flags: vec!["\\Flagged".into()], in_inbox: false, ..Fetched::default() }), ..at() }; let data = arrive(MESSAGE, &plain).unwrap()["email"]["data"].clone(); assert_eq!(data["direction"], "in"); assert_eq!(data["unread"], true); assert_eq!(data["starred"], true); assert_eq!(data["in_inbox"], false); let inbox = Arriving { fetched: Some(Fetched { flags: vec!["\\Seen".into()], in_inbox: true, ..Fetched::default() }), ..at() }; let data = arrive(MESSAGE, &inbox).unwrap()["email"]["data"].clone(); assert_eq!(data["unread"], false); assert_eq!(data["in_inbox"], true); }