mod stand_in;
pub(super) use stand_in::{budget, engine, front_door, impatient, scripted, site, tool};
use super::*;
use crate::boundary::deposit;
use crate::registry::tools::Tool;
use crate::test_support::FakeClock;
use serde_json::{Value, json};
use std::sync::atomic::AtomicBool;
use std::sync::mpsc::Receiver;
use std::thread::JoinHandle;
use std::time::Duration;
use tempfile::TempDir;
fn injection(root: &Path) -> Injection {
at(root, root.join("no-such-front-door"), impatient())
}
fn at(root: &Path, driver_target: PathBuf, budget: ask::Budget) -> Injection {
Injection::new(
root.to_path_buf(),
driver_target,
budget,
budget,
FakeClock::new().arc(),
)
}
macro_rules! call {
($name:expr, $input:expr, $stop:expr) => {
call!($name, $input, $stop, Path::new("/w/home"))
};
($name:expr, $input:expr, $stop:expr, $workspace:expr) => {
RoutedCall {
id: "toolu_1",
name: $name,
input: $input,
workspace: $workspace,
agent: "dulcet-mongoose",
cwd: $workspace,
stop: $stop,
}
};
}
struct At(i64);
impl Clock for At {
fn now(&self) -> std::time::Instant {
std::time::Instant::now()
}
fn stamp(&self) -> String {
self.0.to_string()
}
}
#[test]
fn an_answer_is_dated_at_the_instant_it_was_read() {
let root = TempDir::new().expect("tmp");
assert_eq!(
site(root.path(), impatient()).observed(),
"1970-01-01 00:00:00Z"
);
let mut s = site(root.path(), impatient());
s.clock = Arc::new(At(1_785_630_266));
assert_eq!(s.observed(), "2026-08-02 00:24:26Z");
}
#[test]
fn the_clients_tool_is_declared_on_every_request() {
let root = TempDir::new().expect("tmp");
let declared = injection(root.path()).tools(Path::new("/w/home"), "dulcet-mongoose");
assert_eq!(declared.len(), 1);
assert_eq!(declared[0].name, clients::NAME);
assert_eq!(
declared[0].description.as_deref(),
Some(clients::DESCRIPTION)
);
assert_eq!(declared[0].input_schema, clients::schema());
}
#[test]
fn a_loaded_tool_is_declared_under_its_own_name() {
let root = TempDir::new().expect("tmp");
loaded::add(
root.path(),
"home",
"dulcet-mongoose",
&[loaded::Entry {
client: "laptop".to_owned(),
tool: tool("Bash"),
}],
)
.expect("loaded");
let declared = injection(root.path()).tools(Path::new("/w/home"), "dulcet-mongoose");
assert_eq!(
declared.iter().map(|t| t.name.clone()).collect::<Vec<_>>(),
vec![clients::NAME.to_owned(), "laptop_Bash".to_owned()]
);
assert_eq!(declared[1].input_schema, tool("Bash").input_schema);
assert_eq!(
declared[1].description.as_deref(),
Some("what Bash does"),
"the description is the frozen one"
);
}
#[test]
fn nothing_loaded_refuses_an_ordinary_tool_in_band_and_still_compacts() {
let root = TempDir::new().expect("tmp");
let door = front_door(root.path(), "printf compacted");
let input = json!({});
let stop = AtomicBool::new(false);
let live = at(root.path(), door, budget());
let (engine, _seen) = scripted(
root.path(),
&[json!({"ok": true, "kind": "clients", "rows": []})],
);
let refused = live.route(call!("Read", &input, &stop));
engine.join().expect("engine");
assert_eq!(refused.exit_code, 1);
assert!(refused.stdout.is_empty());
let said = String::from_utf8_lossy(&refused.stderr).into_owned();
assert!(
said.starts_with("Read: no tool of that name is loaded"),
"{said}"
);
assert!(
said.contains("no machine of this workspace advertises"),
"{said}"
);
assert!(
said.contains("clients tool is not a way to do this work"),
"{said}"
);
for name in engine_act::NAMES {
let acted = live.route(call!(name, &input, &stop, root.path()));
assert_eq!(
acted.exit_code, 0,
"{name} is an engine act, not a tool call"
);
assert_eq!(acted.stdout, b"compacted");
}
}
#[test]
fn a_clients_op_answers_as_a_zero_exit_capture() {
let root = TempDir::new().expect("tmp");
let (handle, seen) = engine(
root.path(),
&json!({"ok": true, "kind": "clients", "rows": []}),
);
let input = json!({"op": "list"});
let stop = AtomicBool::new(false);
let capture =
at(root.path(), PathBuf::new(), budget()).route(call!(clients::NAME, &input, &stop));
handle.join().expect("engine");
assert_eq!(capture.exit_code, 0);
assert!(capture.stderr.is_empty());
assert!(
String::from_utf8_lossy(&capture.stdout).contains("workspace \"home\""),
"the workspace comes off the call, not the process"
);
assert_eq!(
seen.recv().expect("a request"),
json!({"op": "clients", "workspace": "home"}),
"the roster read is Query::Clients — no new verb"
);
}
#[test]
fn a_refusal_is_an_in_band_non_zero_capture() {
let root = TempDir::new().expect("tmp");
let input = json!({"op": "nope"});
let stop = AtomicBool::new(false);
let capture = injection(root.path()).route(call!(clients::NAME, &input, &stop));
assert_eq!(capture.exit_code, 1);
assert!(capture.stdout.is_empty());
let said = String::from_utf8_lossy(&capture.stderr).into_owned();
assert!(said.starts_with("clients: "), "{said}");
assert!(said.contains("unknown op"), "{said}");
}
mod routing;