1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
#![cfg_attr(coverage_nightly, coverage(off))]
//! Core analysis methods for `SimpleDeepContext`.
use anyhow::Result;
use std::path::{Path, PathBuf};
use std::time::Instant;
use tracing::info;
use super::types::{
ComplexityMetrics, FileComplexityDetail, FileComplexityMetrics, SimpleAnalysisConfig,
SimpleAnalysisReport, SimpleDeepContext,
};
impl SimpleDeepContext {
/// Create new simple deep context analyzer
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn new() -> Self {
Self
}
/// Perform simplified deep context analysis
///
/// This function analyzes a Rust project to identify complexity patterns and
/// provide refactoring recommendations. After fixing issue #33, it now uses
/// proper AST-based complexity analysis instead of heuristic estimation.
///
/// # Examples
///
/// ```no_run
/// use pmat::services::simple_deep_context::{SimpleDeepContext, SimpleAnalysisConfig};
/// use std::path::PathBuf;
///
/// # async fn example() -> anyhow::Result<()> {
/// let analyzer = SimpleDeepContext::new();
/// let config = SimpleAnalysisConfig {
/// project_path: PathBuf::from("./my-rust-project"),
/// include_features: vec![],
/// include_patterns: vec![],
/// exclude_patterns: vec![],
/// enable_verbose: false,
/// };
///
/// let report = analyzer.analyze(config).await?;
///
/// // Issue #33 fix: Complexity values are now accurate, not fixed at 1.0
/// assert!(report.complexity_metrics.total_functions > 0);
/// assert!(report.complexity_metrics.avg_complexity >= 1.0);
///
/// // High complexity functions are properly detected
/// if report.complexity_metrics.high_complexity_count > 0 {
/// println!("Found {} high-complexity functions",
/// report.complexity_metrics.high_complexity_count);
/// }
///
/// // File-level complexity details are accurate
/// for detail in &report.file_complexity_details {
/// println!("File: {} - {} functions, avg complexity: {:.2}",
/// detail.file_path.display(),
/// detail.function_count,
/// detail.avg_complexity);
/// }
/// # Ok(())
/// # }
/// ```
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub async fn analyze(&self, config: SimpleAnalysisConfig) -> Result<SimpleAnalysisReport> {
let start_time = Instant::now();
info!("🔍 Starting simplified deep context analysis");
info!("📂 Project path: {}", config.project_path.display());
// Phase 1: File discovery
let source_files = self.discover_source_files(&config).await?;
info!("📁 Discovered {} source files", source_files.len());
for file in &source_files {
info!("📄 File: {}", file.display());
}
// Phase 2: Basic analysis
let (complexity_metrics, file_complexity_details) =
self.analyze_complexity(&source_files).await?;
// Phase 3: Generate recommendations
//
// `analyze deep-context` is documented as "deep context analysis with
// defect detection", but only `--format sarif` ran a defect detector:
// json/markdown came through here and reported "Code complexity looks
// good! No immediate recommendations." for the very corpus whose SARIF
// run listed a High-severity self-admitted-debt finding. The same
// detector now runs for every format.
let debts = Self::detect_self_admitted_debt(&source_files);
let recommendations = Self::with_debt_recommendations(
self.generate_recommendations(&complexity_metrics),
&debts,
);
let analysis_duration = start_time.elapsed();
let report = SimpleAnalysisReport {
file_count: source_files.len(),
analysis_duration,
complexity_metrics,
recommendations,
file_complexity_details,
};
info!("✅ Analysis completed in {:?}", analysis_duration);
Ok(report)
}
/// Check if a path matches any include pattern
fn matches_include_patterns(path: &Path, ext: &str, patterns: &[String]) -> bool {
let path_str = path.to_string_lossy();
patterns.iter().any(|pattern| {
// Glob pattern: extract extension from "**/*.rs"
if let Some(ext_from_pattern) = pattern
.strip_prefix("**/")
.and_then(|p| p.strip_prefix("*."))
{
return ext == ext_from_pattern;
}
// Simple pattern: check if filename or path contains the pattern
path.file_name()
.and_then(|n| n.to_str())
.is_some_and(|name| name.contains(pattern) || path_str.contains(pattern))
})
}
/// Build the matcher for `--exclude-pattern` (and the common exclusions the
/// handler appends).
///
/// `analyze deep-context --exclude-pattern '**/*.py'` used to produce a
/// byte-identical report to a run without it — `SimpleAnalysisConfig` stored
/// `exclude_patterns` and nothing ever read them. An unparseable pattern is
/// an error rather than a silently dropped filter, for the same reason.
pub(super) fn build_exclude_matcher(patterns: &[String]) -> Result<Option<globset::GlobSet>> {
if patterns.is_empty() {
return Ok(None);
}
let mut builder = globset::GlobSetBuilder::new();
for pattern in patterns {
let glob = globset::Glob::new(pattern)
.map_err(|e| anyhow::anyhow!("invalid --exclude-pattern '{pattern}': {e}"))?;
builder.add(glob);
}
Ok(Some(builder.build()?))
}
/// Discover source files in the project
pub(super) async fn discover_source_files(
&self,
config: &SimpleAnalysisConfig,
) -> Result<Vec<PathBuf>> {
use ignore::WalkBuilder;
let source_extensions = [
"rs", "js", "ts", "jsx", "tsx", "py", "cpp", "c", "h", "wasm", "wat", "rb", "ruchy",
"go", "java", "cs", "kt", "sh", "bash", "php", "swift", "lua", "lean",
];
let exclude_dirs = ["target", "node_modules", ".git", "build", "dist"];
let abs_project_path = if config.project_path.is_absolute() {
config.project_path.clone()
} else {
std::env::current_dir()?.join(&config.project_path)
};
let excludes = Self::build_exclude_matcher(&config.exclude_patterns)?;
let mut files = Vec::new();
// Gitignore-aware walk. A bare `WalkDir` here counted every file under
// gitignored paths: on this repo the ephemeral `.claude/worktrees/`
// duplicate checkouts took `file_count` to 222022 for a tree with 5,497
// tracked files (and `total_functions` to 1,012,834 against 25,593).
// Duplicating a checkout must not change what the project measures.
for entry in WalkBuilder::new(&abs_project_path)
.follow_links(false)
.hidden(false)
.parents(true)
.ignore(true)
.git_ignore(true)
.git_global(true)
.git_exclude(true)
.build()
.filter_map(std::result::Result::ok)
.filter(|e| e.file_type().is_some_and(|t| t.is_file()))
{
let path = entry.path();
let should_exclude = path.components().any(|comp| {
comp.as_os_str()
.to_str()
.is_some_and(|name| exclude_dirs.contains(&name))
});
if should_exclude {
continue;
}
let Some(ext) = path.extension().and_then(|e| e.to_str()) else {
continue;
};
if !source_extensions.contains(&ext) {
continue;
}
if let Some(excludes) = &excludes {
let relative = path.strip_prefix(&abs_project_path).unwrap_or(path);
if excludes.is_match(path) || excludes.is_match(relative) {
continue;
}
}
if config.include_patterns.is_empty()
|| Self::matches_include_patterns(path, ext, &config.include_patterns)
{
files.push(path.to_path_buf());
}
}
files.sort();
Ok(files)
}
/// Analyze complexity of source files
pub(super) async fn analyze_complexity(
&self,
files: &[PathBuf],
) -> Result<(ComplexityMetrics, Vec<FileComplexityDetail>)> {
let mut total_functions = 0;
let mut high_complexity_count = 0;
let mut complexity_sum = 0.0;
let mut file_details = Vec::new();
for file in files {
let metrics = self.analyze_file_complexity(file.as_path()).await?;
total_functions += metrics.function_count;
high_complexity_count += metrics.high_complexity_functions;
complexity_sum += metrics.avg_complexity * metrics.function_count as f64;
// Calculate complexity score for ranking (weighted by functions and complexity)
let complexity_score = (metrics.avg_complexity * 0.7)
+ (metrics.high_complexity_functions as f64 * 2.0)
+ (metrics.function_count as f64 * 0.3);
file_details.push(FileComplexityDetail {
file_path: file.clone(),
function_count: metrics.function_count,
high_complexity_functions: metrics.high_complexity_functions,
avg_complexity: metrics.avg_complexity,
complexity_score,
function_names: metrics.function_names.clone(),
});
}
let avg_complexity = if total_functions > 0 {
complexity_sum / total_functions as f64
} else {
0.0
};
let complexity_metrics = ComplexityMetrics {
total_functions,
high_complexity_count,
avg_complexity,
};
Ok((complexity_metrics, file_details))
}
/// Analyze complexity of a single file using proper AST-based analysis
///
/// This method uses the unified AST-based complexity analyzer instead of heuristics,
/// ensuring accurate complexity measurements across all analysis commands.
///
/// # Example
///
/// ```compile_fail
/// use pmat::services::simple_deep_context::{SimpleDeepContext, FileComplexityMetrics};
/// use std::path::Path;
///
/// # tokio_test::block_on(async {
/// let analyzer = SimpleDeepContext::new();
/// // This is a private method and cannot be called from outside the module
/// let metrics = analyzer.analyze_file_complexity(Path::new("src/main.rs")).await.unwrap();
///
/// // Metrics now contain accurate AST-based complexity values
/// assert!(metrics.avg_complexity > 0.0);
/// # });
/// ```
pub(super) async fn analyze_file_complexity(
&self,
file_path: &Path,
) -> Result<FileComplexityMetrics> {
let extension = file_path.extension().and_then(|e| e.to_str()).unwrap_or("");
info!(
"🔍 Analyzing file: {} with extension: {}",
file_path.display(),
extension
);
// Dispatch complexity analysis to per-language helpers
let (function_count, high_complexity_functions, avg_complexity) = match extension {
"rs" => self.complexity_for_rust(file_path).await,
"ts" | "tsx" | "js" | "jsx" => {
self.complexity_for_typescript(file_path, extension).await
}
"wasm" | "wat" => self.complexity_for_wasm(file_path, extension).await,
"rb" | "ruchy" => {
self.complexity_heuristic_fallback(file_path, extension)
.await
}
"go" => self.complexity_for_go(file_path).await,
"cs" => self.complexity_for_csharp(file_path).await,
"kt" => self.complexity_for_kotlin(file_path).await,
"sh" | "bash" => self.complexity_for_bash(file_path).await,
"php" => self.complexity_for_php(file_path).await,
"swift" => self.complexity_for_swift(file_path).await,
"lua" => self.complexity_for_lua(file_path).await,
_ => {
self.complexity_heuristic_fallback(file_path, extension)
.await
}
};
// Dispatch function name extraction to per-language helpers
let function_names = match extension {
"rs" => self.function_names_for_rust(file_path).await,
"ts" | "tsx" | "js" | "jsx" => self
.extract_function_names_heuristic(file_path, extension)
.await
.unwrap_or_default(),
"wasm" | "wat" => self.function_names_for_wasm(file_path, extension).await,
"go" => self.function_names_for_go(file_path).await,
"cs" => self.function_names_for_csharp(file_path).await,
"kt" => self.function_names_for_kotlin(file_path).await,
"sh" | "bash" => self.function_names_for_bash(file_path).await,
"php" => self.function_names_for_php(file_path).await,
"swift" => self.function_names_for_swift(file_path).await,
"lua" => self.function_names_for_lua(file_path).await,
_ => self
.extract_function_names_heuristic(file_path, extension)
.await
.unwrap_or_default(),
};
// The per-language analyzers above return a MEASURED triple: how many
// functions they parsed, how many of those exceed the cyclomatic
// threshold, and the mean cyclomatic over them. Those three agree with
// each other by construction.
//
// This block used to throw two of them away whenever function-name
// extraction found anything, substituting `names.len() / 4` for the
// high-complexity count and `2.5` for a zero average. Both are
// constants wearing a measurement's clothes, and the ratio was visible
// in the report: `analyze deep-context` on this repo listed
//
// 1. comprehensive_assert_cmd_coverage.rs - 1.0 avg complexity
// (134 functions, 33 high complexity)
//
// — 134/4 = 33 "functions above complexity 10" in a file whose measured
// mean complexity is 1.0, which is arithmetically impossible. Repo-wide
// it fabricated 4352 high-complexity functions where the AST had found
// a small fraction of that.
//
// A measurement is now reported only when it was taken. The fix is
// scoped to the two values that were being invented: name extraction
// can say nothing about complexity, so it no longer sets
// `high_complexity_functions` or `avg_complexity`.
//
// It remains the better source for the *count*. For languages whose
// AST complexity pass is a stub in this build — Java, Ruby — the pass
// reports one function for a file that plainly declares two, while
// name extraction finds both. Preferring the pass's count there traded
// a fabrication for an undercount; both are wrong answers, and only
// the fabrication was the defect being fixed.
let function_count = if function_names.is_empty() {
function_count
} else {
function_names.len()
};
Ok(FileComplexityMetrics {
function_count,
high_complexity_functions,
avg_complexity,
function_names,
})
}
/// Detect self-admitted technical debt in the discovered source files.
///
/// Uses the same `SATDDetector` the SARIF path reaches through
/// `DeepContextAnalyzer`, so one command cannot report a defect in one
/// format and "no immediate recommendations" in another.
pub(super) fn detect_self_admitted_debt(
source_files: &[PathBuf],
) -> Vec<crate::services::satd_detector::TechnicalDebt> {
let detector = crate::services::satd_detector::SATDDetector::new();
let mut debts = Vec::new();
for file in source_files {
let Ok(content) = std::fs::read_to_string(file) else {
continue;
};
if let Ok(found) = detector.extract_from_content(&content, file) {
debts.extend(found);
}
}
// File-discovery order is not guaranteed; the report must be.
debts.sort_by(|a, b| (&a.file, a.line).cmp(&(&b.file, b.line)));
debts
}
/// Fold detected debt into the recommendation list.
///
/// "Code complexity looks good! No immediate recommendations." must not
/// survive alongside a detected defect — that sentence was the whole
/// contradiction users saw between `--format json` and `--format sarif`.
pub(super) fn with_debt_recommendations(
mut recommendations: Vec<String>,
debts: &[crate::services::satd_detector::TechnicalDebt],
) -> Vec<String> {
if debts.is_empty() {
return recommendations;
}
recommendations.retain(|r| !r.starts_with("Code complexity looks good"));
let sample: Vec<String> = debts
.iter()
.take(3)
.map(|d| format!("{}:{} {}", d.file.display(), d.line, d.text.trim()))
.collect();
recommendations.push(format!(
"Resolve {} self-admitted technical debt comment(s) (e.g. {})",
debts.len(),
sample.join("; ")
));
recommendations
}
/// Generate recommendations based on analysis
pub(super) fn generate_recommendations(&self, metrics: &ComplexityMetrics) -> Vec<String> {
let mut recommendations = Vec::new();
if metrics.high_complexity_count > 0 {
recommendations.push(format!(
"Consider refactoring {} high-complexity functions (complexity > 10)",
metrics.high_complexity_count
));
}
if metrics.avg_complexity > 5.0 {
recommendations.push(format!(
"Average function complexity is {:.1}, consider simplifying functions",
metrics.avg_complexity
));
}
if metrics.total_functions == 0 {
recommendations
.push("No functions detected - verify file discovery patterns".to_string());
}
if recommendations.is_empty() {
recommendations
.push("Code complexity looks good! No immediate recommendations.".to_string());
}
recommendations
}
/// Format report as JSON
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn format_as_json(&self, report: &SimpleAnalysisReport) -> Result<String> {
let json_report = serde_json::json!({
"summary": {
"file_count": report.file_count,
"analysis_duration_ms": report.analysis_duration.as_millis(),
"total_functions": report.complexity_metrics.total_functions,
"high_complexity_functions": report.complexity_metrics.high_complexity_count,
"avg_complexity": report.complexity_metrics.avg_complexity
},
"files": report.file_complexity_details.iter().map(|file| {
serde_json::json!({
"path": file.file_path.to_string_lossy(),
"function_count": file.function_count,
"high_complexity_functions": file.high_complexity_functions,
"avg_complexity": file.avg_complexity,
"complexity_score": file.complexity_score
})
}).collect::<Vec<_>>(),
"recommendations": report.recommendations
});
Ok(serde_json::to_string_pretty(&json_report)?)
}
/// Format report as Markdown
///
/// # Example
///
/// ```rust
/// use pmat::services::simple_deep_context::{SimpleDeepContext, SimpleAnalysisReport, ComplexityMetrics, FileComplexityDetail};
/// use std::path::PathBuf;
/// use std::time::Duration;
///
/// let analyzer = SimpleDeepContext::new();
/// let report = SimpleAnalysisReport {
/// file_count: 5,
/// analysis_duration: Duration::from_millis(500),
/// complexity_metrics: ComplexityMetrics {
/// total_functions: 25,
/// high_complexity_count: 3,
/// avg_complexity: 4.2,
/// },
/// recommendations: vec!["Consider refactoring 3 high-complexity functions".to_string()],
/// file_complexity_details: vec![
/// FileComplexityDetail {
/// file_path: PathBuf::from("src/main.rs"),
/// function_count: 10,
/// high_complexity_functions: 2,
/// avg_complexity: 5.5,
/// complexity_score: 8.5,
/// function_names: vec![],
/// },
/// FileComplexityDetail {
/// file_path: PathBuf::from("src/lib.rs"),
/// function_count: 15,
/// high_complexity_functions: 1,
/// avg_complexity: 3.8,
/// complexity_score: 7.2,
/// function_names: vec![],
/// },
/// ],
/// };
///
/// let output = analyzer.format_as_markdown(&report, 10);
///
/// assert!(output.contains("# Deep Context Analysis Report"));
/// assert!(output.contains("**Files Analyzed**: 5"));
/// assert!(output.contains("## Top Files by Complexity"));
/// assert!(output.contains("1. `main.rs` - 5.5 avg complexity"));
/// ```
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn format_as_markdown(&self, report: &SimpleAnalysisReport, top_files: usize) -> String {
let mut markdown = String::new();
markdown.push_str("# Deep Context Analysis Report\n\n");
markdown.push_str("## Summary\n\n");
markdown.push_str(&format!("- **Files Analyzed**: {}\n", report.file_count));
markdown.push_str(&format!(
"- **Analysis Duration**: {:?}\n",
report.analysis_duration
));
markdown.push_str(&format!(
"- **Total Functions**: {}\n",
report.complexity_metrics.total_functions
));
markdown.push_str(&format!(
"- **High Complexity Functions**: {}\n",
report.complexity_metrics.high_complexity_count
));
markdown.push_str(&format!(
"- **Average Complexity**: {:.1}\n\n",
report.complexity_metrics.avg_complexity
));
// Show top files by complexity
if !report.file_complexity_details.is_empty() {
markdown.push_str("## Top Files by Complexity\n\n");
// Sort files by complexity score (descending)
let mut sorted_files = report.file_complexity_details.clone();
sorted_files.sort_by(|a, b| {
b.complexity_score
.partial_cmp(&a.complexity_score)
.unwrap_or(std::cmp::Ordering::Equal)
});
// `if top_files == 0 { 10 }` contradicted the flag's own help text
// ("0 = all"): `--top-files 0` listed ten files while `--top-files
// 50` listed fifty. One authority: crate::cli::top_files_count.
let files_to_show = crate::cli::top_files_count(sorted_files.len(), top_files);
for (i, file_detail) in sorted_files.iter().take(files_to_show).enumerate() {
let filename = file_detail
.file_path
.file_name()
.and_then(|n| n.to_str())
.map_or_else(
|| file_detail.file_path.to_string_lossy().to_string(),
std::string::ToString::to_string,
);
markdown.push_str(&format!(
"{}. `{}` - {:.1} avg complexity ({} functions, {} high complexity)\n",
i + 1,
filename,
file_detail.avg_complexity,
file_detail.function_count,
file_detail.high_complexity_functions
));
}
markdown.push('\n');
}
markdown.push_str("## Recommendations\n\n");
for (i, rec) in report.recommendations.iter().enumerate() {
markdown.push_str(&format!("{}. {}\n", i + 1, rec));
}
markdown
}
}
#[cfg(test)]
mod top_files_zero_regression_tests {
//! `--top-files 0` is documented as "0 = all". This renderer read it as
//! "0 = ten", so `--top-files 0` listed FEWER files than `--top-files 50`.
use super::*;
fn report(files: usize) -> SimpleAnalysisReport {
SimpleAnalysisReport {
file_count: files,
analysis_duration: std::time::Duration::from_millis(1),
complexity_metrics: crate::services::simple_deep_context::types::ComplexityMetrics {
total_functions: files,
high_complexity_count: 0,
avg_complexity: 1.0,
},
recommendations: vec![],
file_complexity_details: (0..files)
.map(
|i| crate::services::simple_deep_context::types::FileComplexityDetail {
file_path: PathBuf::from(format!("src/f{i}.rs")),
function_count: 1,
high_complexity_functions: 0,
avg_complexity: 1.0,
complexity_score: (files - i) as f64,
function_names: vec![],
},
)
.collect(),
}
}
#[test]
fn zero_lists_every_file_not_ten() {
let report = report(25);
let analyzer = SimpleDeepContext::new();
let zero = analyzer.format_as_markdown(&report, 0);
let fifty = analyzer.format_as_markdown(&report, 50);
assert_eq!(zero, fifty, "0 must mean all, exactly as --help says");
assert!(
zero.contains("src/f24.rs") || zero.contains("f24.rs"),
"--top-files 0 stopped at ten files: {zero}"
);
// And a real limit still bites.
let three = analyzer.format_as_markdown(&report, 3);
assert!(!three.contains("f3.rs"), "--top-files 3 listed a 4th file");
}
}
#[cfg(test)]
mod discovery_regression_tests {
//! What `analyze deep-context` counts as "the project".
use super::*;
fn config(dir: &Path, exclude_patterns: Vec<String>) -> SimpleAnalysisConfig {
SimpleAnalysisConfig {
project_path: dir.to_path_buf(),
include_features: vec![],
include_patterns: vec![],
exclude_patterns,
enable_verbose: false,
}
}
#[tokio::test]
async fn gitignored_paths_are_not_analyzed() {
let dir = tempfile::tempdir().unwrap();
// `ignore` applies .gitignore only inside a git repository.
std::fs::create_dir_all(dir.path().join(".git")).unwrap();
std::fs::write(dir.path().join(".gitignore"), "worktrees/\n").unwrap();
std::fs::write(dir.path().join("main.rs"), "fn main() {}\n").unwrap();
std::fs::create_dir_all(dir.path().join("worktrees/copy")).unwrap();
std::fs::write(dir.path().join("worktrees/copy/main.rs"), "fn main() {}\n").unwrap();
let files = SimpleDeepContext::new()
.discover_source_files(&config(dir.path(), vec![]))
.await
.unwrap();
assert_eq!(
files.len(),
1,
"a gitignored duplicate checkout must not double the file count: {files:?}"
);
assert!(files[0].ends_with("main.rs"));
}
#[tokio::test]
async fn exclude_patterns_drop_matching_files() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("main.py"), "def main():\n pass\n").unwrap();
std::fs::write(dir.path().join("main.rs"), "fn main() {}\n").unwrap();
let all = SimpleDeepContext::new()
.discover_source_files(&config(dir.path(), vec![]))
.await
.unwrap();
assert_eq!(all.len(), 2);
let filtered = SimpleDeepContext::new()
.discover_source_files(&config(dir.path(), vec!["**/*.py".to_string()]))
.await
.unwrap();
assert_eq!(
filtered.len(),
1,
"--exclude-pattern '**/*.py' must drop the python file: {filtered:?}"
);
assert!(filtered[0].ends_with("main.rs"));
}
#[tokio::test]
async fn an_unparseable_exclude_pattern_is_reported_not_dropped() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("main.rs"), "fn main() {}\n").unwrap();
let err = SimpleDeepContext::new()
.discover_source_files(&config(dir.path(), vec!["[".to_string()]))
.await
.expect_err("an unparseable pattern must not silently filter nothing");
assert!(err.to_string().contains("--exclude-pattern"), "{err}");
}
#[tokio::test]
async fn a_detected_defect_reaches_the_json_and_markdown_reports() {
// corpus/poly reproduced this: `--format sarif` listed a High-severity
// "FIXME: broken" in main.py while `--format json` reported "Code
// complexity looks good! No immediate recommendations."
let dir = tempfile::tempdir().unwrap();
std::fs::write(
dir.path().join("main.py"),
"def f():\n # FIXME: broken\n return 1\n",
)
.unwrap();
let report = SimpleDeepContext::new()
.analyze(config(dir.path(), vec![]))
.await
.expect("analysis succeeds");
let recommendations = report.recommendations.join("\n");
assert!(
recommendations.contains("FIXME"),
"a detected defect must appear in the default report, got: {recommendations}"
);
assert!(
!recommendations.contains("Code complexity looks good"),
"must not claim all-clear next to a detected defect: {recommendations}"
);
}
}