use super::super::CAPTURE_LIMIT;
use super::{DEADLINE, call, execute, tool};
use serde_json::json;
use std::time::Duration;
#[test]
fn a_tool_that_leaves_a_helper_holding_the_pipes_still_answers() {
let held = Duration::from_secs(30);
let deadline = Duration::from_secs(10);
assert!(held > deadline, "the helper must outlive the deadline");
let script = format!("sleep {} & printf started", held.as_secs());
let set = [tool("Daemon", &script)];
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let _ = tx.send(execute(&set, &call("Daemon", json!({})), deadline));
});
let got = rx
.recv_timeout(DEADLINE)
.expect("a capture at all: the drain never let go of the pipes");
assert_eq!(got.stdout, "started", "{got:?}");
assert_eq!(got.exit_code, 0, "{got:?}");
}
#[test]
fn output_past_this_box_s_limit_is_truncated_and_the_capture_names_the_drop() {
let over = CAPTURE_LIMIT + 4096;
let set = [tool(
"Loud",
&format!("yes hello | head -c {over}; yes oops | head -c {over} >&2"),
)];
let got = execute(&set, &call("Loud", json!({})), DEADLINE);
assert_eq!(got.exit_code, 0, "{}", got.stderr);
assert_eq!(got.stdout.len(), CAPTURE_LIMIT);
assert!(got.stderr.starts_with("oops"), "the tool's own bytes first");
assert!(
got.stderr
.contains(&format!("stdout exceeded this box's {CAPTURE_LIMIT}-byte")),
"no note for stdout"
);
assert!(
got.stderr
.contains(&format!("stderr exceeded this box's {CAPTURE_LIMIT}-byte")),
"no note for stderr"
);
assert_eq!(got.stderr.matches("4096 further bytes").count(), 2);
}