use super::*;
use crate::cli_outbound::Cli;
use crate::registry::{Grade, Peer};
use serde_json::json;
use tempfile::tempdir;
fn foot(name: &str) -> Peer {
Peer {
client: client(name),
grade: Grade::Foot,
}
}
fn quiet(root: &std::path::Path, data: &std::path::Path) -> ConsumerCtx {
over(
root,
world_of(data, &["home"]),
data.to_path_buf(),
Cli::new("/no/such/litany"),
)
}
fn tools() -> serde_json::Value {
json!([{"name": "Bash", "description": "run a command",
"input_schema": {"type": "object"}}])
}
fn refused_for_grade(reply: &serde_json::Value) -> bool {
reply["ok"] == false && reply["error"] == crate::registry::peer::REFUSAL
}
#[test]
fn a_foot_may_advertise_take_its_invocations_and_complete_one() {
let (root, data) = (tempdir().unwrap(), tempdir().unwrap());
let ctx = quiet(root.path(), data.path());
let host = foot("host");
crate::registry::register(root.path(), &host.client, "home").expect("its enrolment's file");
let advertised = ctx.answer_as(&host, &json!({"op": "advertise", "tools": tools()}));
assert_eq!(advertised["kind"], "advertised", "{advertised}");
assert_eq!(
crate::registry::tools::read(root.path(), &host.client)[0].name,
"Bash"
);
let queue = ctx.answer_as(&host, &json!({"op": "invocations"}));
assert_eq!(queue["kind"], "invocations", "{queue}");
let done = ctx.answer_as(
&host,
&json!({"op": "complete", "invocation": "nobody-minted-this",
"capture": {"stdout": "", "stderr": "", "exit_code": 0}}),
);
assert!(!refused_for_grade(&done), "{done}");
assert!(
done["error"]
.as_str()
.unwrap_or_default()
.contains("nobody-minted-this"),
"the mailbox answered, not the grade: {done}"
);
}
#[test]
fn a_foot_may_not_ask_about_the_world_act_on_it_or_invoke() {
let (root, data) = (tempdir().unwrap(), tempdir().unwrap());
let ctx = quiet(root.path(), data.path());
let host = foot("host");
crate::registry::register(root.path(), &host.client, "home").unwrap();
for request in [
json!({"op": "workspaces"}),
json!({"op": "conversations", "workspace": "home"}),
json!({"op": "clients", "workspace": "home"}),
json!({"op": "capture", "invocation": "x"}),
json!({"op": "invoke", "client": "host", "tool": "Bash", "input": {}}),
] {
let refusal = ctx.answer_as(&host, &request);
assert!(refused_for_grade(&refusal), "{request} -> {refusal}");
}
}
#[test]
fn a_foot_gets_no_stream_and_the_grades_sentence_instead() {
let (root, data) = (tempdir().unwrap(), tempdir().unwrap());
let ctx = quiet(root.path(), data.path());
let host = foot("host");
crate::registry::register(root.path(), &host.client, "home").unwrap();
for request in [
json!({"op": "follow", "workspace": "home", "agent": "c-1"}),
json!({"op": "attention"}),
] {
assert!(ctx.follow(&host, &request).is_none(), "{request}");
assert!(
refused_for_grade(&ctx.answer_as(&host, &request)),
"refused"
);
}
}
#[test]
fn an_operator_grade_peer_is_untouched_by_the_raise() {
let (root, data) = (tempdir().unwrap(), tempdir().unwrap());
let ctx = quiet(root.path(), data.path());
let desk = seat("host");
crate::registry::register(root.path(), &desk.client, "home").expect("its enrolment's file");
assert_eq!(desk.grade, Grade::default(), "unstated is operator");
let listed = ctx.answer_as(&desk, &json!({"op": "workspaces"}));
assert_eq!(listed["kind"], "workspaces", "{listed}");
let advertised = ctx.answer_as(&desk, &json!({"op": "advertise", "tools": tools()}));
assert_eq!(advertised["kind"], "advertised", "{advertised}");
}
#[test]
fn an_undecodable_envelope_is_still_the_codecs_refusal() {
let (root, data) = (tempdir().unwrap(), tempdir().unwrap());
let ctx = quiet(root.path(), data.path());
let refusal = ctx.answer_as(&foot("host"), &json!({"op": "teleport"}));
assert_eq!(refusal["ok"], false);
assert!(
refusal["error"]
.as_str()
.unwrap_or_default()
.contains("teleport"),
"{refusal}"
);
}