pmat 3.30.1

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP)
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
#![cfg_attr(coverage_nightly, coverage(off))]

use super::types::{DepCategory, DepsAuditReport, ParetoEffort, ParetoEntry};
use crate::cli::colors;

/// Print Pareto analysis report
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn print_pareto_report(entries: &[ParetoEntry]) {
    println!("{}", colors::rule());
    println!(
        "{}πŸ“Š  Pareto Analysis: 80/20 Dependency Removal{}",
        colors::BOLD,
        colors::RESET
    );
    println!("{}", colors::rule());
    println!();
    println!(
        "{}ROI = Transitive Deps Saved / Effort{}",
        colors::DIM,
        colors::RESET
    );
    println!(
        "{}Higher ROI = Better bang for buck{}",
        colors::DIM,
        colors::RESET
    );
    println!();

    if entries.is_empty() {
        println!("No removable dependencies found.");
        return;
    }

    // Calculate 80% of total savings
    let total_transitive: usize = entries.iter().map(|e| e.transitive_deps).sum();
    let target_80 = (total_transitive as f32 * 0.8) as usize;

    println!("β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”");
    println!("β”‚ Dependency          β”‚ Trans.Depsβ”‚ Effort β”‚ ROI    β”‚ Reason                      β”‚");
    println!("β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€");

    let mut cumulative = 0;
    let mut marked_80 = false;
    for entry in entries.iter().take(20) {
        cumulative += entry.transitive_deps;
        let marker = if !marked_80 && cumulative >= target_80 {
            marked_80 = true;
            "← 80%"
        } else {
            ""
        };

        let name_str = entry
            .name
            .get(..entry.name.len().min(19))
            .unwrap_or(&entry.name);
        let reason_str = entry
            .reason
            .get(..entry.reason.len().min(21))
            .unwrap_or(&entry.reason);
        println!(
            "β”‚ {}{:<19}{} β”‚ {:>9} β”‚ {:>6} β”‚ {}{:>6.1}{} β”‚ {:<21} {:>5} β”‚",
            colors::CYAN,
            name_str,
            colors::RESET,
            entry.transitive_deps,
            entry.effort.label(),
            colors::BOLD,
            entry.roi,
            colors::RESET,
            reason_str,
            marker
        );
    }
    println!("β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜");
    println!();

    // Summary
    let top_5_savings: usize = entries.iter().take(5).map(|e| e.transitive_deps).sum();
    let top_5_pct = if total_transitive > 0 {
        (top_5_savings as f32 / total_transitive as f32 * 100.0) as usize
    } else {
        0
    };

    println!("{}πŸ’‘ Summary:{}", colors::BOLD, colors::RESET);
    println!(
        "   Total transitive deps from candidates: {}{}{}",
        colors::BOLD_WHITE,
        total_transitive,
        colors::RESET
    );
    println!(
        "   Top 5 removals save: {}{}{} deps ({}{}{}% of total)",
        colors::BOLD_WHITE,
        top_5_savings,
        colors::RESET,
        colors::BOLD_WHITE,
        top_5_pct,
        colors::RESET
    );
    println!();

    // Actionable commands
    println!(
        "{}πŸ”§ Quick Wins (Low Effort, High ROI):{}",
        colors::BOLD,
        colors::RESET
    );
    for entry in entries
        .iter()
        .filter(|e| matches!(e.effort, ParetoEffort::Low) && e.roi > 10.0)
        .take(5)
    {
        println!(
            "   {}cargo rm {}{} # saves {}{}{} transitive deps",
            colors::GREEN,
            entry.name,
            colors::RESET,
            colors::BOLD_WHITE,
            entry.transitive_deps,
            colors::RESET
        );
    }
    println!();
    println!("{}", colors::rule());
}

