fallow-cli 2.10.0

CLI for the fallow TypeScript/JavaScript codebase analyzer
Documentation
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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
use crate::health_types::{
    Confidence, ContributingFactor, EffortEstimate, EvidenceFunction, FileHealthScore,
    HotspotEntry, RecommendationCategory, RefactoringTarget, TargetEvidence, TargetThresholds,
};

/// Auxiliary data used by `compute_refactoring_targets` to generate evidence and apply rules.
pub(super) struct TargetAuxData<'a> {
    pub circular_files: &'a rustc_hash::FxHashSet<std::path::PathBuf>,
    pub top_complex_fns: &'a rustc_hash::FxHashMap<std::path::PathBuf, Vec<(String, u32, u16)>>,
    pub entry_points: &'a rustc_hash::FxHashSet<std::path::PathBuf>,
    pub value_export_counts: &'a rustc_hash::FxHashMap<std::path::PathBuf, usize>,
    pub unused_export_names: &'a rustc_hash::FxHashMap<std::path::PathBuf, Vec<String>>,
    pub cycle_members: &'a rustc_hash::FxHashMap<std::path::PathBuf, Vec<std::path::PathBuf>>,
}

impl<'a> From<&'a super::scoring::FileScoreOutput> for TargetAuxData<'a> {
    fn from(output: &'a super::scoring::FileScoreOutput) -> Self {
        Self {
            circular_files: &output.circular_files,
            top_complex_fns: &output.top_complex_fns,
            entry_points: &output.entry_points,
            value_export_counts: &output.value_export_counts,
            unused_export_names: &output.unused_export_names,
            cycle_members: &output.cycle_members,
        }
    }
}

/// Adaptive thresholds derived from the project's metric distribution.
///
/// Replaces hardcoded constants (fan_in=20, fan_out=30) with percentile-based
/// values that adapt to the codebase size. Floors prevent degenerate thresholds
/// in small projects.
#[expect(
    clippy::struct_field_names,
    reason = "fan_in/fan_out prefix clarifies the metric"
)]
struct DistributionThresholds {
    /// Fan-in saturation point for priority formula (p95, floor 5).
    fan_in_p95: f64,
    /// Fan-in "moderate" threshold for contributing factors and rule 3 (p75, floor 3).
    fan_in_p75: f64,
    /// Fan-in "low" ceiling for effort estimation (p25, floor 2).
    fan_in_p25: usize,
    /// Fan-out saturation point for priority formula (p95, floor 8).
    fan_out_p95: f64,
    /// Fan-out "high" threshold for contributing factors and rule 6 (p90, floor 5).
    fan_out_p90: usize,
}

/// Compute percentile-based thresholds from the file score distribution.
#[expect(
    clippy::cast_possible_truncation,
    reason = "percentile values are bounded by fan-in/fan-out counts"
)]
fn compute_thresholds(file_scores: &[FileHealthScore]) -> DistributionThresholds {
    if file_scores.is_empty() {
        return DistributionThresholds {
            fan_in_p95: 5.0,
            fan_in_p75: 3.0,
            fan_in_p25: 2,
            fan_out_p95: 8.0,
            fan_out_p90: 5,
        };
    }

    let mut fan_ins: Vec<usize> = file_scores.iter().map(|s| s.fan_in).collect();
    let mut fan_outs: Vec<usize> = file_scores.iter().map(|s| s.fan_out).collect();
    fan_ins.sort_unstable();
    fan_outs.sort_unstable();

    DistributionThresholds {
        fan_in_p95: percentile_usize(&fan_ins, 0.95).max(5.0),
        fan_in_p75: percentile_usize(&fan_ins, 0.75).max(3.0),
        fan_in_p25: (percentile_usize(&fan_ins, 0.25) as usize).max(2),
        fan_out_p95: percentile_usize(&fan_outs, 0.95).max(8.0),
        fan_out_p90: (percentile_usize(&fan_outs, 0.90) as usize).max(5),
    }
}

