spark-inquisitor
Ultra-fast compiled native Rust gatekeeper and automated code reviewer
Brought by drakestapleton
spark-inquisitor / tests / cli_exit_code_contracts.rs
| 1 | use std::fs; |
| 2 | use std::process::Command; |
| 3 | |
| 4 | fn get_temp_path(name: &str) -> std::path::PathBuf { |
| 5 | let mut path = std::env::temp_dir(); |
| 6 | let pid = std::process::id(); |
| 7 | path.push(format!("inquisitor_{}_{}", pid, name)); |
| 8 | path |
| 9 | } |
| 10 | |
| 11 | #[test] |
| 12 | fn test_cli_doctor_exit_zero() { |
| 13 | let bin = env!("CARGO_BIN_EXE_spark-inquisitor"); |
| 14 | let output = Command::new(bin) |
| 15 | .arg("doctor") |
| 16 | .output() |
| 17 | .expect("Failed to execute spark-inquisitor doctor"); |
| 18 | |
| 19 | assert!(output.status.success()); |
| 20 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 21 | assert!(stdout.contains("⚖️ Sovereign Inquisitor Diagnostics: OK")); |
| 22 | assert!(stdout.contains("Invariant rules: Active")); |
| 23 | } |
| 24 | |
| 25 | #[test] |
| 26 | fn test_cli_audit_clean_diff_exit_zero() { |
| 27 | let bin = env!("CARGO_BIN_EXE_spark-inquisitor"); |
| 28 | let diff_path = get_temp_path("clean.diff"); |
| 29 | fs::write( |
| 30 | &diff_path, |
| 31 | "diff --git a/clean.rs b/clean.rs\n+pub fn ok() -> bool { true }\n", |
| 32 | ) |
| 33 | .unwrap(); |
| 34 | |
| 35 | let output = Command::new(bin) |
| 36 | .arg("audit") |
| 37 | .arg("--diff") |
| 38 | .arg(&diff_path) |
| 39 | .output() |
| 40 | .expect("Failed to execute audit"); |
| 41 | |
| 42 | let _ = fs::remove_file(&diff_path); |
| 43 | |
| 44 | assert!(output.status.success()); |
| 45 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 46 | assert!(stdout.contains("Diff is clean")); |
| 47 | } |
| 48 | |
| 49 | #[test] |
| 50 | fn test_cli_audit_telemetry_diff_exit_nonzero() { |
| 51 | let bin = env!("CARGO_BIN_EXE_spark-inquisitor"); |
| 52 | let diff_path = get_temp_path("telemetry.diff"); |
| 53 | fs::write( |
| 54 | &diff_path, |
| 55 | "diff --git a/bad.rs b/bad.rs\n+let _ = send_telemetry(\"google-analytics\");\n", |
| 56 | ) |
| 57 | .unwrap(); |
| 58 | |
| 59 | let output = Command::new(bin) |
| 60 | .arg("audit") |
| 61 | .arg("--diff") |
| 62 | .arg(&diff_path) |
| 63 | .output() |
| 64 | .expect("Failed to execute audit"); |
| 65 | |
| 66 | let _ = fs::remove_file(&diff_path); |
| 67 | |
| 68 | assert!(!output.status.success()); |
| 69 | assert_eq!(output.status.code(), Some(1)); |
| 70 | let stderr = String::from_utf8_lossy(&output.stderr); |
| 71 | assert!(stderr.contains("Constitutional audit failed")); |
| 72 | assert!(stderr.contains("google-analytics")); |
| 73 | } |
| 74 | |
| 75 | #[test] |
| 76 | fn test_cli_audit_em_dash_diff_exit_nonzero() { |
| 77 | let bin = env!("CARGO_BIN_EXE_spark-inquisitor"); |
| 78 | let diff_path = get_temp_path("dash.diff"); |
| 79 | fs::write( |
| 80 | &diff_path, |
| 81 | "diff --git a/dash.rs b/dash.rs\n+// note \u{2014} bad dash\n", |
| 82 | ) |
| 83 | .unwrap(); |
| 84 | |
| 85 | let output = Command::new(bin) |
| 86 | .arg("audit") |
| 87 | .arg("--diff") |
| 88 | .arg(&diff_path) |
| 89 | .output() |
| 90 | .expect("Failed to execute audit"); |
| 91 | |
| 92 | let _ = fs::remove_file(&diff_path); |
| 93 | |
| 94 | assert!(!output.status.success()); |
| 95 | assert_eq!(output.status.code(), Some(1)); |
| 96 | let stderr = String::from_utf8_lossy(&output.stderr); |
| 97 | assert!(stderr.contains("Constitutional audit failed")); |
| 98 | assert!(stderr.contains("Em/En dash detected")); |
| 99 | } |
| 100 | |
| 101 | #[test] |
| 102 | fn test_cli_audit_secret_diff_exit_nonzero() { |
| 103 | let bin = env!("CARGO_BIN_EXE_spark-inquisitor"); |
| 104 | let diff_path = get_temp_path("secret.diff"); |
| 105 | fs::write( |
| 106 | &diff_path, |
| 107 | "diff --git a/sec.rs b/sec.rs\n+const KEY: &str = \"AKIA1234567890ABCDEF\";\n", |
| 108 | ) |
| 109 | .unwrap(); |
| 110 | |
| 111 | let output = Command::new(bin) |
| 112 | .arg("audit") |
| 113 | .arg("--diff") |
| 114 | .arg(&diff_path) |
| 115 | .output() |
| 116 | .expect("Failed to execute audit"); |
| 117 | |
| 118 | let _ = fs::remove_file(&diff_path); |
| 119 | |
| 120 | assert!(!output.status.success()); |
| 121 | assert_eq!(output.status.code(), Some(1)); |
| 122 | let stderr = String::from_utf8_lossy(&output.stderr); |
| 123 | assert!(stderr.contains("Plaintext AWS Access Key detected")); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn test_cli_review_clean_diff_exit_zero() { |
| 128 | let bin = env!("CARGO_BIN_EXE_spark-inquisitor"); |
| 129 | let diff_path = get_temp_path("rev_clean.diff"); |
| 130 | fs::write( |
| 131 | &diff_path, |
| 132 | "diff --git a/clean.rs b/clean.rs\n+pub fn check() -> i32 { 42 }\n", |
| 133 | ) |
| 134 | .unwrap(); |
| 135 | |
| 136 | let output = Command::new(bin) |
| 137 | .arg("review") |
| 138 | .arg("--author") |
| 139 | .arg("tester") |
| 140 | .arg("--pr") |
| 141 | .arg("50") |
| 142 | .arg("--title") |
| 143 | .arg("Clean contribution") |
| 144 | .arg("--diff") |
| 145 | .arg(&diff_path) |
| 146 | .output() |
| 147 | .expect("Failed to execute review"); |
| 148 | |
| 149 | let _ = fs::remove_file(&diff_path); |
| 150 | |
| 151 | assert!(output.status.success()); |
| 152 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 153 | assert!(stdout.contains("Constitutional Diff Audit: PASS")); |
| 154 | assert!(stdout.contains("Contributor Alignment Interview")); |
| 155 | } |
| 156 | |
| 157 | #[test] |
| 158 | fn test_cli_review_violating_diff_exit_nonzero() { |
| 159 | let bin = env!("CARGO_BIN_EXE_spark-inquisitor"); |
| 160 | let diff_path = get_temp_path("rev_bad.diff"); |
| 161 | fs::write( |
| 162 | &diff_path, |
| 163 | "diff --git a/bad.rs b/bad.rs\n+// This is a game-changer feature\n", |
| 164 | ) |
| 165 | .unwrap(); |
| 166 | |
| 167 | let output = Command::new(bin) |
| 168 | .arg("review") |
| 169 | .arg("--author") |
| 170 | .arg("violator") |
| 171 | .arg("--pr") |
| 172 | .arg("51") |
| 173 | .arg("--title") |
| 174 | .arg("Violating contribution") |
| 175 | .arg("--diff") |
| 176 | .arg(&diff_path) |
| 177 | .output() |
| 178 | .expect("Failed to execute review"); |
| 179 | |
| 180 | let _ = fs::remove_file(&diff_path); |
| 181 | |
| 182 | assert!(!output.status.success()); |
| 183 | assert_eq!(output.status.code(), Some(1)); |
| 184 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 185 | assert!(stdout.contains("Constitutional Diff Audit: FAILED")); |
| 186 | } |
| 187 | |
| 188 | #[test] |
| 189 | fn test_cli_evaluate_valid_testimony_exit_zero() { |
| 190 | let bin = env!("CARGO_BIN_EXE_spark-inquisitor"); |
| 191 | let testimony_path = get_temp_path("testimony_valid.txt"); |
| 192 | fs::write( |
| 193 | &testimony_path, |
| 194 | "I solemnly certify and affirm the Sovereign Contributor Oath. We ensure zero telemetry and build for human sovereignty and freedom.", |
| 195 | ).unwrap(); |
| 196 | |
| 197 | let output = Command::new(bin) |
| 198 | .arg("evaluate") |
| 199 | .arg("--author") |
| 200 | .arg("sovereign_dev") |
| 201 | .arg("--testimony") |
| 202 | .arg(&testimony_path) |
| 203 | .output() |
| 204 | .expect("Failed to execute evaluate"); |
| 205 | |
| 206 | let _ = fs::remove_file(&testimony_path); |
| 207 | |
| 208 | assert!(output.status.success()); |
| 209 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 210 | assert!(stdout.contains("PASS:")); |
| 211 | assert!(stdout.contains("`sovereign-interview-passed`")); |
| 212 | } |
| 213 | |
| 214 | #[test] |
| 215 | fn test_cli_evaluate_invalid_testimony_exit_nonzero() { |
| 216 | let bin = env!("CARGO_BIN_EXE_spark-inquisitor"); |
| 217 | let testimony_path = get_temp_path("testimony_invalid.txt"); |
| 218 | fs::write(&testimony_path, "Just testing something out, no oath here.").unwrap(); |
| 219 | |
| 220 | let output = Command::new(bin) |
| 221 | .arg("evaluate") |
| 222 | .arg("--author") |
| 223 | .arg("random_user") |
| 224 | .arg("--testimony") |
| 225 | .arg(&testimony_path) |
| 226 | .output() |
| 227 | .expect("Failed to execute evaluate"); |
| 228 | |
| 229 | let _ = fs::remove_file(&testimony_path); |
| 230 | |
| 231 | assert!(!output.status.success()); |
| 232 | assert_eq!(output.status.code(), Some(1)); |
| 233 | let stderr = String::from_utf8_lossy(&output.stderr); |
| 234 | assert!(stderr.contains("FAIL:")); |
| 235 | } |
| 236 | |
| 237 | #[test] |
| 238 | fn test_cli_audit_em_dash_with_advisory_style_exit_zero() { |
| 239 | let bin = env!("CARGO_BIN_EXE_spark-inquisitor"); |
| 240 | let diff_path = get_temp_path("advisory_dash.diff"); |
| 241 | fs::write( |
| 242 | &diff_path, |
| 243 | "diff --git a/dash.rs b/dash.rs\n+// note \u{2014} bad dash\n", |
| 244 | ) |
| 245 | .unwrap(); |
| 246 | |
| 247 | let output = Command::new(bin) |
| 248 | .arg("audit") |
| 249 | .arg("--advisory-style") |
| 250 | .arg("--diff") |
| 251 | .arg(&diff_path) |
| 252 | .output() |
| 253 | .expect("Failed to execute audit with advisory style"); |
| 254 | |
| 255 | let _ = fs::remove_file(&diff_path); |
| 256 | |
| 257 | assert!(output.status.success()); |
| 258 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 259 | assert!(stdout.contains("passed critical security invariants")); |
| 260 | assert!(stdout.contains("Stylistic recommendations")); |
| 261 | } |
| 262 | |
| 263 | #[test] |
| 264 | fn test_cli_fix_command_sanitizes_file() { |
| 265 | let bin = env!("CARGO_BIN_EXE_spark-inquisitor"); |
| 266 | let target_file = get_temp_path("fix_test.md"); |
| 267 | fs::write(&target_file, "Title \u{2014} Section \u{2013} Detail").unwrap(); |
| 268 | |
| 269 | let output = Command::new(bin) |
| 270 | .arg("fix") |
| 271 | .arg("--file") |
| 272 | .arg(&target_file) |
| 273 | .output() |
| 274 | .expect("Failed to execute fix command"); |
| 275 | |
| 276 | assert!(output.status.success()); |
| 277 | let fixed_content = fs::read_to_string(&target_file).unwrap(); |
| 278 | let _ = fs::remove_file(&target_file); |
| 279 | |
| 280 | assert!(!fixed_content.contains('\u{2014}')); |
| 281 | assert!(!fixed_content.contains('\u{2013}')); |
| 282 | assert!(fixed_content.contains("Title: Section - Detail")); |
| 283 | } |