prview 0.4.0

PR Review & Artifact Generator - cross-language PR analysis tool
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
use super::*;
use crate::artifacts::LintMetrics;
use crate::checks::{CheckResult, CheckStatus};
use crate::config::{Config, test_config, test_rust_profile};
use crate::git::{CommitInfo, Diff, DiffStats, FileChange, FileStatus};
use crate::policy::PolicyConfig;
use std::time::Duration;

macro_rules! build_html_test {
    ($config:expr, $diffs:expr, $checks:expr, $heuristics:expr, $ctx:expr, $report_json:expr, $dir:expr, $pr_review:expr, $regression:expr $(,)?) => {
        build_html(BuildHtmlInput {
            config: $config,
            diffs: $diffs,
            checks: $checks,
            heuristics: $heuristics,
            ctx: $ctx,
            report_json: $report_json,
            dir: $dir,
            pr_review: $pr_review,
            regression: $regression,
        })
    };
}

fn mock_config() -> Config {
    let mut config = test_config();
    config.target = Some("feature/test".to_string());
    config.bases = vec!["main".to_string()];
    config.profile = test_rust_profile(true);
    config.do_fetch = false;
    config.use_cache = false;
    config.create_zip = false;
    config.policy = PolicyConfig::default();
    config
}

fn mock_diff() -> Diff {
    Diff {
        base: "main".into(),
        target: "feature/test".into(),
        base_commit_id: "aaa".into(),
        target_commit_id: "bbb".into(),
        stats: DiffStats {
            files_changed: 3,
            additions: 150,
            deletions: 30,
            copied: 0,
        },
        files: vec![FileChange {
            path: "src/main.rs".into(),
            status: FileStatus::Modified,
            additions: 100,
            deletions: 20,
        }],
        commits: vec![CommitInfo {
            id: "aabbcc".into(),
            short_id: "aabbc".into(),
            message: "test commit".into(),
            author: "dev".into(),
            email: "dev@test.com".into(),
            date: "2026-01-01".into(),
        }],
    }
}

fn mock_checks() -> Vec<CheckResult> {
    vec![CheckResult {
        name: "cargo check".into(),
        status: CheckStatus::Passed,
        duration: Duration::from_secs(2),
        output: "ok".into(),
        cached: false,
        provenance: None,
    }]
}

fn mock_ctx() -> DashboardContext {
    DashboardContext {
        verdict: "PASS",
        analysis_status: crate::policy::engine::AnalysisStatus::Complete,
        merge_recommendation: crate::policy::engine::MergeRecommendation::Approve,
        allow_merge: true,
        quality_pass: true,
        policy_allow_merge: true,
        recommended_merge: true,
        review_caveats: vec![],
        quality_failures: vec![],
        introduced_quality_failures: vec![],
        preexisting_quality_failures: vec![],
        mixed_quality_failures: vec![],
        unclassified_quality_failures: vec![],
        quality_failure_details: vec![],
        policy_mode: "standard",
        blocking_issues: vec![],
        check_gates: vec![],
        breaking: vec![],
        coverage: super::super::CoverageDelta {
            pct: 100,
            total_source: 1,
            covered_count: 1,
            uncovered: vec![],
            covered: vec![],
            non_code_count: 0,
            ghost_tests: vec![],
        },
        findings: vec![],
        per_file_diff_files: vec![],
        skipped_checks: vec![],
        previous_run: None,
        run_history: vec![],
        flaky_scores: vec![],
        lint_metrics: vec![],
        ownership_map: vec![],
        risk_scores: vec![],
        i18n_delta: None,
    }
}

#[test]
fn test_trends_empty_history() {
    let ctx = mock_ctx();
    let html = build_trends_section(&ctx);
    assert!(html.is_empty(), "Empty history should produce empty string");
}

#[test]
fn test_trends_single_run() {
    use super::super::HistoricalRun;
    let mut ctx = mock_ctx();
    ctx.run_history = vec![HistoricalRun {
        timestamp: "20260302-143022".into(),
        checks_passed: 5,
        checks_failed: 0,
        checks_warned: 1,
        quality_pass: true,
        findings_count: 0,
    }];
    let html = build_trends_section(&ctx);
    assert!(
        html.is_empty(),
        "Single run should produce empty string (need 2+)"
    );
}