#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
/// Print text report.
///
/// `show_all` is the `--all` flag. The handler already honours it when it
/// builds `report.dependencies` (it stops filtering out `Core` and
/// `Sovereign`), but this renderer only grouped the four categories `--all`
/// does NOT add β€” Removable, Heavy, Replaceable, DevOnly β€” so the extra rows
/// could never be printed for any input, while the footer promised them
/// unconditionally, including on the run that had just supplied `--all`. Text
/// output was byte-identical with and without the flag on a project with
/// `serde` and `rayon` in it; only `-f json` differed.
pub fn print_text_report(report: &DepsAuditReport, show_all: bool) {
    println!("{}", colors::rule());
    println!(
        "{}πŸ”  Dependency Audit Report (with Graph Analysis){}",
        colors::BOLD,
        colors::RESET
    );
    println!("{}", colors::rule());
    println!();
    println!("{}πŸ“Š  Summary{}", colors::BOLD, colors::RESET);
    println!(
        "  Direct Dependencies:   {}{}{}",
        colors::BOLD_WHITE,
        report.direct_deps,
        colors::RESET
    );
    println!(
        "  Transitive Deps:       {}{}{}",
        colors::BOLD_WHITE,
        report.transitive_deps,
        colors::RESET
    );
    // Labelled by its source: this is the Cargo.lock package set, and it is
    // exactly direct + transitive (it used to be a third, smaller population).
    println!(
        "  Total (Cargo.lock):    {}{}{}",
        colors::BOLD_WHITE,
        report.total_deps,
        colors::RESET
    );
    println!(
        "  Sovereign Stack:       {}{}{} βœ…",
        colors::GREEN,
        report.sovereign_deps,
        colors::RESET
    );
    println!(
        "  Replaceable:           {}{}{} πŸ”„",
        colors::YELLOW,
        report.replaceable_deps,
        colors::RESET
    );
    println!(
        "  Removable:             {}{}{} ❌",
        colors::RED,
        report.removable_deps,
        colors::RESET
    );
    println!(
        "  Heavy (bloat):         {}{}{} ⚠️",
        colors::YELLOW,
        report.heavy_deps,
        colors::RESET
    );
    println!(
        "  Orphans (easy remove): {}{}{} 🎯",
        colors::BOLD_WHITE,
        report.orphan_deps,
        colors::RESET
    );
    println!(
        "  Bridges (connectors):  {}{}{} πŸŒ‰",
        colors::BOLD_WHITE,
        report.bridge_deps,
        colors::RESET
    );
    let savings_color = if report.estimated_savings_kb > 0 {
        colors::YELLOW
    } else {
        colors::BOLD_WHITE
    };
    println!(
        "  Est. Savings:          {}~{}KB (~{}MB){}",
        savings_color,
        report.estimated_savings_kb,
        report.estimated_savings_kb / 1024,
        colors::RESET
    );
    println!();

    // Top critical deps by PageRank
    if !report.top_critical.is_empty() {
        println!(
            "{}πŸ“ˆ  Critical Dependencies (by PageRank){}",
            colors::BOLD,
            colors::RESET
        );
        println!("  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”");
        println!("  β”‚ Dependency          β”‚ Score    β”‚");
        println!("  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€");
        for (name, score) in report.top_critical.iter().take(5) {
            let name_str = name.get(..name.len().min(19)).unwrap_or(name);
            println!(
                "  β”‚ {}{:<19}{} β”‚ {}{:.6}{} β”‚",
                colors::CYAN,
                name_str,
                colors::RESET,
                colors::BOLD,
                score,
                colors::RESET
            );
        }
        println!("  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜");
        println!(
            "  {}(Higher = more deps depend on it, harder to remove){}",
            colors::DIM,
            colors::RESET
        );
        println!();
    }

    // Group by category
    let removable: Vec<_> = report
        .dependencies
        .iter()
        .filter(|d| d.category == DepCategory::Removable)
        .collect();
    let heavy: Vec<_> = report
        .dependencies
        .iter()
        .filter(|d| d.category == DepCategory::Heavy)
        .collect();
    let replaceable: Vec<_> = report
        .dependencies
        .iter()
        .filter(|d| d.category == DepCategory::Replaceable)
        .collect();
    let dev_only: Vec<_> = report
        .dependencies
        .iter()
        .filter(|d| d.category == DepCategory::DevOnly)
        .collect();

    if !removable.is_empty() {
        println!(
            "{}❌  Removable Dependencies{}",
            colors::BOLD_RED,
            colors::RESET
        );
        println!("  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”");
        println!("  β”‚ Dependency          β”‚ Reason                                 β”‚");
        println!("  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€");
        for dep in &removable {
            let name_str = dep.name.get(..dep.name.len().min(19)).unwrap_or(&dep.name);
            let reason_str = dep
                .reason
                .get(..dep.reason.len().min(38))
                .unwrap_or(&dep.reason);
            println!(
                "  β”‚ {}{:<19}{} β”‚ {:<38} β”‚",
                colors::CYAN,
                name_str,
                colors::RESET,
                reason_str
            );
        }
        println!("  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜");
        println!();
    }

    if !heavy.is_empty() {
        println!(
            "{}⚠️   Heavy Dependencies (Bloat){}",
            colors::BOLD_YELLOW,
            colors::RESET
        );
        println!("  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”");
        println!("  β”‚ Dependency          β”‚ Size KB  β”‚ Reason                      β”‚");
        println!("  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€");
        for dep in &heavy {
            let name_str = dep.name.get(..dep.name.len().min(19)).unwrap_or(&dep.name);
            let reason_str = dep
                .reason
                .get(..dep.reason.len().min(27))
                .unwrap_or(&dep.reason);
            println!(
                "  β”‚ {}{:<19}{} β”‚ {:>8} β”‚ {:<27} β”‚",
                colors::CYAN,
                name_str,
                colors::RESET,
                dep.estimated_size_kb,
                reason_str
            );
        }
        println!("  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜");
        println!();
    }

    if !replaceable.is_empty() {
        println!(
            "{}πŸ”„  Replaceable with Sovereign Stack{}",
            colors::BOLD_YELLOW,
            colors::RESET
        );
        println!("  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”");
        println!("  β”‚ Dependency          β”‚ Replacement         β”‚ Benefit           β”‚");
        println!("  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€");
        for dep in &replaceable {
            let replacement = dep.replacement.as_deref().unwrap_or("-");
            let name_str = dep.name.get(..dep.name.len().min(19)).unwrap_or(&dep.name);
            let repl_str = replacement
                .get(..replacement.len().min(19))
                .unwrap_or(replacement);
            let reason_str = dep
                .reason
                .get(..dep.reason.len().min(17))
                .unwrap_or(&dep.reason);
            println!(
                "  β”‚ {}{:<19}{} β”‚ {}{:<19}{} β”‚ {:<17} β”‚",
                colors::CYAN,
                name_str,
                colors::RESET,
                colors::GREEN,
                repl_str,
                colors::RESET,
                reason_str
            );
        }
        println!("  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜");
        println!();
    }

    if !dev_only.is_empty() {
        println!(
            "{}πŸ§ͺ  Dev-Only Dependencies ({}){}",
            colors::BOLD,
            dev_only.len(),
            colors::RESET
        );
        let names: Vec<_> = dev_only.iter().map(|d| d.name.as_str()).collect();
        println!("  {}", names.join(", "));
        println!();
    }

    print!("{}", all_sections(report, show_all));

    if !report.recommendations.is_empty() {
        println!("{}πŸ’‘  Recommendations{}", colors::BOLD, colors::RESET);
        for (i, rec) in report.recommendations.iter().enumerate() {
            println!("  {}. {}", i + 1, rec);
        }
        println!();
    }

    println!("{}", colors::rule());
    print!("{}", all_footer(show_all));
}

