use std::{
fs,
io::Write,
path::Path,
process::Command,
thread::sleep,
time::{Duration, SystemTime, UNIX_EPOCH},
};
use io_pimdir::{
client::{blobs::PimdirBlobs, producer::PimdirProducer},
codec::PimdirAction,
object::PimdirObject,
};
const IMAP_ROOT: &str = "imap://127.0.0.1:143";
const SMTP: &str = "smtp://127.0.0.1:2525";
const USER: &str = "test@pimalaya.org";
const PASS: &str = "P!malaya-test-2026";
const SUBMIT: &str = "submit";
fn marker() -> String {
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("system clock after the epoch")
.as_nanos();
format!("SUBMITMARKER{nanos}")
}
fn message(marker: &str) -> Vec<u8> {
format!(
"Message-ID: <{marker}@pimalaya.org>\r\n\
From: {USER}\r\n\
To: {USER}\r\n\
Subject: neverest submission {marker}\r\n\
Date: Tue, 25 Aug 2026 10:00:00 +0000\r\n\
\r\n\
{marker}\r\n",
)
.into_bytes()
}
#[test]
#[ignore = "requires a Stalwart instance (./tests/stalwart2.sh) on :143/:2525 and --ignored"]
fn a_queued_submit_intent_leaves_through_smtp_and_comes_back_through_imap() {
let tmp = tempfile::tempdir().expect("temp dir");
let root = tmp.path();
let state = root.join("state");
let config = root.join("config.toml");
fs::create_dir_all(&state).unwrap();
fs::write(
&config,
format!(
"[accounts.submit]\n\
imap.server = \"{IMAP_ROOT}\"\n\
imap.starttls = false\n\
imap.sasl.plain.username = \"{USER}\"\n\
imap.sasl.plain.password.raw = \"{PASS}\"\n\
smtp.server = \"{SMTP}\"\n",
),
)
.unwrap();
neverest(&["init", "-a", "submit"], &config, &state);
neverest(&["sync", "-a", "submit"], &config, &state);
let store = state.join("neverest").join("submit");
let marker = marker();
let body = message(&marker);
let mut producer = PimdirProducer::open(&store, "neverest-tests").expect("open producer");
let hash = producer.hash(&body);
let blobs = PimdirBlobs::open(&store, producer.hash_algo());
let mut writer = blobs.writer().expect("blob writer");
writer.write_all(&body).unwrap();
let object = PimdirObject {
hash: hash.clone(),
size: writer.commit(&hash).expect("commit the body") as usize,
};
producer
.enqueue(
"INBOX",
&PimdirAction::Unknown {
kind: SUBMIT.into(),
payload: format!(
"{{\"v\":1,\"object\":\"{}\",\"from\":\"{USER}\",\
\"rcpts\":[\"{USER}\"],\"subject\":\"neverest submission {marker}\"}}",
hash.0,
),
object_hash: Some(hash.clone()),
},
Some(&object),
)
.expect("enqueue the intent");
drop(producer);
let queued = pimdir(&state, &["queue", "list", "--json"]);
assert!(
queued.contains(SUBMIT),
"the intent is queued before the run; queue held:\n{queued}",
);
let report = neverest(&["sync", "-a", "submit", "--json"], &config, &state);
assert!(
report.contains("\"submitted\""),
"the run reported the submission; report was:\n{report}",
);
assert!(
!report.contains("\"parked\":true"),
"the intent was not parked; report was:\n{report}",
);
let queued = pimdir(&state, &["queue", "list", "--json"]);
assert!(
!queued.contains(SUBMIT),
"an acknowledged intent leaves the queue; queue held:\n{queued}",
);
let delivered = wait_for_delivery(&marker).expect("the submitted message reached the server");
neverest(&["sync", "-a", "submit"], &config, &state);
let items = pimdir(&state, &["item", "list", &delivered, "--json"]);
assert!(
items.contains(&marker),
"the submitted message came back into the store; {delivered} held:\n{items}",
);
}
fn wait_for_delivery(marker: &str) -> Option<String> {
for _ in 0..30 {
for (mailbox, path) in [("INBOX", "INBOX"), ("Junk Mail", "Junk%20Mail")] {
let search = Command::new("curl")
.args(["-fsS", "--url", &format!("{IMAP_ROOT}/{path}")])
.args(["--user", &format!("{USER}:{PASS}")])
.args(["-X", &format!("SEARCH TEXT {marker}")])
.output()
.expect("spawn curl search");
let hits = String::from_utf8_lossy(&search.stdout);
if hits.split_whitespace().any(|t| t.parse::<u32>().is_ok()) {
return Some(format!("imap/{mailbox}"));
}
}
sleep(Duration::from_secs(1));
}
None
}
fn pimdir(state: &Path, args: &[&str]) -> String {
let store = state.join("neverest").join("submit");
let output = Command::new("pimdir")
.args(["--store", &store.to_string_lossy()])
.args(args)
.output()
.expect("spawn pimdir (cargo install --path ../io-pimdir --features cli)");
assert!(
output.status.success(),
"`pimdir {}` failed:\n{}",
args.join(" "),
String::from_utf8_lossy(&output.stderr),
);
String::from_utf8_lossy(&output.stdout).into_owned()
}
fn neverest(args: &[&str], config: &Path, state: &Path) -> String {
let output = Command::new(env!("CARGO_BIN_EXE_neverest"))
.args(["-c", &config.to_string_lossy()])
.args(args)
.env("XDG_STATE_HOME", state)
.output()
.expect("spawn neverest");
assert!(
output.status.success(),
"`neverest {}` failed:\n--- stdout ---\n{}\n--- stderr ---\n{}",
args.join(" "),
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
);
String::from_utf8_lossy(&output.stdout).into_owned()
}