const NOISE_PATH_PATTERNS: &[&str] = &[
"/var/folders/", "/tmp/", ".tokf-verify-", "/T/", ".tmp", ];
pub fn is_noise_command(command: &str) -> bool {
NOISE_PATH_PATTERNS.iter().any(|p| command.contains(p))
}
pub fn command_shape(command: &str) -> String {
let trimmed = command.trim();
if trimmed.is_empty() {
return String::new();
}
let mut tokens = trimmed.split_whitespace();
let Some(prog) = tokens.next() else {
return String::new();
};
let Some(second) = tokens.next() else {
return prog.to_string();
};
let has_more = tokens.next().is_some();
if looks_like_subcommand(second) {
if has_more {
format!("{prog} {second} <args>")
} else {
format!("{prog} {second}")
}
} else {
format!("{prog} <args>")
}
}
fn looks_like_subcommand(token: &str) -> bool {
!token.is_empty()
&& !token.starts_with('-')
&& !token.contains('/')
&& !token.contains('=')
&& token.chars().all(|c| c.is_ascii_alphabetic() || c == '-')
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn noise_detects_var_folders() {
assert!(is_noise_command("git -C /var/folders/abc/.tmpXYZ status"));
}
#[test]
fn noise_detects_tokf_verify_rigs() {
assert!(is_noise_command(
"git -C /Users/x/.tokf-verify-cache/git_status status"
));
}
#[test]
fn noise_detects_tmp_paths() {
assert!(is_noise_command("ls /tmp/some-test"));
}
#[test]
fn noise_does_not_flag_normal_commands() {
assert!(!is_noise_command("git status"));
assert!(!is_noise_command("git diff --stat"));
assert!(!is_noise_command("cargo test"));
}
#[test]
fn shape_strips_path_args() {
assert_eq!(
command_shape("git diff /home/user/repo/src/main.rs"),
"git diff <args>"
);
}
#[test]
fn shape_keeps_subcommand() {
assert_eq!(command_shape("git status"), "git status");
assert_eq!(command_shape("cargo test"), "cargo test");
assert_eq!(command_shape("npm run"), "npm run");
}
#[test]
fn shape_with_flags_first() {
assert_eq!(command_shape("ls -la /tmp"), "ls <args>");
}
#[test]
fn shape_lone_program() {
assert_eq!(command_shape("ls"), "ls");
}
#[test]
fn shape_empty_input() {
assert_eq!(command_shape(""), "");
assert_eq!(command_shape(" "), "");
}
#[test]
fn shape_redacts_sensitive_args() {
let shape =
command_shape("curl -H Authorization:Bearer-secret-token https://api.example.com");
assert!(!shape.contains("secret"));
assert_eq!(shape, "curl <args>");
}
#[test]
fn looks_like_subcommand_basic() {
assert!(looks_like_subcommand("status"));
assert!(looks_like_subcommand("test"));
assert!(looks_like_subcommand("run"));
assert!(looks_like_subcommand("name-only"));
assert!(!looks_like_subcommand("--name-only"));
assert!(!looks_like_subcommand("/path"));
assert!(!looks_like_subcommand("KEY=val"));
assert!(!looks_like_subcommand(""));
}
}