use crate::pathgate::{PositionalWriteGate, positional_write_gate};
use crate::registry::types::{TomlFile, TomlLevel};
#[test]
fn no_inert_command_is_gated_as_a_writer() {
fn toml_files(dir: &std::path::Path, out: &mut Vec<std::path::PathBuf>) {
for entry in std::fs::read_dir(dir).unwrap() {
let path = entry.unwrap().path();
if path.is_dir() {
toml_files(&path, out);
} else if path.extension().is_some_and(|e| e == "toml") {
out.push(path);
}
}
}
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("commands");
let mut files = Vec::new();
toml_files(&root, &mut files);
let (mut inert, mut blanket, mut unpromoted) = (0usize, Vec::new(), Vec::new());
for file in &files {
let src = std::fs::read_to_string(file).unwrap();
let parsed: TomlFile = toml::from_str(&src).unwrap_or_else(|e| panic!("{}: {e}", file.display()));
for cmd in &parsed.command {
if !matches!(cmd.level, Some(TomlLevel::Inert)) {
continue;
}
for name in std::iter::once(&cmd.name).chain(cmd.aliases.iter()) {
inert += 1;
let where_ = format!(" {name} (in {})", file.file_name().unwrap().to_string_lossy());
match positional_write_gate(name) {
PositionalWriteGate::Unconditional => blanket.push(where_),
PositionalWriteGate::Conditional if cmd.write_flags.is_empty() => {
unpromoted.push(where_);
}
_ => {}
}
}
}
}
assert!(
blanket.is_empty(),
"declared `level = \"Inert\"` but gated as writing their positionals in EVERY \
invocation:\n{}\nEither the level is wrong (there is a writing mode, so it is not \
Inert) or the gate is (its operands are reads — give it \
`[roles.X] positional = \"read\"`).",
blanket.join("\n"),
);
assert!(
unpromoted.is_empty(),
"declared `level = \"Inert\"` with a gate that promotes their positionals to writes \
under a flag, but no `write_flags` to raise the LEVEL in that same mode:\n{}\nThe \
gate would refuse the path while the level still called the invocation Inert.",
unpromoted.join("\n"),
);
assert!(inert > 100, "the Inert roster collapsed — this guard is sweeping nothing");
assert_eq!(positional_write_gate("shred"), PositionalWriteGate::Unconditional);
assert_eq!(positional_write_gate("afhash"), PositionalWriteGate::Conditional);
assert_eq!(positional_write_gate("cat"), PositionalWriteGate::None);
assert_eq!(positional_write_gate("dart"), PositionalWriteGate::Unconditional);
}