/// The `--all`-only sections, or the empty string.
///
/// Returned rather than printed so the flag's whole effect on the text report
/// is one testable value: the defect was that this effect did not exist and
/// `diff` of the two runs was empty.
fn all_sections(report: &DepsAuditReport, show_all: bool) -> String {
    if !show_all {
        // Without the flag the handler has already filtered these two
        // categories out of `report.dependencies`, so the sections would
        // always be empty.
        return String::new();
    }
    let mut out = kept_category_section(
        report,
        DepCategory::Core,
        "🧱  Core Dependencies",
        colors::BOLD,
    );
    out.push_str(&kept_category_section(
        report,
        DepCategory::Sovereign,
        "βœ…  Sovereign Stack Dependencies",
        colors::BOLD_GREEN,
    ));
    out
}

/// The footer hint, which must not advertise `--all` to a run that used it.
fn all_footer(show_all: bool) -> String {
    if show_all {
        return String::new();
    }
    format!(
        "{}Run with --all to see Core and Sovereign deps{}\n",
        colors::DIM,
        colors::RESET
    )
}

/// Render one `--all` section: the dependencies in `category`.
///
/// These two categories are the whole content of `--all`, so a run that
/// supplies the flag and gets no section back has been told the flag did
/// nothing. An empty section is rendered as an empty section.
fn kept_category_section(
    report: &DepsAuditReport,
    category: DepCategory,
    heading: &str,
    color: colors::Sgr,
) -> String {
    let deps: Vec<_> = report
        .dependencies
        .iter()
        .filter(|d| d.category == category)
        .collect();

    let mut out = format!("{}{}  ({}){}\n", color, heading, deps.len(), colors::RESET);
    if deps.is_empty() {
        out.push_str(&format!(
            "  {}none in this project{}\n\n",
            colors::DIM,
            colors::RESET
        ));
        return out;
    }
    out.push_str("  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”\n");
    out.push_str("  β”‚ Dependency          β”‚ Version  β”‚ Reason                      β”‚\n");
    out.push_str("  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€\n");
    for dep in &deps {
        let name_str = dep.name.get(..dep.name.len().min(19)).unwrap_or(&dep.name);
        let version_str = dep
            .version
            .get(..dep.version.len().min(8))
            .unwrap_or(&dep.version);
        let reason_str = dep
            .reason
            .get(..dep.reason.len().min(27))
            .unwrap_or(&dep.reason);
        out.push_str(&format!(
            "  β”‚ {}{:<19}{} β”‚ {:<8} β”‚ {:<27} β”‚\n",
            colors::CYAN,
            name_str,
            colors::RESET,
            version_str,
            reason_str
        ));
    }
    out.push_str("  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜\n\n");
    out
}

