use super::*;
use crate::boundary::deposit;
use crate::test_support::FakeClock;
use crate::tool_host::tests::{budget, front_door, impatient, tool};
use crate::tool_host::{Injection, loaded};
use serde_json::json;
use std::sync::atomic::AtomicBool;
use tempfile::TempDir;
use ::litany::cmd::ToolInjection as _;
macro_rules! act {
($name:expr, $workspace:expr, $input:expr, $stop:expr) => {
RoutedCall {
id: "toolu_9",
name: $name,
input: $input,
workspace: $workspace,
agent: "dulcet-mongoose",
cwd: $workspace,
stop: $stop,
}
};
}
mod set;
#[test]
fn the_pair_is_performed_at_the_engines_own_front_door() {
let root = TempDir::new().expect("tmp");
let door = front_door(
root.path(),
"sleep 0.1\nprintf '%s|%s|%s|%s|%s' \"$1\" \"$2\" \
\"$LITANY_CONV_REPO\" \"$LITANY_CONV_BRANCH\" \"$(cat)\"",
);
let input = json!({"content": "a summary"});
let stop = AtomicBool::new(false);
let capture = perform(
&door,
budget().span(),
&act!("write_summary", root.path(), &input, &stop),
);
assert_eq!(capture.exit_code, 0);
assert!(capture.stderr.is_empty());
assert_eq!(
String::from_utf8_lossy(&capture.stdout),
format!(
"tool|write_summary|{}|dulcet-mongoose|{{\"content\":\"a summary\"}}",
root.path().display()
)
);
}
#[test]
fn a_refusing_front_door_comes_back_verbatim() {
let root = TempDir::new().expect("tmp");
let door = front_door(
root.path(),
"printf 'the dispatch entry may not be nominated' >&2\nexit 7",
);
let input = json!({"path": "messages/001-user.md"});
let stop = AtomicBool::new(false);
let capture = perform(
&door,
budget().span(),
&act!("mark_for_deletion", root.path(), &input, &stop),
);
assert_eq!(capture.exit_code, 7);
assert!(capture.stdout.is_empty());
assert_eq!(capture.stderr, b"the dispatch entry may not be nominated");
}
#[test]
fn an_unspawnable_front_door_refuses_in_band() {
let root = TempDir::new().expect("tmp");
let input = json!({});
let stop = AtomicBool::new(false);
let capture = perform(
&root.path().join("no-such-front-door"),
budget().span(),
&act!("write_summary", root.path(), &input, &stop),
);
assert_eq!(capture.exit_code, 1);
let said = String::from_utf8_lossy(&capture.stderr).into_owned();
assert!(said.starts_with("write_summary: "), "{said}");
}
#[test]
fn a_working_engine_is_ended_by_the_stop_flag_and_by_the_deadline() {
let root = TempDir::new().expect("tmp");
let door = front_door(root.path(), "sleep 30");
let input = json!({});
let stopped = AtomicBool::new(true);
let capture = perform(
&door,
budget().span(),
&act!("write_summary", root.path(), &input, &stopped),
);
assert_eq!(capture.exit_code, 1);
assert!(
String::from_utf8_lossy(&capture.stderr).contains("stopped while the engine was working"),
"{:?}",
String::from_utf8_lossy(&capture.stderr)
);
let running = AtomicBool::new(false);
let capture = perform(
&door,
impatient().span(),
&act!("write_summary", root.path(), &input, &running),
);
assert_eq!(capture.exit_code, 1);
assert!(
String::from_utf8_lossy(&capture.stderr).contains("did not answer within"),
"{:?}",
String::from_utf8_lossy(&capture.stderr)
);
}
#[test]
fn the_worker_grants_perform_at_the_engines_own_front_door() {
let root = TempDir::new().expect("tmp");
let door = front_door(
root.path(),
"printf '%s|%s|%s|%s|%s' \"$1\" \"$2\" \
\"$LITANY_CONV_REPO\" \"$LITANY_CONV_BRANCH\" \"$(cat)\"",
);
let stop = AtomicBool::new(false);
for (name, input) in [
("dispatch", json!({"role": "worker", "goal": "g"})),
("message", json!({"agent": "amber", "content": "hi"})),
("load_skill", json!({"name": "review"})),
("cd", json!({"path": "sub"})),
] {
let capture = perform(
&door,
budget().span(),
&act!(name, root.path(), &input, &stop),
);
assert_eq!(capture.exit_code, 0, "{name}");
assert_eq!(
String::from_utf8_lossy(&capture.stdout),
format!(
"tool|{name}|{}|dulcet-mongoose|{}",
root.path().display(),
input
),
"{name} crosses the front door with the caller's identity"
);
}
assert!(
deposit::pending(root.path()).is_empty(),
"an engine act queues nothing at any machine's mailbox"
);
}