/// Compute a percentile value from a sorted slice of usize values.
#[expect(
    clippy::cast_possible_truncation,
    reason = "index from percentile of slice length is bounded by slice length"
)]
fn percentile_usize(sorted: &[usize], p: f64) -> f64 {
    if sorted.is_empty() {
        return 0.0;
    }
    let idx = (sorted.len() as f64 * p).ceil() as usize;
    let idx = idx.min(sorted.len()) - 1;
    sorted[idx] as f64
}

/// Compute the refactoring priority score for a file.
///
/// Formula (avoids double-counting with MI):
/// ```text
/// priority = min(density, 1) * 30 + hotspot_boost * 25 + dead_code * 20 + fan_in_norm * 15 + fan_out_norm * 10
/// ```
/// Fan-in and fan-out normalization uses adaptive percentile-based thresholds.
/// All inputs are clamped to \[0, 1\] so each weight is a true percentage share.
fn compute_target_priority(
    score: &FileHealthScore,
    hotspot_score: Option<f64>,
    thresholds: &DistributionThresholds,
) -> f64 {
    // Normalize all inputs to [0, 1] so each weight is a true percentage share.
    let density_norm = score.complexity_density.min(1.0);
    let fan_in_norm = (score.fan_in as f64 / thresholds.fan_in_p95).min(1.0);
    let fan_out_norm = (score.fan_out as f64 / thresholds.fan_out_p95).min(1.0);
    let hotspot_boost = hotspot_score.map_or(0.0, |s| s / 100.0);

    #[expect(
        clippy::suboptimal_flops,
        reason = "formula matches documented specification"
    )]
    let priority = density_norm * 30.0
        + hotspot_boost * 25.0
        + score.dead_code_ratio * 20.0
        + fan_in_norm * 15.0
        + fan_out_norm * 10.0;

    (priority.clamp(0.0, 100.0) * 10.0).round() / 10.0
}

