repolens 2.0.0

A CLI tool to audit and prepare repositories for open source or enterprise standards
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
//! Compare module - Compare two audit reports and generate a diff report
//!
//! This module provides functionality to compare two `AuditResults` instances
//! and produce a `CompareReport` that highlights new issues, resolved issues,
//! and score changes between the two audits.

use serde::{Deserialize, Serialize};
use std::collections::HashSet;

use crate::rules::results::{AuditResults, Finding, Severity};

/// A unique key that identifies a finding for comparison purposes.
/// Two findings are considered the same if they share the same rule_id,
/// category, message, and location.
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct FindingKey {
    pub rule_id: String,
    pub category: String,
    pub message: String,
    pub location: Option<String>,
}

impl FindingKey {
    /// Create a key from a Finding
    pub fn from_finding(finding: &Finding) -> Self {
        Self {
            rule_id: finding.rule_id.clone(),
            category: finding.category.clone(),
            message: finding.message.clone(),
            location: finding.location.clone(),
        }
    }
}

/// Summary of findings count changes per category
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CategoryDiff {
    /// Category name
    pub category: String,
    /// Number of findings in base report
    pub base_count: usize,
    /// Number of findings in head report
    pub head_count: usize,
    /// Difference (head - base), positive means regression
    pub diff: i64,
}

/// The result of comparing two audit reports
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompareReport {
    /// The base reference label
    pub base_ref: String,
    /// The head reference label
    pub head_ref: String,
    /// Findings from the base report
    pub base_findings: Vec<Finding>,
    /// Findings from the head report
    pub head_findings: Vec<Finding>,
    /// Findings present in head but not in base (new issues / regressions)
    pub added_findings: Vec<Finding>,
    /// Findings present in base but not in head (resolved issues / improvements)
    pub removed_findings: Vec<Finding>,
    /// Findings present in both base and head
    pub unchanged_findings: Vec<Finding>,
    /// Score for the base report (lower is better -- count of issues)
    pub base_score: i64,
    /// Score for the head report
    pub head_score: i64,
    /// Score difference (head - base), negative means improvement
    pub score_diff: i64,
    /// Per-category breakdown
    pub category_diffs: Vec<CategoryDiff>,
}

impl CompareReport {
    /// Returns true if there are regressions (new issues added)
    pub fn has_regressions(&self) -> bool {
        !self.added_findings.is_empty()
    }

    /// Returns true if there are improvements (issues resolved).
    ///
    /// Part of the public API - allows external code to check
    /// if a comparison shows improvement in audit results.
    #[allow(dead_code)]
    pub fn has_improvements(&self) -> bool {
        !self.removed_findings.is_empty()
    }
}

/// Compute a weighted score from audit results.
/// Critical = 10 points, Warning = 3 points, Info = 1 point.
pub fn compute_score(results: &AuditResults) -> i64 {
    let critical = results.count_by_severity(Severity::Critical) as i64;
    let warning = results.count_by_severity(Severity::Warning) as i64;
    let info = results.count_by_severity(Severity::Info) as i64;
    critical * 10 + warning * 3 + info
}

/// Compare two audit results and produce a CompareReport.
///
/// Matching of findings is done by (rule_id, category, message, location).
pub fn compare_results(
    base: &AuditResults,
    head: &AuditResults,
    base_ref: &str,
    head_ref: &str,
) -> CompareReport {
    let base_keys: HashSet<FindingKey> = base
        .findings()
        .iter()
        .map(FindingKey::from_finding)
        .collect();
    let head_keys: HashSet<FindingKey> = head
        .findings()
        .iter()
        .map(FindingKey::from_finding)
        .collect();

    let added_findings: Vec<Finding> = head
        .findings()
        .iter()
        .filter(|f| !base_keys.contains(&FindingKey::from_finding(f)))
        .cloned()
        .collect();

    let removed_findings: Vec<Finding> = base
        .findings()
        .iter()
        .filter(|f| !head_keys.contains(&FindingKey::from_finding(f)))
        .cloned()
        .collect();

    let unchanged_findings: Vec<Finding> = head
        .findings()
        .iter()
        .filter(|f| base_keys.contains(&FindingKey::from_finding(f)))
        .cloned()
        .collect();

    let base_score = compute_score(base);
    let head_score = compute_score(head);
    let score_diff = head_score - base_score;

    // Collect all categories from both reports
    let mut all_categories: Vec<String> = Vec::new();
    for f in base.findings().iter().chain(head.findings().iter()) {
        if !all_categories.contains(&f.category) {
            all_categories.push(f.category.clone());
        }
    }
    all_categories.sort();

    let category_diffs: Vec<CategoryDiff> = all_categories
        .into_iter()
        .map(|cat| {
            let base_count = base.findings_by_category(&cat).count();
            let head_count = head.findings_by_category(&cat).count();
            CategoryDiff {
                category: cat,
                base_count,
                head_count,
                diff: head_count as i64 - base_count as i64,
            }
        })
        .collect();

    CompareReport {
        base_ref: base_ref.to_string(),
        head_ref: head_ref.to_string(),
        base_findings: base.findings().to_vec(),
        head_findings: head.findings().to_vec(),
        added_findings,
        removed_findings,
        unchanged_findings,
        base_score,
        head_score,
        score_diff,
        category_diffs,
    }
}

