//! The email validators' refusal sentences, pinned. A refusal is part of the//! surface: an assistant reads it and corrects itself, so the words are the//! contract as much as the shape is.use serde_json::{json, Map, Value};usesuper::*;fn args(value: Value) -> Map<String, Value> { value.as_object().cloned().unwrap_or_default()}fn refusal<T>(result: Valid<T>) -> String {match result {Ok(_) => panic!("expected a refusal"),Err(failure) => failure.message().to_string(), }}fn one_email(extra: Value) -> Value {letmut email = json!({"threadKey": "t-1","subject": "Hello","sentAt": "2026-09-01T10:00:00.000Z","body": "Hi","from": { "email": "a@example.com" } });iflet (Some(target), Some(source)) = (email.as_object_mut(), extra.as_object()) {for (key, value) in source { target.insert(key.clone(), value.clone()); } } email}#[test]fn is_address_reads_the_loose_shape_the_archive_needs() { assert!(is_address("a@b")); assert!(is_address("first.last+tag@sub.example.co.uk")); assert!(!is_address("a@b@c")); assert!(!is_address("no-at-sign")); assert!(!is_address("@example.com")); assert!(!is_address("a@")); assert!(!is_address("a b@example.com")); assert!(!is_address("a@exa mple.com")); assert!(!is_address("a\n@example.com"));}#[test]fn ingest_emails_needs_a_non_empty_array() { assert_eq!( refusal(validate_ingest_emails(&args(json!({})))),"emails must be a non-empty array" ); assert_eq!( refusal(validate_ingest_emails(&args(json!({ "emails": [] })))),"emails must be a non-empty array" ); assert_eq!( refusal(validate_ingest_emails(&args(json!({ "emails": "one" })))),"emails must be a non-empty array" );}#[test]fn ingest_emails_caps_the_batch_and_names_the_fix() {let emails: Vec<Value> = (0..=INGEST_EMAILS_MAX_BATCH) .map(|_| one_email(json!({}))) .collect(); assert_eq!( refusal(validate_ingest_emails(&args(json!({ "emails": emails })))),"at most 100 emails per call — split the batch and call again" );}#[test]fn ingest_emails_names_the_index_of_the_email_it_refuses() {let emails = json!({"emails": [one_email(json!({})), { "subject": "x" }] }); assert_eq!( refusal(validate_ingest_emails(&args(emails))),"emails[1].threadKey is required" );}#[test]fn ingest_emails_asks_for_an_empty_string_rather_than_a_missing_field() {letmut email = one_email(json!({})); email.as_object_mut().expect("an object").remove("subject"); assert_eq!( refusal(validate_ingest_emails(&args(json!({ "emails": [email] })))),"emails[0].subject is required (use \"\" for none)" );letmut email = one_email(json!({})); email.as_object_mut().expect("an object").remove("body"); assert_eq!( refusal(validate_ingest_emails(&args(json!({ "emails": [email] })))),"emails[0].body is required (use \"\" for none)" );}#[test]fn ingest_emails_refuses_a_sent_at_that_is_not_a_timestamp() {let email = one_email(json!({ "sentAt": "yesterday" })); assert_eq!( refusal(validate_ingest_emails(&args(json!({ "emails": [email] })))),"emails[0].sentAt must be an ISO 8601 timestamp" );}#[test]fn ingest_emails_names_the_address_field_that_is_wrong() {let email = one_email(json!({ "from": "a@example.com" })); assert_eq!( refusal(validate_ingest_emails(&args(json!({ "emails": [email] })))),"emails[0].from must be an object with an email" );let email = one_email(json!({ "from": { "email": "nope" } })); assert_eq!( refusal(validate_ingest_emails(&args(json!({ "emails": [email] })))),"emails[0].from.email must be an email address" );let email = one_email(json!({ "to": "b@example.com" })); assert_eq!( refusal(validate_ingest_emails(&args(json!({ "emails": [email] })))),"emails[0].to must be an array of { email, name? }" );let email = one_email(json!({ "cc": [{ "email": "b@example.com" }, { "email": "bad" }] })); assert_eq!( refusal(validate_ingest_emails(&args(json!({ "emails": [email] })))),"emails[0].cc[1].email must be an email address" );}#[test]fn ingest_emails_refuses_a_bad_attachment_by_index() {let email = one_email(json!({ "attachments": {} })); assert_eq!( refusal(validate_ingest_emails(&args(json!({ "emails": [email] })))),"emails[0].attachments must be an array" );let email = one_email(json!({ "attachments": ["a.pdf"] })); assert_eq!( refusal(validate_ingest_emails(&args(json!({ "emails": [email] })))),"emails[0].attachments[0] must be an object" );let email = one_email(json!({ "attachments": [{ "mime": "application/pdf" }] })); assert_eq!( refusal(validate_ingest_emails(&args(json!({ "emails": [email] })))),"emails[0].attachments[0].name is required" );let email = one_email(json!({ "attachments": [{ "name": "a.pdf", "size": -1 }] })); assert_eq!( refusal(validate_ingest_emails(&args(json!({ "emails": [email] })))),"emails[0].attachments[0].size must be a whole number of bytes" );let email = one_email(json!({ "attachments": [{ "name": "a.pdf", "size": 1.5 }] })); assert_eq!( refusal(validate_ingest_emails(&args(json!({ "emails": [email] })))),"emails[0].attachments[0].size must be a whole number of bytes" );}#[test]fn ingest_emails_carries_no_gmail_block_from_the_mcp_surface() {// The MCP validator never reads one — see the note above// `validate_ingest_emails`. Mail no mailbox filed takes the columns' own// defaults, and the block reaches the argument only from the Gmail sync.let email = one_email(json!({"gmail": { "id": "g1", "account": "a@example.com", "labels": ["INBOX", "UNREAD"] } }));let parsed = validate_ingest_emails(&args(json!({ "emails": [email] }))).expect("valid"); assert_eq!(parsed.len(), 1); assert!(parsed[0].gmail.is_none());}#[test]fn ingest_emails_trims_the_subject_and_normalises_the_instant() {let email = one_email(json!({ "subject": " Hello ", "sentAt": "2026-09-01T10:00:00Z" }));let parsed = validate_ingest_emails(&args(json!({ "emails": [email] }))).expect("valid"); assert_eq!(parsed[0].subject, "Hello"); assert_eq!(parsed[0].sent_at, "2026-09-01T10:00:00.000Z");}#[test]fn list_email_threads_refuses_an_address_that_is_not_one() { assert_eq!( refusal(validate_list_email_threads(&args( json!({ "participant": "nope" }) ))),"participant must be an email address" ); assert_eq!( refusal(validate_list_email_threads(&args( json!({ "sender": "nope" }) ))),"sender must be an email address" );}#[test]fn list_email_threads_names_the_folders_it_knows() { assert_eq!( refusal(validate_list_email_threads(&args( json!({ "folder": "drafts" }) ))),"folder must be one of inbox, archived, bin, all" );}#[test]fn list_email_threads_lowercases_the_addresses_and_defaults_the_folder() {let parsed = validate_list_email_threads(&args(json!({"participant": "Asha@Example.com","sender": "News@Acme.com" }))) .expect("valid"); assert_eq!(parsed.participant.as_deref(), Some("asha@example.com")); assert_eq!(parsed.sender.as_deref(), Some("news@acme.com")); assert_eq!(parsed.folder, EmailFolder::All); assert_eq!(parsed.limit, 50);}#[test]fn read_email_thread_asks_for_a_thread_id() { assert_eq!( refusal(validate_read_email_thread(&args(json!({})))),"threadId must be a thread id from list_email_threads" ); assert_eq!( refusal(validate_read_email_thread(&args( json!({ "threadId": "not-a-uuid" }) ))),"threadId must be a thread id from list_email_threads" );}#[test]fn read_email_thread_refuses_a_before_that_is_not_a_timestamp() { assert_eq!( refusal(validate_read_email_thread(&args(json!({"threadId": "11111111-1111-4111-8111-111111111111","before": "yesterday" })))),"before must be an ISO 8601 timestamp" );}#[test]fn update_email_asks_for_exactly_one_target() { assert_eq!( refusal(validate_update_email(&args(json!({ "read": true })))),"name exactly one of id (one message) or threadId (the whole conversation)" ); assert_eq!( refusal(validate_update_email(&args(json!({"id": "11111111-1111-4111-8111-111111111111","threadId": "22222222-2222-4222-8222-222222222222","read": true })))),"name exactly one of id (one message) or threadId (the whole conversation)" ); assert_eq!( refusal(validate_update_email(&args(json!({"id": "nope","read": true })))),"id and threadId are ids from the email tools" );}#[test]fn an_empty_state_patch_is_refused_rather_than_treated_as_a_no_op() { assert_eq!( refusal(validate_update_email(&args(json!({"id": "11111111-1111-4111-8111-111111111111" })))),"name at least one of read, starred, inbox or bin — there is nothing to change" );}#[test]fn update_emails_asks_for_exactly_one_list() { assert_eq!( refusal(validate_update_emails(&args(json!({ "read": true })))),"name exactly one of ids (messages) or threadIds (conversations)" ); assert_eq!( refusal(validate_update_emails(&args(json!({ "ids": [] })))),"ids must be a non-empty array of ids" ); assert_eq!( refusal(validate_update_emails(&args(json!({ "ids": ["nope"] })))),"ids must hold ids from list_email_threads or read_email_thread" );let many: Vec<Value> = (0..=UPDATE_EMAILS_MAX_BATCH) .map(|_| Value::from("11111111-1111-4111-8111-111111111111")) .collect(); assert_eq!( refusal(validate_update_emails(&args(json!({ "ids": many })))),"at most 100 ids per call — split the batch and call again" );}#[test]fn update_emails_dedupes_the_ids_it_was_handed() {let parsed = validate_update_emails(&args(json!({"ids": ["11111111-1111-4111-8111-111111111111","11111111-1111-4111-8111-111111111111" ],"read": true }))) .expect("valid"); assert_eq!(parsed.ids.as_deref().map(<[String]>::len), Some(1));}#[test]fn block_sender_lowercases_the_address_because_it_is_an_identity() { assert_eq!( validate_block_sender(&args(json!({ "address": "News@Acme.com" }))).expect("valid"),"news@acme.com" ); assert_eq!( refusal(validate_block_sender(&args(json!({ "address": "nope" })))),"address must be an email address" ); assert_eq!( refusal(validate_block_sender(&args(json!({})))),"address must be an email address" );}#[test]fn list_email_senders_takes_the_same_folder_vocabulary() { assert_eq!( refusal(validate_list_email_senders(&args( json!({ "folder": "spam" }) ))),"folder must be one of inbox, archived, bin, all" );let parsed = validate_list_email_senders(&args(json!({ "folder": "bin" }))).expect("valid"); assert_eq!(parsed.folder, EmailFolder::Bin); assert_eq!(parsed.limit, 50);}