use super::*;
fn bash_use(id: &str, cmd: &str) -> String {
format!(
"{{\"type\":\"assistant\",\"message\":{{\"role\":\"assistant\",\"content\":[{{\"type\":\"tool_use\",\"id\":\"{id}\",\"name\":\"Bash\",\"input\":{{\"command\":\"{cmd}\"}}}}]}}}}"
)
}
fn tool_use(id: &str, name: &str, path: &str) -> String {
format!(
"{{\"type\":\"assistant\",\"message\":{{\"role\":\"assistant\",\"content\":[{{\"type\":\"tool_use\",\"id\":\"{id}\",\"name\":\"{name}\",\"input\":{{\"file_path\":\"{path}\"}}}}]}}}}"
)
}
fn result(id: &str, err: bool) -> String {
format!(
"{{\"type\":\"user\",\"toolUseResult\":\"x\",\"message\":{{\"role\":\"user\",\"content\":[{{\"type\":\"tool_result\",\"tool_use_id\":\"{id}\",\"content\":\"Exit code 1\",\"is_error\":{err}}}]}}}}"
)
}
fn user(text: &str) -> String {
format!("{{\"type\":\"user\",\"message\":{{\"role\":\"user\",\"content\":\"{text}\"}}}}")
}
fn said(text: &str) -> String {
format!("{{\"type\":\"assistant\",\"message\":{{\"role\":\"assistant\",\"content\":[{{\"type\":\"text\",\"text\":\"{text}\"}}]}}}}")
}
fn events(lines: &[String]) -> Vec<mine::Ev> {
mine::gate_events(&lines.join("\n"))
}
#[test]
fn reader_keeps_tool_results_and_flags() {
let evs = events(&[bash_use("a", "ls /nope"), result("a", true)]);
assert_eq!(evs.len(), 2);
assert!(evs[1].err);
}
#[test]
fn normalize_strips_wrappers() {
let want = "cargo test --lib";
for c in [
"cargo test --lib",
"timeout 30 cargo test --lib",
"timeout --foreground 120 cargo test --lib",
"timeout -s KILL 5m cargo test --lib",
"timeout --foreground -s TERM -k 5 90 cargo test --lib",
"sudo cargo test --lib",
"sudo -n env FOO=1 BAR=2 cargo test --lib",
"FOO=1 cargo test --lib",
"cd /tmp/x && cargo test --lib",
"cd /tmp/x && FOO=1 timeout 30 sudo cargo test --lib 2>&1 | tail -20",
"cargo test --lib 2>&1",
"cargo test --lib | tail -n 5",
"cargo test --lib 2>&1 | head -40",
" cargo test\t--lib ",
] {
assert_eq!(normalize(c), want, "{c}");
}
}
#[test]
fn normalize_keeps_what_matters() {
assert_ne!(normalize("cargo test --lib"), normalize("cargo test --bin x"));
assert_eq!(normalize("ls | wc -l"), "ls | wc -l");
assert_eq!(normalize("a | grep x | tail -3"), "a | grep x");
assert_eq!(normalize("timeout 3"), "timeout 3");
assert_eq!(normalize("echo timeout 30 x"), "echo timeout 30 x");
}
#[test]
fn normalize_matches_the_shared_parity_table() {
let table = include_str!("../../bench/premortem-gates/normalize.tsv");
for line in table.lines().filter(|l| !l.is_empty() && !l.starts_with('#')) {
let (a, b) = line.split_once('\t').unwrap();
assert_eq!(normalize(a), b, "{a}");
}
}
#[test]
fn claims() {
for t in [
"All done.",
"I fixed it. Works now.",
"That should work.",
"It is ready and shipped.",
"Everything is all set!",
] {
assert!(claims_done(t), "{t}");
}
for t in [
"It is not done yet.",
"This isn't fixed until the build passes.",
"Is it ready?",
"I read the file and found the bug.",
"The unfixed part remains.",
] {
assert!(!claims_done(t), "{t}");
}
let long = format!("Done. {}", "x ".repeat(600));
assert!(!claims_done(&long));
}
#[test]
fn read_only_commands_do_not_verify() {
for c in ["cat a.rs", "ls -la", "grep -n x src", "sed -n 1,5p a", "git diff", "timeout 5 cat a"] {
assert!(read_only(c), "{c}");
}
for c in ["cargo test", "curl localhost:8080", "node run.mjs", "bash check.sh", "sed -i s/a/b/ f", "git commit -m x"] {
assert!(!read_only(c), "{c}");
}
}
const CORRECTION: &str = "No, that is not what I asked, it still does not work";
#[test]
fn done_gate_fires_after_correction_without_a_run() {
let evs = events(&[
user("fix the login page please"),
tool_use("e1", "Edit", "a.rs"),
said("Fixed."),
user(CORRECTION),
tool_use("e2", "Edit", "a.rs"),
said("All done, it works now."),
]);
let f = run_gate(&evs, None);
assert_eq!(f.len(), 1);
assert_eq!(f[0].0, 6);
}
#[test]
fn done_gate_is_quiet_without_a_correction_or_after_a_run() {
let evs = events(&[user("fix it"), tool_use("e1", "Edit", "a.rs"), said("Done.")]);
assert!(run_gate(&evs, None).is_empty());
let evs = events(&[
user(CORRECTION),
tool_use("e1", "Edit", "a.rs"),
bash_use("b1", "cargo test"),
result("b1", false),
said("Done, tests pass."),
]);
assert!(run_gate(&evs, None).is_empty());
let evs = events(&[user(CORRECTION), tool_use("e1", "Edit", "a.rs"), bash_use("b1", "cat a.rs"), said("Done.")]);
assert_eq!(run_gate(&evs, None).len(), 1);
let evs = events(&[
user(CORRECTION),
bash_use("b1", "cargo test"),
tool_use("e1", "Edit", "a.rs"),
said("Fixed."),
]);
assert_eq!(run_gate(&evs, None).len(), 1);
let evs = events(&[user(CORRECTION), tool_use("e1", "Edit", "a.rs"), said("I changed the parser.")]);
assert!(run_gate(&evs, None).is_empty());
}
#[test]
fn done_gate_fires_once_per_correction_and_rearms() {
let evs = events(&[
user(CORRECTION),
said("Fixed."),
user("ok"),
said("It is done."),
user("no, still broken, you did not run anything"),
said("Fixed now."),
]);
assert_eq!(run_gate(&evs, None).len(), 2);
}
#[test]
fn retry_points_warn_once_after_two_failures() {
let mut l = Vec::new();
for (i, c) in ["cargo build", "timeout 30 cargo build 2>&1 | tail -5", "cd x && cargo build", "cargo build", "cargo build"].iter().enumerate() {
l.push(bash_use(&format!("b{i}"), c));
l.push(result(&format!("b{i}"), true));
}
let evs = events(&l);
let p = retry_points(&evs);
assert_eq!(p.len(), 1);
assert_eq!(p[0].0, 5); assert_eq!(retried_forms(&evs), 1);
let evs = events(&[bash_use("a", "ls x"), result("a", true), bash_use("b", "ls y"), result("b", true), bash_use("c", "ls x"), result("c", false)]);
assert!(retry_points(&evs).is_empty());
}