/// Compute refactoring targets by applying rules to file scores and auxiliary data.
///
/// Rules are evaluated in priority order; first match determines the category and
/// recommendation. All contributing factors are collected regardless of which rule wins.
/// Files matching no rule are skipped.
///
/// Targets are sorted by efficiency (priority / effort) descending to surface quick wins first.
#[expect(
    clippy::cast_possible_truncation,
    reason = "f64 percentile and ratio values are bounded by collection sizes"
)]
pub(super) fn compute_refactoring_targets(
    file_scores: &[FileHealthScore],
    aux: &TargetAuxData,
    hotspots: &[HotspotEntry],
) -> (Vec<RefactoringTarget>, TargetThresholds) {
    // Compute adaptive thresholds from the project's distribution
    let thresholds = compute_thresholds(file_scores);

    // Build hotspot lookup by path for O(1) access
    let hotspot_map: rustc_hash::FxHashMap<&std::path::Path, &HotspotEntry> =
        hotspots.iter().map(|h| (h.path.as_path(), h)).collect();

    let mut targets = Vec::new();

    for score in file_scores {
        let hotspot = hotspot_map.get(score.path.as_path());
        let hotspot_score = hotspot.map(|h| h.score);
        let is_circular = aux.circular_files.contains(&score.path);
        let is_entry = aux.entry_points.contains(&score.path);
        let top_fns = aux.top_complex_fns.get(&score.path);
        let value_exports = aux
            .value_export_counts
            .get(&score.path)
            .copied()
            .unwrap_or(0);

        // Collect all contributing factors (using adaptive thresholds)
        let mut factors = Vec::new();

        if score.complexity_density > 0.3 {
            factors.push(ContributingFactor {
                metric: "complexity_density",
                value: score.complexity_density,
                threshold: 0.3,
                detail: format!("density {:.2} exceeds 0.3", score.complexity_density),
            });
        }
        if score.fan_in as f64 >= thresholds.fan_in_p75 {
            factors.push(ContributingFactor {
                metric: "fan_in",
                value: score.fan_in as f64,
                threshold: thresholds.fan_in_p75,
                detail: format!("{} files depend on this", score.fan_in),
            });
        }
        if score.dead_code_ratio >= 0.5 && value_exports >= 3 {
            let unused_count = (score.dead_code_ratio * value_exports as f64)
                .round()
                .min(value_exports as f64) as usize;
            factors.push(ContributingFactor {
                metric: "dead_code_ratio",
                value: score.dead_code_ratio,
                threshold: 0.5,
                detail: format!(
                    "{} unused of {} value exports ({:.0}%)",
                    unused_count,
                    value_exports,
                    score.dead_code_ratio * 100.0
                ),
            });
        }
        if score.fan_out >= thresholds.fan_out_p90 {
            factors.push(ContributingFactor {
                metric: "fan_out",
                value: score.fan_out as f64,
                threshold: thresholds.fan_out_p90 as f64,
                detail: format!("imports {} modules", score.fan_out),
            });
        }
        if is_circular {
            factors.push(ContributingFactor {
                metric: "circular_dependency",
                value: 1.0,
                threshold: 1.0,
                detail: "participates in an import cycle".into(),
            });
        }
        if let Some(h) = hotspot
            && h.score >= 30.0
        {
            factors.push(ContributingFactor {
                metric: "hotspot_score",
                value: h.score,
                threshold: 30.0,
                detail: format!(
                    "hotspot score {:.0} ({} commits, {} trend)",
                    h.score,
                    h.commits,
                    match h.trend {
                        fallow_core::churn::ChurnTrend::Accelerating => "accelerating",
                        fallow_core::churn::ChurnTrend::Cooling => "cooling",
                        fallow_core::churn::ChurnTrend::Stable => "stable",
                    }
                ),
            });
        }
        if let Some(fns) = top_fns
            && let Some((name, _, cog)) = fns.first()
            && *cog >= 30
        {
            factors.push(ContributingFactor {
                metric: "cognitive_complexity",
                value: f64::from(*cog),
                threshold: 30.0,
                detail: format!("{name} has cognitive complexity {cog}"),
            });
        }

        // Skip if no factors triggered
        if factors.is_empty() {
            continue;
        }

        // Evaluate rules in priority order — first match determines category + recommendation
        let matched = try_match_rules(
            score,
            hotspot.copied(),
            is_circular,
            is_entry,
            top_fns,
            value_exports,
            &thresholds,
        );

        let Some((category, recommendation)) = matched else {
            continue;
        };

        let priority = compute_target_priority(score, hotspot_score, &thresholds);
        let effort = compute_effort_estimate(score, &thresholds);
        let confidence = confidence_for_category(&category);
        let efficiency = (priority / effort.numeric() * 10.0).round() / 10.0;
        let evidence = build_evidence(
            &category,
            &score.path,
            aux.unused_export_names,
            top_fns,
            aux.cycle_members,
        );

        targets.push(RefactoringTarget {
            path: score.path.clone(),
            priority,
            efficiency,
            recommendation,
            category,
            effort,
            confidence,
            factors,
            evidence,
        });
    }

    // Sort by efficiency descending (quick wins first), break ties by priority desc, then path
    targets.sort_by(|a, b| {
        b.efficiency
            .partial_cmp(&a.efficiency)
            .unwrap_or(std::cmp::Ordering::Equal)
            .then_with(|| {
                b.priority
                    .partial_cmp(&a.priority)
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
            .then_with(|| a.path.cmp(&b.path))
    });

    let exported_thresholds = TargetThresholds {
        fan_in_p95: thresholds.fan_in_p95,
        fan_in_p75: thresholds.fan_in_p75,
        fan_out_p95: thresholds.fan_out_p95,
        fan_out_p90: thresholds.fan_out_p90,
    };

    (targets, exported_thresholds)
}

/// Try to match a file against refactoring rules in priority order.
///
/// Returns the first matching `(category, recommendation)`, or `None` if no rule matches.
#[expect(
    clippy::cast_possible_truncation,
    reason = "threshold values and export counts are bounded by project size"
)]
fn try_match_rules(
    score: &FileHealthScore,
    hotspot: Option<&HotspotEntry>,
    is_circular: bool,
    is_entry: bool,
    top_fns: Option<&Vec<(String, u32, u16)>>,
    value_exports: usize,
    thresholds: &DistributionThresholds,
) -> Option<(RecommendationCategory, String)> {
    // Rule 1: Urgent churn + complexity
    if let Some(h) = hotspot
        && h.score >= 50.0
        && matches!(h.trend, fallow_core::churn::ChurnTrend::Accelerating)
        && score.complexity_density > 0.5
    {
        return Some((
            RecommendationCategory::UrgentChurnComplexity,
            "Actively-changing file with growing complexity \u{2014} stabilize before adding features".into(),
        ));
    }

    // Rule 2: Circular dependency with high fan-in
    if is_circular && score.fan_in >= 5 {
        return Some((
            RecommendationCategory::BreakCircularDependency,
            format!(
                "Break import cycle \u{2014} {} files depend on this, changes cascade through the cycle",
                score.fan_in
            ),
        ));
    }

    // Rule 3: Split high-impact file (adaptive fan-in thresholds)
    let fan_in_high = thresholds.fan_in_p95 as usize;
    let fan_in_moderate = thresholds.fan_in_p75 as usize;
    if score.complexity_density > 0.3
        && (score.fan_in >= fan_in_high
            || (score.fan_in >= fan_in_moderate && score.function_count >= 5))
    {
        return Some((
            RecommendationCategory::SplitHighImpact,
            format!(
                "Split high-impact file \u{2014} {} dependents amplify every change",
                score.fan_in
            ),
        ));
    }

    // Rule 4: Remove dead code (gate: >=3 value exports)
    if score.dead_code_ratio >= 0.5 && value_exports >= 3 {
        let unused_count = (score.dead_code_ratio * value_exports as f64).round() as usize;
        return Some((
            RecommendationCategory::RemoveDeadCode,
            format!(
                "Remove {} unused exports to reduce surface area ({:.0}% dead)",
                unused_count,
                score.dead_code_ratio * 100.0
            ),
        ));
    }

    // Rule 5: Extract complex functions (cognitive >= 30)
    if let Some(fns) = top_fns {
        let high: Vec<&(String, u32, u16)> = fns.iter().filter(|(_, _, cog)| *cog >= 30).collect();
        if !high.is_empty() {
            let desc = match high.len() {
                1 => format!(
                    "Extract {} (cognitive: {}) into smaller functions",
                    high[0].0, high[0].2
                ),
                _ => format!(
                    "Extract {} (cognitive: {}) and {} (cognitive: {}) into smaller functions",
                    high[0].0, high[0].2, high[1].0, high[1].2
                ),
            };
            return Some((RecommendationCategory::ExtractComplexFunctions, desc));
        }
    }

    // Rule 6: Extract dependencies (not for entry points, adaptive fan-out threshold)
    if !is_entry && score.fan_out >= thresholds.fan_out_p90 && score.maintainability_index < 60.0 {
        return Some((
            RecommendationCategory::ExtractDependencies,
            format!(
                "Reduce coupling \u{2014} this file imports {} modules, limiting testability",
                score.fan_out
            ),
        ));
    }

    // Rule 7: Circular dependency (low fan-in fallback)
    if is_circular {
        return Some((
            RecommendationCategory::BreakCircularDependency,
            "Break import cycle to reduce change cascade risk".into(),
        ));
    }

    None
}

