use crate::Mailable;
use std::sync::{Mutex, OnceLock};
static FAKE_SENT: OnceLock<Mutex<Vec<SentMail>>> = OnceLock::new();
pub fn fake() {
FAKE_SENT.get_or_init(|| Mutex::new(Vec::new()));
}
pub(crate) fn is_active() -> bool {
FAKE_SENT.get().is_some()
}
#[derive(Debug, Clone)]
pub struct SentMail {
pub(crate) mailable_type: &'static str,
pub to: Vec<String>,
pub subject: String,
pub html_body: String,
}
pub(crate) fn record(mail: SentMail) -> bool {
let Some(recorder) = FAKE_SENT.get() else {
return false;
};
recorder.lock().unwrap().push(mail);
true
}
pub fn assert_sent<M: Mailable>(predicate: impl Fn(&SentMail) -> bool) {
let type_name = std::any::type_name::<M>();
let matching: Vec<SentMail> = {
let sent = FAKE_SENT
.get()
.expect("Mail::fake() was not called before assert_sent()")
.lock()
.unwrap();
sent.iter()
.filter(|mail| mail.mailable_type == type_name)
.cloned()
.collect()
};
assert!(
matching.iter().any(predicate),
"expected a `{type_name}` to have been sent matching the predicate, \
but none did (sent of this type: {matching:#?})"
);
}
pub fn assert_not_sent<M: Mailable>(predicate: impl Fn(&SentMail) -> bool) {
let type_name = std::any::type_name::<M>();
let matched: bool = {
let sent = FAKE_SENT
.get()
.expect("Mail::fake() was not called before assert_not_sent()")
.lock()
.unwrap();
sent.iter()
.any(|mail| mail.mailable_type == type_name && predicate(mail))
};
assert!(
!matched,
"expected no `{type_name}` to have been sent matching the predicate, but one did"
);
}
#[cfg(test)]
mod tests {
use crate::mail;
struct Greeting;
impl crate::Mailable for Greeting {
fn subject(&self) -> String {
"Hello".to_string()
}
fn html_body(&self) -> String {
"<p>Hi</p>".to_string()
}
}
struct Farewell;
impl crate::Mailable for Farewell {
fn subject(&self) -> String {
"Goodbye".to_string()
}
fn html_body(&self) -> String {
"<p>Bye</p>".to_string()
}
}
struct Reminder;
impl crate::Mailable for Reminder {
fn subject(&self) -> String {
"Don't forget".to_string()
}
fn html_body(&self) -> String {
"<p>Reminder</p>".to_string()
}
}
#[tokio::test]
async fn fake_mode_records_instead_of_dispatching_and_assertions_work() {
super::fake();
mail().to("alice@example.com").send(Greeting).await.unwrap();
super::assert_sent::<Greeting>(|sent| sent.to == vec!["alice@example.com".to_string()]);
let result = std::panic::catch_unwind(|| {
super::assert_sent::<Greeting>(|sent| sent.to == vec!["bob@example.com".to_string()]);
});
assert!(
result.is_err(),
"assert_sent should panic when no recorded mail matches the predicate"
);
let result = std::panic::catch_unwind(|| {
super::assert_sent::<Farewell>(|_| true);
});
assert!(
result.is_err(),
"assert_sent should panic for a type nothing was ever sent as"
);
super::assert_not_sent::<Farewell>(|_| true);
let result = std::panic::catch_unwind(|| {
super::assert_not_sent::<Greeting>(|_| true);
});
assert!(
result.is_err(),
"assert_not_sent should panic when a matching mail was in fact sent"
);
mail()
.to("carol@example.com")
.queue(Reminder)
.await
.unwrap();
super::assert_sent::<Reminder>(|sent| sent.to == vec!["carol@example.com".to_string()]);
}
}