105 lines · 6.0 KB · RustRaw
//! People, Notices or Promotions (SENDERS.md "DECIDED: Senders 3"). Every address is invented.use super::*;fn machine(address: &str) -> Facts<'_> { Facts { address, ..Facts::default() }}/// RULE: a person is People and junk is in no group, by the person-or-machine rule, whatever the headers say.#[test]fn people_and_junk_come_from_the_person_or_machine_rule() { let friend = Facts { written_to: true, ..machine("asha.rao@ymail.com") }; let loud = Headers { list_unsubscribe: true, gmail_category: Some("promotions"), ..Headers::default() }; assert_eq!(group(&friend, &loud), Some((Group::People, Because::Person))); assert_eq!(group(&Facts { blocked: true, ..machine("deals@shop.example") }, &Headers::default()), None);}/// RULE: the owner's own word on a sender is above every rule, and Promotions is one he can give.#[test]fn the_owners_word_is_above_the_rules() { let unsub = Headers { list_unsubscribe: true, ..Headers::default() }; assert_eq!(group(&Facts { owner_verdict: Some("service"), ..machine("hello@weekly.example") }, &unsub), Some((Group::Notices, Because::OwnerSaid))); // A personal-looking address he sorted into Promotions is a machine of his sorting. let sorted = Facts { owner_verdict: Some("promotions"), name: Some("Asha Rao"), ..machine("asha.rao@ymail.com") }; assert_eq!(group(&sorted, &Headers::default()), Some((Group::Promotions, Because::OwnerSaid)));}/// RULE: the wanted bulk he met on his own account (bank alerts, a host's invoices, account notices) is Notices even/// with an unsubscribe header or footer; the same sender's offers are Promotions.#[test]fn mail_about_his_things_is_a_notice_even_with_an_unsubscribe() { let unsub = Headers { list_unsubscribe: true, precedence: Some("bulk"), ..Headers::default() }; let alert = Facts { subject: Some("Alert: Rs 2,000 debited from your account"), text: Some("To unsubscribe, manage your preferences."), ..machine("alerts@bank.example") }; assert_eq!(group(&alert, &unsub), Some((Group::Notices, Because::AboutHisThings))); let offer = Facts { subject: Some("Pre-approved loan offer"), ..machine("offers@bank.example") }; assert_eq!(group(&offer, &unsub), Some((Group::Promotions, Because::Marketing)));}/// RULE: Gmail's own category decides a machine nothing above did: Promotions, or Updates, Social and Forums as/// Notices.#[test]fn gmails_category_decides_next() { let promo = Headers { gmail_category: Some("promotions"), ..Headers::default() }; let updates = Headers { gmail_category: Some("updates"), list_unsubscribe: true, ..Headers::default() }; assert_eq!(group(&Facts { subject: Some("What's new"), ..machine("news@host.example") }, &promo), Some((Group::Promotions, Because::GmailSaid))); assert_eq!(group(&Facts { subject: Some("Build failed"), ..machine("notifications@code.example") }, &updates), Some((Group::Notices, Because::GmailSaid)));}/// RULE: with no header kept (mail read before the Mac kept them), an unsubscribe line in the sender's own words or/// offer words make Promotions; any other machine is a Notice; a friend's forwarded newsletter keeps no footer of theirs.#[test]fn with_no_headers_the_words_decide() { let none = Headers::default(); let letter = Facts { subject: Some("Our autumn letter"), text: Some("The garden is full of leaves. To stop these emails, unsubscribe here."), ..machine("letter@old.example") }; assert_eq!(group(&letter, &none), Some((Group::Promotions, Because::Marketing))); let status = Facts { subject: Some("Scheduled maintenance"), text: Some("This is an automated message."), ..machine("status@host.example") }; assert_eq!(group(&status, &none), Some((Group::Notices, Because::Automatic))); let forwarded = Facts { subject: Some("Fwd: thoughts"), text: Some("Automated message from our system.\n---------- Forwarded message ----------\nUnsubscribe here"), ..machine("robot@host.example") }; assert_eq!(group(&forwarded, &none), Some((Group::Notices, Because::Automatic)));}/// The corpus (`group_corpus.txt`): every invented sender lands where a person would put them.#[test]fn the_invented_inbox_lands_where_a_person_would_put_it() { let source = include_str!("group_corpus.txt"); let mut misses = Vec::new(); let mut seen = 0; for (n, line) in source.lines().enumerate() { let line = line.trim(); if line.is_empty() || line.starts_with('#') { continue; } let cols: Vec<&str> = line.split('|').map(str::trim).collect(); assert_eq!(cols.len(), 6, "line {}: six columns", n + 1); let (name, address) = match cols[1].rsplit_once('<') { Some((name, rest)) => (Some(name.trim()).filter(|n| !n.is_empty()), rest.trim_end_matches('>')), None => (None, cols[1]), }; let facts_of: Vec<&str> = cols[3].split(',').map(str::trim).filter(|f| !f.is_empty()).collect(); let value = |key: &str| facts_of.iter().find_map(|f| f.strip_prefix(key).and_then(|r| r.strip_prefix('='))); let has = |key: &str| facts_of.contains(&key); let to = value("to"); let text = cols[4].replace("\\n", "\n"); let facts = Facts { address, blocked: has("blocked"), owner_verdict: value("owner"), in_people: has("people"), written_to: has("written"), stored_kind: value("stored"), name, subject: Some(cols[2]).filter(|s| !s.is_empty()), text: Some(text.as_str()), to_a_list: to.is_some_and(super::super::who::is_list_address), }; let headers = Headers { list_unsubscribe: has("unsub"), precedence: value("prec"), gmail_category: value("cat") }; let got = group(&facts, &headers).map_or("none", |(g, _)| g.as_str()); seen += 1; if got != cols[5] { misses.push(format!("line {}: {} {:?} → {got}, a person says {}", n + 1, cols[1], cols[2], cols[5])); } } assert!(seen >= 30, "the corpus holds {seen}"); assert!(misses.is_empty(), "{}", misses.join("\n"));}