#[cfg(test)]
mod deps_audit_output_tests {
    //! Covers print_pareto_report + print_text_report in
    //! deps_audit_handlers/output.rs (12 uncov on broad, 0% cov; the rest
    //! comes from the dispatch handler.rs).
    use super::*;
    use crate::cli::handlers::deps_audit_handlers::types::{DepAnalysis, ParetoEntry};

    fn pareto(name: &str, trans_deps: usize, effort: ParetoEffort, roi: f32) -> ParetoEntry {
        ParetoEntry {
            name: name.into(),
            transitive_deps: trans_deps,
            effort,
            roi,
            reason: "test".into(),
            category: DepCategory::Removable,
        }
    }

    fn empty_report() -> DepsAuditReport {
        DepsAuditReport {
            total_deps: 0,
            direct_deps: 0,
            transitive_deps: 0,
            sovereign_deps: 0,
            replaceable_deps: 0,
            removable_deps: 0,
            heavy_deps: 0,
            orphan_deps: 0,
            bridge_deps: 0,
            estimated_savings_kb: 0,
            dependencies: vec![],
            recommendations: vec![],
            top_critical: vec![],
            removal_candidates: vec![],
        }
    }

    #[test]
    fn test_print_pareto_report_empty_returns_early() {
        // Empty entries β†’ "No removable dependencies found." early-return.
        print_pareto_report(&[]);
    }

    #[test]
    fn test_print_pareto_report_single_entry_no_panic() {
        let entries = vec![pareto("foo", 5, ParetoEffort::Low, 5.0)];
        print_pareto_report(&entries);
    }

    #[test]
    fn test_print_pareto_report_emits_80_marker_when_cumulative_threshold_hit() {
        // Build entries where the second one pushes cumulative past 80% threshold.
        let entries = vec![
            pareto("a", 100, ParetoEffort::Low, 100.0),
            pareto("b", 50, ParetoEffort::Medium, 25.0),
            pareto("c", 10, ParetoEffort::High, 3.3),
        ];
        print_pareto_report(&entries);
    }

    #[test]
    fn test_print_pareto_report_more_than_20_entries_takes_top_20() {
        let entries: Vec<ParetoEntry> = (0..25)
            .map(|i| pareto(&format!("dep{i}"), i, ParetoEffort::Low, i as f32))
            .collect();
        print_pareto_report(&entries);
    }

    #[test]
    fn test_print_pareto_report_quick_wins_section_with_low_effort_high_roi() {
        let entries = vec![
            pareto("quick", 50, ParetoEffort::Low, 50.0), // qualifies (Low + ROI > 10)
            pareto("hard", 50, ParetoEffort::High, 50.0), // not Low β†’ skipped
            pareto("low_roi", 5, ParetoEffort::Low, 5.0), // ROI ≀ 10 β†’ skipped
        ];
        print_pareto_report(&entries);
    }

    // ── print_text_report ──

    #[test]
    fn test_print_text_report_empty_no_panic() {
        let r = empty_report();
        print_text_report(&r, false);
    }