/// Format the compare report as colored terminal output
pub fn format_terminal(report: &CompareReport) -> String {
    use colored::Colorize;

    let mut output = String::new();

    // Header
    output.push_str(&format!(
        "\n{}\n\n",
        "RepoLens Audit Comparison".cyan().bold()
    ));
    output.push_str(&format!(
        "  {} {}\n",
        "Base:".dimmed(),
        report.base_ref.white().bold()
    ));
    output.push_str(&format!(
        "  {} {}\n",
        "Head:".dimmed(),
        report.head_ref.white().bold()
    ));

    // Score summary
    output.push_str(&format!("\n{}\n", "".repeat(50).dimmed()));
    output.push_str(&format!("  {}\n\n", "SCORE".bold()));

    let score_arrow = if report.score_diff < 0 {
        format!("{}", format!("{} (improved)", report.score_diff).green())
    } else if report.score_diff > 0 {
        format!("{}", format!("+{} (regressed)", report.score_diff).red())
    } else {
        format!("{}", "0 (no change)".dimmed())
    };

    output.push_str(&format!(
        "  {} -> {} ({})\n",
        report.base_score.to_string().white(),
        report.head_score.to_string().white(),
        score_arrow,
    ));

    // New issues (regressions)
    output.push_str(&format!("\n{}\n", "".repeat(50).dimmed()));
    output.push_str(&format!(
        "  {} ({})\n\n",
        "NEW ISSUES".red().bold(),
        report.added_findings.len()
    ));

    if report.added_findings.is_empty() {
        output.push_str(&format!("  {}\n", "No new issues.".green()));
    } else {
        for finding in &report.added_findings {
            let severity_tag = match finding.severity {
                Severity::Critical => "CRITICAL".red().bold().to_string(),
                Severity::Warning => "WARNING".yellow().bold().to_string(),
                Severity::Info => "INFO".blue().bold().to_string(),
            };
            output.push_str(&format!(
                "  {} [{}] [{}] {}\n",
                "+".red(),
                finding.rule_id.cyan(),
                severity_tag,
                finding.message
            ));
            if let Some(loc) = &finding.location {
                output.push_str(&format!("    {} {}\n", "└─".dimmed(), loc.dimmed()));
            }
        }
    }

    // Resolved issues (improvements)
    output.push_str(&format!("\n{}\n", "".repeat(50).dimmed()));
    output.push_str(&format!(
        "  {} ({})\n\n",
        "RESOLVED ISSUES".green().bold(),
        report.removed_findings.len()
    ));

    if report.removed_findings.is_empty() {
        output.push_str(&format!("  {}\n", "No resolved issues.".dimmed()));
    } else {
        for finding in &report.removed_findings {
            let severity_tag = match finding.severity {
                Severity::Critical => "CRITICAL".red().bold().to_string(),
                Severity::Warning => "WARNING".yellow().bold().to_string(),
                Severity::Info => "INFO".blue().bold().to_string(),
            };
            output.push_str(&format!(
                "  {} [{}] [{}] {}\n",
                "-".green(),
                finding.rule_id.cyan(),
                severity_tag,
                finding.message
            ));
            if let Some(loc) = &finding.location {
                output.push_str(&format!("    {} {}\n", "└─".dimmed(), loc.dimmed()));
            }
        }
    }

    // Category breakdown
    if !report.category_diffs.is_empty() {
        output.push_str(&format!("\n{}\n", "".repeat(50).dimmed()));
        output.push_str(&format!("  {}\n\n", "CATEGORY BREAKDOWN".bold()));
        output.push_str(&format!(
            "  {:<15} {:>6} {:>6} {:>8}\n",
            "Category", "Base", "Head", "Diff"
        ));
        output.push_str(&format!("  {}\n", "".repeat(40)));

        for cat in &report.category_diffs {
            let diff_str = if cat.diff > 0 {
                format!("+{}", cat.diff).red().to_string()
            } else if cat.diff < 0 {
                format!("{}", cat.diff).green().to_string()
            } else {
                "0".dimmed().to_string()
            };
            output.push_str(&format!(
                "  {:<15} {:>6} {:>6} {:>8}\n",
                cat.category, cat.base_count, cat.head_count, diff_str
            ));
        }
    }

    // Unchanged summary
    output.push_str(&format!("\n{}\n", "".repeat(50).dimmed()));
    output.push_str(&format!(
        "  {} {} unchanged issue(s)\n\n",
        "Unchanged:".dimmed(),
        report.unchanged_findings.len()
    ));

    output
}

