use std::path::Path;
mod governing;
use super::*;
use crate::boundary::tests::{agent, snapshot};
use crate::git_tree::{AgentState, Stream};
use tempfile::TempDir;
const AGENT: &str = "c-1";
const CHILD: &str = "c-1-w-1";
fn world() -> TempDir {
let dir = tempfile::tempdir().unwrap();
let messages = dir.path().join("agents").join(AGENT).join("messages");
std::fs::create_dir_all(&messages).unwrap();
std::fs::write(messages.join("001-user.md"), b"is this thing on?\n").unwrap();
dir
}
fn snap_at(ws: &Path, state: AgentState) -> Snapshot {
let mut row = agent(AGENT, state, 100);
row.stream = Stream {
text: Some("half a thought".to_owned()),
..Stream::default()
};
snapshot(ws, "alba", vec![row], vec![])
}
#[test]
fn the_live_tail_is_folded_only_while_a_call_is_in_flight() {
let dir = world();
let (ws, other) = (dir.path(), Path::new("/nowhere"));
let flying = snap_at(ws, AgentState::InFlight);
let tail = transcript(&flying, ws, AGENT);
assert_eq!(tail.entries.len(), 2, "the committed half plus the tail");
assert_eq!(
tail.entries[1].kind,
crate::transcript::EntryKind::Streaming {
thinking: String::new(),
text: "half a thought".to_owned(),
}
);
for (snap, at, agent_id) in [
(snap_at(ws, AgentState::Quiescent), ws, AGENT),
(snap_at(ws, AgentState::InFlight), ws, "who"),
(snap_at(other, AgentState::InFlight), ws, AGENT),
] {
assert!(live_tail(&snap, at, agent_id).is_none());
assert_eq!(
transcript(&snap, at, agent_id),
crate::transcript::build(at, agent_id),
"nothing beyond the committed half"
);
}
}
#[test]
fn the_steps_read_their_liveness_off_the_snapshot() {
let dir = world();
let ws = dir.path();
let step = ws.join("steps").join(AGENT).join("001");
std::fs::create_dir_all(&step).unwrap();
std::fs::write(step.join("response.json"), b"").unwrap();
let driven = steps(&snap_at(ws, AgentState::Live), ws, AGENT);
assert_eq!(driven.steps.len(), 1);
assert!(!driven.steps[0].wound.wounded(), "a driver is filling it");
let abandoned = steps(&snap_at(ws, AgentState::Stopped), ws, AGENT);
assert!(abandoned.steps[0].wound.wounded(), "nobody is driving it");
assert!(
steps(&snap_at(Path::new("/nowhere"), AgentState::Live), ws, AGENT).steps[0]
.wound
.wounded()
);
}
#[test]
fn files_opens_only_what_its_own_listing_named() {
let dir = world();
let ws = dir.path();
let work = ws.join("agents").join(AGENT);
std::fs::write(work.join("goal.md"), b"ship it").unwrap();
let (view, none) = files(ws, AGENT, None, None);
let crate::files_view::FilesView::Present { entries, .. } = &view else {
unreachable!("the worktree is on disk")
};
assert!(entries.iter().any(|e| e.rel_path == "goal.md"));
assert!(none.is_none(), "no path named, no bytes read");
assert_eq!(
files(ws, AGENT, Some("goal.md"), None).1,
Some(crate::files_view::Preview::Text("ship it".to_owned()))
);
assert!(files(ws, AGENT, Some("../../etc/passwd"), None).1.is_none());
assert!(files(ws, AGENT, Some("messages"), None).1.is_none());
let (absent, preview) = files(ws, "gone", Some("goal.md"), None);
assert_eq!(absent, crate::files_view::FilesView::AbsentWorktree);
assert!(preview.is_none());
}
#[test]
fn files_reads_the_commits_tree_when_one_is_named() {
let fx = crate::git_tree::tests::fixture::Fixture::new();
let conv = "20260427T120000Z-aaaa";
fx.build_agent(conv, "walk the rail");
let at = format!("agents/{conv}");
let (pinned, bytes) = files(&fx.path, conv, Some("goal.md"), Some(&at));
let crate::files_view::FilesView::Present { entries, .. } = &pinned else {
unreachable!("a real commit lists")
};
assert!(entries.iter().any(|e| e.rel_path == "goal.md"));
assert_eq!(
bytes,
Some(crate::files_view::Preview::Text("walk the rail".to_owned())),
"the bytes come out of that commit's tree"
);
assert!(
files(&fx.path, conv, Some("../etc/passwd"), Some(&at))
.1
.is_none()
);
assert_eq!(
files(&fx.path, conv, None, Some("deadbeef")).0,
crate::files_view::FilesView::AbsentWorktree
);
}
#[test]
fn the_rail_gathers_its_children_off_the_snapshot() {
let dir = world();
let ws = dir.path();
let snap = snapshot(
ws,
"alba",
vec![
agent(AGENT, AgentState::Live, 100),
agent(CHILD, AgentState::Quiescent, 90),
],
vec![],
);
let (view, tx) = (steps(&snap, ws, AGENT), transcript(&snap, ws, AGENT));
let spine = rail(&snap, ws, AGENT, &view, &tx);
assert!(spine.notches.is_empty(), "no steps on disk, no notches");
assert_eq!(
rail(&snap, Path::new("/nowhere"), AGENT, &view, &tx),
crate::rail::Rail::default()
);
}
#[test]
fn the_speaker_is_the_roots_display_name_or_the_id_itself() {
let mut root = agent(AGENT, AgentState::Live, 100);
root.name = Some("koi".to_owned());
let agents = vec![root, agent(CHILD, AgentState::Quiescent, 90)];
assert_eq!(
speaker(&agents, CHILD),
"koi",
"the child speaks as its root"
);
assert_eq!(speaker(&agents, "unheard-of"), "unheard-of");
}