/// Map recommendation category to confidence level based on data source reliability.
const fn confidence_for_category(category: &RecommendationCategory) -> Confidence {
    match category {
        // Deterministic: graph analysis (dead code, cycles) + AST analysis (complexity)
        RecommendationCategory::RemoveDeadCode
        | RecommendationCategory::BreakCircularDependency
        | RecommendationCategory::ExtractComplexFunctions => Confidence::High,
        // Heuristic thresholds (fan-in/fan-out coupling)
        RecommendationCategory::SplitHighImpact | RecommendationCategory::ExtractDependencies => {
            Confidence::Medium
        }
        // Depends on git history quality
        RecommendationCategory::UrgentChurnComplexity => Confidence::Low,
    }
}

/// Compute effort estimate based on file size, function count, and fan-in.
///
/// Uses adaptive thresholds for fan-in.
#[expect(
    clippy::cast_possible_truncation,
    reason = "percentile threshold values are bounded by project size"
)]
fn compute_effort_estimate(
    score: &FileHealthScore,
    thresholds: &DistributionThresholds,
) -> EffortEstimate {
    let fan_in_high = thresholds.fan_in_p95 as usize;
    if score.lines >= 500
        || score.fan_in >= fan_in_high
        || (score.function_count >= 15 && score.complexity_density > 0.5)
    {
        EffortEstimate::High
    } else if score.lines < 100 && score.function_count <= 3 && score.fan_in < thresholds.fan_in_p25
    {
        EffortEstimate::Low
    } else {
        EffortEstimate::Medium
    }
}

