barad-dur 0.18.0

The all-seeing repository 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
pub(crate) mod format;
use format::*;

use colored::Colorize;

use crate::scorer::AnalysisReport;
use crate::trend::TrendSummary;

/// Render the analysis report as a colored CLI string.
///
/// When `trend` is `Some` and `!trend.delta.is_first`, a delta line and direction
/// indicator are injected after the overall score line. When `trend` is `None` or
/// `trend.delta.is_first`, the first-snapshot notice is emitted instead.
pub fn render(report: &AnalysisReport, verbosity: u8, trend: Option<&TrendSummary>) -> String {
    let mut out = String::new();
    out.push_str(&render_repo_info(report));
    out.push_str(&render_score_and_trend(report, trend));
    out.push_str(&render_categories(report, trend, verbosity));
    out.push_str(&render_actions_and_footer(report));
    out
}

fn render_repo_info(report: &AnalysisReport) -> String {
    let mut out = String::new();

    out.push_str(&format!(
        "\n{}\n",
        "━━━ Barad-dur ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━".bold()
    ));
    out.push_str(&format!(
        "  {} {} on {}\n",
        "Repository:".dimmed(),
        report.repo_name.bold(),
        report.branch
    ));
    out.push_str(&format!(
        "  {} {} commits, {} authors, {} files\n",
        "Scope:".dimmed(),
        report.total_commits,
        report.total_authors,
        report.total_files
    ));
    if report.time_window_months > 0 {
        out.push_str(&format!(
            "  {} last {} months\n",
            "Window:".dimmed(),
            report.time_window_months
        ));
    }
    if let Some(meta) = &report.remote_meta {
        out.push_str(&format!("  {} {}\n", "Source:".dimmed(), meta.url.bold()));
        let mut details = Vec::new();
        if let Some(stars) = meta.stars {
            details.push(format!("Stars: {}", stars));
        }
        if let Some(lang) = &meta.language {
            details.push(format!("Language: {}", lang));
        }
        if let Some(issues) = meta.open_issues {
            details.push(format!("Issues: {}", issues));
        }
        if !details.is_empty() {
            out.push_str(&format!("  {}\n", details.join("   ").dimmed()));
        }
        if let Some(desc) = &meta.description {
            if !desc.is_empty() {
                out.push_str(&format!("  {}\n", desc.dimmed()));
            }
        }
    }

    out
}

fn render_trend_line(trend: &TrendSummary, report: &AnalysisReport) -> String {
    let mut out = String::new();

    if !trend.delta.is_first && !trend.branch_mismatch_warning {
        let delta = trend.delta.overall;
        let delta_str = if delta >= 0 {
            format!("+{} vs last run", delta)
        } else {
            format!("{} vs last run", delta)
        };
        out.push_str(&format!("  {}\n", delta_str.dimmed()));

        if let Some(velocity) = &trend.velocity {
            let direction_str = direction_word(&velocity.direction);
            let arrow = direction_arrow(&velocity.direction);
            out.push_str(&format!("  {} {}\n", arrow, direction_str.dimmed()));
        }
    }

    let _ = report; // kept for API symmetry
    out
}

fn render_score_and_trend(report: &AnalysisReport, trend: Option<&TrendSummary>) -> String {
    let mut out = String::new();

    // Branch mismatch warning — suppresses delta and notifies the user.
    if let Some(summary) = trend {
        if summary.branch_mismatch_warning {
            let prior_branch = summary
                .history
                .last()
                .map(|e| e.branch.as_str())
                .unwrap_or("unknown");
            out.push_str(&format!(
                "  {}\n",
                format!(
                    "Warning: prior history on '{}'; current branch is '{}'",
                    prior_branch, report.branch
                )
                .yellow()
            ));
        }
    }

    // First-run notice or trend delta
    let is_first = trend.map(|t| t.delta.is_first).unwrap_or(true);
    let is_branch_mismatch = trend.map(|t| t.branch_mismatch_warning).unwrap_or(false);
    if is_first && !is_branch_mismatch {
        out.push_str(&format!(
            "  {}\n",
            "Trend: first snapshot recorded".dimmed()
        ));
    }

    // Overall score
    out.push_str(&format!(
        "\n  {} {} {}\n",
        "Overall Score:".bold(),
        format_score_bar(report.overall_score, 20),
        format_score_number(report.overall_score)
    ));

    // Delta and direction (only when a prior run exists on this branch, and no branch mismatch)
    if let Some(summary) = trend {
        out.push_str(&render_trend_line(summary, report));
    }

    out
}

