use std::path::PathBuf;
use std::process::Command;
fn script_path() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.parent()
.unwrap()
.join("scripts/telemetry_demo.sh")
}
fn run_mode(mode: &str) -> std::process::Output {
Command::new("bash")
.arg(script_path())
.arg(mode)
.output()
.unwrap_or_else(|e| panic!("spawning script in mode {mode:?} failed: {e}"))
}
fn command_is_available(name: &str) -> bool {
Command::new(name)
.arg("--version")
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
fn line_uses_eval(raw_line: &str) -> bool {
let code = raw_line.split('#').next().unwrap_or("");
code.split_whitespace().any(|tok| tok == "eval")
}
#[test]
fn script_modes_exit_zero_with_output() {
let has_jq = command_is_available("jq");
for mode in ["full", "subjects", "redaction"] {
let out = run_mode(mode);
assert!(
out.status.success(),
"mode {mode:?} exit={}; stderr={}",
out.status,
String::from_utf8_lossy(&out.stderr)
);
assert!(
!out.stdout.is_empty(),
"mode {mode:?} produced empty stdout"
);
}
if has_jq {
for mode in ["types", "agent-only"] {
let out = run_mode(mode);
assert!(
out.status.success(),
"mode {mode:?} exit={}; stderr={}",
out.status,
String::from_utf8_lossy(&out.stderr)
);
assert!(
!out.stdout.is_empty(),
"mode {mode:?} produced empty stdout"
);
}
}
}
#[test]
fn script_rejects_unknown_mode() {
let out = run_mode("not-a-real-mode");
assert_eq!(
out.status.code(),
Some(2),
"unknown mode should exit 2; status={:?}, stdout={}, stderr={}",
out.status.code(),
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
);
}
#[test]
fn script_does_not_use_eval() {
let body = std::fs::read_to_string(script_path()).expect("reading demo script");
for (lineno, line) in body.lines().enumerate() {
if line.trim_start().starts_with('#') {
continue;
}
assert!(
!line_uses_eval(line),
"line {}: unexpected `eval` usage: {line}",
lineno + 1
);
}
}
#[test]
fn command_is_available_checks_exit_status() {
assert!(
!command_is_available("false"),
"a command that exits non-zero must NOT be reported as available"
);
assert!(
command_is_available("true"),
"a command that exits zero must be reported as available"
);
assert!(
!command_is_available("nsed-definitely-not-a-real-binary-9d3b1c2e"),
"a missing binary must be reported as unavailable"
);
}
#[test]
fn line_uses_eval_token_classification() {
for line in [
" eval \"$RUN\"", "\teval\t\"$RUN\"", "if true; then eval foo; fi", " eval $cmd && echo done", ] {
assert!(
line_uses_eval(line),
"expected eval-detector to flag: {line:?}"
);
}
for line in [
"evaluate the result", "eval_loop=foo", "echo done # eval here", "echo \"eval\"", ] {
assert!(
!line_uses_eval(line),
"eval-detector should not flag: {line:?}"
);
}
}