use super::super::hold::hold;
use super::{advertised, aside, echo, gesture, ops, receipt, refusal, row, said, set, wired, work};
use serde_json::json;
#[test]
fn the_loop_presents_waits_hands_off_and_answers() {
let (_scratch, engine, channel) = wired(vec![
advertised(),
work(vec![row("i-1", "Bash")]),
receipt("i-1"),
advertised(),
refusal("the engine is going down"),
]);
assert_eq!(
said(hold(&channel, &set(), echo, &aside(), None)),
"the engine is going down",
"the channel ends with the sentence that ended it"
);
assert_eq!(
ops(&engine),
[
"advertise",
"invocations",
"complete",
"advertise",
"invocations"
]
);
assert_eq!(
gesture(&engine, 0)["tools"],
json!([{"name": "Bash", "description": "run a command in a shell",
"input_schema": {"type": "object"}}])
);
assert_eq!(
gesture(&engine, 2),
json!({"op": "complete", "invocation": "i-1",
"capture": {"stdout": "Bash", "stderr": "1 tools", "exit_code": 0}})
);
}
#[test]
fn a_refused_advertisement_ends_the_channel_before_any_read() {
let (_scratch, engine, channel) = wired(vec![refusal("this leaf is not registered here")]);
assert_eq!(
said(hold(&channel, &set(), echo, &aside(), None)),
"this leaf is not registered here"
);
assert_eq!(ops(&engine), ["advertise"]);
}
#[test]
fn an_empty_answer_is_waited_through_and_not_an_ending() {
let (_scratch, engine, channel) = wired(vec![
advertised(),
work(vec![]),
work(vec![]),
refusal("the engine is going down"),
]);
assert_eq!(
said(hold(&channel, &set(), echo, &aside(), None)),
"the engine is going down"
);
assert_eq!(
ops(&engine),
["advertise", "invocations", "invocations", "invocations"]
);
}
#[test]
fn work_in_one_answer_is_run_one_at_a_time_in_order() {
let (_scratch, engine, channel) = wired(vec![
advertised(),
work(vec![row("i-1", "Bash"), row("i-2", "Bash")]),
receipt("i-1"),
advertised(),
receipt("i-2"),
advertised(),
refusal("stop"),
]);
assert_eq!(said(hold(&channel, &set(), echo, &aside(), None)), "stop");
assert_eq!(
ops(&engine),
[
"advertise",
"invocations",
"complete",
"advertise",
"complete",
"advertise",
"invocations"
]
);
assert_eq!(gesture(&engine, 2)["invocation"], json!("i-1"));
assert_eq!(gesture(&engine, 4)["invocation"], json!("i-2"));
}
#[test]
fn the_set_is_asserted_again_at_the_end_of_every_hand_off() {
let (_scratch, engine, channel) = wired(vec![
advertised(),
work(vec![row("i-1", "Bash")]),
receipt("i-1"),
advertised(),
refusal("stop"),
]);
assert_eq!(said(hold(&channel, &set(), echo, &aside(), None)), "stop");
assert_eq!(
gesture(&engine, 3),
gesture(&engine, 0),
"the re-assertion is the presentation, not a second projection"
);
}
#[test]
fn a_refused_re_assertion_ends_the_channel() {
let refused = "another connection is holding this engine's follow-class read";
let (_scratch, engine, channel) = wired(vec![
advertised(),
work(vec![row("i-1", "Bash")]),
receipt("i-1"),
refusal(refused),
]);
assert_eq!(said(hold(&channel, &set(), echo, &aside(), None)), refused);
assert_eq!(
ops(&engine),
["advertise", "invocations", "complete", "advertise"],
"it stops at the refusal rather than reading for more work"
);
}
#[test]
fn a_refused_completion_ends_the_channel() {
let (_scratch, _engine, channel) = wired(vec![
advertised(),
work(vec![row("i-1", "Bash")]),
refusal("no invocation \"i-1\" is in flight"),
]);
assert_eq!(
said(hold(&channel, &set(), echo, &aside(), None)),
"no invocation \"i-1\" is in flight"
);
}
#[test]
fn an_answer_that_is_not_this_machine_s_work_ends_the_channel() {
let (_scratch, _engine, channel) = wired(vec![advertised(), advertised()]);
let said = said(hold(&channel, &set(), echo, &aside(), None));
assert!(said.contains("not this machine's work"), "{said}");
assert!(said.contains("Advertised"), "{said}");
}
#[test]
fn an_answer_no_foot_gesture_can_earn_ends_the_channel() {
let (_scratch, _engine, channel) =
wired(vec![json!({"ok": true, "kind": "board", "rows": []})]);
assert_eq!(
said(hold(&channel, &set(), echo, &aside(), None)),
"reply: unusable kind \"board\""
);
}