    #[test]
    fn test_print_text_report_with_savings_uses_yellow_color() {
        let mut r = empty_report();
        r.estimated_savings_kb = 4096; // > 0 β†’ yellow color branch
        print_text_report(&r, false);
    }

    #[test]
    fn test_print_text_report_with_critical_deps_emits_table() {
        let mut r = empty_report();
        r.top_critical = vec![("serde".into(), 0.123456), ("tokio".into(), 0.098765)];
        print_text_report(&r, false);
    }

    #[test]
    fn test_print_text_report_with_recommendations_filters_heavy_deps() {
        let mut r = empty_report();
        r.dependencies = vec![DepAnalysis {
            name: "huge_dep".into(),
            version: "1.0".into(),
            category: DepCategory::Heavy,
            replacement: None,
            reason: "5MB binary bloat".into(),
            transitive_count: 50,
            estimated_size_kb: 5000,
            pagerank_score: 0.5,
            in_degree: 0,
            out_degree: 0,
            is_bridge: false,
            is_orphan: false,
        }];
        print_text_report(&r, false);
    }
}

#[cfg(test)]
mod deps_audit_all_flag_tests {
    //! `--all` must add rows to the TEXT report, not only to JSON.
    //!
    //! The handler honoured `--all` when it built `report.dependencies`
    //! (dropping the `Core | Sovereign` filter), but `print_text_report`
    //! grouped only Removable / Heavy / Replaceable / DevOnly β€” precisely the
    //! four categories `--all` does NOT add. So on a project with `serde` and
    //! `rayon` in it, `diff <(deps-audit) <(deps-audit --all)` was empty while
    //! `-f json` differed, and the footer printed "Run with --all to see Core
    //! and Sovereign deps" on the run that had just supplied `--all`.
    use super::*;
    use crate::cli::handlers::deps_audit_handlers::types::DepAnalysis;

    fn dep(name: &str, category: DepCategory) -> DepAnalysis {
        DepAnalysis {
            name: name.into(),
            version: "1.0".into(),
            category,
            replacement: None,
            reason: "fixture".into(),
            transitive_count: 1,
            estimated_size_kb: 10,
            pagerank_score: 0.1,
            in_degree: 0,
            out_degree: 0,
            is_bridge: false,
            is_orphan: false,
        }
    }

    fn report_with_kept_deps() -> DepsAuditReport {
        DepsAuditReport {
            total_deps: 3,
            direct_deps: 3,
            transitive_deps: 0,
            sovereign_deps: 1,
            replaceable_deps: 0,
            removable_deps: 0,
            heavy_deps: 0,
            orphan_deps: 0,
            bridge_deps: 0,
            estimated_savings_kb: 0,
            dependencies: vec![
                dep("serde", DepCategory::Core),
                dep("trueno", DepCategory::Sovereign),
                dep("bloaty", DepCategory::Heavy),
            ],
            recommendations: vec![],
            top_critical: vec![],
            removal_candidates: vec![],
        }
    }

    /// The two categories the flag unlocks reach the text report.
    #[test]
    fn all_adds_core_and_sovereign_sections_to_text() {
        let report = report_with_kept_deps();

        let plain = all_sections(&report, false);
        let all = all_sections(&report, true);

        assert!(
            plain.is_empty(),
            "without --all these categories are filtered out upstream:\n{plain}"
        );
        assert_ne!(plain, all, "--all must change the text report");
        for expected in [
            "Core Dependencies",
            "serde",
            "Sovereign Stack Dependencies",
            "trueno",
        ] {
            assert!(all.contains(expected), "--all must name {expected}:\n{all}");
        }
    }

    /// A `--all` section with nothing in it says so, rather than vanishing and
    /// reading as "the flag did nothing".
    #[test]
    fn empty_all_section_is_still_printed() {
        let mut report = report_with_kept_deps();
        report
            .dependencies
            .retain(|d| d.category != DepCategory::Core);

        let all = all_sections(&report, true);
        assert!(all.contains("Core Dependencies"), "{all}");
        assert!(all.contains("none in this project"), "{all}");
    }

    /// The footer must not advertise a flag the caller has already used.
    #[test]
    fn footer_stops_promising_all_once_all_is_supplied() {
        assert!(all_footer(false).contains("Run with --all"));
        assert_eq!(
            all_footer(true),
            "",
            "--all printed 'Run with --all to see Core and Sovereign deps'"
        );
    }
}