|
| 1 | +//! Progress reporting tests to ensure accurate counting and prevent regression |
| 2 | +
|
| 3 | +use std::path::PathBuf; |
| 4 | +use std::sync::{Arc, Mutex}; |
| 5 | + |
| 6 | +use scanner_core::{Config, PatternRegistry, Scanner}; |
| 7 | + |
| 8 | +/// Mock progress callback that captures all progress updates |
| 9 | +#[derive(Debug, Default)] |
| 10 | +struct ProgressCapture { |
| 11 | + updates: Arc<Mutex<Vec<(usize, usize, usize)>>>, |
| 12 | + final_counts: Arc<Mutex<Option<(usize, usize, usize)>>>, |
| 13 | +} |
| 14 | + |
| 15 | +impl ProgressCapture { |
| 16 | + fn new() -> Self { |
| 17 | + Self::default() |
| 18 | + } |
| 19 | + |
| 20 | + fn create_callback(&self) -> Arc<dyn Fn(usize, usize, usize) + Send + Sync> { |
| 21 | + let updates = self.updates.clone(); |
| 22 | + let final_counts = self.final_counts.clone(); |
| 23 | + |
| 24 | + Arc::new(move |processed, discovered, findings| { |
| 25 | + // Store all updates for analysis |
| 26 | + updates |
| 27 | + .lock() |
| 28 | + .unwrap() |
| 29 | + .push((processed, discovered, findings)); |
| 30 | + |
| 31 | + // Store final counts (last update should be final) |
| 32 | + *final_counts.lock().unwrap() = Some((processed, discovered, findings)); |
| 33 | + }) |
| 34 | + } |
| 35 | + |
| 36 | + fn get_final_counts(&self) -> Option<(usize, usize, usize)> { |
| 37 | + *self.final_counts.lock().unwrap() |
| 38 | + } |
| 39 | + |
| 40 | + fn get_all_updates(&self) -> Vec<(usize, usize, usize)> { |
| 41 | + self.updates.lock().unwrap().clone() |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +#[test] |
| 46 | +fn test_progress_reporting_accuracy() { |
| 47 | + // Create simple test patterns that will match our fixture files |
| 48 | + let patterns_toml = r##" |
| 49 | +[version] |
| 50 | +schema = "1.0" |
| 51 | +updated = "2024-01-01" |
| 52 | +
|
| 53 | +[[library]] |
| 54 | +name = "test-lib" |
| 55 | +languages = ["rust", "go", "java", "c", "cpp", "python"] |
| 56 | +
|
| 57 | +[library.patterns] |
| 58 | +include = ["#include", "use ", "import "] |
| 59 | +apis = ["printf", "println", "print", "main"] |
| 60 | + "##; |
| 61 | + |
| 62 | + let registry = PatternRegistry::load(patterns_toml).expect("Failed to load patterns"); |
| 63 | + |
| 64 | + // Set up progress capture |
| 65 | + let progress_capture = ProgressCapture::new(); |
| 66 | + |
| 67 | + let config = Config { |
| 68 | + max_file_size: 1024 * 1024, // 1MB |
| 69 | + include_globs: vec![ |
| 70 | + "**/*.rs".to_string(), |
| 71 | + "**/*.go".to_string(), |
| 72 | + "**/*.java".to_string(), |
| 73 | + "**/*.c".to_string(), |
| 74 | + "**/*.cpp".to_string(), |
| 75 | + "**/*.py".to_string(), |
| 76 | + ], |
| 77 | + exclude_globs: vec![], |
| 78 | + deterministic: true, |
| 79 | + progress_callback: Some(progress_capture.create_callback()), |
| 80 | + }; |
| 81 | + |
| 82 | + // Create scanner with empty detectors for this test |
| 83 | + let detectors = vec![]; |
| 84 | + let scanner = Scanner::new(®istry, detectors, config); |
| 85 | + |
| 86 | + // Scan the fixtures directory |
| 87 | + let fixtures_path = PathBuf::from("../../fixtures"); |
| 88 | + let roots = vec![fixtures_path]; |
| 89 | + |
| 90 | + // First, count the expected files using discover_files (dry run) |
| 91 | + let expected_files = scanner.discover_files(&roots); |
| 92 | + let expected_count = expected_files.len(); |
| 93 | + |
| 94 | + // Run the actual scan with progress reporting |
| 95 | + let _findings = scanner.run(&roots).expect("Scan failed"); |
| 96 | + |
| 97 | + // Verify progress reporting accuracy |
| 98 | + let final_counts = progress_capture |
| 99 | + .get_final_counts() |
| 100 | + .expect("No progress updates received"); |
| 101 | + |
| 102 | + let (final_processed, final_discovered, _final_findings) = final_counts; |
| 103 | + |
| 104 | + // Core assertion: discovered count should match our dry-run count |
| 105 | + assert_eq!( |
| 106 | + final_discovered, expected_count, |
| 107 | + "Progress reported {} discovered files, but dry-run found {} files. This indicates a regression in progress counting.", |
| 108 | + final_discovered, expected_count |
| 109 | + ); |
| 110 | + |
| 111 | + // Core assertion: processed count should equal discovered count |
| 112 | + // (all discovered files should be processed) |
| 113 | + assert_eq!( |
| 114 | + final_processed, final_discovered, |
| 115 | + "Progress reported {} processed files but {} discovered files. All discovered files should be processed.", |
| 116 | + final_processed, final_discovered |
| 117 | + ); |
| 118 | + |
| 119 | + // Verify we actually found some files (fixtures should contain test files) |
| 120 | + assert!( |
| 121 | + final_discovered > 0, |
| 122 | + "No files were discovered. Check that fixtures directory exists and contains source files." |
| 123 | + ); |
| 124 | + |
| 125 | + println!("✅ Progress reporting test passed:"); |
| 126 | + println!(" Discovered: {} files", final_discovered); |
| 127 | + println!(" Processed: {} files", final_processed); |
| 128 | + println!(" Expected: {} files (from dry-run)", expected_count); |
| 129 | +} |
| 130 | + |
| 131 | +#[test] |
| 132 | +fn test_progress_monotonic_increase() { |
| 133 | + // Test that progress counts only increase (never decrease) |
| 134 | + let patterns_toml = r##" |
| 135 | +[version] |
| 136 | +schema = "1.0" |
| 137 | +updated = "2024-01-01" |
| 138 | +
|
| 139 | +[[library]] |
| 140 | +name = "test-lib" |
| 141 | +languages = ["rust"] |
| 142 | +
|
| 143 | +[library.patterns] |
| 144 | +apis = ["main"] |
| 145 | + "##; |
| 146 | + |
| 147 | + let registry = PatternRegistry::load(patterns_toml).expect("Failed to load patterns"); |
| 148 | + let progress_capture = ProgressCapture::new(); |
| 149 | + |
| 150 | + let config = Config { |
| 151 | + max_file_size: 1024 * 1024, |
| 152 | + include_globs: vec!["**/*.rs".to_string()], |
| 153 | + exclude_globs: vec![], |
| 154 | + deterministic: true, |
| 155 | + progress_callback: Some(progress_capture.create_callback()), |
| 156 | + }; |
| 157 | + |
| 158 | + let detectors = vec![]; |
| 159 | + let scanner = Scanner::new(®istry, detectors, config); |
| 160 | + |
| 161 | + let fixtures_path = PathBuf::from("../../fixtures"); |
| 162 | + let _findings = scanner.run(&[fixtures_path]).expect("Scan failed"); |
| 163 | + |
| 164 | + // Verify that progress counts are monotonically increasing |
| 165 | + let all_updates = progress_capture.get_all_updates(); |
| 166 | + |
| 167 | + let mut prev_processed = 0; |
| 168 | + let mut prev_discovered = 0; |
| 169 | + let mut prev_findings = 0; |
| 170 | + |
| 171 | + for (i, &(processed, discovered, findings)) in all_updates.iter().enumerate() { |
| 172 | + assert!( |
| 173 | + processed >= prev_processed, |
| 174 | + "Progress regression at update {}: processed count decreased from {} to {}", |
| 175 | + i, |
| 176 | + prev_processed, |
| 177 | + processed |
| 178 | + ); |
| 179 | + |
| 180 | + assert!( |
| 181 | + discovered >= prev_discovered, |
| 182 | + "Progress regression at update {}: discovered count decreased from {} to {}", |
| 183 | + i, |
| 184 | + prev_discovered, |
| 185 | + discovered |
| 186 | + ); |
| 187 | + |
| 188 | + assert!( |
| 189 | + findings >= prev_findings, |
| 190 | + "Progress regression at update {}: findings count decreased from {} to {}", |
| 191 | + i, |
| 192 | + prev_findings, |
| 193 | + findings |
| 194 | + ); |
| 195 | + |
| 196 | + prev_processed = processed; |
| 197 | + prev_discovered = discovered; |
| 198 | + prev_findings = findings; |
| 199 | + } |
| 200 | + |
| 201 | + println!( |
| 202 | + "✅ Monotonic progress test passed with {} updates", |
| 203 | + all_updates.len() |
| 204 | + ); |
| 205 | +} |
| 206 | + |
| 207 | +#[test] |
| 208 | +fn test_progress_file_extension_accuracy() { |
| 209 | + // Test that progress counting respects file extension filtering |
| 210 | + let patterns_toml = r##" |
| 211 | +[version] |
| 212 | +schema = "1.0" |
| 213 | +updated = "2024-01-01" |
| 214 | +
|
| 215 | +[[library]] |
| 216 | +name = "rust-only-lib" |
| 217 | +languages = ["rust"] |
| 218 | +
|
| 219 | +[library.patterns] |
| 220 | +apis = ["main"] |
| 221 | + "##; |
| 222 | + |
| 223 | + let registry = PatternRegistry::load(patterns_toml).expect("Failed to load patterns"); |
| 224 | + |
| 225 | + // Create two progress captures - one for Rust-only, one for all files |
| 226 | + let rust_only_capture = ProgressCapture::new(); |
| 227 | + let all_files_capture = ProgressCapture::new(); |
| 228 | + |
| 229 | + // Scan 1: Rust files only |
| 230 | + let rust_config = Config { |
| 231 | + max_file_size: 1024 * 1024, |
| 232 | + include_globs: vec!["**/*.rs".to_string()], |
| 233 | + exclude_globs: vec![], |
| 234 | + deterministic: true, |
| 235 | + progress_callback: Some(rust_only_capture.create_callback()), |
| 236 | + }; |
| 237 | + |
| 238 | + let detectors1 = vec![]; |
| 239 | + let rust_scanner = Scanner::new(®istry, detectors1, rust_config); |
| 240 | + let fixtures_path = PathBuf::from("../../fixtures"); |
| 241 | + let _rust_findings = rust_scanner |
| 242 | + .run(&[fixtures_path.clone()]) |
| 243 | + .expect("Rust scan failed"); |
| 244 | + |
| 245 | + // Scan 2: All supported file types |
| 246 | + let all_config = Config { |
| 247 | + max_file_size: 1024 * 1024, |
| 248 | + include_globs: vec![ |
| 249 | + "**/*.rs".to_string(), |
| 250 | + "**/*.go".to_string(), |
| 251 | + "**/*.java".to_string(), |
| 252 | + "**/*.c".to_string(), |
| 253 | + "**/*.py".to_string(), |
| 254 | + ], |
| 255 | + exclude_globs: vec![], |
| 256 | + deterministic: true, |
| 257 | + progress_callback: Some(all_files_capture.create_callback()), |
| 258 | + }; |
| 259 | + |
| 260 | + let detectors2 = vec![]; |
| 261 | + let all_scanner = Scanner::new(®istry, detectors2, all_config); |
| 262 | + let _all_findings = all_scanner |
| 263 | + .run(&[fixtures_path]) |
| 264 | + .expect("All files scan failed"); |
| 265 | + |
| 266 | + let rust_counts = rust_only_capture.get_final_counts().unwrap(); |
| 267 | + let all_counts = all_files_capture.get_final_counts().unwrap(); |
| 268 | + |
| 269 | + let (_rust_processed, rust_discovered, _) = rust_counts; |
| 270 | + let (_all_processed, all_discovered, _) = all_counts; |
| 271 | + |
| 272 | + // All-files scan should discover at least as many files as Rust-only |
| 273 | + assert!( |
| 274 | + all_discovered >= rust_discovered, |
| 275 | + "All-files scan discovered {} files, but Rust-only scan discovered {} files. This suggests filtering is broken.", |
| 276 | + all_discovered, rust_discovered |
| 277 | + ); |
| 278 | + |
| 279 | + // If there are non-Rust files in fixtures, all-files should discover more |
| 280 | + // (This is informational - fixtures may only contain Rust files) |
| 281 | + if all_discovered > rust_discovered { |
| 282 | + println!( |
| 283 | + "✅ File extension filtering working: {} total files, {} Rust files", |
| 284 | + all_discovered, rust_discovered |
| 285 | + ); |
| 286 | + } else { |
| 287 | + println!("ℹ️ Only Rust files found in fixtures directory"); |
| 288 | + } |
| 289 | + |
| 290 | + println!("✅ File extension accuracy test passed"); |
| 291 | +} |
0 commit comments