/// Build structured evidence for a refactoring target based on its category.
fn build_evidence(
    category: &RecommendationCategory,
    path: &std::path::Path,
    unused_export_names: &rustc_hash::FxHashMap<std::path::PathBuf, Vec<String>>,
    top_fns: Option<&Vec<(String, u32, u16)>>,
    cycle_members: &rustc_hash::FxHashMap<std::path::PathBuf, Vec<std::path::PathBuf>>,
) -> Option<TargetEvidence> {
    match category {
        RecommendationCategory::RemoveDeadCode => {
            let exports = unused_export_names.get(path).cloned().unwrap_or_default();
            if exports.is_empty() {
                None
            } else {
                Some(TargetEvidence {
                    unused_exports: exports,
                    complex_functions: vec![],
                    cycle_path: vec![],
                })
            }
        }
        RecommendationCategory::ExtractComplexFunctions => {
            let functions = top_fns
                .map(|fns| {
                    fns.iter()
                        .filter(|(_, _, cog)| *cog >= 30)
                        .map(|(name, line, cog)| EvidenceFunction {
                            name: name.clone(),
                            line: *line,
                            cognitive: *cog,
                        })
                        .collect::<Vec<_>>()
                })
                .unwrap_or_default();
            if functions.is_empty() {
                None
            } else {
                Some(TargetEvidence {
                    unused_exports: vec![],
                    complex_functions: functions,
                    cycle_path: vec![],
                })
            }
        }
        RecommendationCategory::BreakCircularDependency => {
            let members = cycle_members
                .get(path)
                .map(|files| {
                    files
                        .iter()
                        .map(|f| f.to_string_lossy().into_owned())
                        .collect::<Vec<_>>()
                })
                .unwrap_or_default();
            if members.is_empty() {
                None
            } else {
                Some(TargetEvidence {
                    unused_exports: vec![],
                    complex_functions: vec![],
                    cycle_path: members,
                })
            }
        }
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // --- helpers ---

    fn make_score(overrides: impl FnOnce(&mut FileHealthScore)) -> FileHealthScore {
        let mut s = FileHealthScore {
            path: std::path::PathBuf::from("/src/foo.ts"),
            fan_in: 0,
            fan_out: 0,
            dead_code_ratio: 0.0,
            complexity_density: 0.0,
            maintainability_index: 100.0,
            total_cyclomatic: 0,
            total_cognitive: 0,
            function_count: 1,
            lines: 100,
        };
        overrides(&mut s);
        s
    }

    /// Default thresholds matching the old hardcoded values for test compatibility.
    fn default_thresholds() -> DistributionThresholds {
        DistributionThresholds {
            fan_in_p95: 20.0,
            fan_in_p75: 10.0,
            fan_in_p25: 5,
            fan_out_p95: 30.0,
            fan_out_p90: 15,
        }
    }

    // --- compute_thresholds ---

    #[test]
    fn thresholds_empty_scores_use_floors() {
        let t = compute_thresholds(&[]);
        assert!((t.fan_in_p95 - 5.0).abs() < f64::EPSILON);
        assert!((t.fan_in_p75 - 3.0).abs() < f64::EPSILON);
        assert_eq!(t.fan_in_p25, 2);
        assert!((t.fan_out_p95 - 8.0).abs() < f64::EPSILON);
        assert_eq!(t.fan_out_p90, 5);
    }

    #[test]
    fn thresholds_floors_prevent_degenerate_values() {
        // All files have fan_in=1, fan_out=1 — floors should kick in
        let scores: Vec<FileHealthScore> = (0..10)
            .map(|i| {
                make_score(|s| {
                    s.path = std::path::PathBuf::from(format!("/src/{i}.ts"));
                    s.fan_in = 1;
                    s.fan_out = 1;
                })
            })
            .collect();
        let t = compute_thresholds(&scores);
        assert!(t.fan_in_p95 >= 5.0, "floor should apply: {}", t.fan_in_p95);
        assert!(t.fan_in_p75 >= 3.0, "floor should apply: {}", t.fan_in_p75);
        assert!(
            t.fan_out_p95 >= 8.0,
            "floor should apply: {}",
            t.fan_out_p95
        );
        assert!(t.fan_out_p90 >= 5, "floor should apply: {}", t.fan_out_p90);
    }

    #[test]
    fn thresholds_adapt_to_large_project() {
        // Simulate a project with varied fan_in distribution including high values
        let mut scores: Vec<FileHealthScore> = (0..80)
            .map(|i| {
                make_score(|s| {
                    s.path = std::path::PathBuf::from(format!("/src/{i}.ts"));
                    s.fan_in = i % 5; // 0..4
                    s.fan_out = i % 8; // 0..7
                })
            })
            .collect();
        // Top 20% have higher fan_in — enough to push p95 above floors
        for i in 80..100 {
            scores.push(make_score(|s| {
                s.path = std::path::PathBuf::from(format!("/src/{i}.ts"));
                s.fan_in = 15 + (i - 80); // 15..34
                s.fan_out = 10 + (i - 80); // 10..29
            }));
        }
        let t = compute_thresholds(&scores);
        // p95 should reflect the distribution, not just the floor
        assert!(
            t.fan_in_p95 > 5.0,
            "p95 should exceed floor: {}",
            t.fan_in_p95
        );
        assert!(
            t.fan_out_p95 > 8.0,
            "p95 should exceed floor: {}",
            t.fan_out_p95
        );
    }

    // --- compute_target_priority ---

    #[test]
    fn target_priority_all_zero() {
        let score = make_score(|_| {});
        let t = default_thresholds();
        let priority = compute_target_priority(&score, None, &t);
        assert!((priority).abs() < f64::EPSILON);
    }

    #[test]
    fn target_priority_max_all_inputs() {
        let score = make_score(|s| {
            s.complexity_density = 2.0; // clamped to 1.0
            s.fan_in = 40; // clamped to 1.0
            s.fan_out = 60; // clamped to 1.0
            s.dead_code_ratio = 1.0;
        });
        let t = default_thresholds();
        let priority = compute_target_priority(&score, Some(100.0), &t);
        assert!((priority - 100.0).abs() < f64::EPSILON);
    }

    #[test]
    fn target_priority_complexity_density_weight() {
        // density=1.0, all else zero -> 30 points
        let score = make_score(|s| s.complexity_density = 1.0);
        let t = default_thresholds();
        let priority = compute_target_priority(&score, None, &t);
        assert!((priority - 30.0).abs() < f64::EPSILON);
    }

    #[test]
    fn target_priority_hotspot_weight() {
        // hotspot_score=100 -> boost=1.0 -> 25 points
        let score = make_score(|_| {});
        let t = default_thresholds();
        let priority = compute_target_priority(&score, Some(100.0), &t);
        assert!((priority - 25.0).abs() < f64::EPSILON);
    }

    #[test]
    fn target_priority_dead_code_weight() {
        // dead_code_ratio=1.0 -> 20 points
        let score = make_score(|s| s.dead_code_ratio = 1.0);
        let t = default_thresholds();
        let priority = compute_target_priority(&score, None, &t);
        assert!((priority - 20.0).abs() < f64::EPSILON);
    }

    #[test]
    fn target_priority_fan_in_weight() {
        // fan_in=20 (== p95) -> norm=1.0 -> 15 points
        let score = make_score(|s| s.fan_in = 20);
        let t = default_thresholds();
        let priority = compute_target_priority(&score, None, &t);
        assert!((priority - 15.0).abs() < f64::EPSILON);
    }

    #[test]
    fn target_priority_fan_out_weight() {
        // fan_out=30 (== p95) -> norm=1.0 -> 10 points
        let score = make_score(|s| s.fan_out = 30);
        let t = default_thresholds();
        let priority = compute_target_priority(&score, None, &t);
        assert!((priority - 10.0).abs() < f64::EPSILON);
    }

    #[test]
    fn target_priority_adapts_to_thresholds() {
        let score = make_score(|s| s.fan_in = 10);
        // With threshold=20: norm=0.5 -> 7.5 points
        let t_default = default_thresholds();
        let p1 = compute_target_priority(&score, None, &t_default);
        // With threshold=10: norm=1.0 -> 15 points
        let t_small = DistributionThresholds {
            fan_in_p95: 10.0,
            ..default_thresholds()
        };
        let p2 = compute_target_priority(&score, None, &t_small);
        assert!(
            p2 > p1,
            "smaller project threshold should yield higher priority"
        );
    }

    // --- confidence ---

    #[test]
    fn confidence_mapping() {
        assert!(matches!(
            confidence_for_category(&RecommendationCategory::RemoveDeadCode),
            Confidence::High
        ));
        assert!(matches!(
            confidence_for_category(&RecommendationCategory::BreakCircularDependency),
            Confidence::High
        ));
        assert!(matches!(
            confidence_for_category(&RecommendationCategory::ExtractComplexFunctions),
            Confidence::High
        ));
        assert!(matches!(
            confidence_for_category(&RecommendationCategory::SplitHighImpact),
            Confidence::Medium
        ));
        assert!(matches!(
            confidence_for_category(&RecommendationCategory::ExtractDependencies),
            Confidence::Medium
        ));
        assert!(matches!(
            confidence_for_category(&RecommendationCategory::UrgentChurnComplexity),
            Confidence::Low
        ));
    }

    // --- efficiency ---

    #[test]
    fn efficiency_surfaces_quick_wins() {
        // Low effort, high priority should have higher efficiency than high effort, high priority
        let low_effort_priority = 60.0_f64;
        let high_effort_priority = 90.0_f64;
        let low_eff = low_effort_priority / EffortEstimate::Low.numeric();
        let high_eff = high_effort_priority / EffortEstimate::High.numeric();
        assert!(
            low_eff > high_eff,
            "low effort (eff={low_eff}) should rank above high effort (eff={high_eff})"
        );
    }

    #[test]
    fn targets_sorted_by_efficiency_descending() {
        // High-priority + high-effort (eff ~30) vs low-priority + low-effort (eff ~20)
        // The low-effort file should appear first because efficiency = priority/effort
        let scores = vec![
            make_score(|s| {
                s.path = std::path::PathBuf::from("/src/big.ts");
                s.complexity_density = 0.8;
                s.fan_in = 25;
                s.lines = 600;
                s.function_count = 20;
                s.dead_code_ratio = 0.6;
            }),
            make_score(|s| {
                s.path = std::path::PathBuf::from("/src/small.ts");
                s.dead_code_ratio = 0.7;
                s.lines = 50;
                s.function_count = 2;
                s.fan_in = 1;
            }),
        ];
        let aux = TargetAuxData {
            circular_files: &rustc_hash::FxHashSet::default(),
            top_complex_fns: &rustc_hash::FxHashMap::default(),
            entry_points: &rustc_hash::FxHashSet::default(),
            value_export_counts: &[
                (std::path::PathBuf::from("/src/big.ts"), 10_usize),
                (std::path::PathBuf::from("/src/small.ts"), 5_usize),
            ]
            .into_iter()
            .collect(),
            unused_export_names: &rustc_hash::FxHashMap::default(),
            cycle_members: &rustc_hash::FxHashMap::default(),
        };
        let (targets, _thresholds) = compute_refactoring_targets(&scores, &aux, &[]);
        assert!(targets.len() >= 2, "expected at least 2 targets");
        // First target should have higher efficiency (quick win)
        assert!(
            targets[0].efficiency >= targets[1].efficiency,
            "targets should be sorted by efficiency desc: {} >= {}",
            targets[0].efficiency,
            targets[1].efficiency
        );
    }

    // --- try_match_rules ---

    #[test]
    fn rule_no_match_clean_file() {
        let score = make_score(|_| {});
        let t = default_thresholds();
        let result = try_match_rules(&score, None, false, false, None, 0, &t);
        assert!(result.is_none());
    }

    #[test]
    fn rule_circular_dep_high_fan_in() {
        let score = make_score(|s| s.fan_in = 5);
        let t = default_thresholds();
        let result = try_match_rules(&score, None, true, false, None, 0, &t);
        assert!(result.is_some());
        let (cat, _) = result.unwrap();
        assert!(matches!(
            cat,
            RecommendationCategory::BreakCircularDependency
        ));
    }

    #[test]
    fn rule_circular_dep_low_fan_in_fallback() {
        let score = make_score(|s| s.fan_in = 1);
        let t = default_thresholds();
        let result = try_match_rules(&score, None, true, false, None, 0, &t);
        assert!(result.is_some());
        let (cat, _) = result.unwrap();
        assert!(matches!(
            cat,
            RecommendationCategory::BreakCircularDependency
        ));
    }

    #[test]
    fn rule_split_high_impact() {
        let score = make_score(|s| {
            s.complexity_density = 0.5;
            s.fan_in = 20;
        });
        let t = default_thresholds();
        let result = try_match_rules(&score, None, false, false, None, 0, &t);
        assert!(result.is_some());
        let (cat, _) = result.unwrap();
        assert!(matches!(cat, RecommendationCategory::SplitHighImpact));
    }

    #[test]
    fn rule_remove_dead_code() {
        let score = make_score(|s| s.dead_code_ratio = 0.6);
        let t = default_thresholds();
        let result = try_match_rules(&score, None, false, false, None, 5, &t);
        assert!(result.is_some());
        let (cat, _) = result.unwrap();
        assert!(matches!(cat, RecommendationCategory::RemoveDeadCode));
    }

    #[test]
    fn rule_dead_code_gate_too_few_exports() {
        // dead_code_ratio high but only 2 value exports — below gate of 3
        let score = make_score(|s| s.dead_code_ratio = 0.8);
        let t = default_thresholds();
        let result = try_match_rules(&score, None, false, false, None, 2, &t);
        // Should NOT match dead code rule
        assert!(result.is_none());
    }

    #[test]
    fn rule_extract_complex_functions() {
        let score = make_score(|_| {});
        let fns = vec![("handleSubmit".to_string(), 10u32, 35u16)];
        let t = default_thresholds();
        let result = try_match_rules(&score, None, false, false, Some(&fns), 0, &t);
        assert!(result.is_some());
        let (cat, rec) = result.unwrap();
        assert!(matches!(
            cat,
            RecommendationCategory::ExtractComplexFunctions
        ));
        assert!(rec.contains("handleSubmit"));
    }

    #[test]
    fn rule_extract_dependencies_not_entry() {
        let score = make_score(|s| {
            s.fan_out = 20;
            s.maintainability_index = 50.0;
        });
        let t = default_thresholds();
        let result = try_match_rules(&score, None, false, false, None, 0, &t);
        assert!(result.is_some());
        let (cat, _) = result.unwrap();
        assert!(matches!(cat, RecommendationCategory::ExtractDependencies));
    }

    #[test]
    fn rule_extract_dependencies_skipped_for_entry() {
        let score = make_score(|s| {
            s.fan_out = 20;
            s.maintainability_index = 50.0;
        });
        let t = default_thresholds();
        // is_entry=true -> rule 6 should not match
        let result = try_match_rules(&score, None, false, true, None, 0, &t);
        assert!(result.is_none());
    }

    #[test]
    fn rule_urgent_churn_complexity() {
        let score = make_score(|s| s.complexity_density = 0.8);
        let hotspot = HotspotEntry {
            path: std::path::PathBuf::from("/src/foo.ts"),
            score: 60.0,
            commits: 20,
            weighted_commits: 15.0,
            lines_added: 500,
            lines_deleted: 100,
            complexity_density: 0.8,
            fan_in: 5,
            trend: fallow_core::churn::ChurnTrend::Accelerating,
        };
        let t = default_thresholds();
        let result = try_match_rules(&score, Some(&hotspot), false, false, None, 0, &t);
        assert!(result.is_some());
        let (cat, _) = result.unwrap();
        assert!(matches!(cat, RecommendationCategory::UrgentChurnComplexity));
    }
}