use kintsugi_core::{classify_line, Class};
use proptest::prelude::*;
fn safe_command() -> impl Strategy<Value = &'static str> {
prop::sample::select(vec![
"ls",
"pwd",
"cat README.md",
"git status",
"cargo build",
"echo hi",
"grep x y",
])
}
fn catastrophic_command() -> impl Strategy<Value = &'static str> {
prop::sample::select(vec![
"rm -rf /",
"git push --force",
"terraform destroy",
"kubectl delete ns prod",
"dd if=/dev/zero of=/dev/sda",
])
}
proptest! {
#[test]
fn never_panics_on_arbitrary_input(s in ".*") {
let _ = classify_line(&s);
}
#[test]
fn never_panics_on_structured_noise(
parts in prop::collection::vec(
prop_oneof![Just(";"), Just("&&"), Just("|"), Just("rm"), Just("-rf"),
Just("\""), Just("'"), Just("/"), Just("git")],
0..20,
)
) {
let line = parts.join(" ");
let _ = classify_line(&line);
}
#[test]
fn appending_catastrophe_wins(safe in safe_command(), cat in catastrophic_command()) {
let line = format!("{safe} && {cat}");
prop_assert_eq!(classify_line(&line).class, Class::Catastrophic);
}
#[test]
fn prepending_catastrophe_wins(safe in safe_command(), cat in catastrophic_command()) {
let line = format!("{cat} ; {safe}");
prop_assert_eq!(classify_line(&line).class, Class::Catastrophic);
}
#[test]
fn sudo_never_downgrades(cat in catastrophic_command()) {
prop_assert_eq!(classify_line(&format!("sudo {cat}")).class, Class::Catastrophic);
}
#[test]
fn recursive_rm_always_catastrophic(extra in "[a-z/.]{0,20}") {
let line = format!("rm -rf {extra}");
prop_assert_eq!(classify_line(&line).class, Class::Catastrophic);
}
#[test]
fn safe_stays_non_catastrophic_with_word_args(word in "[a-zA-Z0-9_.-]{1,12}") {
let line = format!("ls {word}");
prop_assert_ne!(classify_line(&line).class, Class::Catastrophic);
}
}