#[test]
fn test_trends_multiple_runs() {
    use super::super::HistoricalRun;
    let mut ctx = mock_ctx();
    ctx.run_history = vec![
        HistoricalRun {
            timestamp: "20260302-160000".into(),
            checks_passed: 6,
            checks_failed: 0,
            checks_warned: 1,
            quality_pass: true,
            findings_count: 0,
        },
        HistoricalRun {
            timestamp: "20260302-143022".into(),
            checks_passed: 4,
            checks_failed: 2,
            checks_warned: 1,
            quality_pass: false,
            findings_count: 3,
        },
        HistoricalRun {
            timestamp: "20260301-120000".into(),
            checks_passed: 3,
            checks_failed: 3,
            checks_warned: 0,
            quality_pass: false,
            findings_count: 5,
        },
    ];
    let html = build_trends_section(&ctx);

    assert!(html.contains("Historical Trends"), "Should have title");
    assert!(html.contains("section-trends"), "Should have section id");
    assert!(html.contains("3 runs"), "Should show run count");
    assert!(html.contains("latest"), "Should show latest badge");
    assert!(
        html.contains("03-02 16:00"),
        "Should format newest timestamp"
    );
    assert!(
        html.contains("03-01 12:00"),
        "Should format oldest timestamp"
    );
    assert!(html.contains("trends-bar-pass"), "Should have pass bars");
    assert!(html.contains("trends-bar-fail"), "Should have fail bars");
    assert!(
        html.contains("trend-improving"),
        "Latest run improved (0 failed vs avg 2.5)"
    );
}

#[test]
fn test_trends_degrading() {
    use super::super::HistoricalRun;
    let mut ctx = mock_ctx();
    ctx.run_history = vec![
        HistoricalRun {
            timestamp: "20260302-160000".into(),
            checks_passed: 2,
            checks_failed: 4,
            checks_warned: 0,
            quality_pass: false,
            findings_count: 10,
        },
        HistoricalRun {
            timestamp: "20260301-120000".into(),
            checks_passed: 5,
            checks_failed: 1,
            checks_warned: 0,
            quality_pass: true,
            findings_count: 2,
        },
    ];
    let html = build_trends_section(&ctx);

    assert!(
        html.contains("trend-degrading"),
        "Should show degrading trend"
    );
}

#[test]
fn test_trends_mixed_conflicting_signals() {
    use super::super::HistoricalRun;
    let mut ctx = mock_ctx();
    // Newest run has fewer failed checks (improving) but MORE findings
    // (worsening). The old improving-first logic hid this behind "Improving";
    // it must now be reported as "Mixed".
    ctx.run_history = vec![
        HistoricalRun {
            timestamp: "20260302-160000".into(),
            checks_passed: 6,
            checks_failed: 0,
            checks_warned: 0,
            quality_pass: false,
            findings_count: 10,
        },
        HistoricalRun {
            timestamp: "20260301-120000".into(),
            checks_passed: 2,
            checks_failed: 4,
            checks_warned: 0,
            quality_pass: false,
            findings_count: 2,
        },
    ];
    let html = build_trends_section(&ctx);

    assert!(
        html.contains("trend-mixed"),
        "Conflicting signals must render as mixed, not improving"
    );
    assert!(
        !html.contains("trend-improving"),
        "A regression in findings must not be badged as improving"
    );
}

#[test]
fn test_trends_stable() {
    use super::super::HistoricalRun;
    let mut ctx = mock_ctx();
    ctx.run_history = vec![
        HistoricalRun {
            timestamp: "20260302-160000".into(),
            checks_passed: 5,
            checks_failed: 1,
            checks_warned: 0,
            quality_pass: true,
            findings_count: 2,
        },
        HistoricalRun {
            timestamp: "20260301-120000".into(),
            checks_passed: 5,
            checks_failed: 1,
            checks_warned: 0,
            quality_pass: true,
            findings_count: 2,
        },
    ];
    let html = build_trends_section(&ctx);

    assert!(html.contains("trend-stable"), "Should show stable trend");
}