/// Format the compare report as JSON
pub fn format_json(report: &CompareReport) -> Result<String, serde_json::Error> {
    serde_json::to_string_pretty(report)
}

/// Format the compare report as Markdown
pub fn format_markdown(report: &CompareReport) -> String {
    let mut output = String::new();

    output.push_str("# RepoLens Audit Comparison\n\n");
    output.push_str(&format!("**Base:** {}\n", report.base_ref));
    output.push_str(&format!("**Head:** {}\n\n", report.head_ref));

    // Score
    output.push_str("## Score\n\n");
    let trend = if report.score_diff < 0 {
        format!("{} (improved)", report.score_diff)
    } else if report.score_diff > 0 {
        format!("+{} (regressed)", report.score_diff)
    } else {
        "0 (no change)".to_string()
    };
    output.push_str(&format!(
        "| Base | Head | Diff |\n|------|------|------|\n| {} | {} | {} |\n\n",
        report.base_score, report.head_score, trend
    ));

    // New issues
    output.push_str(&format!(
        "## New Issues ({})\n\n",
        report.added_findings.len()
    ));
    if report.added_findings.is_empty() {
        output.push_str("No new issues.\n\n");
    } else {
        output.push_str("| Rule | Severity | Message | Location |\n");
        output.push_str("|------|----------|---------|----------|\n");
        for f in &report.added_findings {
            let sev = match f.severity {
                Severity::Critical => "Critical",
                Severity::Warning => "Warning",
                Severity::Info => "Info",
            };
            let loc = f.location.as_deref().unwrap_or("-");
            output.push_str(&format!(
                "| {} | {} | {} | {} |\n",
                f.rule_id, sev, f.message, loc
            ));
        }
        output.push('\n');
    }

    // Resolved issues
    output.push_str(&format!(
        "## Resolved Issues ({})\n\n",
        report.removed_findings.len()
    ));
    if report.removed_findings.is_empty() {
        output.push_str("No resolved issues.\n\n");
    } else {
        output.push_str("| Rule | Severity | Message | Location |\n");
        output.push_str("|------|----------|---------|----------|\n");
        for f in &report.removed_findings {
            let sev = match f.severity {
                Severity::Critical => "Critical",
                Severity::Warning => "Warning",
                Severity::Info => "Info",
            };
            let loc = f.location.as_deref().unwrap_or("-");
            output.push_str(&format!(
                "| {} | {} | {} | {} |\n",
                f.rule_id, sev, f.message, loc
            ));
        }
        output.push('\n');
    }

    // Category breakdown
    if !report.category_diffs.is_empty() {
        output.push_str("## Category Breakdown\n\n");
        output.push_str("| Category | Base | Head | Diff |\n");
        output.push_str("|----------|------|------|------|\n");
        for cat in &report.category_diffs {
            let diff_str = if cat.diff > 0 {
                format!("+{}", cat.diff)
            } else {
                format!("{}", cat.diff)
            };
            output.push_str(&format!(
                "| {} | {} | {} | {} |\n",
                cat.category, cat.base_count, cat.head_count, diff_str
            ));
        }
        output.push('\n');
    }

    // Unchanged
    output.push_str(&format!(
        "**Unchanged issues:** {}\n\n",
        report.unchanged_findings.len()
    ));

    output.push_str("---\n\n");
    output.push_str("*Report generated by [RepoLens](https://github.com/systm-d/repolens)*\n");

    output
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rules::results::{AuditResults, Finding, Severity};

    fn make_finding(rule_id: &str, category: &str, severity: Severity, message: &str) -> Finding {
        Finding::new(rule_id, category, severity, message)
    }

    fn make_finding_with_location(
        rule_id: &str,
        category: &str,
        severity: Severity,
        message: &str,
        location: &str,
    ) -> Finding {
        Finding::new(rule_id, category, severity, message).with_location(location)
    }

    fn make_results(name: &str, findings: Vec<Finding>) -> AuditResults {
        let mut results = AuditResults::new(name, "opensource");
        results.add_findings(findings);
        results
    }

    // --- Test: identical results ---
    #[test]
    fn test_compare_identical_results() {
        let findings = vec![
            make_finding("SEC001", "secrets", Severity::Critical, "Secret found"),
            make_finding("DOC001", "docs", Severity::Warning, "README missing"),
        ];
        let base = make_results("repo", findings.clone());
        let head = make_results("repo", findings);

        let report = compare_results(&base, &head, "v1.0", "v1.1");

        assert!(report.added_findings.is_empty());
        assert!(report.removed_findings.is_empty());
        assert_eq!(report.unchanged_findings.len(), 2);
        assert_eq!(report.score_diff, 0);
        assert!(!report.has_regressions());
        assert!(!report.has_improvements());
    }

    // --- Test: findings added (regressions) ---
    #[test]
    fn test_compare_with_added_findings() {
        let base = make_results(
            "repo",
            vec![make_finding(
                "DOC001",
                "docs",
                Severity::Warning,
                "README missing",
            )],
        );
        let head = make_results(
            "repo",
            vec![
                make_finding("DOC001", "docs", Severity::Warning, "README missing"),
                make_finding("SEC001", "secrets", Severity::Critical, "Secret found"),
            ],
        );

        let report = compare_results(&base, &head, "base", "head");

        assert_eq!(report.added_findings.len(), 1);
        assert_eq!(report.added_findings[0].rule_id, "SEC001");
        assert!(report.removed_findings.is_empty());
        assert_eq!(report.unchanged_findings.len(), 1);
        assert!(report.has_regressions());
        assert!(!report.has_improvements());
        assert!(report.score_diff > 0); // head has more issues
    }

    // --- Test: findings removed (improvements) ---
    #[test]
    fn test_compare_with_removed_findings() {
        let base = make_results(
            "repo",
            vec![
                make_finding("DOC001", "docs", Severity::Warning, "README missing"),
                make_finding("SEC001", "secrets", Severity::Critical, "Secret found"),
            ],
        );
        let head = make_results(
            "repo",
            vec![make_finding(
                "DOC001",
                "docs",
                Severity::Warning,
                "README missing",
            )],
        );

        let report = compare_results(&base, &head, "before", "after");

        assert!(report.added_findings.is_empty());
        assert_eq!(report.removed_findings.len(), 1);
        assert_eq!(report.removed_findings[0].rule_id, "SEC001");
        assert_eq!(report.unchanged_findings.len(), 1);
        assert!(!report.has_regressions());
        assert!(report.has_improvements());
        assert!(report.score_diff < 0); // head has fewer issues
    }

    // --- Test: mixed changes ---
    #[test]
    fn test_compare_with_mixed_changes() {
        let base = make_results(
            "repo",
            vec![
                make_finding("SEC001", "secrets", Severity::Critical, "Secret found"),
                make_finding("DOC001", "docs", Severity::Warning, "README missing"),
                make_finding(
                    "INFO001",
                    "quality",
                    Severity::Info,
                    "Consider adding tests",
                ),
            ],
        );
        let head = make_results(
            "repo",
            vec![
                make_finding("DOC001", "docs", Severity::Warning, "README missing"),
                make_finding("SEC002", "secrets", Severity::Warning, "Weak password"),
                make_finding("WF001", "workflows", Severity::Info, "No CI configured"),
            ],
        );

        let report = compare_results(&base, &head, "v1", "v2");

        // SEC002 and WF001 are new
        assert_eq!(report.added_findings.len(), 2);
        // SEC001 and INFO001 are resolved
        assert_eq!(report.removed_findings.len(), 2);
        // DOC001 is unchanged
        assert_eq!(report.unchanged_findings.len(), 1);
        assert!(report.has_regressions());
        assert!(report.has_improvements());
    }

    // --- Test: score diff calculation ---
    #[test]
    fn test_score_diff() {
        // base: 1 critical (10) + 1 warning (3) = 13
        let base = make_results(
            "repo",
            vec![
                make_finding("SEC001", "secrets", Severity::Critical, "Secret found"),
                make_finding("DOC001", "docs", Severity::Warning, "README missing"),
            ],
        );
        // head: 1 warning (3) + 1 info (1) = 4
        let head = make_results(
            "repo",
            vec![
                make_finding("DOC001", "docs", Severity::Warning, "README missing"),
                make_finding("INFO001", "quality", Severity::Info, "Consider tests"),
            ],
        );

        let report = compare_results(&base, &head, "old", "new");

        assert_eq!(report.base_score, 13);
        assert_eq!(report.head_score, 4);
        assert_eq!(report.score_diff, -9); // improved by 9 points
    }

    // --- Test: compute_score ---
    #[test]
    fn test_compute_score() {
        let results = make_results(
            "repo",
            vec![
                make_finding("C1", "test", Severity::Critical, "Critical"),
                make_finding("C2", "test", Severity::Critical, "Critical 2"),
                make_finding("W1", "test", Severity::Warning, "Warning"),
                make_finding("I1", "test", Severity::Info, "Info"),
            ],
        );
        // 2*10 + 1*3 + 1*1 = 24
        assert_eq!(compute_score(&results), 24);
    }

    #[test]
    fn test_compute_score_empty() {
        let results = make_results("repo", vec![]);
        assert_eq!(compute_score(&results), 0);
    }

    // --- Test: JSON output ---
    #[test]
    fn test_format_json() {
        let base = make_results(
            "repo",
            vec![make_finding(
                "SEC001",
                "secrets",
                Severity::Critical,
                "Secret found",
            )],
        );
        let head = make_results("repo", vec![]);

        let report = compare_results(&base, &head, "v1", "v2");
        let json_str = format_json(&report).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();

        assert_eq!(parsed["base_ref"], "v1");
        assert_eq!(parsed["head_ref"], "v2");
        assert_eq!(parsed["score_diff"], -10);
        assert_eq!(parsed["removed_findings"].as_array().unwrap().len(), 1);
        assert!(parsed["added_findings"].as_array().unwrap().is_empty());
    }

    // --- Test: fail-on-regression logic ---
    #[test]
    fn test_fail_on_regression_true() {
        let base = make_results("repo", vec![]);
        let head = make_results(
            "repo",
            vec![make_finding(
                "SEC001",
                "secrets",
                Severity::Critical,
                "Secret found",
            )],
        );

        let report = compare_results(&base, &head, "base", "head");
        assert!(report.has_regressions());
    }

    #[test]
    fn test_fail_on_regression_false() {
        let base = make_results(
            "repo",
            vec![make_finding(
                "SEC001",
                "secrets",
                Severity::Critical,
                "Secret found",
            )],
        );
        let head = make_results("repo", vec![]);

        let report = compare_results(&base, &head, "base", "head");
        assert!(!report.has_regressions());
    }

    // --- Test: category diffs ---
    #[test]
    fn test_category_diffs() {
        let base = make_results(
            "repo",
            vec![
                make_finding("SEC001", "secrets", Severity::Critical, "Secret 1"),
                make_finding("SEC002", "secrets", Severity::Warning, "Secret 2"),
                make_finding("DOC001", "docs", Severity::Warning, "Doc issue"),
            ],
        );
        let head = make_results(
            "repo",
            vec![
                make_finding("SEC001", "secrets", Severity::Critical, "Secret 1"),
                make_finding("WF001", "workflows", Severity::Info, "No CI"),
            ],
        );

        let report = compare_results(&base, &head, "v1", "v2");

        // Should have categories: docs, secrets, workflows (sorted)
        assert_eq!(report.category_diffs.len(), 3);

        let docs = report
            .category_diffs
            .iter()
            .find(|c| c.category == "docs")
            .unwrap();
        assert_eq!(docs.base_count, 1);
        assert_eq!(docs.head_count, 0);
        assert_eq!(docs.diff, -1);

        let secrets = report
            .category_diffs
            .iter()
            .find(|c| c.category == "secrets")
            .unwrap();
        assert_eq!(secrets.base_count, 2);
        assert_eq!(secrets.head_count, 1);
        assert_eq!(secrets.diff, -1);

        let workflows = report
            .category_diffs
            .iter()
            .find(|c| c.category == "workflows")
            .unwrap();
        assert_eq!(workflows.base_count, 0);
        assert_eq!(workflows.head_count, 1);
        assert_eq!(workflows.diff, 1);
    }

    // --- Test: FindingKey ---
    #[test]
    fn test_finding_key_from_finding() {
        let finding =
            make_finding_with_location("SEC001", "secrets", Severity::Critical, "Secret", "a.rs");
        let key = FindingKey::from_finding(&finding);
        assert_eq!(key.rule_id, "SEC001");
        assert_eq!(key.category, "secrets");
        assert_eq!(key.message, "Secret");
        assert_eq!(key.location, Some("a.rs".to_string()));
    }

    #[test]
    fn test_finding_key_equality() {
        let f1 = make_finding("SEC001", "secrets", Severity::Critical, "Secret");
        let f2 = make_finding("SEC001", "secrets", Severity::Warning, "Secret");
        // Same key regardless of severity
        assert_eq!(FindingKey::from_finding(&f1), FindingKey::from_finding(&f2));
    }

    #[test]
    fn test_finding_key_inequality_different_message() {
        let f1 = make_finding("SEC001", "secrets", Severity::Critical, "Secret A");
        let f2 = make_finding("SEC001", "secrets", Severity::Critical, "Secret B");
        assert_ne!(FindingKey::from_finding(&f1), FindingKey::from_finding(&f2));
    }

    // --- Test: terminal output ---
    #[test]
    fn test_format_terminal_with_changes() {
        let base = make_results(
            "repo",
            vec![make_finding(
                "SEC001",
                "secrets",
                Severity::Critical,
                "Secret found",
            )],
        );
        let head = make_results(
            "repo",
            vec![make_finding(
                "DOC001",
                "docs",
                Severity::Warning,
                "README missing",
            )],
        );

        let report = compare_results(&base, &head, "v1.0", "v2.0");
        let output = format_terminal(&report);

        assert!(output.contains("v1.0"));
        assert!(output.contains("v2.0"));
        assert!(output.contains("NEW ISSUES"));
        assert!(output.contains("RESOLVED ISSUES"));
        assert!(output.contains("DOC001"));
        assert!(output.contains("SEC001"));
    }

    #[test]
    fn test_format_terminal_no_changes() {
        let findings = vec![make_finding(
            "DOC001",
            "docs",
            Severity::Warning,
            "README missing",
        )];
        let base = make_results("repo", findings.clone());
        let head = make_results("repo", findings);

        let report = compare_results(&base, &head, "a", "b");
        let output = format_terminal(&report);

        assert!(output.contains("No new issues"));
        assert!(output.contains("No resolved issues"));
        assert!(output.contains("0 (no change)"));
    }

    #[test]
    fn test_format_terminal_improvement() {
        let base = make_results(
            "repo",
            vec![make_finding(
                "SEC001",
                "secrets",
                Severity::Critical,
                "Secret found",
            )],
        );
        let head = make_results("repo", vec![]);

        let report = compare_results(&base, &head, "before", "after");
        let output = format_terminal(&report);

        assert!(output.contains("improved"));
    }

    #[test]
    fn test_format_terminal_regression() {
        let base = make_results("repo", vec![]);
        let head = make_results(
            "repo",
            vec![make_finding(
                "SEC001",
                "secrets",
                Severity::Critical,
                "Secret found",
            )],
        );

        let report = compare_results(&base, &head, "before", "after");
        let output = format_terminal(&report);

        assert!(output.contains("regressed"));
    }

    #[test]
    fn test_format_terminal_with_location() {
        let base = make_results("repo", vec![]);
        let head = make_results(
            "repo",
            vec![make_finding_with_location(
                "SEC001",
                "secrets",
                Severity::Critical,
                "Secret found",
                "src/config.rs:42",
            )],
        );

        let report = compare_results(&base, &head, "a", "b");
        let output = format_terminal(&report);

        assert!(output.contains("src/config.rs:42"));
    }

    // --- Test: markdown output ---
    #[test]
    fn test_format_markdown_with_changes() {
        let base = make_results(
            "repo",
            vec![make_finding(
                "SEC001",
                "secrets",
                Severity::Critical,
                "Secret found",
            )],
        );
        let head = make_results(
            "repo",
            vec![make_finding(
                "DOC001",
                "docs",
                Severity::Warning,
                "README missing",
            )],
        );

        let report = compare_results(&base, &head, "v1", "v2");
        let output = format_markdown(&report);

        assert!(output.contains("# RepoLens Audit Comparison"));
        assert!(output.contains("**Base:** v1"));
        assert!(output.contains("**Head:** v2"));
        assert!(output.contains("## New Issues (1)"));
        assert!(output.contains("## Resolved Issues (1)"));
        assert!(output.contains("DOC001"));
        assert!(output.contains("SEC001"));
        assert!(output.contains("## Category Breakdown"));
    }

    #[test]
    fn test_format_markdown_no_issues() {
        let base = make_results("repo", vec![]);
        let head = make_results("repo", vec![]);

        let report = compare_results(&base, &head, "a", "b");
        let output = format_markdown(&report);

        assert!(output.contains("No new issues"));
        assert!(output.contains("No resolved issues"));
        assert!(output.contains("0 (no change)"));
    }

    #[test]
    fn test_format_markdown_regression_score() {
        let base = make_results("repo", vec![]);
        let head = make_results(
            "repo",
            vec![make_finding(
                "SEC001",
                "secrets",
                Severity::Critical,
                "Secret found",
            )],
        );

        let report = compare_results(&base, &head, "a", "b");
        let output = format_markdown(&report);

        assert!(output.contains("+10 (regressed)"));
    }

    #[test]
    fn test_format_markdown_improved_score() {
        let base = make_results(
            "repo",
            vec![make_finding(
                "SEC001",
                "secrets",
                Severity::Critical,
                "Secret found",
            )],
        );
        let head = make_results("repo", vec![]);

        let report = compare_results(&base, &head, "a", "b");
        let output = format_markdown(&report);

        assert!(output.contains("-10 (improved)"));
    }

    #[test]
    fn test_format_markdown_with_location() {
        let base = make_results("repo", vec![]);
        let head = make_results(
            "repo",
            vec![make_finding_with_location(
                "SEC001",
                "secrets",
                Severity::Critical,
                "Secret found",
                "src/main.rs:10",
            )],
        );

        let report = compare_results(&base, &head, "a", "b");
        let output = format_markdown(&report);

        assert!(output.contains("src/main.rs:10"));
    }

    #[test]
    fn test_format_markdown_no_location() {
        let base = make_results("repo", vec![]);
        let head = make_results(
            "repo",
            vec![make_finding(
                "SEC001",
                "secrets",
                Severity::Critical,
                "Secret found",
            )],
        );

        let report = compare_results(&base, &head, "a", "b");
        let output = format_markdown(&report);

        // Should use "-" for missing location
        assert!(output.contains("| - |"));
    }

    // --- Test: empty results ---
    #[test]
    fn test_compare_empty_results() {
        let base = make_results("repo", vec![]);
        let head = make_results("repo", vec![]);

        let report = compare_results(&base, &head, "empty1", "empty2");

        assert!(report.added_findings.is_empty());
        assert!(report.removed_findings.is_empty());
        assert!(report.unchanged_findings.is_empty());
        assert_eq!(report.base_score, 0);
        assert_eq!(report.head_score, 0);
        assert_eq!(report.score_diff, 0);
        assert!(report.category_diffs.is_empty());
        assert!(!report.has_regressions());
        assert!(!report.has_improvements());
    }

    // --- Test: CompareReport refs ---
    #[test]
    fn test_compare_report_refs() {
        let base = make_results("repo", vec![]);
        let head = make_results("repo", vec![]);

        let report = compare_results(&base, &head, "my-base", "my-head");

        assert_eq!(report.base_ref, "my-base");
        assert_eq!(report.head_ref, "my-head");
    }

    // --- Test: format_terminal with all severity types in added ---
    #[test]
    fn test_format_terminal_all_severities() {
        let base = make_results("repo", vec![]);
        let head = make_results(
            "repo",
            vec![
                make_finding("C1", "test", Severity::Critical, "Critical issue"),
                make_finding("W1", "test", Severity::Warning, "Warning issue"),
                make_finding("I1", "test", Severity::Info, "Info issue"),
            ],
        );

        let report = compare_results(&base, &head, "a", "b");
        let output = format_terminal(&report);

        assert!(output.contains("CRITICAL"));
        assert!(output.contains("WARNING"));
        assert!(output.contains("INFO"));
    }

    // --- Test: format_terminal with all severity types in resolved ---
    #[test]
    fn test_format_terminal_all_severities_resolved() {
        let base = make_results(
            "repo",
            vec![
                make_finding("C1", "test", Severity::Critical, "Critical issue"),
                make_finding("W1", "test", Severity::Warning, "Warning issue"),
                make_finding("I1", "test", Severity::Info, "Info issue"),
            ],
        );
        let head = make_results("repo", vec![]);

        let report = compare_results(&base, &head, "a", "b");
        let output = format_terminal(&report);

        assert!(output.contains("RESOLVED ISSUES"));
        assert!(output.contains("C1"));
        assert!(output.contains("W1"));
        assert!(output.contains("I1"));
    }

    // --- Test: format_terminal resolved with location ---
    #[test]
    fn test_format_terminal_resolved_with_location() {
        let base = make_results(
            "repo",
            vec![make_finding_with_location(
                "SEC001",
                "secrets",
                Severity::Critical,
                "Secret found",
                "src/lib.rs:5",
            )],
        );
        let head = make_results("repo", vec![]);

        let report = compare_results(&base, &head, "a", "b");
        let output = format_terminal(&report);

        assert!(output.contains("src/lib.rs:5"));
    }

    // --- Test: category_diffs positive diff ---
    #[test]
    fn test_format_terminal_category_positive_diff() {
        let base = make_results("repo", vec![]);
        let head = make_results(
            "repo",
            vec![
                make_finding("SEC001", "secrets", Severity::Critical, "Secret 1"),
                make_finding("SEC002", "secrets", Severity::Warning, "Secret 2"),
            ],
        );

        let report = compare_results(&base, &head, "a", "b");
        let output = format_terminal(&report);

        assert!(output.contains("CATEGORY BREAKDOWN"));
        assert!(output.contains("secrets"));
    }

    // --- Test: markdown with all severity types in new issues ---
    #[test]
    fn test_format_markdown_new_issues_all_severities() {
        let base = make_results("repo", vec![]);
        let head = make_results(
            "repo",
            vec![
                make_finding("C1", "test", Severity::Critical, "Critical issue"),
                make_finding("W1", "test", Severity::Warning, "Warning issue"),
                make_finding("I1", "test", Severity::Info, "Info issue"),
            ],
        );

        let report = compare_results(&base, &head, "a", "b");
        let output = format_markdown(&report);

        assert!(output.contains("| C1 | Critical |"));
        assert!(output.contains("| W1 | Warning |"));
        assert!(output.contains("| I1 | Info |"));
    }

    // --- Test: markdown with all severity types in resolved issues ---
    #[test]
    fn test_format_markdown_resolved_issues_all_severities() {
        let base = make_results(
            "repo",
            vec![
                make_finding("C1", "test", Severity::Critical, "Critical resolved"),
                make_finding("W1", "test", Severity::Warning, "Warning resolved"),
                make_finding("I1", "test", Severity::Info, "Info resolved"),
            ],
        );
        let head = make_results("repo", vec![]);

        let report = compare_results(&base, &head, "a", "b");
        let output = format_markdown(&report);

        // Verify resolved section has all severities
        assert!(output.contains("## Resolved Issues (3)"));
        assert!(output.contains("| C1 | Critical | Critical resolved"));
        assert!(output.contains("| W1 | Warning | Warning resolved"));
        assert!(output.contains("| I1 | Info | Info resolved"));
    }

    // --- Test: format_markdown footer ---
    #[test]
    fn test_format_markdown_footer() {
        let base = make_results("repo", vec![]);
        let head = make_results("repo", vec![]);

        let report = compare_results(&base, &head, "a", "b");
        let output = format_markdown(&report);

        assert!(output.contains("---"));
        assert!(output.contains("*Report generated by [RepoLens]"));
    }
}