Skip to main content

fallow_cli/
health_types.rs

1//! Health / complexity analysis report types.
2//!
3//! Separated from the `health` command module so that report formatters
4//! (which are compiled as part of both the lib and bin targets) can
5//! reference these types without pulling in binary-only dependencies.
6
7/// Result of complexity analysis for reporting.
8#[derive(Debug, serde::Serialize)]
9pub struct HealthReport {
10    /// Functions exceeding thresholds.
11    pub findings: Vec<HealthFinding>,
12    /// Summary statistics.
13    pub summary: HealthSummary,
14    /// Project-wide vital signs (always computed from available data).
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub vital_signs: Option<VitalSigns>,
17    /// Project-wide health score (only populated with `--score`).
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub health_score: Option<HealthScore>,
20    /// Per-file health scores (only populated with `--file-scores` or `--hotspots`).
21    #[serde(skip_serializing_if = "Vec::is_empty")]
22    pub file_scores: Vec<FileHealthScore>,
23    /// Hotspot entries (only populated with `--hotspots`).
24    #[serde(skip_serializing_if = "Vec::is_empty")]
25    pub hotspots: Vec<HotspotEntry>,
26    /// Hotspot analysis summary (only set with `--hotspots`).
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub hotspot_summary: Option<HotspotSummary>,
29    /// Ranked refactoring recommendations (only populated with `--targets`).
30    #[serde(skip_serializing_if = "Vec::is_empty")]
31    pub targets: Vec<RefactoringTarget>,
32    /// Adaptive thresholds used for target scoring (only set with `--targets`).
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub target_thresholds: Option<TargetThresholds>,
35    /// Health trend comparison against a previous snapshot (only set with `--trend`).
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub health_trend: Option<HealthTrend>,
38}
39
40/// Project-level health score: a single 0–100 number with letter grade.
41///
42/// ## Score Formula
43///
44/// ```text
45/// score = 100
46///   - min(dead_file_pct × 0.2, 15)
47///   - min(dead_export_pct × 0.2, 15)
48///   - min(max(0, avg_cyclomatic − 1.5) × 5, 20)
49///   - min(max(0, p90_cyclomatic − 10), 10)
50///   - min(max(0, 70 − maintainability_avg) × 0.5, 15)
51///   - min(hotspot_count / total_files × 200, 10)
52///   - min(unused_dep_count, 10)
53///   - min(circular_dep_count, 10)
54/// ```
55///
56/// Missing metrics (from pipelines that didn't run) don't penalize — run
57/// `--score` (which forces full pipeline) for the most accurate result.
58///
59/// ## Letter Grades
60///
61/// A: score ≥ 85, B: 70–84, C: 55–69, D: 40–54, F: below 40.
62#[derive(Debug, Clone, serde::Serialize)]
63pub struct HealthScore {
64    /// Overall score (0–100, higher is better).
65    pub score: f64,
66    /// Letter grade: A, B, C, D, or F.
67    pub grade: &'static str,
68    /// Per-component penalty breakdown. Shows what drove the score down.
69    pub penalties: HealthScorePenalties,
70}
71
72/// Per-component penalty breakdown for the health score.
73///
74/// Each field shows how many points were subtracted for that component.
75/// `None` means the metric was not available (pipeline didn't run).
76#[derive(Debug, Clone, serde::Serialize)]
77pub struct HealthScorePenalties {
78    /// Points lost from dead files (max 15).
79    #[serde(skip_serializing_if = "Option::is_none")]
80    pub dead_files: Option<f64>,
81    /// Points lost from dead exports (max 15).
82    #[serde(skip_serializing_if = "Option::is_none")]
83    pub dead_exports: Option<f64>,
84    /// Points lost from average cyclomatic complexity (max 20).
85    pub complexity: f64,
86    /// Points lost from p90 cyclomatic complexity (max 10).
87    pub p90_complexity: f64,
88    /// Points lost from low maintainability index (max 15).
89    #[serde(skip_serializing_if = "Option::is_none")]
90    pub maintainability: Option<f64>,
91    /// Points lost from hotspot files (max 10).
92    #[serde(skip_serializing_if = "Option::is_none")]
93    pub hotspots: Option<f64>,
94    /// Points lost from unused dependencies (max 10).
95    #[serde(skip_serializing_if = "Option::is_none")]
96    pub unused_deps: Option<f64>,
97    /// Points lost from circular dependencies (max 10).
98    #[serde(skip_serializing_if = "Option::is_none")]
99    pub circular_deps: Option<f64>,
100}
101
102/// Map a numeric score (0–100) to a letter grade.
103#[must_use]
104#[expect(
105    clippy::cast_possible_truncation,
106    reason = "score is 0-100, fits in u32"
107)]
108pub const fn letter_grade(score: f64) -> &'static str {
109    // Truncate to u32 so that 84.9 maps to B and 85.0 maps to A —
110    // fractional digits don't affect the grade bucket.
111    let s = score as u32;
112    if s >= 85 {
113        "A"
114    } else if s >= 70 {
115        "B"
116    } else if s >= 55 {
117        "C"
118    } else if s >= 40 {
119        "D"
120    } else {
121        "F"
122    }
123}
124
125/// Project-wide vital signs — a fixed set of metrics for trend tracking.
126///
127/// Metrics are `Option` when the data source was not available in the current run
128/// (e.g., `duplication_pct` is `None` unless the duplication pipeline was run,
129/// `hotspot_count` is `None` without git history).
130#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
131pub struct VitalSigns {
132    /// Percentage of files not reachable from any entry point.
133    #[serde(skip_serializing_if = "Option::is_none")]
134    pub dead_file_pct: Option<f64>,
135    /// Percentage of exports never imported by other modules.
136    #[serde(skip_serializing_if = "Option::is_none")]
137    pub dead_export_pct: Option<f64>,
138    /// Average cyclomatic complexity across all functions.
139    pub avg_cyclomatic: f64,
140    /// 90th percentile cyclomatic complexity.
141    pub p90_cyclomatic: u32,
142    /// Code duplication percentage (None if duplication pipeline was not run).
143    #[serde(skip_serializing_if = "Option::is_none")]
144    pub duplication_pct: Option<f64>,
145    /// Number of hotspot files (score >= 50). None if git history unavailable.
146    #[serde(skip_serializing_if = "Option::is_none")]
147    pub hotspot_count: Option<u32>,
148    /// Average maintainability index across all scored files (0–100).
149    #[serde(skip_serializing_if = "Option::is_none")]
150    pub maintainability_avg: Option<f64>,
151    /// Number of unused dependencies (dependencies + devDependencies + optional).
152    #[serde(skip_serializing_if = "Option::is_none")]
153    pub unused_dep_count: Option<u32>,
154    /// Number of circular dependency chains.
155    #[serde(skip_serializing_if = "Option::is_none")]
156    pub circular_dep_count: Option<u32>,
157}
158
159/// Raw counts backing the vital signs percentages.
160///
161/// Stored alongside `VitalSigns` in snapshots so that Phase 2b trend reporting
162/// can decompose percentage changes into numerator vs denominator shifts.
163#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
164pub struct VitalSignsCounts {
165    pub total_files: usize,
166    pub total_exports: usize,
167    pub dead_files: usize,
168    pub dead_exports: usize,
169    #[serde(skip_serializing_if = "Option::is_none")]
170    pub duplicated_lines: Option<usize>,
171    #[serde(skip_serializing_if = "Option::is_none")]
172    pub total_lines: Option<usize>,
173    #[serde(skip_serializing_if = "Option::is_none")]
174    pub files_scored: Option<usize>,
175    pub total_deps: usize,
176}
177
178/// A point-in-time snapshot of project vital signs, persisted to disk.
179#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
180pub struct VitalSignsSnapshot {
181    /// Schema version for snapshot format (independent of report schema_version).
182    pub snapshot_schema_version: u32,
183    /// Fallow version that produced this snapshot.
184    pub version: String,
185    /// ISO 8601 timestamp.
186    pub timestamp: String,
187    /// Git commit SHA at time of snapshot (None if not in a git repo).
188    #[serde(skip_serializing_if = "Option::is_none")]
189    pub git_sha: Option<String>,
190    /// Git branch name (None if not in a git repo or detached HEAD).
191    #[serde(skip_serializing_if = "Option::is_none")]
192    pub git_branch: Option<String>,
193    /// Whether the repository is a shallow clone.
194    #[serde(default)]
195    pub shallow_clone: bool,
196    /// The vital signs metrics.
197    pub vital_signs: VitalSigns,
198    /// Raw counts for trend decomposition.
199    pub counts: VitalSignsCounts,
200    /// Project health score (0–100). Added in schema v2.
201    #[serde(skip_serializing_if = "Option::is_none", default)]
202    pub score: Option<f64>,
203    /// Letter grade (A/B/C/D/F). Added in schema v2.
204    #[serde(skip_serializing_if = "Option::is_none", default)]
205    pub grade: Option<String>,
206}
207
208/// Current snapshot schema version. Independent of the report's SCHEMA_VERSION.
209/// v2: Added `score` and `grade` fields.
210pub const SNAPSHOT_SCHEMA_VERSION: u32 = 2;
211
212// ---------------------------------------------------------------------------
213// Trend types — comparing current run against a saved snapshot.
214// ---------------------------------------------------------------------------
215
216/// Health trend comparison: current run vs. a previous snapshot.
217#[derive(Debug, Clone, serde::Serialize)]
218pub struct HealthTrend {
219    /// The snapshot being compared against.
220    pub compared_to: TrendPoint,
221    /// Per-metric deltas.
222    pub metrics: Vec<TrendMetric>,
223    /// Number of snapshots found in the snapshot directory.
224    pub snapshots_loaded: usize,
225    /// Overall direction across all metrics.
226    pub overall_direction: TrendDirection,
227}
228
229/// A reference to a snapshot used in trend comparison.
230#[derive(Debug, Clone, serde::Serialize)]
231pub struct TrendPoint {
232    /// ISO 8601 timestamp of the snapshot.
233    pub timestamp: String,
234    /// Git SHA at time of snapshot.
235    #[serde(skip_serializing_if = "Option::is_none")]
236    pub git_sha: Option<String>,
237    /// Health score from the snapshot (stored, not re-derived).
238    #[serde(skip_serializing_if = "Option::is_none")]
239    pub score: Option<f64>,
240    /// Letter grade from the snapshot.
241    #[serde(skip_serializing_if = "Option::is_none")]
242    pub grade: Option<String>,
243}
244
245/// A single metric's trend between two snapshots.
246#[derive(Debug, Clone, serde::Serialize)]
247pub struct TrendMetric {
248    /// Metric identifier (e.g., `"score"`, `"dead_file_pct"`).
249    pub name: &'static str,
250    /// Human-readable label (e.g., `"Health Score"`, `"Dead Files"`).
251    pub label: &'static str,
252    /// Previous value (from snapshot).
253    pub previous: f64,
254    /// Current value (from this run).
255    pub current: f64,
256    /// Absolute change (current − previous).
257    pub delta: f64,
258    /// Direction of change.
259    pub direction: TrendDirection,
260    /// Unit for display (e.g., `"%"`, `""`, `"pts"`).
261    pub unit: &'static str,
262    /// Raw count from previous snapshot (for JSON consumers).
263    #[serde(skip_serializing_if = "Option::is_none")]
264    pub previous_count: Option<TrendCount>,
265    /// Raw count from current run (for JSON consumers).
266    #[serde(skip_serializing_if = "Option::is_none")]
267    pub current_count: Option<TrendCount>,
268}
269
270/// Raw numerator/denominator for a percentage metric.
271#[derive(Debug, Clone, serde::Serialize)]
272pub struct TrendCount {
273    /// The numerator (e.g., dead files count).
274    pub value: usize,
275    /// The denominator (e.g., total files).
276    pub total: usize,
277}
278
279/// Direction of a metric's change, semantically (improving/declining/stable).
280#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
281#[serde(rename_all = "snake_case")]
282pub enum TrendDirection {
283    /// The metric moved in a beneficial direction.
284    Improving,
285    /// The metric moved in a detrimental direction.
286    Declining,
287    /// The metric stayed within tolerance.
288    Stable,
289}
290
291impl TrendDirection {
292    /// Arrow symbol for terminal output.
293    #[must_use]
294    pub const fn arrow(self) -> &'static str {
295        match self {
296            Self::Improving => "\u{2191}", // ↑
297            Self::Declining => "\u{2193}", // ↓
298            Self::Stable => "\u{2192}",    // →
299        }
300    }
301
302    /// Human-readable label.
303    #[must_use]
304    pub const fn label(self) -> &'static str {
305        match self {
306            Self::Improving => "improving",
307            Self::Declining => "declining",
308            Self::Stable => "stable",
309        }
310    }
311}
312
313/// Hotspot score threshold for counting a file as a hotspot in vital signs.
314pub const HOTSPOT_SCORE_THRESHOLD: f64 = 50.0;
315
316/// A single function that exceeds a complexity threshold.
317#[derive(Debug, serde::Serialize)]
318pub struct HealthFinding {
319    /// Absolute file path.
320    pub path: std::path::PathBuf,
321    /// Function name.
322    pub name: String,
323    /// 1-based line number.
324    pub line: u32,
325    /// 0-based column.
326    pub col: u32,
327    /// Cyclomatic complexity.
328    pub cyclomatic: u16,
329    /// Cognitive complexity.
330    pub cognitive: u16,
331    /// Number of lines in the function.
332    pub line_count: u32,
333    /// Which threshold was exceeded.
334    pub exceeded: ExceededThreshold,
335}
336
337/// Which complexity threshold was exceeded.
338#[derive(Debug, serde::Serialize)]
339#[serde(rename_all = "snake_case")]
340pub enum ExceededThreshold {
341    /// Only cyclomatic exceeded.
342    Cyclomatic,
343    /// Only cognitive exceeded.
344    Cognitive,
345    /// Both thresholds exceeded.
346    Both,
347}
348
349/// Summary statistics for the health report.
350#[derive(Debug, serde::Serialize)]
351pub struct HealthSummary {
352    /// Number of files analyzed.
353    pub files_analyzed: usize,
354    /// Total number of functions found.
355    pub functions_analyzed: usize,
356    /// Number of functions above threshold.
357    pub functions_above_threshold: usize,
358    /// Configured cyclomatic threshold.
359    pub max_cyclomatic_threshold: u16,
360    /// Configured cognitive threshold.
361    pub max_cognitive_threshold: u16,
362    /// Number of files scored (only set with `--file-scores`).
363    #[serde(skip_serializing_if = "Option::is_none")]
364    pub files_scored: Option<usize>,
365    /// Average maintainability index across all scored files (only set with `--file-scores`).
366    #[serde(skip_serializing_if = "Option::is_none")]
367    pub average_maintainability: Option<f64>,
368}
369
370/// Per-file health score combining complexity, coupling, and dead code metrics.
371///
372/// Files with zero functions (barrel files, re-export files) are excluded by default.
373///
374/// ## Maintainability Index Formula
375///
376/// ```text
377/// fan_out_penalty = min(ln(fan_out + 1) × 4, 15)
378/// maintainability = 100
379///     - (complexity_density × 30)
380///     - (dead_code_ratio × 20)
381///     - fan_out_penalty
382/// ```
383///
384/// Clamped to \[0, 100\]. Higher is better.
385///
386/// - **complexity_density**: total cyclomatic complexity / lines of code
387/// - **dead_code_ratio**: fraction of value exports (excluding type-only exports) with zero references (0.0–1.0)
388/// - **fan_out_penalty**: logarithmic scaling with cap at 15 points; reflects diminishing marginal risk of additional imports
389#[derive(Debug, Clone, serde::Serialize)]
390pub struct FileHealthScore {
391    /// File path (absolute; stripped to relative in output).
392    pub path: std::path::PathBuf,
393    /// Number of files that import this file.
394    pub fan_in: usize,
395    /// Number of files this file imports.
396    pub fan_out: usize,
397    /// Fraction of value exports with zero references (0.0–1.0). Files with no value exports get 0.0.
398    /// Type-only exports (interfaces, type aliases) are excluded from both numerator and denominator
399    /// to avoid inflating the ratio for well-typed codebases that export props types alongside components.
400    pub dead_code_ratio: f64,
401    /// Total cyclomatic complexity / lines of code.
402    pub complexity_density: f64,
403    /// Weighted composite score (0–100, higher is better).
404    pub maintainability_index: f64,
405    /// Sum of cyclomatic complexity across all functions.
406    pub total_cyclomatic: u32,
407    /// Sum of cognitive complexity across all functions.
408    pub total_cognitive: u32,
409    /// Number of functions in this file.
410    pub function_count: usize,
411    /// Total lines of code (from line_offsets).
412    pub lines: u32,
413}
414
415/// A hotspot: a file that is both complex and frequently changing.
416///
417/// ## Score Formula
418///
419/// ```text
420/// normalized_churn = weighted_commits / max_weighted_commits   (0..1)
421/// normalized_complexity = complexity_density / max_density      (0..1)
422/// score = normalized_churn × normalized_complexity × 100       (0..100)
423/// ```
424///
425/// Score uses within-project max normalization. Higher score = higher risk.
426/// Fan-in is shown separately as "blast radius" — not baked into the score.
427#[derive(Debug, Clone, serde::Serialize)]
428pub struct HotspotEntry {
429    /// File path (absolute; stripped to relative in output).
430    pub path: std::path::PathBuf,
431    /// Hotspot score (0–100). Higher means more risk.
432    pub score: f64,
433    /// Number of commits in the analysis window.
434    pub commits: u32,
435    /// Recency-weighted commit count (exponential decay, half-life 90 days).
436    pub weighted_commits: f64,
437    /// Total lines added across all commits.
438    pub lines_added: u32,
439    /// Total lines deleted across all commits.
440    pub lines_deleted: u32,
441    /// Cyclomatic complexity / lines of code.
442    pub complexity_density: f64,
443    /// Number of files that import this file (blast radius).
444    pub fan_in: usize,
445    /// Churn trend: accelerating, stable, or cooling.
446    pub trend: fallow_core::churn::ChurnTrend,
447}
448
449/// Summary statistics for hotspot analysis.
450#[derive(Debug, serde::Serialize)]
451pub struct HotspotSummary {
452    /// Analysis window display string (e.g., "6 months").
453    pub since: String,
454    /// Minimum commits threshold.
455    pub min_commits: u32,
456    /// Number of files with churn data meeting the threshold.
457    pub files_analyzed: usize,
458    /// Number of files excluded (below min_commits).
459    pub files_excluded: usize,
460    /// Whether the repository is a shallow clone.
461    pub shallow_clone: bool,
462}
463
464/// Adaptive thresholds used for refactoring target scoring.
465///
466/// Derived from the project's metric distribution (percentile-based with floors).
467/// Exposed in JSON output so consumers can interpret scores in context.
468#[derive(Debug, Clone, serde::Serialize)]
469#[allow(clippy::struct_field_names)] // triggered in bin but not lib — #[expect] would fail in lib
470pub struct TargetThresholds {
471    /// Fan-in saturation point for priority formula (p95, floor 5).
472    pub fan_in_p95: f64,
473    /// Fan-in moderate threshold for contributing factors (p75, floor 3).
474    pub fan_in_p75: f64,
475    /// Fan-out saturation point for priority formula (p95, floor 8).
476    pub fan_out_p95: f64,
477    /// Fan-out high threshold for rules and contributing factors (p90, floor 5).
478    pub fan_out_p90: usize,
479}
480
481/// Category of refactoring recommendation.
482#[derive(Debug, Clone, serde::Serialize)]
483#[serde(rename_all = "snake_case")]
484pub enum RecommendationCategory {
485    /// Actively-changing file with growing complexity — highest urgency.
486    UrgentChurnComplexity,
487    /// File participates in an import cycle with significant blast radius.
488    BreakCircularDependency,
489    /// High fan-in + high complexity — changes here ripple widely.
490    SplitHighImpact,
491    /// Majority of exports are unused — reduce surface area.
492    RemoveDeadCode,
493    /// Contains functions with very high cognitive complexity.
494    ExtractComplexFunctions,
495    /// Excessive imports reduce testability and increase coupling.
496    ExtractDependencies,
497}
498
499impl RecommendationCategory {
500    /// Human-readable label for terminal output.
501    #[must_use]
502    pub const fn label(&self) -> &'static str {
503        match self {
504            Self::UrgentChurnComplexity => "churn+complexity",
505            Self::BreakCircularDependency => "circular dep",
506            Self::SplitHighImpact => "high impact",
507            Self::RemoveDeadCode => "dead code",
508            Self::ExtractComplexFunctions => "complexity",
509            Self::ExtractDependencies => "coupling",
510        }
511    }
512
513    /// Machine-parseable label for compact output (no spaces).
514    #[must_use]
515    pub const fn compact_label(&self) -> &'static str {
516        match self {
517            Self::UrgentChurnComplexity => "churn_complexity",
518            Self::BreakCircularDependency => "circular_dep",
519            Self::SplitHighImpact => "high_impact",
520            Self::RemoveDeadCode => "dead_code",
521            Self::ExtractComplexFunctions => "complexity",
522            Self::ExtractDependencies => "coupling",
523        }
524    }
525}
526
527/// A contributing factor that triggered or strengthened a recommendation.
528#[derive(Debug, Clone, serde::Serialize)]
529pub struct ContributingFactor {
530    /// Metric name (matches JSON field names: `"fan_in"`, `"dead_code_ratio"`, etc.).
531    pub metric: &'static str,
532    /// Raw metric value for programmatic use.
533    pub value: f64,
534    /// Threshold that was exceeded.
535    pub threshold: f64,
536    /// Human-readable explanation.
537    pub detail: String,
538}
539
540/// A ranked refactoring recommendation for a file.
541///
542/// ## Priority Formula
543///
544/// ```text
545/// priority = min(density, 1) × 30 + hotspot_boost × 25 + dead_code × 20 + fan_in_norm × 15 + fan_out_norm × 10
546/// ```
547///
548/// Fan-in and fan-out normalization uses adaptive percentile-based thresholds
549/// (p95 of the project distribution, with floors) instead of fixed constants.
550///
551/// ## Efficiency (default sort)
552///
553/// ```text
554/// efficiency = priority / effort_numeric   (Low=1, Medium=2, High=3)
555/// ```
556///
557/// Surfaces quick wins: high-priority, low-effort targets rank first.
558/// Effort estimate for a refactoring target.
559#[derive(Debug, Clone, serde::Serialize)]
560#[serde(rename_all = "snake_case")]
561pub enum EffortEstimate {
562    /// Small file, few functions, low fan-in — quick to address.
563    Low,
564    /// Moderate size or coupling — needs planning.
565    Medium,
566    /// Large file, many functions, or high fan-in — significant effort.
567    High,
568}
569
570impl EffortEstimate {
571    /// Human-readable label for terminal output.
572    #[must_use]
573    pub const fn label(&self) -> &'static str {
574        match self {
575            Self::Low => "low",
576            Self::Medium => "medium",
577            Self::High => "high",
578        }
579    }
580
581    /// Numeric value for arithmetic (efficiency = priority / effort).
582    #[must_use]
583    pub const fn numeric(&self) -> f64 {
584        match self {
585            Self::Low => 1.0,
586            Self::Medium => 2.0,
587            Self::High => 3.0,
588        }
589    }
590}
591
592/// Confidence level for a refactoring recommendation.
593///
594/// Based on the data source reliability:
595/// - **High**: deterministic graph/AST analysis (dead code, circular deps, complexity)
596/// - **Medium**: heuristic thresholds (fan-in/fan-out coupling)
597/// - **Low**: depends on git history quality (churn-based recommendations)
598#[derive(Debug, Clone, serde::Serialize)]
599#[serde(rename_all = "snake_case")]
600pub enum Confidence {
601    /// Recommendation based on deterministic analysis (graph, AST).
602    High,
603    /// Recommendation based on heuristic thresholds.
604    Medium,
605    /// Recommendation depends on external data quality (git history).
606    Low,
607}
608
609impl Confidence {
610    /// Human-readable label for terminal output.
611    #[must_use]
612    pub const fn label(&self) -> &'static str {
613        match self {
614            Self::High => "high",
615            Self::Medium => "medium",
616            Self::Low => "low",
617        }
618    }
619}
620
621/// Evidence linking a target back to specific analysis data.
622///
623/// Provides enough detail for an AI agent to act on a recommendation
624/// without a second tool call.
625#[derive(Debug, Clone, serde::Serialize)]
626pub struct TargetEvidence {
627    /// Names of unused exports (populated for `RemoveDeadCode` targets).
628    #[serde(skip_serializing_if = "Vec::is_empty")]
629    pub unused_exports: Vec<String>,
630    /// Complex functions with line numbers and cognitive scores (populated for `ExtractComplexFunctions`).
631    #[serde(skip_serializing_if = "Vec::is_empty")]
632    pub complex_functions: Vec<EvidenceFunction>,
633    /// Files forming the import cycle (populated for `BreakCircularDependency` targets).
634    #[serde(skip_serializing_if = "Vec::is_empty")]
635    pub cycle_path: Vec<String>,
636}
637
638/// A function referenced in target evidence.
639#[derive(Debug, Clone, serde::Serialize)]
640pub struct EvidenceFunction {
641    /// Function name.
642    pub name: String,
643    /// 1-based line number.
644    pub line: u32,
645    /// Cognitive complexity score.
646    pub cognitive: u16,
647}
648
649#[derive(Debug, Clone, serde::Serialize)]
650pub struct RefactoringTarget {
651    /// Absolute file path (stripped to relative in output).
652    pub path: std::path::PathBuf,
653    /// Priority score (0–100, higher = more urgent).
654    pub priority: f64,
655    /// Efficiency score (priority / effort). Higher = better quick-win value.
656    /// Surfaces low-effort, high-priority targets first.
657    pub efficiency: f64,
658    /// One-line actionable recommendation.
659    pub recommendation: String,
660    /// Recommendation category for tooling/filtering.
661    pub category: RecommendationCategory,
662    /// Estimated effort to address this target.
663    pub effort: EffortEstimate,
664    /// Confidence in this recommendation based on data source reliability.
665    pub confidence: Confidence,
666    /// Which metric values contributed to this recommendation.
667    #[serde(skip_serializing_if = "Vec::is_empty")]
668    pub factors: Vec<ContributingFactor>,
669    /// Structured evidence linking to specific analysis data.
670    #[serde(skip_serializing_if = "Option::is_none")]
671    pub evidence: Option<TargetEvidence>,
672}
673
674#[cfg(test)]
675mod tests {
676    use super::*;
677
678    // --- RecommendationCategory ---
679
680    #[test]
681    fn category_labels_are_non_empty() {
682        let categories = [
683            RecommendationCategory::UrgentChurnComplexity,
684            RecommendationCategory::BreakCircularDependency,
685            RecommendationCategory::SplitHighImpact,
686            RecommendationCategory::RemoveDeadCode,
687            RecommendationCategory::ExtractComplexFunctions,
688            RecommendationCategory::ExtractDependencies,
689        ];
690        for cat in &categories {
691            assert!(!cat.label().is_empty(), "{cat:?} should have a label");
692        }
693    }
694
695    #[test]
696    fn category_labels_are_unique() {
697        let categories = [
698            RecommendationCategory::UrgentChurnComplexity,
699            RecommendationCategory::BreakCircularDependency,
700            RecommendationCategory::SplitHighImpact,
701            RecommendationCategory::RemoveDeadCode,
702            RecommendationCategory::ExtractComplexFunctions,
703            RecommendationCategory::ExtractDependencies,
704        ];
705        let labels: Vec<&str> = categories
706            .iter()
707            .map(super::RecommendationCategory::label)
708            .collect();
709        let unique: rustc_hash::FxHashSet<&&str> = labels.iter().collect();
710        assert_eq!(labels.len(), unique.len(), "category labels must be unique");
711    }
712
713    // --- Serde serialization ---
714
715    #[test]
716    fn category_serializes_as_snake_case() {
717        let json = serde_json::to_string(&RecommendationCategory::UrgentChurnComplexity).unwrap();
718        assert_eq!(json, r#""urgent_churn_complexity""#);
719
720        let json = serde_json::to_string(&RecommendationCategory::BreakCircularDependency).unwrap();
721        assert_eq!(json, r#""break_circular_dependency""#);
722    }
723
724    #[test]
725    fn exceeded_threshold_serializes_as_snake_case() {
726        let json = serde_json::to_string(&ExceededThreshold::Both).unwrap();
727        assert_eq!(json, r#""both""#);
728
729        let json = serde_json::to_string(&ExceededThreshold::Cyclomatic).unwrap();
730        assert_eq!(json, r#""cyclomatic""#);
731    }
732
733    #[test]
734    fn health_report_skips_empty_collections() {
735        let report = HealthReport {
736            findings: vec![],
737            summary: HealthSummary {
738                files_analyzed: 0,
739                functions_analyzed: 0,
740                functions_above_threshold: 0,
741                max_cyclomatic_threshold: 20,
742                max_cognitive_threshold: 15,
743                files_scored: None,
744                average_maintainability: None,
745            },
746            vital_signs: None,
747            health_score: None,
748            file_scores: vec![],
749            hotspots: vec![],
750            hotspot_summary: None,
751            targets: vec![],
752            target_thresholds: None,
753            health_trend: None,
754        };
755        let json = serde_json::to_string(&report).unwrap();
756        // Empty vecs should be omitted due to skip_serializing_if
757        assert!(!json.contains("file_scores"));
758        assert!(!json.contains("hotspots"));
759        assert!(!json.contains("hotspot_summary"));
760        assert!(!json.contains("targets"));
761        assert!(!json.contains("vital_signs"));
762        assert!(!json.contains("health_score"));
763    }
764
765    #[test]
766    fn vital_signs_serialization_roundtrip() {
767        let vs = VitalSigns {
768            dead_file_pct: Some(3.2),
769            dead_export_pct: Some(8.1),
770            avg_cyclomatic: 4.7,
771            p90_cyclomatic: 12,
772            duplication_pct: None,
773            hotspot_count: Some(5),
774            maintainability_avg: Some(72.4),
775            unused_dep_count: Some(4),
776            circular_dep_count: Some(2),
777        };
778        let json = serde_json::to_string(&vs).unwrap();
779        let deserialized: VitalSigns = serde_json::from_str(&json).unwrap();
780        assert!((deserialized.avg_cyclomatic - 4.7).abs() < f64::EPSILON);
781        assert_eq!(deserialized.p90_cyclomatic, 12);
782        assert_eq!(deserialized.hotspot_count, Some(5));
783        // duplication_pct should be absent in JSON and None after deser
784        assert!(!json.contains("duplication_pct"));
785        assert!(deserialized.duplication_pct.is_none());
786    }
787
788    #[test]
789    fn vital_signs_snapshot_roundtrip() {
790        let snapshot = VitalSignsSnapshot {
791            snapshot_schema_version: SNAPSHOT_SCHEMA_VERSION,
792            version: "1.8.1".into(),
793            timestamp: "2026-03-25T14:30:00Z".into(),
794            git_sha: Some("abc1234".into()),
795            git_branch: Some("main".into()),
796            shallow_clone: false,
797            vital_signs: VitalSigns {
798                dead_file_pct: Some(3.2),
799                dead_export_pct: Some(8.1),
800                avg_cyclomatic: 4.7,
801                p90_cyclomatic: 12,
802                duplication_pct: None,
803                hotspot_count: None,
804                maintainability_avg: Some(72.4),
805                unused_dep_count: Some(4),
806                circular_dep_count: Some(2),
807            },
808            counts: VitalSignsCounts {
809                total_files: 1200,
810                total_exports: 5400,
811                dead_files: 38,
812                dead_exports: 437,
813                duplicated_lines: None,
814                total_lines: None,
815                files_scored: Some(1150),
816                total_deps: 42,
817            },
818            score: Some(78.5),
819            grade: Some("B".into()),
820        };
821        let json = serde_json::to_string_pretty(&snapshot).unwrap();
822        let rt: VitalSignsSnapshot = serde_json::from_str(&json).unwrap();
823        assert_eq!(rt.snapshot_schema_version, SNAPSHOT_SCHEMA_VERSION);
824        assert_eq!(rt.git_sha.as_deref(), Some("abc1234"));
825        assert_eq!(rt.counts.total_files, 1200);
826        assert_eq!(rt.counts.dead_exports, 437);
827        assert_eq!(rt.score, Some(78.5));
828        assert_eq!(rt.grade.as_deref(), Some("B"));
829    }
830
831    #[test]
832    fn refactoring_target_skips_empty_factors() {
833        let target = RefactoringTarget {
834            path: std::path::PathBuf::from("/src/foo.ts"),
835            priority: 75.0,
836            efficiency: 75.0,
837            recommendation: "Test recommendation".into(),
838            category: RecommendationCategory::RemoveDeadCode,
839            effort: EffortEstimate::Low,
840            confidence: Confidence::High,
841            factors: vec![],
842            evidence: None,
843        };
844        let json = serde_json::to_string(&target).unwrap();
845        assert!(!json.contains("factors"));
846        assert!(!json.contains("evidence"));
847    }
848
849    #[test]
850    fn effort_numeric_values() {
851        assert!((EffortEstimate::Low.numeric() - 1.0).abs() < f64::EPSILON);
852        assert!((EffortEstimate::Medium.numeric() - 2.0).abs() < f64::EPSILON);
853        assert!((EffortEstimate::High.numeric() - 3.0).abs() < f64::EPSILON);
854    }
855
856    #[test]
857    fn confidence_labels_are_non_empty() {
858        let levels = [Confidence::High, Confidence::Medium, Confidence::Low];
859        for level in &levels {
860            assert!(!level.label().is_empty(), "{level:?} should have a label");
861        }
862    }
863
864    #[test]
865    fn confidence_serializes_as_snake_case() {
866        let json = serde_json::to_string(&Confidence::High).unwrap();
867        assert_eq!(json, r#""high""#);
868        let json = serde_json::to_string(&Confidence::Medium).unwrap();
869        assert_eq!(json, r#""medium""#);
870        let json = serde_json::to_string(&Confidence::Low).unwrap();
871        assert_eq!(json, r#""low""#);
872    }
873
874    #[test]
875    fn contributing_factor_serializes_correctly() {
876        let factor = ContributingFactor {
877            metric: "fan_in",
878            value: 15.0,
879            threshold: 10.0,
880            detail: "15 files depend on this".into(),
881        };
882        let json = serde_json::to_string(&factor).unwrap();
883        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
884        assert_eq!(parsed["metric"], "fan_in");
885        assert_eq!(parsed["value"], 15.0);
886        assert_eq!(parsed["threshold"], 10.0);
887    }
888
889    // --- RecommendationCategory compact_labels ---
890
891    #[test]
892    fn category_compact_labels_are_non_empty() {
893        let categories = [
894            RecommendationCategory::UrgentChurnComplexity,
895            RecommendationCategory::BreakCircularDependency,
896            RecommendationCategory::SplitHighImpact,
897            RecommendationCategory::RemoveDeadCode,
898            RecommendationCategory::ExtractComplexFunctions,
899            RecommendationCategory::ExtractDependencies,
900        ];
901        for cat in &categories {
902            assert!(
903                !cat.compact_label().is_empty(),
904                "{cat:?} should have a compact_label"
905            );
906        }
907    }
908
909    #[test]
910    fn category_compact_labels_are_unique() {
911        let categories = [
912            RecommendationCategory::UrgentChurnComplexity,
913            RecommendationCategory::BreakCircularDependency,
914            RecommendationCategory::SplitHighImpact,
915            RecommendationCategory::RemoveDeadCode,
916            RecommendationCategory::ExtractComplexFunctions,
917            RecommendationCategory::ExtractDependencies,
918        ];
919        let labels: Vec<&str> = categories
920            .iter()
921            .map(RecommendationCategory::compact_label)
922            .collect();
923        let unique: rustc_hash::FxHashSet<&&str> = labels.iter().collect();
924        assert_eq!(labels.len(), unique.len(), "compact labels must be unique");
925    }
926
927    #[test]
928    fn category_compact_labels_have_no_spaces() {
929        let categories = [
930            RecommendationCategory::UrgentChurnComplexity,
931            RecommendationCategory::BreakCircularDependency,
932            RecommendationCategory::SplitHighImpact,
933            RecommendationCategory::RemoveDeadCode,
934            RecommendationCategory::ExtractComplexFunctions,
935            RecommendationCategory::ExtractDependencies,
936        ];
937        for cat in &categories {
938            assert!(
939                !cat.compact_label().contains(' '),
940                "compact_label for {:?} should not contain spaces: '{}'",
941                cat,
942                cat.compact_label()
943            );
944        }
945    }
946
947    // --- EffortEstimate ---
948
949    #[test]
950    fn effort_labels_are_non_empty() {
951        let efforts = [
952            EffortEstimate::Low,
953            EffortEstimate::Medium,
954            EffortEstimate::High,
955        ];
956        for effort in &efforts {
957            assert!(!effort.label().is_empty(), "{effort:?} should have a label");
958        }
959    }
960
961    #[test]
962    fn effort_serializes_as_snake_case() {
963        assert_eq!(
964            serde_json::to_string(&EffortEstimate::Low).unwrap(),
965            r#""low""#
966        );
967        assert_eq!(
968            serde_json::to_string(&EffortEstimate::Medium).unwrap(),
969            r#""medium""#
970        );
971        assert_eq!(
972            serde_json::to_string(&EffortEstimate::High).unwrap(),
973            r#""high""#
974        );
975    }
976
977    // --- VitalSigns omits None fields ---
978
979    #[test]
980    fn vital_signs_all_none_optional_fields_omitted() {
981        let vs = VitalSigns {
982            dead_file_pct: None,
983            dead_export_pct: None,
984            avg_cyclomatic: 5.0,
985            p90_cyclomatic: 10,
986            duplication_pct: None,
987            hotspot_count: None,
988            maintainability_avg: None,
989            unused_dep_count: None,
990            circular_dep_count: None,
991        };
992        let json = serde_json::to_string(&vs).unwrap();
993        assert!(!json.contains("dead_file_pct"));
994        assert!(!json.contains("dead_export_pct"));
995        assert!(!json.contains("duplication_pct"));
996        assert!(!json.contains("hotspot_count"));
997        assert!(!json.contains("maintainability_avg"));
998        assert!(!json.contains("unused_dep_count"));
999        assert!(!json.contains("circular_dep_count"));
1000        // Required fields always present
1001        assert!(json.contains("avg_cyclomatic"));
1002        assert!(json.contains("p90_cyclomatic"));
1003    }
1004
1005    // --- ExceededThreshold ---
1006
1007    #[test]
1008    fn exceeded_threshold_all_variants_serialize() {
1009        for variant in [
1010            ExceededThreshold::Cyclomatic,
1011            ExceededThreshold::Cognitive,
1012            ExceededThreshold::Both,
1013        ] {
1014            let json = serde_json::to_string(&variant).unwrap();
1015            assert!(!json.is_empty());
1016        }
1017    }
1018
1019    // --- TargetEvidence ---
1020
1021    #[test]
1022    fn target_evidence_skips_empty_fields() {
1023        let evidence = TargetEvidence {
1024            unused_exports: vec![],
1025            complex_functions: vec![],
1026            cycle_path: vec![],
1027        };
1028        let json = serde_json::to_string(&evidence).unwrap();
1029        assert!(!json.contains("unused_exports"));
1030        assert!(!json.contains("complex_functions"));
1031        assert!(!json.contains("cycle_path"));
1032    }
1033
1034    #[test]
1035    fn target_evidence_with_data() {
1036        let evidence = TargetEvidence {
1037            unused_exports: vec!["foo".to_string(), "bar".to_string()],
1038            complex_functions: vec![EvidenceFunction {
1039                name: "processData".into(),
1040                line: 42,
1041                cognitive: 30,
1042            }],
1043            cycle_path: vec![],
1044        };
1045        let json = serde_json::to_string(&evidence).unwrap();
1046        assert!(json.contains("unused_exports"));
1047        assert!(json.contains("complex_functions"));
1048        assert!(json.contains("processData"));
1049        assert!(!json.contains("cycle_path"));
1050    }
1051
1052    // --- VitalSignsSnapshot schema version ---
1053
1054    #[test]
1055    fn snapshot_schema_version_is_two() {
1056        assert_eq!(SNAPSHOT_SCHEMA_VERSION, 2);
1057    }
1058
1059    #[test]
1060    fn hotspot_score_threshold_is_50() {
1061        assert!((HOTSPOT_SCORE_THRESHOLD - 50.0).abs() < f64::EPSILON);
1062    }
1063
1064    #[test]
1065    fn snapshot_v1_deserializes_with_default_score_and_grade() {
1066        // A v1 snapshot without score/grade fields must still deserialize
1067        let json = r#"{
1068            "snapshot_schema_version": 1,
1069            "version": "1.5.0",
1070            "timestamp": "2025-01-01T00:00:00Z",
1071            "shallow_clone": false,
1072            "vital_signs": {
1073                "avg_cyclomatic": 2.0,
1074                "p90_cyclomatic": 5
1075            },
1076            "counts": {
1077                "total_files": 100,
1078                "total_exports": 500,
1079                "dead_files": 0,
1080                "dead_exports": 0,
1081                "total_deps": 20
1082            }
1083        }"#;
1084        let snap: VitalSignsSnapshot = serde_json::from_str(json).unwrap();
1085        assert!(snap.score.is_none());
1086        assert!(snap.grade.is_none());
1087        assert_eq!(snap.snapshot_schema_version, 1);
1088    }
1089
1090    // --- letter_grade ---
1091
1092    #[test]
1093    fn letter_grade_boundaries() {
1094        assert_eq!(letter_grade(100.0), "A");
1095        assert_eq!(letter_grade(85.0), "A");
1096        assert_eq!(letter_grade(84.9), "B");
1097        assert_eq!(letter_grade(70.0), "B");
1098        assert_eq!(letter_grade(69.9), "C");
1099        assert_eq!(letter_grade(55.0), "C");
1100        assert_eq!(letter_grade(54.9), "D");
1101        assert_eq!(letter_grade(40.0), "D");
1102        assert_eq!(letter_grade(39.9), "F");
1103        assert_eq!(letter_grade(0.0), "F");
1104    }
1105
1106    // --- HealthScore ---
1107
1108    #[test]
1109    fn health_score_serializes_correctly() {
1110        let score = HealthScore {
1111            score: 78.5,
1112            grade: "B",
1113            penalties: HealthScorePenalties {
1114                dead_files: Some(3.1),
1115                dead_exports: Some(6.0),
1116                complexity: 0.0,
1117                p90_complexity: 0.0,
1118                maintainability: None,
1119                hotspots: None,
1120                unused_deps: Some(5.0),
1121                circular_deps: Some(4.0),
1122            },
1123        };
1124        let json = serde_json::to_string(&score).unwrap();
1125        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
1126        assert_eq!(parsed["score"], 78.5);
1127        assert_eq!(parsed["grade"], "B");
1128        assert_eq!(parsed["penalties"]["dead_files"], 3.1);
1129        // None fields should be absent
1130        assert!(!json.contains("maintainability"));
1131        assert!(!json.contains("hotspots"));
1132    }
1133
1134    #[test]
1135    fn health_score_none_skipped_in_report() {
1136        let report = HealthReport {
1137            findings: vec![],
1138            summary: HealthSummary {
1139                files_analyzed: 0,
1140                functions_analyzed: 0,
1141                functions_above_threshold: 0,
1142                max_cyclomatic_threshold: 20,
1143                max_cognitive_threshold: 15,
1144                files_scored: None,
1145                average_maintainability: None,
1146            },
1147            vital_signs: None,
1148            health_score: None,
1149            file_scores: vec![],
1150            hotspots: vec![],
1151            hotspot_summary: None,
1152            targets: vec![],
1153            target_thresholds: None,
1154            health_trend: None,
1155        };
1156        let json = serde_json::to_string(&report).unwrap();
1157        assert!(!json.contains("health_score"));
1158    }
1159}