1use std::path::{Path, PathBuf};
8
9use crate::git_env::clear_ambient_git_env;
10
11const SECS_PER_DAY: u64 = 86_400;
13
14use fallow_output::{
15 DEFAULT_CYCLOMATIC_CRITICAL, FileHealthScore, HEALTH_SCORE_FORMULA_VERSION,
16 HOTSPOT_SCORE_THRESHOLD, HealthScore, HealthScorePenalties, HealthTrend, HotspotEntry,
17 RiskProfile, SNAPSHOT_SCHEMA_VERSION, TrendCount, TrendDirection, TrendMetric, TrendPoint,
18 VitalSigns, VitalSignsCounts, VitalSignsSnapshot, letter_grade,
19};
20
21pub struct VitalSignsInput<'a> {
25 pub modules: &'a [crate::source::ModuleInfo],
27 pub module_filter: Option<&'a rustc_hash::FxHashSet<crate::discover::FileId>>,
33 pub file_scores: Option<&'a [FileHealthScore]>,
35 pub hotspots: Option<&'a [HotspotEntry]>,
37 pub total_files: usize,
39 pub analysis_counts: Option<AnalysisCounts>,
44}
45
46impl<'a> VitalSignsInput<'a> {
47 fn selected_modules(&self) -> impl Iterator<Item = &'a crate::source::ModuleInfo> + '_ {
49 let filter = self.module_filter;
50 self.modules
51 .iter()
52 .filter(move |m| filter.is_none_or(|set| set.contains(&m.file_id)))
53 }
54}
55
56#[derive(Clone, Copy)]
58pub struct AnalysisCounts {
59 pub total_exports: usize,
60 pub dead_files: usize,
61 pub dead_exports: usize,
62 pub unused_deps: usize,
63 pub circular_deps: usize,
64 pub total_deps: usize,
65}
66
67fn collect_sorted_cyclomatic(input: &VitalSignsInput<'_>) -> Vec<u16> {
68 let mut values: Vec<u16> = input
69 .selected_modules()
70 .flat_map(|m| m.complexity.iter().map(|c| c.cyclomatic))
71 .collect();
72 values.sort_unstable();
73 values
74}
75
76fn average_cyclomatic(all_cyclomatic: &[u16]) -> f64 {
77 if all_cyclomatic.is_empty() {
78 return 0.0;
79 }
80
81 let sum: u64 = all_cyclomatic.iter().map(|&c| u64::from(c)).sum();
82 (sum as f64 / all_cyclomatic.len() as f64 * 10.0).round() / 10.0
83}
84
85fn critical_complexity_pct(all_cyclomatic: &[u16]) -> Option<f64> {
86 if all_cyclomatic.is_empty() {
87 return None;
88 }
89
90 let critical_count = all_cyclomatic
91 .iter()
92 .filter(|&&c| c >= DEFAULT_CYCLOMATIC_CRITICAL)
93 .count();
94 Some((critical_count as f64 / all_cyclomatic.len() as f64 * 1000.0).round() / 10.0)
95}
96
97#[expect(
98 clippy::cast_possible_truncation,
99 reason = "percentile index is bounded by the cyclomatic collection length"
100)]
101fn p90_cyclomatic(all_cyclomatic: &[u16]) -> u32 {
102 if all_cyclomatic.is_empty() {
103 return 0;
104 }
105
106 let idx = (all_cyclomatic.len() as f64 * 0.9).ceil() as usize;
107 let idx = idx.min(all_cyclomatic.len()) - 1;
108 u32::from(all_cyclomatic[idx])
109}
110
111#[expect(
112 clippy::cast_possible_truncation,
113 reason = "analysis counts are bounded by project size and emitted as compact u32 metrics"
114)]
115fn analysis_count_vitals(
116 counts: Option<&AnalysisCounts>,
117 total_files: usize,
118) -> (Option<f64>, Option<f64>, Option<u32>, Option<u32>) {
119 let Some(counts) = counts else {
120 return (None, None, None, None);
121 };
122
123 let dead_file_pct = if total_files > 0 {
124 Some((counts.dead_files as f64 / total_files as f64 * 1000.0).round() / 10.0)
125 } else {
126 Some(0.0)
127 };
128 let dead_export_pct = if counts.total_exports > 0 {
129 Some((counts.dead_exports as f64 / counts.total_exports as f64 * 1000.0).round() / 10.0)
130 } else {
131 Some(0.0)
132 };
133
134 (
135 dead_file_pct,
136 dead_export_pct,
137 Some(counts.unused_deps as u32),
138 Some(counts.circular_deps as u32),
139 )
140}
141
142struct SelectedModuleMetrics {
143 total_loc: u64,
144 line_counts: Vec<u32>,
145 param_counts: Vec<u8>,
146}
147
148fn selected_module_metrics(input: &VitalSignsInput<'_>) -> SelectedModuleMetrics {
149 let mut total_loc = 0;
150 let mut line_counts = Vec::new();
151 let mut param_counts = Vec::new();
152
153 for module in input.selected_modules() {
154 total_loc += module.line_offsets.len() as u64;
155 line_counts.extend(module.complexity.iter().map(|c| c.line_count));
156 param_counts.extend(module.complexity.iter().map(|c| c.param_count));
157 }
158
159 SelectedModuleMetrics {
160 total_loc,
161 line_counts,
162 param_counts,
163 }
164}
165
166fn vital_sign_counts(input: &VitalSignsInput<'_>, total_loc: u64) -> Option<VitalSignsCounts> {
167 input.analysis_counts.as_ref().map(|ac| VitalSignsCounts {
168 total_files: input.total_files,
169 total_exports: ac.total_exports,
170 dead_files: ac.dead_files,
171 dead_exports: ac.dead_exports,
172 duplicated_lines: None,
173 total_lines: Some(total_loc as usize),
174 files_scored: input.file_scores.map(<[_]>::len),
175 total_deps: ac.total_deps,
176 })
177}
178
179pub fn compute_vital_signs(input: &VitalSignsInput<'_>) -> VitalSigns {
181 let all_cyclomatic = collect_sorted_cyclomatic(input);
182 let avg_cyclomatic = average_cyclomatic(&all_cyclomatic);
183 let critical_complexity_pct = critical_complexity_pct(&all_cyclomatic);
184 let p90_cyclomatic = p90_cyclomatic(&all_cyclomatic);
185
186 let (dead_file_pct, dead_export_pct, unused_dep_count, circular_dep_count) =
187 analysis_count_vitals(input.analysis_counts.as_ref(), input.total_files);
188 let unused_deps_per_k_files =
189 unused_dep_count.map(|count| per_k_files(count, input.total_files));
190 let circular_deps_per_k_files =
191 circular_dep_count.map(|count| per_k_files(count, input.total_files));
192
193 let (maintainability_avg, maintainability_low_pct) = maintainability_vitals(input.file_scores);
194
195 let (hotspot_count, hotspot_top_pct_count) = hotspot_vitals(input.hotspots, input.total_files);
196
197 let module_metrics = selected_module_metrics(input);
198 let counts = vital_sign_counts(input, module_metrics.total_loc);
199 let functions_over_60_loc_per_k = functions_over_60_loc_per_k(&module_metrics.line_counts);
200 let unit_size_profile = unit_size_profile(&module_metrics.line_counts);
201
202 let unit_interfacing_profile =
203 unit_interfacing_profile(&module_metrics.param_counts, &all_cyclomatic);
204
205 let (p95_fan_in, coupling_high_pct) = if let Some(scores) = input.file_scores {
206 compute_coupling_concentration(scores)
207 } else {
208 (None, None)
209 };
210
211 VitalSigns {
212 dead_file_pct,
213 dead_export_pct,
214 avg_cyclomatic,
215 critical_complexity_pct,
216 p90_cyclomatic,
217 duplication_pct: None, hotspot_count,
219 hotspot_top_pct_count,
220 maintainability_avg,
221 maintainability_low_pct,
222 unused_dep_count,
223 unused_deps_per_k_files,
224 circular_dep_count,
225 circular_deps_per_k_files,
226 counts,
227 unit_size_profile,
228 functions_over_60_loc_per_k,
229 unit_interfacing_profile,
230 p95_fan_in,
231 coupling_high_pct,
232 prop_drilling_chain_count: None,
235 prop_drilling_max_depth: None,
236 p95_render_fan_in: None,
240 render_fan_in_high_pct: None,
241 max_render_fan_in: None,
242 top_render_fan_in: Vec::new(),
243 total_loc: module_metrics.total_loc,
244 }
245}
246
247fn per_k_files(count: u32, total_files: usize) -> f64 {
248 if total_files == 0 {
249 0.0
250 } else {
251 (f64::from(count) / total_files as f64 * 10_000.0).round() / 10.0
252 }
253}
254
255fn maintainability_vitals(scores: Option<&[FileHealthScore]>) -> (Option<f64>, Option<f64>) {
256 let Some(scores) = scores.filter(|scores| !scores.is_empty()) else {
257 return (None, None);
258 };
259 let sum: f64 = scores.iter().map(|s| s.maintainability_index).sum();
260 let low_count = scores
261 .iter()
262 .filter(|s| s.maintainability_index < 70.0)
263 .count();
264 (
265 Some((sum / scores.len() as f64 * 10.0).round() / 10.0),
266 Some((low_count as f64 / scores.len() as f64 * 1000.0).round() / 10.0),
267 )
268}
269
270fn hotspot_vitals(
271 hotspots: Option<&[HotspotEntry]>,
272 total_files: usize,
273) -> (Option<u32>, Option<u32>) {
274 let hotspot_count = hotspots.map(|entries| {
275 entries
276 .iter()
277 .filter(|e| e.score >= HOTSPOT_SCORE_THRESHOLD)
278 .count() as u32
279 });
280 let hotspot_top_pct_count = hotspots.map(|entries| {
281 if total_files == 0 || entries.is_empty() {
282 return 0;
283 }
284 let top_count = (total_files as f64 * 0.01).ceil() as usize;
285 entries
286 .iter()
287 .take(top_count.max(1))
288 .filter(|entry| entry.score > 0.0)
289 .count() as u32
290 });
291 (hotspot_count, hotspot_top_pct_count)
292}
293
294fn functions_over_60_loc_per_k(line_counts: &[u32]) -> Option<f64> {
295 if line_counts.is_empty() {
296 return None;
297 }
298 let over_60 = line_counts
299 .iter()
300 .filter(|&&line_count| line_count > 60)
301 .count();
302 Some((over_60 as f64 / line_counts.len() as f64 * 10_000.0).round() / 10.0)
303}
304
305fn unit_size_profile(line_counts: &[u32]) -> Option<RiskProfile> {
306 (!line_counts.is_empty()).then(|| compute_size_risk_profile(line_counts))
307}
308
309fn unit_interfacing_profile(param_counts: &[u8], all_cyclomatic: &[u16]) -> Option<RiskProfile> {
310 if all_cyclomatic.is_empty() {
311 return None;
312 }
313 Some(compute_interfacing_risk_profile(param_counts))
314}
315
316fn compute_size_risk_profile(line_counts: &[u32]) -> RiskProfile {
320 if line_counts.is_empty() {
321 return RiskProfile {
322 low_risk: 0.0,
323 medium_risk: 0.0,
324 high_risk: 0.0,
325 very_high_risk: 0.0,
326 };
327 }
328 let total = line_counts.len() as f64;
329 let low = line_counts.iter().filter(|&&lc| lc <= 15).count() as f64;
330 let medium = line_counts
331 .iter()
332 .filter(|&&lc| (16..=30).contains(&lc))
333 .count() as f64;
334 let high = line_counts
335 .iter()
336 .filter(|&&lc| (31..=60).contains(&lc))
337 .count() as f64;
338 let very_high = line_counts.iter().filter(|&&lc| lc > 60).count() as f64;
339 RiskProfile {
340 low_risk: (low / total * 1000.0).round() / 10.0,
341 medium_risk: (medium / total * 1000.0).round() / 10.0,
342 high_risk: (high / total * 1000.0).round() / 10.0,
343 very_high_risk: (very_high / total * 1000.0).round() / 10.0,
344 }
345}
346
347fn compute_interfacing_risk_profile(param_counts: &[u8]) -> RiskProfile {
351 if param_counts.is_empty() {
352 return RiskProfile {
353 low_risk: 0.0,
354 medium_risk: 0.0,
355 high_risk: 0.0,
356 very_high_risk: 0.0,
357 };
358 }
359 let total = param_counts.len() as f64;
360 let low = param_counts.iter().filter(|&&pc| pc <= 2).count() as f64;
361 let medium = param_counts
362 .iter()
363 .filter(|&&pc| (3..=4).contains(&pc))
364 .count() as f64;
365 let high = param_counts
366 .iter()
367 .filter(|&&pc| (5..=6).contains(&pc))
368 .count() as f64;
369 let very_high = param_counts.iter().filter(|&&pc| pc >= 7).count() as f64;
370 RiskProfile {
371 low_risk: (low / total * 1000.0).round() / 10.0,
372 medium_risk: (medium / total * 1000.0).round() / 10.0,
373 high_risk: (high / total * 1000.0).round() / 10.0,
374 very_high_risk: (very_high / total * 1000.0).round() / 10.0,
375 }
376}
377
378#[expect(
391 clippy::cast_possible_truncation,
392 reason = "fan-in values are bounded by project size"
393)]
394fn compute_coupling_concentration(scores: &[FileHealthScore]) -> (Option<u32>, Option<f64>) {
395 if scores.is_empty() {
396 return (None, None);
397 }
398 let mut fan_ins: Vec<usize> = scores.iter().map(|s| s.fan_in).collect();
399 fan_ins.sort_unstable();
400 let idx = (fan_ins.len() as f64 * 0.95).ceil() as usize;
401 let idx = idx.min(fan_ins.len()) - 1;
402 let p95 = fan_ins[idx] as u32;
403
404 let threshold = (p95 as usize).max(10);
405 let high_count = fan_ins.iter().filter(|&&fi| fi > threshold).count();
406 let high_pct = (high_count as f64 / fan_ins.len() as f64 * 1000.0).round() / 10.0;
407
408 (Some(p95), Some(high_pct))
409}
410
411pub fn compute_health_score(vs: &VitalSigns, total_files: usize) -> HealthScore {
417 let penalties = compute_health_score_penalties(vs, total_files);
418 let score = apply_health_score_penalties(&penalties);
419 let grade = letter_grade(score);
420
421 HealthScore {
422 formula_version: HEALTH_SCORE_FORMULA_VERSION,
423 score,
424 grade,
425 penalties,
426 }
427}
428
429fn compute_health_score_penalties(vs: &VitalSigns, total_files: usize) -> HealthScorePenalties {
430 HealthScorePenalties {
431 dead_files: vs.dead_file_pct.map(|pct| round1((pct * 0.2).min(15.0))),
432 dead_exports: vs.dead_export_pct.map(|pct| round1((pct * 0.2).min(15.0))),
433 complexity: complexity_penalty(vs),
434 p90_complexity: p90_complexity_penalty(vs),
435 maintainability: maintainability_penalty(vs),
436 hotspots: hotspot_penalty(vs, total_files),
437 unused_deps: dependency_count_penalty(
438 vs.unused_deps_per_k_files,
439 vs.unused_dep_count,
440 25.0,
441 10.0,
442 ),
443 circular_deps: dependency_count_penalty(
444 vs.circular_deps_per_k_files,
445 vs.circular_dep_count,
446 25.0,
447 10.0,
448 ),
449 unit_size: unit_size_penalty(vs),
450 coupling: coupling_penalty(vs),
451 duplication: vs
452 .duplication_pct
453 .map(|dp| round1((dp - 5.0).clamp(0.0, 10.0))),
454 prop_drilling: prop_drilling_penalty(vs),
455 }
456}
457
458fn apply_health_score_penalties(penalties: &HealthScorePenalties) -> f64 {
459 let mut score = 100.0_f64;
460
461 subtract_optional_penalty(&mut score, penalties.dead_files);
462 subtract_optional_penalty(&mut score, penalties.dead_exports);
463 score -= penalties.complexity;
464 score -= penalties.p90_complexity;
465 subtract_optional_penalty(&mut score, penalties.maintainability);
466 subtract_optional_penalty(&mut score, penalties.hotspots);
467 subtract_optional_penalty(&mut score, penalties.unused_deps);
468 subtract_optional_penalty(&mut score, penalties.circular_deps);
469 subtract_optional_penalty(&mut score, penalties.unit_size);
470 subtract_optional_penalty(&mut score, penalties.coupling);
471 subtract_optional_penalty(&mut score, penalties.duplication);
472 subtract_optional_penalty(&mut score, penalties.prop_drilling);
473
474 round1(score).clamp(0.0, 100.0)
475}
476
477fn prop_drilling_penalty(vs: &VitalSigns) -> Option<f64> {
483 vs.prop_drilling_chain_count
484 .map(|count| round1((f64::from(count) * 1.0).min(5.0)))
485}
486
487fn round1(value: f64) -> f64 {
488 (value * 10.0).round() / 10.0
489}
490
491fn subtract_optional_penalty(score: &mut f64, penalty: Option<f64>) {
492 if let Some(penalty) = penalty {
493 *score -= penalty;
494 }
495}
496
497fn complexity_penalty(vs: &VitalSigns) -> f64 {
498 if let Some(critical_pct) = vs.critical_complexity_pct {
499 round1((critical_pct * 4.0).min(20.0))
500 } else {
501 round1(((vs.avg_cyclomatic - 1.5).max(0.0) * 5.0).min(20.0))
502 }
503}
504
505fn p90_complexity_penalty(vs: &VitalSigns) -> f64 {
506 if vs.critical_complexity_pct.is_some() {
507 0.0
508 } else {
509 round1((f64::from(vs.p90_cyclomatic) - 10.0).clamp(0.0, 10.0))
510 }
511}
512
513fn maintainability_penalty(vs: &VitalSigns) -> Option<f64> {
514 if let Some(low_pct) = vs.maintainability_low_pct {
515 Some(round1((low_pct * 1.5).min(15.0)))
516 } else {
517 vs.maintainability_avg
518 .map(|mi| round1(((70.0 - mi).max(0.0) * 0.5).min(15.0)))
519 }
520}
521
522fn hotspot_penalty(vs: &VitalSigns, total_files: usize) -> Option<f64> {
523 if let Some(top_pct_count) = vs.hotspot_top_pct_count {
524 return Some(if total_files > 0 {
525 let top_pct_bucket = (total_files as f64 * 0.01).ceil().max(1.0);
526 round1((f64::from(top_pct_count) / top_pct_bucket * 10.0).min(10.0))
527 } else {
528 0.0
529 });
530 }
531
532 vs.hotspot_count.map(|hc| {
533 if total_files > 0 {
534 round1((f64::from(hc) / total_files as f64 * 200.0).min(10.0))
535 } else {
536 0.0
537 }
538 })
539}
540
541fn dependency_count_penalty(
542 per_k: Option<f64>,
543 count: Option<u32>,
544 per_k_cap: f64,
545 count_cap: f64,
546) -> Option<f64> {
547 if let Some(per_k) = per_k {
548 Some(round1((per_k * 0.5).min(per_k_cap)))
549 } else {
550 count.map(|count| round1(f64::from(count).min(count_cap)))
551 }
552}
553
554fn unit_size_penalty(vs: &VitalSigns) -> Option<f64> {
555 if let Some(per_k) = vs.functions_over_60_loc_per_k {
556 Some(round1((per_k * 0.5).min(10.0)))
557 } else {
558 vs.unit_size_profile
559 .as_ref()
560 .map(|profile| round1(((profile.very_high_risk - 5.0).max(0.0) * 0.5).min(10.0)))
561 }
562}
563
564fn coupling_penalty(vs: &VitalSigns) -> Option<f64> {
565 if let Some(high_pct) = vs.coupling_high_pct {
566 Some(round1((high_pct * 0.5).min(5.0)))
567 } else {
568 vs.p95_fan_in
569 .map(|p95| round1(((f64::from(p95) - 30.0).max(0.0) * 0.25).min(5.0)))
570 }
571}
572
573pub fn build_counts(input: &VitalSignsInput<'_>) -> VitalSignsCounts {
575 let (total_exports, dead_files, dead_exports, total_deps) = input
576 .analysis_counts
577 .as_ref()
578 .map_or((0, 0, 0, 0), |counts| {
579 (
580 counts.total_exports,
581 counts.dead_files,
582 counts.dead_exports,
583 counts.total_deps,
584 )
585 });
586
587 let total_lines: usize = input.selected_modules().map(|m| m.line_offsets.len()).sum();
588
589 VitalSignsCounts {
590 total_files: input.total_files,
591 total_exports,
592 dead_files,
593 dead_exports,
594 duplicated_lines: None,
595 total_lines: Some(total_lines),
596 files_scored: input.file_scores.map(<[_]>::len),
597 total_deps,
598 }
599}
600
601#[expect(
603 clippy::disallowed_methods,
604 reason = "trusted git spawn with ambient repo-state env stripped, matching the core git spawn policy"
605)]
606fn git_sha(root: &Path) -> Option<String> {
607 let mut command = std::process::Command::new("git");
608 command
609 .args(["rev-parse", "--short", "HEAD"])
610 .current_dir(root);
611 clear_ambient_git_env(&mut command);
612 command
613 .output()
614 .ok()
615 .filter(|o| o.status.success())
616 .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
617}
618
619#[expect(
621 clippy::disallowed_methods,
622 reason = "trusted git spawn with ambient repo-state env stripped, matching the core git spawn policy"
623)]
624fn git_branch(root: &Path) -> Option<String> {
625 let mut command = std::process::Command::new("git");
626 command
627 .args(["rev-parse", "--abbrev-ref", "HEAD"])
628 .current_dir(root);
629 clear_ambient_git_env(&mut command);
630 command
631 .output()
632 .ok()
633 .filter(|o| o.status.success())
634 .and_then(|o| {
635 let name = String::from_utf8_lossy(&o.stdout).trim().to_string();
636 if name == "HEAD" { None } else { Some(name) }
637 })
638}
639
640pub fn build_snapshot(
642 vital_signs: VitalSigns,
643 counts: VitalSignsCounts,
644 root: &Path,
645 shallow_clone: bool,
646 health_score: Option<&HealthScore>,
647 coverage_model: Option<fallow_output::CoverageModel>,
648) -> VitalSignsSnapshot {
649 let now = chrono_timestamp();
650
651 VitalSignsSnapshot {
652 snapshot_schema_version: SNAPSHOT_SCHEMA_VERSION,
653 version: env!("CARGO_PKG_VERSION").to_string(),
654 timestamp: now,
655 git_sha: git_sha(root),
656 git_branch: git_branch(root),
657 shallow_clone,
658 vital_signs,
659 counts,
660 score: health_score.map(|s| s.score),
661 grade: health_score.map(|s| s.grade.to_string()),
662 coverage_model,
663 }
664}
665
666pub fn chrono_timestamp() -> String {
668 use std::time::SystemTime;
669 let now = SystemTime::now()
670 .duration_since(SystemTime::UNIX_EPOCH)
671 .unwrap_or_default();
672 let secs = now.as_secs();
673
674 let days = secs / SECS_PER_DAY;
675 let time_secs = secs % SECS_PER_DAY;
676 let hours = time_secs / 3600;
677 let minutes = (time_secs % 3600) / 60;
678 let seconds = time_secs % 60;
679
680 let (year, month, day) = days_to_ymd(days);
681
682 format!("{year:04}-{month:02}-{day:02}T{hours:02}:{minutes:02}:{seconds:02}Z")
683}
684
685const fn days_to_ymd(days: u64) -> (u64, u64, u64) {
687 let z = days + 719_468;
688 let era = z / 146_097;
689 let doe = z - era * 146_097;
690 let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146_096) / 365;
691 let y = yoe + era * 400;
692 let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
693 let mp = (5 * doy + 2) / 153;
694 let d = doy - (153 * mp + 2) / 5 + 1;
695 let m = if mp < 10 { mp + 3 } else { mp - 9 };
696 let y = if m <= 2 { y + 1 } else { y };
697 (y, m, d)
698}
699
700pub fn save_snapshot(
705 snapshot: &VitalSignsSnapshot,
706 root: &Path,
707 explicit_path: Option<&Path>,
708) -> Result<PathBuf, String> {
709 let path = explicit_path.map_or_else(
710 || {
711 let dir = root.join(".fallow").join("snapshots");
712 let filename = snapshot.timestamp.replace(':', "-");
713 dir.join(format!("{filename}.json"))
714 },
715 Path::to_path_buf,
716 );
717
718 if let Some(parent) = path.parent() {
719 std::fs::create_dir_all(parent)
720 .map_err(|e| format!("failed to create snapshot directory: {e}"))?;
721 }
722
723 let json =
724 serde_json::to_string_pretty(snapshot).map_err(|e| format!("failed to serialize: {e}"))?;
725 std::fs::write(&path, json).map_err(|e| format!("failed to write snapshot: {e}"))?;
726
727 Ok(path)
728}
729
730#[expect(
735 clippy::print_stderr,
736 reason = "corrupt-snapshot warnings to stderr, preserved verbatim from the CLI health path"
737)]
738pub fn load_snapshots(root: &Path) -> Vec<VitalSignsSnapshot> {
739 let dir = root.join(".fallow").join("snapshots");
740 let Ok(entries) = std::fs::read_dir(&dir) else {
741 return Vec::new();
742 };
743
744 let mut snapshots = Vec::new();
745 for entry in entries {
746 let Ok(entry) = entry else { continue };
747 let path = entry.path();
748 if path.extension().is_some_and(|ext| ext == "json") {
749 match std::fs::read_to_string(&path) {
750 Ok(content) => match serde_json::from_str::<VitalSignsSnapshot>(&content) {
751 Ok(snap) => snapshots.push(snap),
752 Err(e) => {
753 eprintln!("warning: skipping corrupt snapshot {}: {e}", path.display());
754 }
755 },
756 Err(e) => {
757 eprintln!("warning: could not read snapshot {}: {e}", path.display());
758 }
759 }
760 }
761 }
762
763 snapshots.sort_by(|a, b| a.timestamp.cmp(&b.timestamp));
764 snapshots
765}
766
767const TREND_TOLERANCE: f64 = 0.5;
769
770fn trend_point_from_snapshot(prev: &VitalSignsSnapshot) -> TrendPoint {
771 TrendPoint {
772 timestamp: prev.timestamp.clone(),
773 git_sha: prev.git_sha.clone(),
774 score: prev.score,
775 grade: prev.grade.clone(),
776 coverage_model: prev.coverage_model.clone(),
777 snapshot_schema_version: Some(prev.snapshot_schema_version),
778 }
779}
780
781fn overall_trend_direction(metrics: &[TrendMetric]) -> TrendDirection {
782 let (improving, declining) = metrics.iter().fold((0usize, 0usize), |(imp, dec), metric| {
783 match metric.direction {
784 TrendDirection::Improving => (imp + 1, dec),
785 TrendDirection::Declining => (imp, dec + 1),
786 TrendDirection::Stable => (imp, dec),
787 }
788 });
789
790 match improving.cmp(&declining) {
791 std::cmp::Ordering::Greater => TrendDirection::Improving,
792 std::cmp::Ordering::Less => TrendDirection::Declining,
793 std::cmp::Ordering::Equal => TrendDirection::Stable,
794 }
795}
796
797pub fn compute_trend(
802 current_vs: &VitalSigns,
803 current_counts: &VitalSignsCounts,
804 current_score: Option<f64>,
805 snapshots: &[VitalSignsSnapshot],
806) -> Option<HealthTrend> {
807 let prev = snapshots.last()?;
808
809 let compared_to = trend_point_from_snapshot(prev);
810
811 let metrics = TrendBuilder::new(prev, current_vs, current_counts, current_score).build();
812
813 let overall_direction = overall_trend_direction(&metrics);
814
815 Some(HealthTrend {
816 compared_to,
817 metrics,
818 snapshots_loaded: snapshots.len(),
819 overall_direction,
820 })
821}
822
823struct TrendBuilder<'a> {
824 prev: &'a VitalSignsSnapshot,
825 current_vs: &'a VitalSigns,
826 current_counts: &'a VitalSignsCounts,
827 current_score: Option<f64>,
828 metrics: Vec<TrendMetric>,
829}
830
831impl TrendBuilder<'_> {
832 fn new<'a>(
833 prev: &'a VitalSignsSnapshot,
834 current_vs: &'a VitalSigns,
835 current_counts: &'a VitalSignsCounts,
836 current_score: Option<f64>,
837 ) -> TrendBuilder<'a> {
838 TrendBuilder {
839 prev,
840 current_vs,
841 current_counts,
842 current_score,
843 metrics: Vec::new(),
844 }
845 }
846
847 fn build(mut self) -> Vec<TrendMetric> {
848 self.add_score_metric();
849 self.add_dead_code_metrics();
850 self.add_complexity_metrics();
851 self.add_dependency_metrics();
852 self.add_structure_metrics();
853 self.metrics
854 }
855
856 fn push(&mut self, input: TrendMetricInput) {
857 self.metrics.push(make_metric(input));
858 }
859
860 fn add_score_metric(&mut self) {
861 if let (Some(prev_score), Some(cur_score)) = (self.prev.score, self.current_score) {
862 self.push(TrendMetricInput {
863 name: "score",
864 label: "Health Score",
865 previous: prev_score,
866 current: cur_score,
867 unit: "",
868 higher_is_better: true,
869 previous_count: None,
870 current_count: None,
871 });
872 }
873 }
874
875 fn add_dead_code_metrics(&mut self) {
876 if let (Some(prev_val), Some(cur_val)) = (
877 self.prev.vital_signs.dead_file_pct,
878 self.current_vs.dead_file_pct,
879 ) {
880 self.push(TrendMetricInput {
881 name: "dead_file_pct",
882 label: "Dead Files",
883 previous: prev_val,
884 current: cur_val,
885 unit: "%",
886 higher_is_better: false,
887 previous_count: Some(TrendCount {
888 value: self.prev.counts.dead_files,
889 total: self.prev.counts.total_files,
890 }),
891 current_count: Some(TrendCount {
892 value: self.current_counts.dead_files,
893 total: self.current_counts.total_files,
894 }),
895 });
896 }
897
898 if let (Some(prev_val), Some(cur_val)) = (
899 self.prev.vital_signs.dead_export_pct,
900 self.current_vs.dead_export_pct,
901 ) {
902 self.push(TrendMetricInput {
903 name: "dead_export_pct",
904 label: "Dead Exports",
905 previous: prev_val,
906 current: cur_val,
907 unit: "%",
908 higher_is_better: false,
909 previous_count: Some(TrendCount {
910 value: self.prev.counts.dead_exports,
911 total: self.prev.counts.total_exports,
912 }),
913 current_count: Some(TrendCount {
914 value: self.current_counts.dead_exports,
915 total: self.current_counts.total_exports,
916 }),
917 });
918 }
919 }
920
921 fn add_complexity_metrics(&mut self) {
922 self.push(TrendMetricInput {
923 name: "avg_cyclomatic",
924 label: "Avg Cyclomatic",
925 previous: self.prev.vital_signs.avg_cyclomatic,
926 current: self.current_vs.avg_cyclomatic,
927 unit: "",
928 higher_is_better: false,
929 previous_count: None,
930 current_count: None,
931 });
932
933 if let (Some(prev_val), Some(cur_val)) = (
934 self.prev.vital_signs.maintainability_avg,
935 self.current_vs.maintainability_avg,
936 ) {
937 self.push(TrendMetricInput {
938 name: "maintainability_avg",
939 label: "Maintainability",
940 previous: prev_val,
941 current: cur_val,
942 unit: "",
943 higher_is_better: true,
944 previous_count: None,
945 current_count: None,
946 });
947 }
948
949 if let (Some(prev_profile), Some(cur_profile)) = (
950 &self.prev.vital_signs.unit_size_profile,
951 &self.current_vs.unit_size_profile,
952 ) {
953 self.push(TrendMetricInput {
954 name: "unit_size_very_high_pct",
955 label: "Oversized Fns",
956 previous: prev_profile.very_high_risk,
957 current: cur_profile.very_high_risk,
958 unit: "%",
959 higher_is_better: false,
960 previous_count: None,
961 current_count: None,
962 });
963 }
964
965 self.add_duplication_metric();
966 }
967
968 fn add_duplication_metric(&mut self) {
969 if let (Some(prev_val), Some(cur_val)) = (
970 self.prev.vital_signs.duplication_pct,
971 self.current_vs.duplication_pct,
972 ) {
973 self.push(TrendMetricInput {
974 name: "duplication_pct",
975 label: "Duplication",
976 previous: prev_val,
977 current: cur_val,
978 unit: "%",
979 higher_is_better: false,
980 previous_count: self
981 .prev
982 .counts
983 .duplicated_lines
984 .zip(self.prev.counts.total_lines)
985 .map(|(d, t)| TrendCount { value: d, total: t }),
986 current_count: self
987 .current_counts
988 .duplicated_lines
989 .zip(self.current_counts.total_lines)
990 .map(|(d, t)| TrendCount { value: d, total: t }),
991 });
992 }
993 }
994
995 fn add_dependency_metrics(&mut self) {
996 if let (Some(prev_val), Some(cur_val)) = (
997 self.prev.vital_signs.unused_dep_count,
998 self.current_vs.unused_dep_count,
999 ) {
1000 self.push(TrendMetricInput {
1001 name: "unused_dep_count",
1002 label: "Unused Deps",
1003 previous: f64::from(prev_val),
1004 current: f64::from(cur_val),
1005 unit: "",
1006 higher_is_better: false,
1007 previous_count: None,
1008 current_count: None,
1009 });
1010 }
1011 }
1012
1013 fn add_structure_metrics(&mut self) {
1014 if let (Some(prev_val), Some(cur_val)) = (
1015 self.prev.vital_signs.circular_dep_count,
1016 self.current_vs.circular_dep_count,
1017 ) {
1018 self.push(TrendMetricInput {
1019 name: "circular_dep_count",
1020 label: "Circular Deps",
1021 previous: f64::from(prev_val),
1022 current: f64::from(cur_val),
1023 unit: "",
1024 higher_is_better: false,
1025 previous_count: None,
1026 current_count: None,
1027 });
1028 }
1029
1030 if let (Some(prev_val), Some(cur_val)) = (
1031 self.prev.vital_signs.hotspot_count,
1032 self.current_vs.hotspot_count,
1033 ) {
1034 self.push(TrendMetricInput {
1035 name: "hotspot_count",
1036 label: "Hotspots",
1037 previous: f64::from(prev_val),
1038 current: f64::from(cur_val),
1039 unit: "",
1040 higher_is_better: false,
1041 previous_count: None,
1042 current_count: None,
1043 });
1044 }
1045
1046 if let (Some(prev_val), Some(cur_val)) =
1047 (self.prev.vital_signs.p95_fan_in, self.current_vs.p95_fan_in)
1048 {
1049 self.push(TrendMetricInput {
1050 name: "p95_fan_in",
1051 label: "P95 Fan-in",
1052 previous: f64::from(prev_val),
1053 current: f64::from(cur_val),
1054 unit: "",
1055 higher_is_better: false,
1056 previous_count: None,
1057 current_count: None,
1058 });
1059 }
1060 }
1061}
1062
1063struct TrendMetricInput {
1065 name: &'static str,
1066 label: &'static str,
1067 previous: f64,
1068 current: f64,
1069 unit: &'static str,
1070 higher_is_better: bool,
1071 previous_count: Option<TrendCount>,
1072 current_count: Option<TrendCount>,
1073}
1074
1075fn make_metric(input: TrendMetricInput) -> TrendMetric {
1076 let TrendMetricInput {
1077 name,
1078 label,
1079 previous,
1080 current,
1081 unit,
1082 higher_is_better,
1083 previous_count,
1084 current_count,
1085 } = input;
1086 let delta = (current - previous).round_to(1);
1087 let direction = if delta.abs() < TREND_TOLERANCE {
1088 TrendDirection::Stable
1089 } else if (higher_is_better && delta > 0.0) || (!higher_is_better && delta < 0.0) {
1090 TrendDirection::Improving
1091 } else {
1092 TrendDirection::Declining
1093 };
1094
1095 TrendMetric {
1096 name,
1097 label,
1098 previous,
1099 current,
1100 delta,
1101 direction,
1102 unit,
1103 previous_count,
1104 current_count,
1105 }
1106}
1107
1108trait RoundTo {
1110 fn round_to(self, decimals: u32) -> Self;
1111}
1112
1113impl RoundTo for f64 {
1114 fn round_to(self, decimals: u32) -> Self {
1115 let factor = 10_f64.powi(decimals as i32);
1116 (self * factor).round() / factor
1117 }
1118}
1119
1120#[cfg(test)]
1121mod tests {
1122 use super::*;
1123
1124 fn make_module(id: u32, cyclomatic: u16) -> crate::source::ModuleInfo {
1125 crate::source::ModuleInfo {
1126 file_id: crate::discover::FileId(id),
1127 exports: Vec::new(),
1128 imports: Vec::new(),
1129 re_exports: Vec::new(),
1130 dynamic_imports: Vec::new(),
1131 dynamic_import_patterns: Vec::new(),
1132 require_calls: Vec::new(),
1133 package_path_references: Box::default(),
1134 member_accesses: Vec::new(),
1135 semantic_facts: Box::default(),
1136 whole_object_uses: Box::default(),
1137 has_cjs_exports: false,
1138 has_angular_component_template_url: false,
1139 content_hash: 0,
1140 suppressions: Vec::new(),
1141 unknown_suppression_kinds: Vec::new(),
1142 unused_import_bindings: Vec::new(),
1143 type_referenced_import_bindings: vec![],
1144 value_referenced_import_bindings: vec![],
1145 line_offsets: Vec::new(),
1146 flag_uses: Vec::new(),
1147 class_heritage: Vec::new(),
1148 exported_factory_returns: Box::default(),
1149 exported_factory_return_object_shapes: Box::default(),
1150 type_member_types: Box::default(),
1151 injection_tokens: Vec::new(),
1152 local_type_declarations: Vec::new(),
1153 public_signature_type_references: Vec::new(),
1154 namespace_object_aliases: Vec::new(),
1155 iconify_prefixes: Vec::new(),
1156 iconify_icon_names: Vec::new(),
1157 auto_import_candidates: Vec::new(),
1158 directives: Vec::new(),
1159 client_only_dynamic_import_spans: Vec::new(),
1160 security_sinks: Vec::new(),
1161 security_sinks_skipped: 0,
1162 security_unresolved_callee_sites: Vec::new(),
1163 tainted_bindings: Vec::new(),
1164 sanitized_sink_args: Vec::new(),
1165 security_control_sites: Vec::new(),
1166 callee_uses: Vec::new(),
1167 misplaced_directives: Vec::new(),
1168 inline_server_action_exports: Vec::new(),
1169 di_key_sites: Vec::new(),
1170 has_dynamic_provide: false,
1171 referenced_import_bindings: Vec::new(),
1172 component_props: Vec::new(),
1173 has_props_attrs_fallthrough: false,
1174 has_define_expose: false,
1175 has_define_model: false,
1176 has_unharvestable_props: false,
1177 component_emits: Vec::new(),
1178 angular_inputs: Vec::new(),
1179 angular_outputs: Vec::new(),
1180 has_unharvestable_emits: false,
1181 has_dynamic_emit: false,
1182 has_emit_whole_object_use: false,
1183 load_return_keys: Vec::new(),
1184 has_unharvestable_load: false,
1185 has_load_data_whole_use: false,
1186 has_page_data_store_whole_use: false,
1187 has_route_loader_data_whole_use: false,
1188 component_functions: Vec::new(),
1189 react_props: Vec::new(),
1190 hook_uses: Vec::new(),
1191 render_edges: Vec::new(),
1192 svelte_dispatched_events: Vec::new(),
1193 svelte_listened_events: Vec::new(),
1194 angular_component_selectors: Vec::new(),
1195 registered_custom_elements: Vec::new(),
1196 used_custom_element_tags: Vec::new(),
1197 angular_used_selectors: Vec::new(),
1198 angular_entry_component_refs: Vec::new(),
1199 has_dynamic_component_render: false,
1200 has_dynamic_dispatch: false,
1201 complexity: vec![fallow_types::extract::FunctionComplexity {
1202 name: format!("fn_{id}"),
1203 line: id + 1,
1204 col: 0,
1205 cyclomatic,
1206 cognitive: 0,
1207 line_count: 10,
1208 param_count: 0,
1209 react_hook_count: 0,
1210 react_jsx_max_depth: 0,
1211 react_prop_count: 0,
1212 source_hash: None,
1213 contributions: Vec::new(),
1214 }],
1215 }
1216 }
1217
1218 #[expect(
1219 clippy::cast_possible_truncation,
1220 reason = "test values are trivially small"
1221 )]
1222 fn make_modules() -> Vec<crate::source::ModuleInfo> {
1223 (0..10)
1224 .map(|i| make_module(i, (i as u16 + 1) * 2))
1225 .collect()
1226 }
1227
1228 fn assert_close(actual: f64, expected: f64) {
1229 assert!(
1230 (actual - expected).abs() < f64::EPSILON,
1231 "expected {expected}, got {actual}"
1232 );
1233 }
1234
1235 fn assert_some_close(actual: Option<f64>, expected: f64) {
1236 assert_close(actual.expect("expected metric to be present"), expected);
1237 }
1238
1239 #[test]
1240 fn compute_cyclomatic_stats() {
1241 let modules = make_modules();
1242 let input = VitalSignsInput {
1243 modules: &modules,
1244 module_filter: None,
1245 file_scores: None,
1246 hotspots: None,
1247 total_files: 10,
1248 analysis_counts: None,
1249 };
1250 let vs = compute_vital_signs(&input);
1251 assert!((vs.avg_cyclomatic - 11.0).abs() < f64::EPSILON);
1252 assert_eq!(vs.p90_cyclomatic, 18);
1253 }
1254
1255 #[test]
1256 fn compute_with_analysis_counts() {
1257 let modules = make_modules();
1258 let input = VitalSignsInput {
1259 modules: &modules,
1260 module_filter: None,
1261 file_scores: None,
1262 hotspots: None,
1263 total_files: 100,
1264 analysis_counts: Some(AnalysisCounts {
1265 total_exports: 500,
1266 dead_files: 5,
1267 dead_exports: 50,
1268 unused_deps: 3,
1269 circular_deps: 2,
1270 total_deps: 40,
1271 }),
1272 };
1273 let vs = compute_vital_signs(&input);
1274 assert_eq!(vs.dead_file_pct, Some(5.0)); assert_eq!(vs.dead_export_pct, Some(10.0)); assert_eq!(vs.unused_dep_count, Some(3));
1277 assert_eq!(vs.circular_dep_count, Some(2));
1278 }
1279
1280 #[test]
1281 fn compute_hotspot_count_with_threshold() {
1282 let hotspots = vec![
1283 HotspotEntry {
1284 path: PathBuf::from("a.ts"),
1285 score: 80.0,
1286 commits: 10,
1287 weighted_commits: 8.0,
1288 lines_added: 100,
1289 lines_deleted: 50,
1290 complexity_density: 0.5,
1291 fan_in: 5,
1292 trend: crate::churn::ChurnTrend::Stable,
1293 ownership: None,
1294 is_test_path: false,
1295 },
1296 HotspotEntry {
1297 path: PathBuf::from("b.ts"),
1298 score: 30.0, commits: 5,
1300 weighted_commits: 3.0,
1301 lines_added: 40,
1302 lines_deleted: 20,
1303 complexity_density: 0.2,
1304 fan_in: 2,
1305 trend: crate::churn::ChurnTrend::Cooling,
1306 ownership: None,
1307 is_test_path: false,
1308 },
1309 HotspotEntry {
1310 path: PathBuf::from("c.ts"),
1311 score: 50.0, commits: 8,
1313 weighted_commits: 6.0,
1314 lines_added: 80,
1315 lines_deleted: 30,
1316 complexity_density: 0.4,
1317 fan_in: 3,
1318 trend: crate::churn::ChurnTrend::Accelerating,
1319 ownership: None,
1320 is_test_path: false,
1321 },
1322 ];
1323 let modules = Vec::new();
1324 let input = VitalSignsInput {
1325 modules: &modules,
1326 module_filter: None,
1327 file_scores: None,
1328 hotspots: Some(&hotspots),
1329 total_files: 10,
1330 analysis_counts: None,
1331 };
1332 let vs = compute_vital_signs(&input);
1333 assert_eq!(vs.hotspot_count, Some(2)); assert_eq!(vs.hotspot_top_pct_count, Some(1)); }
1336
1337 #[test]
1338 fn compute_without_hotspots_gives_none() {
1339 let modules = Vec::new();
1340 let input = VitalSignsInput {
1341 modules: &modules,
1342 module_filter: None,
1343 file_scores: None,
1344 hotspots: None,
1345 total_files: 0,
1346 analysis_counts: None,
1347 };
1348 let vs = compute_vital_signs(&input);
1349 assert!(vs.hotspot_count.is_none());
1350 }
1351
1352 #[test]
1353 fn snapshot_save_and_load() {
1354 let dir = tempfile::tempdir().unwrap();
1355 let root = dir.path();
1356 let vs = VitalSigns {
1357 dead_file_pct: Some(3.2),
1358 dead_export_pct: Some(8.1),
1359 avg_cyclomatic: 4.7,
1360 p90_cyclomatic: 12,
1361 hotspot_count: Some(5),
1362 maintainability_avg: Some(72.4),
1363 unused_dep_count: Some(4),
1364 circular_dep_count: Some(2),
1365 ..Default::default()
1366 };
1367 let counts = VitalSignsCounts {
1368 total_files: 1200,
1369 total_exports: 5400,
1370 dead_files: 38,
1371 dead_exports: 437,
1372 files_scored: Some(1150),
1373 total_deps: 42,
1374 ..Default::default()
1375 };
1376 let health_score = compute_health_score(&vs, 1200);
1377 let snapshot = build_snapshot(vs, counts, root, false, Some(&health_score), None);
1378 let saved_path = save_snapshot(&snapshot, root, None).unwrap();
1379
1380 assert!(saved_path.exists());
1381 assert!(saved_path.starts_with(root.join(".fallow/snapshots")));
1382
1383 let content = std::fs::read_to_string(&saved_path).unwrap();
1384 let loaded: VitalSignsSnapshot = serde_json::from_str(&content).unwrap();
1385 assert_eq!(loaded.snapshot_schema_version, SNAPSHOT_SCHEMA_VERSION);
1386 assert!((loaded.vital_signs.avg_cyclomatic - 4.7).abs() < f64::EPSILON);
1387 assert_eq!(loaded.counts.total_files, 1200);
1388 assert!(loaded.score.is_some());
1389 assert!(loaded.grade.is_some());
1390 }
1391
1392 #[test]
1393 fn snapshot_save_explicit_path() {
1394 let dir = tempfile::tempdir().unwrap();
1395 let root = dir.path();
1396 let explicit = root.join("my-snapshot.json");
1397 let vs = VitalSigns {
1398 avg_cyclomatic: 1.0,
1399 p90_cyclomatic: 2,
1400 ..Default::default()
1401 };
1402 let counts = VitalSignsCounts::default();
1403 let snapshot = build_snapshot(vs, counts, root, false, None, None);
1404 let saved = save_snapshot(&snapshot, root, Some(&explicit)).unwrap();
1405 assert_eq!(saved, explicit);
1406 assert!(explicit.exists());
1407 }
1408
1409 #[test]
1410 fn snapshot_save_creates_nested_dirs() {
1411 let dir = tempfile::tempdir().unwrap();
1412 let root = dir.path();
1413 let nested = root.join("a/b/c/snapshot.json");
1414 let vs = VitalSigns {
1415 avg_cyclomatic: 1.0,
1416 p90_cyclomatic: 2,
1417 ..Default::default()
1418 };
1419 let counts = VitalSignsCounts::default();
1420 let snapshot = build_snapshot(vs, counts, root, false, None, None);
1421 let saved = save_snapshot(&snapshot, root, Some(&nested)).unwrap();
1422 assert_eq!(saved, nested);
1423 assert!(nested.exists());
1424 }
1425
1426 #[test]
1427 fn days_to_ymd_epoch() {
1428 assert_eq!(days_to_ymd(0), (1970, 1, 1));
1429 }
1430
1431 #[test]
1432 fn days_to_ymd_known_date() {
1433 assert_eq!(days_to_ymd(20_537), (2026, 3, 25));
1434 }
1435
1436 #[test]
1437 fn health_score_perfect() {
1438 let vs = VitalSigns {
1439 dead_file_pct: Some(0.0),
1440 dead_export_pct: Some(0.0),
1441 avg_cyclomatic: 1.0,
1442 p90_cyclomatic: 2,
1443 hotspot_count: Some(0),
1444 maintainability_avg: Some(90.0),
1445 unused_dep_count: Some(0),
1446 circular_dep_count: Some(0),
1447 ..Default::default()
1448 };
1449 let score = compute_health_score(&vs, 100);
1450 assert!((score.score - 100.0).abs() < f64::EPSILON);
1451 assert_eq!(score.grade, "A");
1452 }
1453
1454 #[test]
1455 fn health_score_no_optional_metrics() {
1456 let vs = VitalSigns {
1457 avg_cyclomatic: 1.0,
1458 p90_cyclomatic: 2,
1459 ..Default::default()
1460 };
1461 let score = compute_health_score(&vs, 0);
1462 assert!((score.score - 100.0).abs() < f64::EPSILON);
1463 assert_eq!(score.grade, "A");
1464 assert!(score.penalties.dead_files.is_none());
1465 assert!(score.penalties.unused_deps.is_none());
1466 assert!(score.penalties.duplication.is_none());
1467 }
1468
1469 #[test]
1470 fn health_score_dead_code_penalty() {
1471 let vs = VitalSigns {
1472 dead_file_pct: Some(50.0),
1473 dead_export_pct: Some(30.0),
1474 avg_cyclomatic: 1.0,
1475 p90_cyclomatic: 2,
1476 ..Default::default()
1477 };
1478 let score = compute_health_score(&vs, 100);
1479 assert!((score.score - 84.0).abs() < 0.1);
1480 assert_eq!(score.grade, "B");
1481 }
1482
1483 #[test]
1484 fn health_score_complexity_penalty() {
1485 let vs = VitalSigns {
1486 avg_cyclomatic: 5.5,
1487 p90_cyclomatic: 15,
1488 ..Default::default()
1489 };
1490 let score = compute_health_score(&vs, 100);
1491 assert!((score.score - 75.0).abs() < 0.1);
1492 assert_eq!(score.grade, "B");
1493 }
1494
1495 #[test]
1496 fn health_score_prop_drilling_penalty_opt_in() {
1497 let base = || VitalSigns {
1498 avg_cyclomatic: 1.0,
1499 p90_cyclomatic: 2,
1500 ..Default::default()
1501 };
1502 let base_score = compute_health_score(&base(), 100).score;
1503
1504 let three = VitalSigns {
1506 prop_drilling_chain_count: Some(3),
1507 prop_drilling_max_depth: Some(5),
1508 ..base()
1509 };
1510 assert!((base_score - compute_health_score(&three, 100).score - 3.0).abs() < 0.1);
1511
1512 let many = VitalSigns {
1514 prop_drilling_chain_count: Some(20),
1515 ..base()
1516 };
1517 assert!((base_score - compute_health_score(&many, 100).score - 5.0).abs() < 0.1);
1518
1519 let off = VitalSigns {
1522 prop_drilling_chain_count: None,
1523 ..base()
1524 };
1525 assert!((base_score - compute_health_score(&off, 100).score).abs() < f64::EPSILON);
1526 }
1527
1528 #[test]
1529 fn health_score_clamped_at_zero() {
1530 let vs = VitalSigns {
1531 dead_file_pct: Some(100.0),
1532 dead_export_pct: Some(100.0),
1533 avg_cyclomatic: 10.0,
1534 p90_cyclomatic: 30,
1535 hotspot_count: Some(50),
1536 maintainability_avg: Some(20.0),
1537 unused_dep_count: Some(100),
1538 circular_dep_count: Some(50),
1539 ..Default::default()
1540 };
1541 let score = compute_health_score(&vs, 100);
1542 assert!((score.score).abs() < f64::EPSILON);
1543 assert_eq!(score.grade, "F");
1544 }
1545
1546 #[test]
1547 fn health_score_hotspot_normalized_by_files() {
1548 let vs = VitalSigns {
1549 avg_cyclomatic: 1.0,
1550 p90_cyclomatic: 2,
1551 hotspot_count: Some(5),
1552 ..Default::default()
1553 };
1554 let score_100 = compute_health_score(&vs, 100);
1555 let score_1000 = compute_health_score(&vs, 1000);
1556 assert!(score_1000.score > score_100.score);
1557 }
1558
1559 #[test]
1560 fn health_score_hotspot_top_pct_can_use_full_budget() {
1561 let vs = VitalSigns {
1562 avg_cyclomatic: 1.0,
1563 p90_cyclomatic: 2,
1564 hotspot_count: Some(0),
1565 hotspot_top_pct_count: Some(250),
1566 ..Default::default()
1567 };
1568
1569 let score = compute_health_score(&vs, 25_000);
1570
1571 assert_some_close(score.penalties.hotspots, 10.0);
1572 assert_close(score.score, 90.0);
1573 }
1574
1575 #[test]
1576 fn health_score_duplication_penalty() {
1577 let vs = VitalSigns {
1578 dead_file_pct: None,
1579 dead_export_pct: None,
1580 avg_cyclomatic: 1.0,
1581 critical_complexity_pct: None,
1582 p90_cyclomatic: 2,
1583 duplication_pct: Some(10.0), hotspot_count: None,
1585 hotspot_top_pct_count: None,
1586 maintainability_avg: None,
1587 maintainability_low_pct: None,
1588 unused_dep_count: None,
1589 unused_deps_per_k_files: None,
1590 circular_dep_count: None,
1591 circular_deps_per_k_files: None,
1592 counts: None,
1593 unit_size_profile: None,
1594 functions_over_60_loc_per_k: None,
1595 unit_interfacing_profile: None,
1596 p95_fan_in: None,
1597 coupling_high_pct: None,
1598 prop_drilling_chain_count: None,
1599 prop_drilling_max_depth: None,
1600 p95_render_fan_in: None,
1601 render_fan_in_high_pct: None,
1602 max_render_fan_in: None,
1603 top_render_fan_in: Vec::new(),
1604 total_loc: 0,
1605 };
1606 let score = compute_health_score(&vs, 100);
1607 assert_eq!(score.penalties.duplication, Some(5.0));
1608
1609 let vs_low = VitalSigns {
1610 duplication_pct: Some(4.0),
1611 ..vs.clone()
1612 };
1613 let score_low = compute_health_score(&vs_low, 100);
1614 assert_eq!(score_low.penalties.duplication, Some(0.0));
1615
1616 let vs_high = VitalSigns {
1617 duplication_pct: Some(20.0),
1618 ..vs
1619 };
1620 let score_high = compute_health_score(&vs_high, 100);
1621 assert_eq!(score_high.penalties.duplication, Some(10.0));
1622 }
1623
1624 #[test]
1625 fn health_score_uses_scale_invariant_monorepo_signals() {
1626 let vs = VitalSigns {
1627 dead_file_pct: Some(4.0),
1628 dead_export_pct: Some(9.0),
1629 avg_cyclomatic: 2.3,
1630 critical_complexity_pct: Some(2.3),
1631 p90_cyclomatic: 4,
1632 duplication_pct: Some(6.0),
1633 hotspot_count: Some(0),
1634 hotspot_top_pct_count: Some(250),
1635 maintainability_avg: Some(91.0),
1636 maintainability_low_pct: Some(8.0),
1637 unused_dep_count: Some(180),
1638 unused_deps_per_k_files: Some(7.2),
1639 circular_dep_count: Some(450),
1640 circular_deps_per_k_files: Some(18.0),
1641 unit_size_profile: Some(RiskProfile {
1642 low_risk: 80.0,
1643 medium_risk: 12.7,
1644 high_risk: 5.0,
1645 very_high_risk: 2.3,
1646 }),
1647 functions_over_60_loc_per_k: Some(23.0),
1648 p95_fan_in: Some(7),
1649 coupling_high_pct: Some(4.0),
1650 ..Default::default()
1651 };
1652 let score = compute_health_score(&vs, 25_000);
1653 let penalties = &score.penalties;
1654
1655 assert_some_close(penalties.dead_files, 0.8);
1656 assert_some_close(penalties.dead_exports, 1.8);
1657 assert_close(penalties.complexity, 9.2);
1658 assert!((penalties.p90_complexity).abs() < f64::EPSILON);
1659 assert_some_close(penalties.maintainability, 12.0);
1660 assert_some_close(penalties.hotspots, 10.0);
1661 assert_some_close(penalties.unused_deps, 3.6);
1662 assert_some_close(penalties.circular_deps, 9.0);
1663 assert_some_close(penalties.unit_size, 10.0);
1664 assert_some_close(penalties.coupling, 2.0);
1665 assert_some_close(penalties.duplication, 1.0);
1666 assert_close(score.score, 40.6);
1667 assert_eq!(score.grade, "D");
1668 }
1669
1670 #[test]
1671 fn load_snapshots_empty_dir() {
1672 let dir = tempfile::tempdir().unwrap();
1673 let snaps = load_snapshots(dir.path());
1674 assert!(snaps.is_empty());
1675 }
1676
1677 #[test]
1678 fn load_snapshots_returns_sorted() {
1679 let dir = tempfile::tempdir().unwrap();
1680 let root = dir.path();
1681 let snap_dir = root.join(".fallow/snapshots");
1682 std::fs::create_dir_all(&snap_dir).unwrap();
1683
1684 let older = make_test_snapshot("2026-01-01T00:00:00Z", Some(72.0));
1685 let newer = make_test_snapshot("2026-03-01T00:00:00Z", Some(78.0));
1686
1687 std::fs::write(
1688 snap_dir.join("2026-03-01T00-00-00Z.json"),
1689 serde_json::to_string(&newer).unwrap(),
1690 )
1691 .unwrap();
1692 std::fs::write(
1693 snap_dir.join("2026-01-01T00-00-00Z.json"),
1694 serde_json::to_string(&older).unwrap(),
1695 )
1696 .unwrap();
1697
1698 let loaded = load_snapshots(root);
1699 assert_eq!(loaded.len(), 2);
1700 assert_eq!(loaded[0].timestamp, "2026-01-01T00:00:00Z");
1701 assert_eq!(loaded[1].timestamp, "2026-03-01T00:00:00Z");
1702 }
1703
1704 #[test]
1705 fn load_snapshots_skips_corrupt_files() {
1706 let dir = tempfile::tempdir().unwrap();
1707 let root = dir.path();
1708 let snap_dir = root.join(".fallow/snapshots");
1709 std::fs::create_dir_all(&snap_dir).unwrap();
1710
1711 std::fs::write(snap_dir.join("corrupt.json"), "not valid json").unwrap();
1712 let good = make_test_snapshot("2026-02-01T00:00:00Z", Some(80.0));
1713 std::fs::write(
1714 snap_dir.join("good.json"),
1715 serde_json::to_string(&good).unwrap(),
1716 )
1717 .unwrap();
1718
1719 let loaded = load_snapshots(root);
1720 assert_eq!(loaded.len(), 1);
1721 assert_eq!(loaded[0].timestamp, "2026-02-01T00:00:00Z");
1722 }
1723
1724 #[test]
1725 fn load_snapshots_ignores_non_json() {
1726 let dir = tempfile::tempdir().unwrap();
1727 let root = dir.path();
1728 let snap_dir = root.join(".fallow/snapshots");
1729 std::fs::create_dir_all(&snap_dir).unwrap();
1730
1731 std::fs::write(snap_dir.join("readme.txt"), "not a snapshot").unwrap();
1732
1733 let loaded = load_snapshots(root);
1734 assert!(loaded.is_empty());
1735 }
1736
1737 #[test]
1738 fn compute_trend_no_snapshots() {
1739 let vs = make_test_vital_signs();
1740 let counts = make_test_counts();
1741 assert!(compute_trend(&vs, &counts, Some(78.0), &[]).is_none());
1742 }
1743
1744 #[test]
1745 fn compute_trend_improving() {
1746 let prev = make_test_snapshot("2026-01-01T00:00:00Z", Some(72.0));
1747 let vs = VitalSigns {
1748 dead_file_pct: Some(2.8),
1749 dead_export_pct: Some(7.5),
1750 avg_cyclomatic: 4.1,
1751 p90_cyclomatic: 12,
1752 hotspot_count: Some(3),
1753 maintainability_avg: Some(75.0),
1754 unused_dep_count: Some(3),
1755 circular_dep_count: Some(1),
1756 ..Default::default()
1757 };
1758 let counts = VitalSignsCounts {
1759 total_files: 100,
1760 total_exports: 500,
1761 dead_files: 3,
1762 dead_exports: 38,
1763 files_scored: Some(95),
1764 total_deps: 40,
1765 ..Default::default()
1766 };
1767
1768 let trend = compute_trend(&vs, &counts, Some(78.0), &[prev]).unwrap();
1769 assert_eq!(trend.compared_to.timestamp, "2026-01-01T00:00:00Z");
1770 assert_eq!(trend.snapshots_loaded, 1);
1771 assert_eq!(trend.overall_direction, TrendDirection::Improving);
1772
1773 let score_metric = trend.metrics.iter().find(|m| m.name == "score").unwrap();
1774 assert_eq!(score_metric.direction, TrendDirection::Improving);
1775 assert!((score_metric.delta - 6.0).abs() < f64::EPSILON);
1776 }
1777
1778 #[test]
1779 fn compute_trend_stable_within_tolerance() {
1780 let prev = make_test_snapshot("2026-01-01T00:00:00Z", Some(78.0));
1781 let vs = make_test_vital_signs();
1782 let counts = make_test_counts();
1783
1784 let trend = compute_trend(&vs, &counts, Some(78.3), &[prev]).unwrap();
1785 let score_metric = trend.metrics.iter().find(|m| m.name == "score").unwrap();
1786 assert_eq!(score_metric.direction, TrendDirection::Stable);
1787 }
1788
1789 #[test]
1790 fn compute_trend_uses_most_recent_snapshot() {
1791 let older = make_test_snapshot("2026-01-01T00:00:00Z", Some(60.0));
1792 let newer = make_test_snapshot("2026-03-01T00:00:00Z", Some(72.0));
1793 let vs = make_test_vital_signs();
1794 let counts = make_test_counts();
1795
1796 let trend = compute_trend(&vs, &counts, Some(78.0), &[older, newer]).unwrap();
1797 assert_eq!(trend.compared_to.score, Some(72.0));
1798 assert_eq!(trend.snapshots_loaded, 2);
1799 }
1800
1801 #[test]
1802 fn compute_trend_includes_raw_counts() {
1803 let prev = make_test_snapshot("2026-01-01T00:00:00Z", Some(72.0));
1804 let vs = make_test_vital_signs();
1805 let counts = make_test_counts();
1806
1807 let trend = compute_trend(&vs, &counts, Some(78.0), &[prev]).unwrap();
1808 let dead_files = trend
1809 .metrics
1810 .iter()
1811 .find(|m| m.name == "dead_file_pct")
1812 .unwrap();
1813 assert!(dead_files.previous_count.is_some());
1814 assert!(dead_files.current_count.is_some());
1815 }
1816
1817 fn make_test_vital_signs() -> VitalSigns {
1818 VitalSigns {
1819 dead_file_pct: Some(3.2),
1820 dead_export_pct: Some(8.1),
1821 avg_cyclomatic: 4.2,
1822 p90_cyclomatic: 12,
1823 hotspot_count: Some(5),
1824 maintainability_avg: Some(72.4),
1825 unused_dep_count: Some(4),
1826 circular_dep_count: Some(2),
1827 ..Default::default()
1828 }
1829 }
1830
1831 fn make_test_counts() -> VitalSignsCounts {
1832 VitalSignsCounts {
1833 total_files: 100,
1834 total_exports: 500,
1835 dead_files: 3,
1836 dead_exports: 40,
1837 files_scored: Some(95),
1838 total_deps: 42,
1839 ..Default::default()
1840 }
1841 }
1842
1843 fn make_test_snapshot(timestamp: &str, score: Option<f64>) -> VitalSignsSnapshot {
1844 VitalSignsSnapshot {
1845 snapshot_schema_version: SNAPSHOT_SCHEMA_VERSION,
1846 version: "2.5.5".into(),
1847 timestamp: timestamp.into(),
1848 git_sha: Some("abc1234".into()),
1849 git_branch: Some("main".into()),
1850 shallow_clone: false,
1851 vital_signs: VitalSigns {
1852 dead_file_pct: Some(3.2),
1853 dead_export_pct: Some(8.1),
1854 avg_cyclomatic: 4.7,
1855 p90_cyclomatic: 12,
1856 hotspot_count: Some(5),
1857 maintainability_avg: Some(72.4),
1858 unused_dep_count: Some(4),
1859 circular_dep_count: Some(2),
1860 ..Default::default()
1861 },
1862 counts: VitalSignsCounts {
1863 total_files: 100,
1864 total_exports: 500,
1865 dead_files: 3,
1866 dead_exports: 40,
1867 files_scored: Some(95),
1868 total_deps: 42,
1869 ..Default::default()
1870 },
1871 score,
1872 grade: score.map(|s| letter_grade(s).to_string()),
1873 coverage_model: None,
1874 }
1875 }
1876}