fn render_single_category(
    cat: &crate::metrics::CategoryResult,
    delta: Option<i32>,
    verbosity: u8,
) -> String {
    let mut out = String::new();

    let delta_suffix = delta
        .map(|d| {
            if d >= 0 {
                format!("  (+{})", d)
            } else {
                format!("  ({})", d)
            }
        })
        .unwrap_or_default();

    out.push_str(&format!(
        "\n  {} {} {}{}\n",
        format!("{}", cat.name).bold(),
        format_score_bar(cat.score, 12),
        format_score_number(cat.score),
        delta_suffix.dimmed()
    ));

    if verbosity > 0 {
        for metric in &cat.metrics {
            // Unscored metric (insufficient data): dimmed dot + dash.
            let score_indicator = metric
                .score
                .map(format_score_dot)
                .unwrap_or_else(|| "".dimmed().to_string());
            let score_label = metric
                .score
                .map(format_score_number)
                .unwrap_or_else(|| "".dimmed().to_string());
            out.push_str(&format!(
                "    {} {} {}  {}\n",
                score_indicator,
                metric.name,
                score_label,
                metric.description.dimmed()
            ));
            if verbosity > 1 {
                out.push_str(&format!(
                    "      {} {}\n",
                    "value:".dimmed(),
                    metric.raw_value.to_string().bold()
                ));
            }
        }
    }

    out
}

fn render_dep_section(dep_reports: &[crate::deps::EcosystemReport]) -> String {
    let mut out = String::new();

    out.push_str(&format!(
        "\n{}\n",
        "───────────────────────────────────────────────────".dimmed()
    ));
    out.push_str(&format!("  {}\n", "Dependencies:".bold()));
    for eco in dep_reports {
        out.push_str(&format!(
            "  {} {}: {:.1} libyears avg ({} deps)\n",
            "".bright_blue(),
            eco.ecosystem.display_name(),
            eco.mean_drift_years,
            eco.total_deps
        ));
        for dep in &eco.critical_deps {
            let suffix = if dep.vulnerabilities.is_empty() {
                String::new()
            } else {
                format!(" [{} CVE(s)]", dep.vulnerabilities.len())
            };
            out.push_str(&format!(
                "    {} {} {}{:.1}y behind{}\n",
                "".yellow(),
                dep.name,
                dep.current_version,
                dep.drift_years,
                suffix
            ));
        }
    }

    out
}

fn render_top_unstable_files(report: &AnalysisReport) -> String {
    if report.per_file_coupling.is_empty() {
        return String::new();
    }

    report.per_file_coupling.iter().take(5).fold(
        String::from("    Top unstable files:\n"),
        |acc, m| {
            acc + &format!(
                "      {:.2}  {}  (Ca={}, Ce={})\n",
                m.instability, m.path, m.ca, m.ce
            )
        },
    )
}

fn render_categories(
    report: &AnalysisReport,
    trend: Option<&TrendSummary>,
    verbosity: u8,
) -> String {
    let mut out = String::new();

    out.push_str(&format!(
        "\n{}\n",
        "───────────────────────────────────────────────────".dimmed()
    ));

    let category_deltas = trend
        .filter(|t| !t.delta.is_first && !t.branch_mismatch_warning)
        .map(|t| &t.delta.categories);

    for cat in &report.categories {
        let delta = category_deltas
            .and_then(|deltas| deltas.get(&cat.name))
            .copied();
        out.push_str(&render_single_category(cat, delta, verbosity));
    }

    out.push_str(&render_top_unstable_files(report));

    if !report.dep_ecosystem_reports.is_empty() {
        out.push_str(&render_dep_section(&report.dep_ecosystem_reports));
    }

    out
}

