[core] Tests: the mandatory suites, red against the API stubs (55 tests)
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
//! Mandatory: compose round trip, and the headers and bodies of reply, reply all and forward.
|
||||
|
||||
use epistula_core::{Address, Draft, OutAttachment, ReplyMode, compose, forward, message_id, parse, reply};
|
||||
use proptest::prelude::*;
|
||||
|
||||
fn me() -> Address {
|
||||
Address::new(Some("Mark Novák"), "mark@example.cz")
|
||||
}
|
||||
|
||||
fn original() -> epistula_core::Parsed {
|
||||
let raw = "From: Anna <anna@example.com>\r\n\
|
||||
To: Mark Novák <mark@example.cz>, Jiří <jiri@example.cz>\r\n\
|
||||
Cc: eva@example.cz, MARK@example.cz\r\n\
|
||||
Subject: Výlet v sobotu\r\n\
|
||||
Date: Sat, 08 Nov 2026 10:00:00 +0100\r\n\
|
||||
Message-ID: <orig-2@example.com>\r\n\
|
||||
References: <root-0@example.com> <orig-1@example.com>\r\n\
|
||||
In-Reply-To: <orig-1@example.com>\r\n\
|
||||
Content-Type: multipart/mixed; boundary=b\r\n\r\n\
|
||||
--b\r\nContent-Type: multipart/alternative; boundary=a\r\n\r\n\
|
||||
--a\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nPojedeme v 8.\r\nVezmi deštník.\r\n\
|
||||
--a\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<p>Pojedeme v <b>8</b>.</p>\r\n\
|
||||
--a--\r\n\
|
||||
--b\r\nContent-Type: application/pdf\r\nContent-Disposition: attachment; filename=mapa.pdf\r\n\r\nPDFDATA\r\n\
|
||||
--b--\r\n";
|
||||
parse(raw.as_bytes()).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reply_sets_threading_headers_and_quotes() {
|
||||
let orig = original();
|
||||
let d = reply(&orig, &me(), ReplyMode::Sender);
|
||||
assert_eq!(d.subject, "Re: Výlet v sobotu");
|
||||
assert_eq!(d.to, vec![Address::new(Some("Anna"), "anna@example.com")]);
|
||||
assert!(d.cc.is_empty());
|
||||
assert_eq!(d.in_reply_to.as_deref(), Some("orig-2@example.com"));
|
||||
assert_eq!(d.references, vec!["root-0@example.com", "orig-1@example.com", "orig-2@example.com"]);
|
||||
assert!(d.text.contains("Anna <anna@example.com> wrote:"), "{}", d.text);
|
||||
assert!(d.text.contains("> Pojedeme v 8.\n> Vezmi deštník."), "{}", d.text);
|
||||
let html = d.html.as_deref().unwrap();
|
||||
assert!(html.contains("<blockquote"), "{html}");
|
||||
assert!(html.contains("<b>8</b>"), "{html}");
|
||||
assert!(d.attachments.is_empty(), "a reply does not carry attachments");
|
||||
assert_eq!(d.from, me());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reply_does_not_stack_prefixes_and_prefers_reply_to() {
|
||||
let raw = b"From: list@example.com\r\nReply-To: Anna <anna@example.com>\r\nSubject: RE: Re: plan\r\nMessage-ID: <x@y>\r\n\r\nhi\r\n";
|
||||
let d = reply(&parse(raw).unwrap(), &me(), ReplyMode::Sender);
|
||||
assert_eq!(d.subject, "Re: plan");
|
||||
assert_eq!(d.to[0].email, "anna@example.com");
|
||||
assert_eq!(d.references, vec!["x@y"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reply_all_keeps_everyone_but_me_once() {
|
||||
let d = reply(&original(), &me(), ReplyMode::All);
|
||||
let to: Vec<_> = d.to.iter().map(|a| a.email.as_str()).collect();
|
||||
let cc: Vec<_> = d.cc.iter().map(|a| a.email.as_str()).collect();
|
||||
assert_eq!(to, vec!["anna@example.com"]);
|
||||
assert_eq!(cc, vec!["jiri@example.cz", "eva@example.cz"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reply_to_my_own_sent_mail_goes_to_the_original_recipients() {
|
||||
let raw = b"From: Mark <mark@example.cz>\r\nTo: anna@example.com\r\nSubject: s\r\nMessage-ID: <m@x>\r\n\r\nhi\r\n";
|
||||
let d = reply(&parse(raw).unwrap(), &me(), ReplyMode::Sender);
|
||||
assert_eq!(d.to[0].email, "anna@example.com");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forward_keeps_attachments_and_the_original() {
|
||||
let orig = original();
|
||||
let d = forward(&orig, &me());
|
||||
assert_eq!(d.subject, "Fwd: Výlet v sobotu");
|
||||
assert!(d.to.is_empty() && d.cc.is_empty());
|
||||
assert_eq!(d.attachments.len(), 1);
|
||||
assert_eq!(d.attachments[0].filename, "mapa.pdf");
|
||||
assert_eq!(d.attachments[0].content_type, "application/pdf");
|
||||
assert_eq!(d.attachments[0].data, orig.attachments[0].data);
|
||||
assert!(d.text.contains("Forwarded message"), "{}", d.text);
|
||||
assert!(d.text.contains("Subject: Výlet v sobotu"), "{}", d.text);
|
||||
assert!(d.text.contains("Pojedeme v 8."), "{}", d.text);
|
||||
assert!(d.html.as_deref().unwrap().contains("<b>8</b>"));
|
||||
// Forwarded attachments survive compose -> parse.
|
||||
let back = parse(&compose(&d, "fwd-1@example.cz", 1_794_128_400)).unwrap();
|
||||
assert_eq!(back.attachments.len(), 1);
|
||||
assert_eq!(back.attachments[0].data, orig.attachments[0].data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn composed_reply_carries_in_reply_to_and_references() {
|
||||
let d = reply(&original(), &me(), ReplyMode::All);
|
||||
let raw = compose(&d, "reply-1@example.cz", 1_794_128_400);
|
||||
let back = parse(&raw).unwrap();
|
||||
assert_eq!(back.message_id.as_deref(), Some("reply-1@example.cz"));
|
||||
assert_eq!(back.in_reply_to, vec!["orig-2@example.com"]);
|
||||
assert_eq!(back.references, vec!["root-0@example.com", "orig-1@example.com", "orig-2@example.com"]);
|
||||
assert_eq!(back.date, Some(1_794_128_400));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bcc_is_never_written_into_the_message() {
|
||||
let d = Draft {
|
||||
from: me(),
|
||||
to: vec![Address::new(None, "anna@example.com")],
|
||||
bcc: vec![Address::new(Some("Secret"), "hidden@example.cz")],
|
||||
subject: "s".into(),
|
||||
text: "t".into(),
|
||||
..Default::default()
|
||||
};
|
||||
let raw = String::from_utf8(compose(&d, "b@example.cz", 0)).unwrap();
|
||||
assert!(!raw.contains("hidden@example.cz"), "{raw}");
|
||||
assert!(!raw.to_ascii_lowercase().contains("bcc:"), "{raw}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compose_is_deterministic() {
|
||||
let d = forward(&original(), &me());
|
||||
assert_eq!(compose(&d, "same@example.cz", 5), compose(&d, "same@example.cz", 5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn message_id_shape() {
|
||||
let id = message_id([0xAB; 16], "example.cz");
|
||||
assert_eq!(id, format!("{}@example.cz", "ab".repeat(16)));
|
||||
assert_ne!(message_id([1; 16], "example.cz"), message_id([2; 16], "example.cz"));
|
||||
}
|
||||
|
||||
fn address() -> impl Strategy<Value = Address> {
|
||||
(
|
||||
proptest::option::of("[A-Za-zÁČĎÉĚÍŇÓŘŠŤÚŮÝŽáčďéěíňóřšťúůýž][A-Za-zčřšžý .'-]{0,20}[a-zý]"),
|
||||
"[a-z][a-z0-9._-]{0,10}[a-z0-9]@[a-z]{2,10}\\.(cz|com|org)",
|
||||
)
|
||||
.prop_map(|(name, email)| Address::new(name.as_deref(), &email))
|
||||
}
|
||||
|
||||
fn body_text() -> impl Strategy<Value = String> {
|
||||
proptest::collection::vec("[A-Za-z0-9ěščřžýáíéúůĚŠČŘŽ€ .,!?:;()+-]{0,70}", 0..8)
|
||||
.prop_map(|lines| lines.iter().map(|l| l.trim_end()).collect::<Vec<_>>().join("\n"))
|
||||
}
|
||||
|
||||
fn attachment() -> impl Strategy<Value = OutAttachment> {
|
||||
(
|
||||
"[A-Za-z0-9_-]{1,20}",
|
||||
prop_oneof![Just(("pdf", "application/pdf")), Just(("png", "image/png")), Just(("bin", "application/octet-stream"))],
|
||||
proptest::collection::vec(any::<u8>(), 0..3000),
|
||||
)
|
||||
.prop_map(|(stem, (ext, ct), data)| OutAttachment {
|
||||
filename: format!("{stem}.{ext}"),
|
||||
content_type: ct.into(),
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
fn draft() -> impl Strategy<Value = Draft> {
|
||||
(
|
||||
address(),
|
||||
proptest::collection::vec(address(), 1..4),
|
||||
proptest::collection::vec(address(), 0..3),
|
||||
"[A-Za-z0-9ěščřžýáíé€][A-Za-z0-9ěščřžýáíé€ ,.!?:-]{0,120}[A-Za-z0-9ěščřžýáíé€]",
|
||||
body_text(),
|
||||
any::<bool>(),
|
||||
proptest::collection::vec(attachment(), 0..3),
|
||||
)
|
||||
.prop_map(|(from, to, cc, subject, text, with_html, attachments)| {
|
||||
let html = with_html.then(|| {
|
||||
format!("<p>{}</p>", text.replace('&', "&").replace('<', "<").replace('\n', "<br>"))
|
||||
});
|
||||
Draft { from, to, cc, subject, text, html, attachments, ..Default::default() }
|
||||
})
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#![proptest_config(ProptestConfig::with_cases(300))]
|
||||
|
||||
#[test]
|
||||
fn parse_of_compose_is_identity(d in draft()) {
|
||||
let raw = compose(&d, "prop@example.cz", 1_700_000_000);
|
||||
let back = parse(&raw).unwrap();
|
||||
prop_assert_eq!(&back.subject, &d.subject);
|
||||
prop_assert_eq!(&back.from, &vec![d.from.clone()]);
|
||||
prop_assert_eq!(&back.to, &d.to);
|
||||
prop_assert_eq!(&back.cc, &d.cc);
|
||||
prop_assert_eq!(back.text.as_deref().unwrap_or("").trim_end(), d.text.trim_end());
|
||||
prop_assert_eq!(back.html.as_deref().map(str::trim_end), d.html.as_deref().map(str::trim_end));
|
||||
let files: Vec<_> = back.attachments.iter()
|
||||
.map(|a| (a.filename.clone(), a.content_type.clone(), a.data.clone())).collect();
|
||||
let expected: Vec<_> = d.attachments.iter()
|
||||
.map(|a| (a.filename.clone(), a.content_type.clone(), a.data.clone())).collect();
|
||||
prop_assert_eq!(files, expected);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user