#[test]
fn test_trends_nav_conditional() {
    let config = mock_config();
    let diffs = vec![mock_diff()];
    let checks = mock_checks();
    let ctx = mock_ctx();
    let dir = std::env::temp_dir();

    let html = build_html_test!(&config, &diffs, &checks, None, &ctx, "", &dir, "", None);
    assert!(
        !html.contains(r##"href="#section-trends""##),
        "Nav should NOT have Trends link when no history"
    );
}

#[test]
fn test_trends_nav_present_with_history() {
    use super::super::HistoricalRun;
    let config = mock_config();
    let diffs = vec![mock_diff()];
    let checks = mock_checks();
    let mut ctx = mock_ctx();
    ctx.run_history = vec![
        HistoricalRun {
            timestamp: "20260302-160000".into(),
            checks_passed: 5,
            checks_failed: 0,
            checks_warned: 0,
            quality_pass: true,
            findings_count: 0,
        },
        HistoricalRun {
            timestamp: "20260301-120000".into(),
            checks_passed: 4,
            checks_failed: 1,
            checks_warned: 0,
            quality_pass: true,
            findings_count: 1,
        },
    ];
    let dir = std::env::temp_dir();

    let html = build_html_test!(&config, &diffs, &checks, None, &ctx, "", &dir, "", None);
    assert!(
        html.contains(r##"href="#section-trends""##),
        "Nav SHOULD have Trends link when history >= 2"
    );
    assert!(
        html.contains("Historical Trends"),
        "Should render trends section"
    );
}

#[test]
fn test_format_run_timestamp() {
    assert_eq!(format_run_timestamp("20260302-143022"), "03-02 14:30");
    assert_eq!(format_run_timestamp("20260115-091500"), "01-15 09:15");
    assert_eq!(format_run_timestamp("short"), "short");
}

// ---- PRV-203: Ownership Map ----

#[test]
fn test_ownership_section_groups_by_owner() {
    let mut ctx = mock_ctx();
    ctx.ownership_map = vec![
        ("src/main.rs".into(), "core".into()),
        ("src/lib.rs".into(), "core".into()),
        ("tests/test_foo.rs".into(), "testing".into()),
    ];
    let html = build_ownership_section(&ctx);

    assert!(html.contains("Ownership"), "Should have Ownership title");
    assert!(html.contains("2 owners"), "Should show 2 owners");
    assert!(html.contains("core"), "Should show core owner");
    assert!(html.contains("testing"), "Should show testing owner");
    assert!(html.contains("2 files"), "core should have 2 files");
    assert!(html.contains("1 file"), "testing should have 1 file");
}

#[test]
fn test_ownership_section_empty() {
    let mut ctx = mock_ctx();
    ctx.ownership_map = vec![];
    let html = build_ownership_section(&ctx);
    assert!(
        html.is_empty(),
        "Empty ownership map should produce empty HTML"
    );
}

#[test]
fn test_ownership_badge_in_files_section() {
    let diff = mock_diff(); // has src/main.rs
    let mut ctx = mock_ctx();
    ctx.ownership_map = vec![("src/main.rs".into(), "core".into())];
    let html = build_files_section(Some(&diff), &ctx);

    // Owner badges should appear in file rows
    assert!(
        html.contains("badge badge-muted"),
        "Should have owner badges"
    );
    assert!(html.contains(">core<"), "Should show 'core' owner badge");
}

// ---- PRV-204: Flaky Checks ----

#[test]
fn test_flaky_section_hidden_with_no_history() {
    let ctx = mock_ctx();
    let html = build_flaky_section(&ctx);
    assert!(html.is_empty(), "Should be empty with no run history");
}

#[test]
fn test_flaky_section_hidden_with_single_run() {
    use super::super::HistoricalRun;
    let mut ctx = mock_ctx();
    ctx.run_history = vec![HistoricalRun {
        timestamp: "20260302-143022".into(),
        checks_passed: 5,
        checks_failed: 0,
        checks_warned: 1,
        quality_pass: true,
        findings_count: 0,
    }];
    let html = build_flaky_section(&ctx);
    assert!(html.is_empty(), "Should be empty with only 1 run");
}

#[test]
fn test_flaky_section_stable_card() {
    use super::super::HistoricalRun;
    let mut ctx = mock_ctx();
    ctx.run_history = vec![
        HistoricalRun {
            timestamp: "20260302-160000".into(),
            checks_passed: 5,
            checks_failed: 0,
            checks_warned: 0,
            quality_pass: true,
            findings_count: 0,
        },
        HistoricalRun {
            timestamp: "20260302-143022".into(),
            checks_passed: 5,
            checks_failed: 0,
            checks_warned: 0,
            quality_pass: true,
            findings_count: 0,
        },
    ];
    // No flaky scores = stable
    ctx.flaky_scores = vec![];
    let html = build_flaky_section(&ctx);
    assert!(
        html.contains("All checks stable across 2 runs"),
        "Should show stable card"
    );
    assert!(html.contains("section-flaky"), "Should have section id");
}

#[test]
fn test_flaky_section_with_flaky_checks() {
    use super::super::{FlakyCheckScore, HistoricalRun};
    let mut ctx = mock_ctx();
    ctx.run_history = vec![
        HistoricalRun {
            timestamp: "20260302-160000".into(),
            checks_passed: 5,
            checks_failed: 1,
            checks_warned: 0,
            quality_pass: false,
            findings_count: 0,
        },
        HistoricalRun {
            timestamp: "20260302-143022".into(),
            checks_passed: 6,
            checks_failed: 0,
            checks_warned: 0,
            quality_pass: true,
            findings_count: 0,
        },
        HistoricalRun {
            timestamp: "20260301-120000".into(),
            checks_passed: 5,
            checks_failed: 1,
            checks_warned: 0,
            quality_pass: false,
            findings_count: 0,
        },
    ];
    ctx.flaky_scores = vec![FlakyCheckScore {
        check_id: "cargo_test".into(),
        check_name: "cargo test".into(),
        flaky_score: 1.0,
        total_runs: 3,
        transitions: 2,
        last_statuses: vec!["FAIL".into(), "PASS".into(), "FAIL".into()],
        confidence: "low",
    }];
    let html = build_flaky_section(&ctx);
    assert!(
        html.contains(r#"data-i18n="section.flakyChecks""#),
        "Should have localized title"
    );
    assert!(html.contains("1 flaky"), "Should show flaky count");
    assert!(html.contains("cargo test"), "Should show check name");
    assert!(html.contains("100%"), "Should show 100% flaky score");
    assert!(html.contains("flaky-dot-fail"), "Should have fail dots");
    assert!(html.contains("flaky-dot-pass"), "Should have pass dots");
    assert!(
        html.contains("flaky-confidence-low"),
        "Should show low confidence"
    );
}

#[test]
fn test_flaky_section_score_classes() {
    use super::super::{FlakyCheckScore, HistoricalRun};
    let mut ctx = mock_ctx();
    ctx.run_history = vec![
        HistoricalRun {
            timestamp: "20260302-160000".into(),
            checks_passed: 5,
            checks_failed: 0,
            checks_warned: 0,
            quality_pass: true,
            findings_count: 0,
        },
        HistoricalRun {
            timestamp: "20260302-143022".into(),
            checks_passed: 5,
            checks_failed: 0,
            checks_warned: 0,
            quality_pass: true,
            findings_count: 0,
        },
    ];
    ctx.flaky_scores = vec![
        FlakyCheckScore {
            check_id: "high".into(),
            check_name: "high score".into(),
            flaky_score: 0.75,
            total_runs: 5,
            transitions: 3,
            last_statuses: vec![
                "PASS".into(),
                "FAIL".into(),
                "PASS".into(),
                "FAIL".into(),
                "PASS".into(),
            ],
            confidence: "medium",
        },
        FlakyCheckScore {
            check_id: "mid".into(),
            check_name: "mid score".into(),
            flaky_score: 0.33,
            total_runs: 4,
            transitions: 1,
            last_statuses: vec!["PASS".into(), "PASS".into(), "PASS".into(), "FAIL".into()],
            confidence: "medium",
        },
        FlakyCheckScore {
            check_id: "low".into(),
            check_name: "low score".into(),
            flaky_score: 0.1,
            total_runs: 10,
            transitions: 1,
            last_statuses: vec!["PASS".into(); 10],
            confidence: "high",
        },
    ];
    let html = build_flaky_section(&ctx);
    assert!(
        html.contains("flaky-score-high"),
        "Score >= 0.5 should be high"
    );
    assert!(
        html.contains("flaky-score-mid"),
        "Score >= 0.25 should be mid"
    );
    assert!(
        html.contains("flaky-score-low"),
        "Score < 0.25 should be low"
    );
    assert!(html.contains("3 flaky"), "Should show 3 flaky checks");
}

#[test]
fn test_flaky_nav_link_conditional() {
    use super::super::HistoricalRun;
    let config = mock_config();
    let diffs = vec![mock_diff()];
    let checks = mock_checks();
    let mut ctx = mock_ctx();
    let dir = std::env::temp_dir();

    // No history => no flaky nav link
    let html = build_html_test!(&config, &diffs, &checks, None, &ctx, "", &dir, "", None);
    assert!(
        !html.contains(r##"href="#section-flaky""##),
        "Nav should NOT have Flaky link when no history"
    );

    // With history => flaky nav link present
    ctx.run_history = vec![
        HistoricalRun {
            timestamp: "20260302-160000".into(),
            checks_passed: 5,
            checks_failed: 0,
            checks_warned: 0,
            quality_pass: true,
            findings_count: 0,
        },
        HistoricalRun {
            timestamp: "20260302-143022".into(),
            checks_passed: 5,
            checks_failed: 0,
            checks_warned: 0,
            quality_pass: true,
            findings_count: 0,
        },
    ];
    let html = build_html_test!(&config, &diffs, &checks, None, &ctx, "", &dir, "", None);
    assert!(
        html.contains(r##"href="#section-flaky""##),
        "Nav SHOULD have Flaky link when history >= 2"
    );
}

// -----------------------------------------------------------------------
// PRV-205: Lint metrics dashboard tests
// -----------------------------------------------------------------------

#[test]
fn test_lint_section_hidden_when_no_lint_checks() {
    let ctx = mock_ctx();
    let diffs = vec![mock_diff()];
    let html = build_lint_metrics_section(&ctx, &diffs);
    assert!(html.is_empty(), "No lint checks = section hidden");
}

#[test]
fn test_lint_section_clean() {
    let mut ctx = mock_ctx();
    ctx.lint_metrics = vec![LintMetrics {
        check_name: "cargo clippy".into(),
        new_issues: 0,
        legacy_issues: 0,
        total_issues: 0,
        changed_files_with_issues: vec![],
    }];
    let diffs = vec![mock_diff()];
    let html = build_lint_metrics_section(&ctx, &diffs);
    assert!(html.contains("section-lint"), "Section should exist");
    assert!(
        html.contains(r#"data-i18n="label.clean""#),
        "Should localize clean badge"
    );
    assert!(
        html.contains(r#"data-i18n-template="message.cleanLintAcrossChecks""#),
        "Should localize clean helper copy"
    );
}

#[test]
fn test_lint_section_no_diff_data() {
    let mut ctx = mock_ctx();
    ctx.lint_metrics = vec![LintMetrics {
        check_name: "cargo clippy".into(),
        new_issues: 0,
        legacy_issues: 0,
        total_issues: 0,
        changed_files_with_issues: vec![],
    }];
    let diffs: Vec<Diff> = vec![];
    let html = build_lint_metrics_section(&ctx, &diffs);
    assert!(html.contains("section-lint"), "Section should exist");
    assert!(
        html.contains(r#"data-i18n="message.lintMetricsUnavailable""#),
        "Should localize fallback message"
    );
}

#[test]
fn test_lint_section_new_issues() {
    let mut ctx = mock_ctx();
    ctx.lint_metrics = vec![LintMetrics {
        check_name: "cargo clippy".into(),
        new_issues: 3,
        legacy_issues: 1,
        total_issues: 4,
        changed_files_with_issues: vec!["src/main.rs".into(), "src/lib.rs".into()],
    }];
    let diffs = vec![mock_diff()];
    let html = build_lint_metrics_section(&ctx, &diffs);
    assert!(html.contains("section-lint"), "Section should exist");
    assert!(
        html.contains(r#"data-i18n="label.newIssues""#),
        "Should localize NEW ISSUES badge"
    );
    assert!(
        html.contains(r#"data-i18n-template="message.lintNewInChangedFiles""#),
        "Should localize new issue count"
    );
    assert!(
        html.contains(r#"data-i18n-template="message.lintLegacyPreExisting""#),
        "Should localize legacy issue count"
    );
    assert!(
        html.contains(r#"data-i18n-template="message.lintChangedFilesWithIssues""#),
        "Should localize changed-files helper copy"
    );
    assert!(html.contains("src/main.rs"), "Should list affected files");
    assert!(html.contains("src/lib.rs"), "Should list affected files");
}

#[test]
fn test_lint_section_legacy_only() {
    let mut ctx = mock_ctx();
    ctx.lint_metrics = vec![LintMetrics {
        check_name: "ESLint".into(),
        new_issues: 0,
        legacy_issues: 5,
        total_issues: 5,
        changed_files_with_issues: vec![],
    }];
    let diffs = vec![mock_diff()];
    let html = build_lint_metrics_section(&ctx, &diffs);
    assert!(
        html.contains(r#"data-i18n="label.legacyOnly""#),
        "Should localize LEGACY ONLY badge"
    );
    assert!(
        html.contains(r#"data-i18n-template="message.lintLegacyPreExisting""#),
        "Should localize legacy count"
    );
    assert!(
        !html.contains(r#"data-i18n-template="message.lintNewInChangedFiles""#),
        "Should NOT show new count"
    );
}

#[test]
fn test_regression_details_use_localized_markers() {
    let ctx = mock_ctx();
    let report = crate::regression::compute_regression(&crate::regression::RegressionContext {
        files_changed: 2,
        insertions: 40,
        deletions: 10,
        file_stats: vec![
            ("src/main.rs".into(), 'M', 30, 10),
            ("src/lib.rs".into(), 'M', 10, 0),
        ],
        ..Default::default()
    });

    let html = build_regression_details_section(&ctx, None, Some(&report));

    assert!(
        html.contains(r#"data-i18n-template="summary.regressionScore""#),
        "Regression summary should be localizable"
    );
    assert!(
        html.contains(r#"data-i18n="label.hotspots""#),
        "Hotspots tab should be localizable"
    );
    assert!(
        html.contains(r#"data-i18n-template="summary.severityValue""#),
        "Severity label should be localizable"
    );
}

#[test]
fn test_lint_nav_link_conditional() {
    let dir = std::env::temp_dir().join("prv205-nav-test");
    let _ = std::fs::create_dir_all(&dir);
    let config = mock_config();
    let diffs = vec![mock_diff()];
    let checks = mock_checks();

    // Without lint metrics — no nav link
    let ctx = mock_ctx();
    let html = build_html_test!(&config, &diffs, &checks, None, &ctx, "", &dir, "", None);
    assert!(
        !html.contains(r##"href="#section-lint""##),
        "No lint = no nav link"
    );

    // With lint metrics — statistics stays the single sidebar entry
    let mut ctx2 = mock_ctx();
    ctx2.lint_metrics = vec![LintMetrics {
        check_name: "cargo clippy".into(),
        new_issues: 1,
        legacy_issues: 0,
        total_issues: 1,
        changed_files_with_issues: vec!["src/main.rs".into()],
    }];
    let html2 = build_html_test!(&config, &diffs, &checks, None, &ctx2, "", &dir, "", None);
    assert!(
        !html2.contains(r##"href="#section-lint""##),
        "Lint should be folded under Statistics in the sidebar"
    );
    assert!(
        html2.contains(r##"href="#section-statistics""##),
        "Statistics link should remain available"
    );
    let _ = std::fs::remove_dir_all(&dir);
}

// ── file_category tests ──────────────────────────────────────────

#[test]
fn test_file_category_image_assets() {
    assert_eq!(file_category("assets/logo.png"), "asset");
    assert_eq!(file_category("public/icon.svg"), "asset");
    assert_eq!(file_category("img/photo.jpg"), "asset");
    assert_eq!(file_category("img/photo.jpeg"), "asset");
    assert_eq!(file_category("img/anim.gif"), "asset");
    assert_eq!(file_category("img/hero.webp"), "asset");
    assert_eq!(file_category("img/icon.ico"), "asset");
    assert_eq!(file_category("img/next.avif"), "asset");
    assert_eq!(file_category("img/old.bmp"), "asset");
}

#[test]
fn test_file_category_i18n_under_locales() {
    assert_eq!(file_category("src/locales/en/common.json"), "i18n");
    assert_eq!(file_category("src/i18n/fr/messages.json"), "i18n");
    assert_eq!(file_category("app/translations/de/strings.json"), "i18n");
}

#[test]
fn test_file_category_json_not_under_locales_is_non_code() {
    assert_eq!(file_category("src/data/config.json"), "non-code");
    assert_eq!(file_category("fixtures/data.json"), "non-code");
}

#[test]
fn test_file_category_lock_files_are_config() {
    assert_eq!(file_category("Cargo.lock"), "config");
    assert_eq!(file_category("yarn.lock"), "config");
    assert_eq!(file_category("package-lock.json"), "config");
    assert_eq!(file_category("pnpm-lock.yaml"), "config");
}

#[test]
fn test_file_category_yaml_toml_are_config() {
    assert_eq!(file_category("Cargo.toml"), "config");
    assert_eq!(file_category(".github/workflows/ci.yaml"), "config");
    assert_eq!(file_category(".github/workflows/ci.yml"), "config");
}

#[test]
fn test_file_category_source_code() {
    assert_eq!(file_category("src/main.rs"), "code");
    assert_eq!(file_category("src/app.ts"), "code");
    assert_eq!(file_category("src/components/Button.tsx"), "code");
}

#[test]
fn test_file_category_root_tests_dir() {
    assert_eq!(file_category("tests/json_contract.rs"), "test");
    assert_eq!(file_category("src/regression/tests.rs"), "test");
}

#[test]
fn test_file_category_specific_config_files() {
    assert_eq!(file_category("tsconfig.json"), "config");
    assert_eq!(file_category(".gitignore"), "config");
    assert_eq!(file_category("package.json"), "config");
    assert_eq!(file_category(".editorconfig"), "config");
    assert_eq!(file_category("Makefile"), "config"); // lowercased → "makefile"
    assert_eq!(file_category("Dockerfile"), "config"); // lowercased → "dockerfile"
}

#[test]
fn test_file_category_markdown_is_non_code() {
    assert_eq!(file_category("README.md"), "non-code");
    assert_eq!(file_category("docs/DESIGN.md"), "non-code");
}

#[test]
fn test_file_category_font_is_non_code() {
    assert_eq!(file_category("assets/Inter.woff2"), "non-code");
}

#[test]
fn test_extract_narrative_preview_strips_markdown_noise() {
    let md = r#"
## PR Review

> **Branch:** feat/x | **Base:** develop | **Profile:** Mixed
> **Commits:** 3 • **Files:** 12
"#;

    let preview = extract_narrative_preview(md);
    assert!(preview.contains("Branch: feat/x | Base: develop | Profile: Mixed"));
    assert!(!preview.contains("**"));
    assert!(!preview.contains('>'));
}

// ── extract_finding_location tests ───────────────────────────────

#[test]
fn test_extract_finding_path_line_message() {
    let (path, line, msg) = extract_finding_location("src/foo.rs:42: error message");
    assert_eq!(path, Some("src/foo.rs"));
    assert_eq!(line, Some(42));
    assert_eq!(msg, "error message");
}

#[test]
fn test_extract_finding_path_no_line() {
    let (path, line, msg) = extract_finding_location("src/foo.rs: message without line");
    assert_eq!(path, Some("src/foo.rs"));
    assert_eq!(line, None);
    assert_eq!(msg, "message without line");
}

#[test]
fn test_extract_finding_plain_message() {
    let (path, line, msg) = extract_finding_location("just a message");
    assert_eq!(path, None);
    assert_eq!(line, None);
    assert_eq!(msg, "just a message");
}

#[test]
fn test_extract_finding_empty_string() {
    let (path, line, msg) = extract_finding_location("");
    assert_eq!(path, None);
    assert_eq!(line, None);
    assert_eq!(msg, "");
}

#[test]
fn test_extract_finding_path_line_col() {
    // Pattern: path:line:col: message
    let (path, line, msg) = extract_finding_location("src/lib.rs:10:5: unused variable");
    assert_eq!(path, Some("src/lib.rs"));
    assert_eq!(line, Some(10));
    // After line 10, rest is "5: unused variable" — parser takes 5 as col, returns msg
    assert!(msg.contains("unused variable"));
}