fn render_actions_and_footer(report: &AnalysisReport) -> String {
    let mut out = String::new();

    if !report.top_actions.is_empty() {
        out.push_str(&format!(
            "\n{}\n",
            "───────────────────────────────────────────────────".dimmed()
        ));
        out.push_str(&format!("  {}\n", "Top Actions:".bold()));
        for (i, action) in report.top_actions.iter().enumerate() {
            out.push_str(&format!("  {}. {}\n", i + 1, action.text));
        }
    }

    out.push_str(&format!(
        "{}\n\n",
        "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━".bold()
    ));

    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::metrics::{CategoryResult, MetricValue, RawValue};
    use crate::scorer::ActionItem;
    use crate::trend::{TrendDelta, TrendSummary, TrendVelocity, VelocityDirection};
    use std::collections::HashMap;

    fn make_report() -> AnalysisReport {
        AnalysisReport {
            repo_name: "test-repo".into(),
            branch: "main".into(),
            time_window_months: 6,
            total_commits: 100,
            total_authors: 5,
            total_files: 50,
            overall_score: 72,
            categories: vec![CategoryResult {
                name: "Health".into(),
                score: 72,
                metrics: vec![MetricValue {
                    name: "Bus factor".into(),
                    description: "2 (risky)".into(),
                    raw_value: RawValue::Integer(2),
                    score: Some(50),
                }],
            }],
            top_actions: vec![ActionItem {
                text: "[Health] Bus factor (score: 50) — Improve".into(),
                target_tab: Some("ownership".into()),
                sort_by: None,
            }],
            remote_meta: None,
            file_hotspots: vec![],
            coupling_pairs: vec![],
            author_ownership: vec![],
            file_ages: vec![],
            author_cards: vec![],
            history: vec![],
            dep_ecosystem_reports: vec![],
            audit: None,
            per_file_coupling: vec![],
            import_edges: vec![],
            import_cycles: vec![],
            score_thresholds: Default::default(),
        }
    }

    fn make_first_run_trend() -> TrendSummary {
        TrendSummary {
            delta: TrendDelta {
                overall: 0,
                delta_vs_oldest: 0,
                categories: HashMap::new(),
                is_first: true,
            },
            sparkline: vec![],
            velocity: None,
            branch_mismatch_warning: false,
            history: vec![],
        }
    }

    fn make_subsequent_run_trend(delta: i32) -> TrendSummary {
        TrendSummary {
            delta: TrendDelta {
                overall: delta,
                delta_vs_oldest: delta,
                categories: HashMap::new(),
                is_first: false,
            },
            sparkline: vec![],
            velocity: Some(TrendVelocity {
                direction: VelocityDirection::Stable,
                points_per_run: delta as f64,
                window_size: 2,
            }),
            branch_mismatch_warning: false,
            history: vec![],
        }
    }

    #[test]
    fn render_contains_header() {
        let report = make_report();
        let output = render(&report, 0, None);
        assert!(output.contains("Barad-dur"));
    }

    #[test]
    fn render_verbose_shows_dash_for_unscored_metric() {
        let mut report = make_report();
        report.categories[0].metrics.push(MetricValue {
            name: "Knowledge distribution".into(),
            description: "Solo project — not applicable".into(),
            raw_value: RawValue::Text("N/A".into()),
            score: None,
        });
        let output = render(&report, 1, None);
        assert!(
            output.contains(""),
            "unscored metrics must render a dash in verbose CLI output"
        );
        assert!(
            !output.contains("Knowledge distribution 100"),
            "unscored metrics must not display a fake score"
        );
    }

    #[test]
    fn render_contains_repo_info() {
        let report = make_report();
        let output = render(&report, 0, None);
        assert!(output.contains("test-repo"));
        assert!(output.contains("main"));
    }

    #[test]
    fn render_contains_category() {
        let report = make_report();
        let output = render(&report, 0, None);
        assert!(output.contains("Health"));
    }

    #[test]
    fn render_verbose_shows_metrics() {
        let report = make_report();
        let output = render(&report, 1, None);
        assert!(output.contains("Bus factor"));
        assert!(output.contains("50/100"));
    }

    #[test]
    fn render_very_verbose_shows_raw_value() {
        let report = make_report();
        let output = render(&report, 2, None);
        assert!(output.contains("value:"));
        assert!(output.contains('2'));
    }

    #[test]
    fn render_first_run_shows_trend_notice() {
        let report = make_report();
        let trend = make_first_run_trend();
        let output = render(&report, 0, Some(&trend));
        assert!(
            output.contains("Trend: first snapshot recorded"),
            "first run should show trend notice"
        );
    }

    #[test]
    fn render_subsequent_run_no_trend_notice() {
        let report = make_report();
        let trend = make_subsequent_run_trend(0);
        let output = render(&report, 0, Some(&trend));
        assert!(
            !output.contains("Trend: first snapshot recorded"),
            "subsequent run should not show first-run trend notice"
        );
    }

    #[test]
    fn render_contains_actions() {
        let report = make_report();
        let output = render(&report, 0, None);
        assert!(output.contains("Top Actions"));
    }

    #[test]
    fn render_category_row_includes_delta_on_subsequent_run() {
        let report = make_report(); // Has "Health" category at score 72
        let mut category_deltas = HashMap::new();
        category_deltas.insert("Health".to_string(), 3_i32);
        let trend = TrendSummary {
            delta: TrendDelta {
                overall: 3,
                delta_vs_oldest: 3,
                categories: category_deltas,
                is_first: false,
            },
            sparkline: vec![],
            velocity: Some(TrendVelocity {
                direction: VelocityDirection::Improving,
                points_per_run: 3.0,
                window_size: 2,
            }),
            branch_mismatch_warning: false,
            history: vec![],
        };
        let output = render(&report, 0, Some(&trend));

        // The Health category row must contain the delta marker "+3"
        let health_line = output.lines().find(|l| l.contains("Health")).unwrap_or("");
        assert!(
            health_line.contains("+3"),
            "Health category row should show delta '+3', got: {health_line:?}"
        );
    }

    #[test]
    fn render_category_row_shows_negative_delta() {
        let report = make_report(); // Has "Health" category at score 72
        let mut category_deltas = HashMap::new();
        category_deltas.insert("Health".to_string(), -5_i32);
        let trend = TrendSummary {
            delta: TrendDelta {
                overall: -5,
                delta_vs_oldest: -5,
                categories: category_deltas,
                is_first: false,
            },
            sparkline: vec![],
            velocity: Some(TrendVelocity {
                direction: VelocityDirection::Declining,
                points_per_run: -5.0,
                window_size: 2,
            }),
            branch_mismatch_warning: false,
            history: vec![],
        };
        let output = render(&report, 0, Some(&trend));

        let health_line = output.lines().find(|l| l.contains("Health")).unwrap_or("");
        assert!(
            health_line.contains("-5"),
            "Health category row should show delta '-5', got: {health_line:?}"
        );
    }

    #[test]
    fn direction_arrow_maps_correctly() {
        assert_eq!(direction_arrow(&VelocityDirection::Improving), "");
        assert_eq!(direction_arrow(&VelocityDirection::Declining), "");
        assert_eq!(direction_arrow(&VelocityDirection::Stable), "");
    }

    #[test]
    fn render_category_row_no_delta_on_first_run() {
        let report = make_report();
        let trend = make_first_run_trend();
        let output = render(&report, 0, Some(&trend));

        // On first run, category rows should NOT have delta markers
        let health_line = output.lines().find(|l| l.contains("Health")).unwrap_or("");
        // No (+N) or (-N) pattern should appear on the category line for a first run
        assert!(
            !health_line.contains("(+") && !health_line.contains("(-"),
            "Health category row should not show delta on first run, got: {health_line:?}"
        );
    }

    #[test]
    fn render_shows_ecosystem_summary_line() {
        use crate::deps::{Ecosystem, EcosystemReport};
        let mut report = make_report();
        report.dep_ecosystem_reports = vec![EcosystemReport {
            ecosystem: Ecosystem::Cargo,
            total_deps: 3,
            mean_drift_years: 1.5,
            total_drift_years: 4.5,
            critical_deps: vec![],
        }];
        let output = render(&report, 0, None);
        assert!(
            output.contains("Cargo"),
            "output should contain ecosystem name"
        );
        assert!(
            output.contains("1.5 libyears avg"),
            "output should contain mean drift"
        );
        assert!(output.contains("3 deps"), "output should contain dep count");
    }

    #[test]
    fn render_shows_critical_dep_with_cve_count() {
        use crate::deps::{DepAge, DepTier, Ecosystem, EcosystemReport, Vuln};
        let mut report = make_report();
        report.dep_ecosystem_reports = vec![EcosystemReport {
            ecosystem: Ecosystem::Cargo,
            total_deps: 1,
            mean_drift_years: 6.2,
            total_drift_years: 6.2,
            critical_deps: vec![DepAge {
                name: "old-crate".into(),
                ecosystem: Ecosystem::Cargo,
                current_version: "0.1.0".into(),
                drift_years: 6.2,
                tier: DepTier::Critical,
                vulnerabilities: vec![Vuln {
                    id: "RUSTSEC-0000-0001".into(),
                    severity: "HIGH".into(),
                    description: "a vulnerability".into(),
                }],
            }],
        }];
        let output = render(&report, 0, None);
        assert!(
            output.contains("old-crate"),
            "output should contain dep name"
        );
        assert!(
            output.contains("0.1.0"),
            "output should contain dep version"
        );
        assert!(output.contains("6.2y"), "output should contain drift years");
        assert!(output.contains("1 CVE"), "output should contain CVE count");
    }

    #[test]
    fn cli_shows_top_unstable_files_sorted_by_instability() {
        use crate::scorer::FileCouplingMetrics;
        let mut report = make_report();
        // Builder (step 02-02) produces vec sorted descending by instability
        report.per_file_coupling = vec![
            FileCouplingMetrics {
                path: "src/unstable.rs".into(),
                ca: 1,
                ce: 9,
                instability: 0.9,
            },
            FileCouplingMetrics {
                path: "src/mid.rs".into(),
                ca: 2,
                ce: 3,
                instability: 0.5,
            },
            FileCouplingMetrics {
                path: "src/stable.rs".into(),
                ca: 5,
                ce: 0,
                instability: 0.1,
            },
        ];
        let output = render(&report, 0, None);

        // Section header must appear
        assert!(
            output.contains("unstable"),
            "output should contain 'unstable' section header"
        );

        // All 3 file paths must appear
        assert!(
            output.contains("src/unstable.rs"),
            "output should contain most unstable file"
        );
        assert!(
            output.contains("src/mid.rs"),
            "output should contain mid-instability file"
        );
        assert!(
            output.contains("src/stable.rs"),
            "output should contain stable file"
        );

        // Instability values must appear in descending order
        let pos_unstable = output
            .find("src/unstable.rs")
            .expect("unstable.rs not found");
        let pos_mid = output.find("src/mid.rs").expect("mid.rs not found");
        let pos_stable = output.find("src/stable.rs").expect("stable.rs not found");
        assert!(
            pos_unstable < pos_mid && pos_mid < pos_stable,
            "files must appear in descending instability order: unstable={pos_unstable}, mid={pos_mid}, stable={pos_stable}"
        );

        // Instability values and Ca/Ce values must appear
        assert!(
            output.contains("0.90"),
            "output should contain instability 0.90"
        );
        assert!(
            output.contains("0.50"),
            "output should contain instability 0.50"
        );
        assert!(
            output.contains("0.10"),
            "output should contain instability 0.10"
        );
        assert!(output.contains("Ca=1"), "output should contain Ca=1");
        assert!(output.contains("Ce=9"), "output should contain Ce=9");
    }

    #[test]
    fn cli_skips_unstable_section_when_no_data() {
        let report = make_report(); // per_file_coupling is empty by default
        let output = render(&report, 0, None);
        assert!(
            !output.contains("unstable"),
            "output should not contain 'unstable' when per_file_coupling is empty"
        );
    }
}