#![cfg(feature = "comms")]
use std::path::Path;
use std::process::Command;
const BIN: &str = env!("CARGO_BIN_EXE_basemind");
fn comms(comms_dir: &Path, agent: &str, args: &[&str]) -> (bool, String, String) {
let out = Command::new(BIN)
.arg("comms")
.args(args)
.env("BASEMIND_COMMS_DIR", comms_dir)
.env("BASEMIND_AGENT_ID", agent)
.output()
.expect("spawn basemind comms");
(
out.status.success(),
String::from_utf8_lossy(&out.stdout).into_owned(),
String::from_utf8_lossy(&out.stderr).into_owned(),
)
}
#[test]
fn comms_daemon_round_trip_history_is_front_matter_only() {
let tmp = tempfile::tempdir().expect("tempdir");
let comms_dir = tmp.path().join("comms");
let root = tmp.path().to_string_lossy().into_owned();
let scope = format!("path:{root}");
const BODY: &str = "SECRET-BODY-must-never-appear-in-history-lookups";
let (ok, _out, err) = comms(&comms_dir, "agent-alice", &["start"]);
assert!(ok, "comms start failed: {err}");
struct Stop<'a>(&'a Path);
impl Drop for Stop<'_> {
fn drop(&mut self) {
let _ = Command::new(BIN)
.args(["comms", "stop"])
.env("BASEMIND_COMMS_DIR", self.0)
.output();
}
}
let _stop = Stop(&comms_dir);
let (ok, _o, e) = comms(
&comms_dir,
"agent-alice",
&["room-create", "--root", &root, "--scope", &scope, "devroom"],
);
assert!(ok, "room-create failed: {e}");
let (ok, _o, e) = comms(
&comms_dir,
"agent-alice",
&[
"post",
"--root",
&root,
"--body",
BODY,
"devroom",
"Hello team",
],
);
assert!(ok, "post failed: {e}");
let (ok, history, e) = comms(
&comms_dir,
"agent-bob",
&["history", "--root", &root, "devroom", "--json"],
);
assert!(ok, "history failed: {e}");
assert!(
history.contains("Hello team"),
"history should carry the subject front-matter, got: {history}"
);
assert!(
history.contains("agent-alice"),
"history should carry the sender, got: {history}"
);
assert!(
!history.contains(BODY),
"history lookup must be front-matter only — the body leaked: {history}"
);
let id = history
.split("\"id\":\"")
.nth(1)
.and_then(|s| s.split('"').next())
.expect("message id in history json")
.to_string();
let (ok, body, e) = comms(&comms_dir, "agent-bob", &["read", "--root", &root, &id]);
assert!(ok, "read body failed: {e}");
assert!(
body.contains(BODY),
"the explicit body path must return the body, got: {body}"
);
}