use super::*;
fn test_config() -> MultiAgentConfig {
MultiAgentConfig::enabled()
}
fn test_path(name: &str) -> AgentPath {
AgentPath::root().join(name)
}
fn registered(config: MultiAgentConfig, name: &str) -> (AgentRegistry, AgentPath) {
let mut reg = AgentRegistry::new(config);
let path = test_path(name);
reg.register(&path, 1).unwrap();
(reg, path)
}
#[test]
fn derive_status_is_a_pure_function() {
use AgentStatus::*;
use ResultKind as R;
let failed = R::Failed("boom".to_string());
assert_eq!(derive_status(false, true, 3, None), Closed);
assert_eq!(derive_status(false, false, 0, Some(&failed)), Closed);
assert_eq!(derive_status(true, true, 0, Some(&failed)), Running);
assert_eq!(derive_status(true, true, 5, None), Running);
assert_eq!(derive_status(true, false, 0, None), Done);
assert_eq!(derive_status(true, false, 0, Some(&R::Success)), Done);
assert_eq!(derive_status(true, false, 0, Some(&failed)), Failed);
assert_eq!(
derive_status(true, false, 0, Some(&R::Cancelled)),
Cancelled
);
assert_eq!(derive_status(true, false, 1, None), Queued);
assert_eq!(derive_status(true, false, 9, Some(&failed)), Queued);
}
#[test]
fn status_names_are_wire_stable() {
assert_eq!(AgentStatus::Queued.name(), "queued");
assert_eq!(AgentStatus::Running.name(), "running");
assert_eq!(AgentStatus::Done.name(), "done");
assert_eq!(AgentStatus::Failed.name(), "failed");
assert_eq!(AgentStatus::Cancelled.name(), "cancelled");
assert_eq!(AgentStatus::Closed.name(), "closed");
assert_eq!(AgentStatus::Paused.name(), "paused");
assert_eq!(AgentStatus::Unknown.name(), "unknown");
}
#[test]
fn name_from_name_round_trips() {
for status in [
AgentStatus::Queued,
AgentStatus::Running,
AgentStatus::Done,
AgentStatus::Failed,
AgentStatus::Cancelled,
AgentStatus::Closed,
AgentStatus::Paused,
AgentStatus::Unknown,
] {
assert_eq!(AgentStatus::from_name(status.name()), status);
}
assert_eq!(AgentStatus::from_name("bogus"), AgentStatus::Unknown);
assert_eq!(AgentStatus::from_name(""), AgentStatus::Unknown);
}
#[test]
fn register_and_close() {
let mut reg = AgentRegistry::new(test_config());
let path = test_path("worker");
assert!(reg.register(&path, 1).is_ok());
assert_eq!(reg.count(), 1);
assert!(reg.contains(&path));
let entry = reg.get(&path).unwrap();
assert_eq!(entry.status(), AgentStatus::Done);
assert_eq!(entry.depth, 1);
assert_eq!(entry.queue_len, 0);
assert!(!entry.in_flight);
assert_eq!(entry.results_posted, 0);
assert_eq!(entry.tool_calls, 0);
assert!(entry.last_activity.is_none());
let closed = reg.close(&path).unwrap();
assert_eq!(closed.path, path);
assert_eq!(reg.count(), 0);
assert!(!reg.contains(&path));
}
#[test]
fn duplicate_register_fails() {
let mut reg = AgentRegistry::new(test_config());
let path = test_path("worker");
assert!(reg.register(&path, 1).is_ok());
assert_eq!(
reg.register(&path, 1).unwrap_err(),
SpawnError::AlreadyExists
);
}
#[test]
fn max_agents_limit() {
let config = MultiAgentConfig::with_limits(2, 1);
let mut reg = AgentRegistry::new(config);
assert!(reg.register(&test_path("a"), 1).is_ok());
assert!(reg.register(&test_path("b"), 1).is_ok());
assert_eq!(
reg.register(&test_path("c"), 1).unwrap_err(),
SpawnError::MaxAgentsReached { max: 2 }
);
}
#[test]
fn depth_limit() {
let config = MultiAgentConfig::with_limits(8, 1);
let reg = AgentRegistry::new(config);
assert!(reg.can_spawn(1).is_ok());
assert_eq!(
reg.can_spawn(2).unwrap_err(),
SpawnError::DepthLimitReached {
max: 1,
attempted: 2
}
);
}
#[test]
fn facts_drive_the_whole_lifecycle() {
let (mut reg, path) = registered(test_config(), "worker");
assert_eq!(reg.get(&path).unwrap().status(), AgentStatus::Done);
assert!(reg.note_enqueued(&path));
assert_eq!(reg.get(&path).unwrap().status(), AgentStatus::Queued);
assert!(reg.note_enqueued(&path));
assert_eq!(reg.get(&path).unwrap().status(), AgentStatus::Queued);
assert!(reg.note_dequeued(&path));
assert_eq!(reg.get(&path).unwrap().status(), AgentStatus::Running);
assert!(reg.get(&path).unwrap().running_since.is_some());
assert_eq!(reg.get(&path).unwrap().queue_len, 1);
assert!(reg.note_posted(&path, ResultKind::Success));
assert_eq!(reg.get(&path).unwrap().status(), AgentStatus::Queued);
assert!(reg.get(&path).unwrap().running_since.is_none());
assert_eq!(reg.get(&path).unwrap().results_posted, 1);
assert!(reg.note_dequeued(&path));
assert_eq!(reg.get(&path).unwrap().status(), AgentStatus::Running);
assert!(reg.note_posted(&path, ResultKind::Success));
assert_eq!(reg.get(&path).unwrap().status(), AgentStatus::Done);
assert_eq!(reg.get(&path).unwrap().results_posted, 2);
assert_eq!(reg.count(), 1);
}
#[test]
fn failed_result_derives_failed_until_the_next_result() {
let (mut reg, path) = registered(test_config(), "worker");
reg.note_enqueued(&path);
reg.note_dequeued(&path);
assert!(reg.note_posted(&path, ResultKind::Failed("exit 1".into())));
assert_eq!(reg.get(&path).unwrap().status(), AgentStatus::Failed);
let row = ®.snapshot().agents[0];
assert_eq!(row.status, "failed");
assert_eq!(row.last_error.as_deref(), Some("exit 1"));
reg.note_enqueued(&path);
assert_eq!(reg.get(&path).unwrap().status(), AgentStatus::Queued);
reg.note_dequeued(&path);
assert_eq!(reg.get(&path).unwrap().status(), AgentStatus::Running);
assert!(reg.note_posted(&path, ResultKind::Success));
assert_eq!(reg.get(&path).unwrap().status(), AgentStatus::Done);
let row = ®.snapshot().agents[0];
assert_eq!(row.status, "done");
assert!(row.last_error.is_none(), "a later success clears the error");
}
#[test]
fn cancelled_result_derives_cancelled() {
let (mut reg, path) = registered(test_config(), "worker");
reg.note_enqueued(&path);
reg.note_dequeued(&path);
assert!(reg.note_posted(&path, ResultKind::Cancelled));
assert_eq!(reg.get(&path).unwrap().status(), AgentStatus::Cancelled);
assert_eq!(reg.snapshot().agents[0].status, "cancelled");
reg.note_enqueued(&path);
reg.note_dequeued(&path);
assert!(reg.note_posted(&path, ResultKind::Success));
assert_eq!(reg.get(&path).unwrap().status(), AgentStatus::Done);
}
#[test]
fn close_frees_quota() {
let config = MultiAgentConfig::with_limits(1, 1);
let mut reg = AgentRegistry::new(config);
let path = test_path("worker");
reg.register(&path, 1).unwrap();
assert_eq!(reg.count(), 1);
assert!(reg.can_spawn(1).is_err());
reg.close(&path);
assert_eq!(reg.count(), 0);
assert!(reg.can_spawn(1).is_ok());
}
#[test]
fn list_sorted() {
let mut reg = AgentRegistry::new(test_config());
reg.register(&test_path("b"), 1).unwrap();
reg.register(&test_path("a"), 1).unwrap();
let list = reg.list();
assert_eq!(list.len(), 2);
assert_eq!(list[0].path.name(), "a");
assert_eq!(list[1].path.name(), "b");
}
#[test]
fn tool_call_counter_is_monotonic() {
let (mut reg, path) = registered(test_config(), "worker");
for _ in 0..3 {
assert!(reg.record_tool_call(&path));
}
assert_eq!(reg.get(&path).unwrap().tool_calls, 3);
assert!(reg.get(&path).unwrap().last_activity.is_some());
assert!(reg.record_tool_call(&path));
assert_eq!(reg.get(&path).unwrap().tool_calls, 4);
assert!(!reg.record_tool_call(&test_path("ghost")));
}
#[test]
fn touch_marks_activity_without_counting() {
let (mut reg, path) = registered(test_config(), "worker");
assert!(reg.touch(&path));
assert!(reg.get(&path).unwrap().last_activity.is_some());
assert_eq!(reg.get(&path).unwrap().tool_calls, 0);
assert!(!reg.touch(&test_path("ghost")));
}
#[test]
fn busy_count_tracks_outstanding_work() {
let (mut reg, a) = registered(test_config(), "a");
let b = test_path("b");
reg.register(&b, 1).unwrap();
assert_eq!(reg.busy_count(), 0);
assert!(reg.note_enqueued(&a));
assert!(reg.note_dequeued(&a));
assert!(reg.note_enqueued(&b));
assert_eq!(reg.busy_count(), 2);
assert!(reg.note_posted(&a, ResultKind::Success));
assert_eq!(reg.busy_count(), 1);
assert!(reg.note_dequeued(&b));
assert!(reg.note_posted(&b, ResultKind::Success));
assert_eq!(reg.busy_count(), 0);
}
#[test]
fn quiescence_requires_delivery_from_every_registered_agent() {
let (mut reg, a) = registered(test_config(), "a");
let b = test_path("b");
reg.register(&b, 1).unwrap();
assert!(reg.note_enqueued(&a));
assert!(reg.note_dequeued(&a));
assert!(reg.note_posted(&a, ResultKind::Success));
assert!(!reg.quiescent(), "b never delivered — spawn→send window");
assert!(reg.note_enqueued(&b));
assert!(reg.note_dequeued(&b));
assert!(!reg.quiescent(), "b is in flight");
assert!(reg.note_posted(&b, ResultKind::Success));
assert!(reg.quiescent(), "everyone idle and delivered");
assert!(reg.note_enqueued(&b));
assert!(!reg.quiescent(), "queued work blocks the batch");
}
#[test]
fn a_killed_child_settles_quiescence() {
let (mut reg, a) = registered(test_config(), "a");
let b = test_path("b");
reg.register(&b, 1).unwrap();
assert!(reg.note_enqueued(&a));
assert!(reg.note_dequeued(&a));
assert!(reg.note_posted(&a, ResultKind::Success));
assert!(reg.note_enqueued(&b));
assert!(reg.note_dequeued(&b));
assert!(!reg.quiescent(), "b is in flight");
assert!(reg.note_closing(&b));
assert!(
reg.quiescent(),
"a closing child counts as settled — its cancel is the terminal fact"
);
}
#[test]
fn batch_handover_closes_and_reopens_the_delivery_gap() {
let (mut reg, path) = registered(test_config(), "worker");
reg.note_enqueued(&path);
reg.note_dequeued(&path);
reg.note_posted(&path, ResultKind::Success);
let snap = |reg: &AgentRegistry| reg.snapshot().agents[0].pending_results;
assert_eq!(snap(®), 1, "posted but no batch has fired yet");
assert!(reg.note_batch_handed_over(&path));
assert_eq!(snap(®), 0, "handed over — no longer pending");
reg.note_enqueued(&path);
reg.note_dequeued(&path);
reg.note_posted(&path, ResultKind::Success);
assert_eq!(snap(®), 1, "new post is pending until the next batch");
assert!(!reg.note_batch_handed_over(&test_path("ghost")));
assert_eq!(reg.get(&path).unwrap().results_handed_over, 1);
}
#[test]
fn note_on_unknown_path_is_a_noop() {
let mut reg = AgentRegistry::new(test_config());
assert!(!reg.note_enqueued(&test_path("ghost")));
assert!(!reg.note_dequeued(&test_path("ghost")));
assert!(!reg.note_posted(&test_path("ghost"), ResultKind::Success));
assert!(!reg.note_send_failed(&test_path("ghost")));
assert!(reg.recent_events(10).is_empty());
assert!(reg.latest_snapshot().agents.is_empty());
}
#[test]
fn send_failure_rolls_back_the_enqueue_fact() {
let (mut reg, path) = registered(test_config(), "worker");
assert!(reg.note_enqueued(&path));
assert_eq!(reg.get(&path).unwrap().queue_len, 1);
assert!(reg.note_send_failed(&path));
assert_eq!(reg.get(&path).unwrap().queue_len, 0);
assert_eq!(reg.get(&path).unwrap().status(), AgentStatus::Done);
assert!(!reg.quiescent());
assert!(reg.note_send_failed(&path));
assert_eq!(reg.get(&path).unwrap().queue_len, 0);
let (mut reg2, p2) = registered(test_config(), "solo");
reg2.note_enqueued(&p2);
reg2.note_dequeued(&p2);
reg2.note_posted(&p2, ResultKind::Success);
reg2.note_enqueued(&p2);
assert!(!reg2.quiescent());
reg2.note_send_failed(&p2);
assert!(
reg2.quiescent(),
"rolled-back phantom queue must not wedge the batch"
);
}
#[test]
fn disabled_config_bypasses_spawn_limits() {
let config = MultiAgentConfig {
enabled: false,
..MultiAgentConfig::default()
};
let reg = AgentRegistry::new(config);
assert!(reg.can_spawn(999).is_ok());
}
#[tokio::test]
async fn running_secs_and_watch_receiver_track_running_state() {
let (mut reg, path) = registered(test_config(), "worker");
let mut rx = reg.subscribe();
assert!(
reg.snapshot().agents[0].running_secs.is_none(),
"not running yet"
);
reg.note_enqueued(&path);
reg.note_dequeued(&path);
rx.changed()
.await
.expect("watch receiver must be woken by the transition");
assert_eq!(rx.borrow().agents[0].running_secs, Some(0));
reg.note_posted(&path, ResultKind::Success);
assert!(
reg.snapshot().agents[0].running_secs.is_none(),
"stale seconds must clear on post"
);
}
#[test]
fn events_and_snapshot_track_transitions() {
let (mut reg, path) = registered(test_config(), "worker");
assert!(reg.note_enqueued(&path));
assert!(reg.note_dequeued(&path));
assert!(reg.note_posted(&path, ResultKind::Success));
let events = reg.recent_events(10);
assert_eq!(events.len(), 3);
assert_eq!(events[0].reason, "task_enqueued");
assert_eq!(events[0].from, AgentStatus::Done);
assert_eq!(events[0].to, AgentStatus::Queued);
assert_eq!(events[1].reason, "task_dequeued");
assert_eq!(events[1].to, AgentStatus::Running);
assert_eq!(events[2].reason, "result_posted");
assert_eq!(events[2].to, AgentStatus::Done);
reg.close(&path);
let events = reg.recent_events(10);
assert_eq!(events.last().unwrap().reason, "unregistered");
assert_eq!(events.last().unwrap().to, AgentStatus::Closed);
assert!(reg.snapshot().agents.is_empty());
}
#[test]
fn event_ring_is_bounded() {
let (mut reg, path) = registered(test_config(), "worker");
for _ in 0..(EVENT_RING_CAP + 100) {
reg.note_enqueued(&path);
reg.note_dequeued(&path);
reg.note_posted(&path, ResultKind::Success);
}
assert_eq!(reg.events.len(), EVENT_RING_CAP);
assert_eq!(reg.recent_events(3).len(), 3);
}
#[test]
fn snapshot_reflects_derived_state() {
let (mut reg, path) = registered(test_config(), "worker");
reg.note_enqueued(&path);
reg.note_dequeued(&path);
reg.set_task(&path, "do the thing".to_string());
reg.record_tool_call(&path);
let snap = reg.snapshot();
assert_eq!(snap.agents.len(), 1);
let row = &snap.agents[0];
assert_eq!(row.path, "root/worker");
assert_eq!(row.status, "running");
assert!(row.running_secs.is_some());
assert_eq!(row.tool_calls, 1);
assert_eq!(row.task.as_deref(), Some("do the thing"));
let latest = reg.latest_snapshot();
assert_eq!(latest.agents[0].status, "running");
}
#[test]
fn spawn_error_display() {
assert_eq!(
SpawnError::MaxAgentsReached { max: 8 }.to_string(),
"max agents reached (limit: 8)"
);
assert_eq!(
SpawnError::DepthLimitReached {
max: 1,
attempted: 2
}
.to_string(),
"agent depth limit reached (max: 1, attempted: 2)"
);
assert_eq!(
SpawnError::AlreadyExists.to_string(),
"agent with this path already exists"
);
}