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
//! Test/coverage regression analysis (v4)
//!
//! Detects coverage ratio drops and untested critical files.
//! Classifies files into code vs non-code to avoid false positives
//! from config/doc/script changes inflating the untested count.

use super::RegressionContext;
use serde::{Deserialize, Serialize};
use std::path::Path;

/// Max non-code files to include in the report (avoid bloat).
const NON_CODE_CAP: usize = 20;

/// Source code extensions we consider "must be tested".
const CODE_EXTENSIONS: &[&str] = &[
    "rs", "ts", "tsx", "js", "jsx", "mjs", "cjs", "cts", "mts", "py", "swift", "go", "java", "kt",
    "c", "cpp", "h", "hpp",
];

/// Directory markers that indicate a test directory.
/// Matched boundary-aware: must appear at path start or after `/`.
const TEST_DIR_MARKERS: &[&str] = &[
    "tests/",
    "test/",
    "__tests__/",
    "e2e/",
    "spec/",
    "__mocks__/",
    "__fixtures__/",
];

/// Basename suffix markers (before the extension) that indicate a test file.
const TEST_SUFFIX_MARKERS: &[&str] = &["_test", ".test", "_spec", ".spec"];

// ---------------------------------------------------------------------------
// Classification helpers
// ---------------------------------------------------------------------------

/// Returns `true` if `path` looks like a source-code file based on extension.
pub fn is_code_file(path: &str) -> bool {
    Path::new(path)
        .extension()
        .and_then(|e| e.to_str())
        .is_some_and(|ext| CODE_EXTENSIONS.contains(&ext))
}

/// Returns `true` if `path` is a config-like file that has a code extension
/// but should not be treated as "must be tested" source code.
///
/// Matches:
/// - `*.config.{ts,js,mjs,cjs,...}` — build/tool config files
/// - `*.setup.{ts,js}` — test setup files
/// - `*.d.ts` — TypeScript declaration files
/// - Basename exactly `index.{ts,js,tsx,jsx}` — barrel/re-export files
/// - Basename exactly `types.ts` or `types.d.ts` — pure type files
/// - Files in `devtools/`, `storybook/`, `.storybook/`, `stories/` directories
/// - Files with `mock` or `Mock` in basename (e.g. `browserMocks.ts`)
pub fn is_config_like(path: &str) -> bool {
    let p = Path::new(path);
    let basename = p.file_name().and_then(|n| n.to_str()).unwrap_or("");
    let basename_lower = basename.to_lowercase();

    // TypeScript declaration files: *.d.ts
    if basename_lower.ends_with(".d.ts") {
        return true;
    }

    // *.config.EXT or *.setup.EXT (where EXT is a code extension)
    // Strip the final extension first, then check if the stem ends with .config or .setup
    if let Some(ext) = p.extension().and_then(|e| e.to_str())
        && CODE_EXTENSIONS.contains(&ext)
    {
        let stem = &basename_lower[..basename_lower.len() - ext.len() - 1];
        if stem.ends_with(".config") || stem.ends_with(".setup") {
            return true;
        }
    }

    // Barrel files: basename is exactly index.{ts,js,tsx,jsx}
    const BARREL_NAMES: &[&str] = &["index.ts", "index.js", "index.tsx", "index.jsx"];
    if BARREL_NAMES.contains(&basename_lower.as_str()) {
        return true;
    }

    // Pure type files: basename is exactly types.ts
    if basename_lower == "types.ts" {
        return true;
    }

    // Devtools / storybook directories (development tooling, not production code)
    let lower_path = path.to_lowercase();
    const DEV_DIRS: &[&str] = &["devtools/", "storybook/", ".storybook/", "stories/"];
    for dir in DEV_DIRS {
        if lower_path.starts_with(dir) || lower_path.contains(&format!("/{}", dir)) {
            return true;
        }
    }

    // Mock files: basename contains "mock" (case-insensitive) with code extension
    if is_code_file(path) && basename_lower.contains("mock") {
        return true;
    }

    false
}

/// Returns `true` if `path` looks like a test file.
///
/// Uses boundary-aware matching to avoid false positives:
/// - Directory markers must be at path start or after `/` (no `contest/` match)
/// - Suffix markers checked against the file stem (no `latest_version.ts` match)
/// - `test_` prefix checked only on the basename (no `src/contest_handler.rs` match)
pub fn is_test_file(path: &str) -> bool {
    let lower = path.to_lowercase();

    // 1. Directory markers (boundary-aware)
    for marker in TEST_DIR_MARKERS {
        if lower.starts_with(marker) || lower.contains(&format!("/{}", marker)) {
            return true;
        }
    }

    // 2. Extract basename and stem for precise matching
    let basename = lower.rsplit('/').next().unwrap_or(&lower);
    let stem = match basename.rfind('.') {
        Some(pos) => &basename[..pos],
        None => basename,
    };

    // 3. Suffix markers: stem must END with `_test`, `.test`, `_spec`, `.spec`
    for marker in TEST_SUFFIX_MARKERS {
        if stem.ends_with(marker) {
            return true;
        }
    }

    // 4. Prefix marker: basename starts with `test_`
    if basename.starts_with("test_") {
        return true;
    }

    // 5. Exact stem matches (e.g. `tests.rs`, `test.py`)
    if stem == "tests" || stem == "test" {
        return true;
    }

    false
}

// ---------------------------------------------------------------------------
// Rust inline test detection
// ---------------------------------------------------------------------------

/// Check if a `.rs` file has inline test evidence (`#[cfg(test)]` or `#[test]`)
/// visible in the patch hunks (added, removed, or context lines).
fn has_inline_test_in_patch(file: &str, patch: &str) -> bool {
    if !file.ends_with(".rs") {
        return false;
    }

    let mut in_file = false;
    for line in patch.lines() {
        if line.starts_with("+++ b/") {
            in_file = &line[6..] == file;
            continue;
        }
        if in_file && (line.contains("#[cfg(test)]") || line.contains("#[test]")) {
            return true;
        }
    }
    false
}

// ---------------------------------------------------------------------------
// Struct
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TestRegression {
    pub coverage_ratio: Option<f64>,
    pub base_coverage_ratio: Option<f64>,
    pub coverage_delta: Option<f64>,

    /// Code-only untested files (backward-compat: was all files before v4).
    pub untested_critical_files: Vec<String>,
    /// Count of code-only untested files.
    pub untested_critical_count: usize,

    /// Code files without matching test change.
    #[serde(default)]
    pub untested_code_files: Vec<String>,
    #[serde(default)]
    pub untested_code_count: usize,

    /// Non-code files (config, docs, scripts, etc.) — capped.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub untested_non_code_files: Vec<String>,
    #[serde(default)]
    pub untested_non_code_count: usize,

    /// Rust files excluded from untested count because they have inline
    /// `#[cfg(test)]` / `#[test]` evidence in the patch.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub rust_inline_test_files: Vec<String>,

    /// Count of `.rs` files still in the untested list that *may* have inline
    /// tests not visible in the patch (caveat for reviewers).
    #[serde(default, skip_serializing_if = "is_zero")]
    pub rust_inline_test_caveat_count: usize,

    #[serde(default)]
    pub coverage_improvement_detected: bool,
    pub coverage_regression_detected: bool,
}

fn is_zero(v: &usize) -> bool {
    *v == 0
}

// ---------------------------------------------------------------------------
// Analysis
// ---------------------------------------------------------------------------

pub fn analyze(ctx: &RegressionContext) -> TestRegression {
    let coverage_delta = match (ctx.coverage_ratio, ctx.base_coverage_ratio) {
        (Some(current), Some(base)) => Some(current - base),
        _ => None,
    };

    // Classify incoming untested files
    let mut untested_code: Vec<String> = Vec::new();
    let mut untested_non_code: Vec<String> = Vec::new();

    for file in &ctx.untested_critical_files {
        if is_test_file(file) {
            // Test files are neither "code that needs testing" nor "non-code";
            // they are tests themselves — skip entirely.
            continue;
        }
        if is_code_file(file) && !is_config_like(file) {
            untested_code.push(file.clone());
        } else {
            untested_non_code.push(file.clone());
        }
    }

    let mut rust_inline_test_files = Vec::new();
    if let Some(patch_text) = ctx.patch_text.as_deref() {
        let mut remaining = Vec::with_capacity(untested_code.len());
        for file in untested_code {
            if has_inline_test_in_patch(&file, patch_text) {
                rust_inline_test_files.push(file);
            } else {
                remaining.push(file);
            }
        }
        untested_code = remaining;
    }

    let rust_inline_test_caveat_count = untested_code
        .iter()
        .filter(|file| file.ends_with(".rs"))
        .count();
    let untested_code_count = untested_code.len();
    let untested_non_code_count = untested_non_code.len();

    // Cap non-code list to avoid report bloat
    untested_non_code.truncate(NON_CODE_CAP);

    // Regression detection — based on CODE files only
    let ratio_dropped = coverage_delta.is_some_and(|d| d < -0.01);
    let untested_increased = ctx
        .base_untested_critical_count
        .is_some_and(|base| untested_code_count > base);

    // Adaptive threshold: compute code-specific test coverage ratio from file_stats.
    // total_code_in_pr = code files in the PR (excluding tests and config-like).
    // coverage_ratio = (total_code - untested_code) / total_code.
    let total_code_from_stats = ctx
        .file_stats
        .iter()
        .filter(|(path, _, _, _)| {
            is_code_file(path) && !is_test_file(path) && !is_config_like(path)
        })
        .count();

    // When file_stats is unavailable, fall back to untested_code_count as the
    // total (worst-case: 0% coverage). This keeps the detection conservative.
    let total_code_in_pr = if total_code_from_stats > 0 {
        total_code_from_stats
    } else {
        untested_code_count
    };

    let code_coverage_ratio = if total_code_in_pr > 0 {
        (total_code_in_pr.saturating_sub(untested_code_count)) as f64 / total_code_in_pr as f64
    } else {
        1.0 // no code files at all → fully covered by definition
    };

    let coverage_delta_drop = coverage_delta.is_some_and(|d| d < -0.05);
    let low_coverage = untested_code_count > 0 && code_coverage_ratio < 0.4;
    let hard_cap = untested_code_count > 10 && code_coverage_ratio < 0.5;

    // Suppress soft signals (low_coverage, hard_cap, untested_increased) when
    // coverage actually improved — those indicate current state, not regression.
    let coverage_improved = coverage_delta.is_some_and(|d| d > 0.01);

    let detected = ratio_dropped
        || coverage_delta_drop
        || (untested_increased && !coverage_improved)
        || (!coverage_improved && (low_coverage || hard_cap));

    TestRegression {
        coverage_ratio: ctx.coverage_ratio,
        base_coverage_ratio: ctx.base_coverage_ratio,
        coverage_delta,
        // Backward compat: these now only contain code files
        untested_critical_files: untested_code.clone(),
        untested_critical_count: untested_code_count,
        // New granular fields
        untested_code_files: untested_code,
        untested_code_count,
        untested_non_code_files: untested_non_code,
        untested_non_code_count,
        rust_inline_test_files,
        rust_inline_test_caveat_count,
        coverage_improvement_detected: coverage_improved,
        coverage_regression_detected: detected,
    }
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests_unit {
    use super::*;

    // -- Classification tests --

    #[test]
    fn test_is_code_file_rust() {
        assert!(is_code_file("src/main.rs"));
        assert!(is_code_file("lib/foo.rs"));
    }

    #[test]
    fn test_is_code_file_js_ts() {
        assert!(is_code_file("src/app.ts"));
        assert!(is_code_file("src/index.tsx"));
        assert!(is_code_file("src/util.js"));
        assert!(is_code_file("src/worker.mjs"));
        assert!(is_code_file("src/App.jsx"));
    }

    #[test]
    fn test_is_code_file_other_langs() {
        assert!(is_code_file("main.py"));
        assert!(is_code_file("main.go"));
        assert!(is_code_file("Main.java"));
        assert!(is_code_file("Main.kt"));
        assert!(is_code_file("lib.swift"));
        assert!(is_code_file("util.c"));
        assert!(is_code_file("util.cpp"));
        assert!(is_code_file("util.h"));
        assert!(is_code_file("util.hpp"));
    }

    #[test]
    fn test_is_not_code_file() {
        assert!(!is_code_file(".env"));
        assert!(!is_code_file(".gitignore"));
        assert!(!is_code_file("Makefile"));
        assert!(!is_code_file("README.md"));
        assert!(!is_code_file("Cargo.toml"));
        assert!(!is_code_file("package.json"));
        assert!(!is_code_file("docs/guide.md"));
        assert!(!is_code_file("scripts/deploy.sh"));
        assert!(!is_code_file(".env.production"));
        assert!(!is_code_file("Dockerfile"));
        assert!(!is_code_file("docker-compose.yml"));
    }

    #[test]
    fn test_is_test_file() {
        assert!(is_test_file("src/tests/foo.rs"));
        assert!(is_test_file("tests/integration.rs"));
        assert!(is_test_file("__tests__/App.test.tsx"));
        assert!(is_test_file("e2e/login.spec.ts"));
        assert!(is_test_file("src/foo_test.go"));
        assert!(is_test_file("src/foo.test.js"));
        assert!(is_test_file("src/foo_spec.rb"));
        assert!(is_test_file("src/foo.spec.ts"));
        assert!(is_test_file("src/regression/tests.rs"));
        assert!(is_test_file("test_utils.py"));
        // Mock, fixture, and spec dirs
        assert!(is_test_file("__mocks__/api.ts"));
        assert!(is_test_file("src/__mocks__/service.ts"));
        assert!(is_test_file("__fixtures__/data.json"));
        assert!(is_test_file("spec/api_handler.ts"));
        assert!(is_test_file("src/spec/models/user.ts"));
    }

    #[test]
    fn test_is_not_test_file() {
        assert!(!is_test_file("src/main.rs"));
        assert!(!is_test_file("src/lib.rs"));
        assert!(!is_test_file("src/app.ts"));
    }

    // -- Analyze: classification --

    #[test]
    fn test_analyze_separates_code_and_non_code() {
        let ctx = RegressionContext {
            untested_critical_files: vec![
                "src/main.rs".into(),
                "src/lib.rs".into(),
                ".env".into(),
                "README.md".into(),
                "Makefile".into(),
                "scripts/deploy.sh".into(),
            ],
            // 2 untested out of 8 code files → 75% coverage → no regression
            file_stats: (0..8)
                .map(|i| (format!("src/mod_{}.rs", i), 'M', 10, 5))
                .collect(),
            ..Default::default()
        };

        let result = analyze(&ctx);

        assert_eq!(result.untested_code_count, 2);
        assert_eq!(result.untested_non_code_count, 4);
        assert_eq!(result.untested_critical_count, 2); // backward compat = code only
        assert_eq!(
            result.untested_critical_files,
            vec!["src/main.rs", "src/lib.rs"]
        );
        assert!(!result.coverage_regression_detected); // 2/8 = 75% coverage, no ratio drop
    }

    #[test]
    fn test_analyze_test_files_excluded() {
        let ctx = RegressionContext {
            untested_critical_files: vec![
                "src/main.rs".into(),
                "tests/integration.rs".into(),
                "__tests__/App.test.tsx".into(),
            ],
            ..Default::default()
        };

        let result = analyze(&ctx);
        assert_eq!(result.untested_code_count, 1); // only main.rs
        assert_eq!(result.untested_non_code_count, 0);
    }

    #[test]
    fn test_analyze_rust_inline_tests_remove_file_from_untested_code() {
        let ctx = RegressionContext {
            untested_critical_files: vec!["src/lib.rs".into()],
            file_stats: vec![("src/lib.rs".into(), 'M', 20, 3)],
            patch_text: Some(
                "diff --git a/src/lib.rs b/src/lib.rs\n\
                 --- a/src/lib.rs\n\
                 +++ b/src/lib.rs\n\
                 @@ -1,3 +1,9 @@\n\
                  pub fn hello() {}\n\
                 +#[cfg(test)]\n\
                 +mod tests {\n\
                 +    #[test]\n\
                 +    fn it_works() {}\n\
                 +}\n"
                    .into(),
            ),
            ..Default::default()
        };

        let result = analyze(&ctx);

        assert_eq!(result.untested_code_count, 0);
        assert_eq!(result.untested_critical_count, 0);
        assert_eq!(result.rust_inline_test_files, vec!["src/lib.rs"]);
        assert_eq!(result.rust_inline_test_caveat_count, 0);
    }

    #[test]
    fn test_analyze_non_code_capped() {
        let mut files: Vec<String> = (0..30).map(|i| format!("docs/page_{}.md", i)).collect();
        files.push("src/real.rs".into());

        let ctx = RegressionContext {
            untested_critical_files: files,
            ..Default::default()
        };

        let result = analyze(&ctx);
        assert_eq!(result.untested_code_count, 1);
        assert_eq!(result.untested_non_code_count, 30); // full count
        assert_eq!(result.untested_non_code_files.len(), NON_CODE_CAP); // capped in vec
    }

    // -- Regression detection --

    #[test]
    fn test_no_regression_for_non_code_only() {
        let ctx = RegressionContext {
            untested_critical_files: (0..20).map(|i| format!("docs/page_{}.md", i)).collect(),
            ..Default::default()
        };

        let result = analyze(&ctx);
        assert!(!result.coverage_regression_detected);
        assert_eq!(result.untested_code_count, 0);
        assert_eq!(result.untested_non_code_count, 20);
    }

    #[test]
    fn test_regression_detected_many_code_files() {
        let ctx = RegressionContext {
            untested_critical_files: (0..8).map(|i| format!("src/module_{}.rs", i)).collect(),
            ..Default::default()
        };

        let result = analyze(&ctx);
        assert!(result.coverage_regression_detected); // 8 untested, 0% coverage (fallback)
        assert_eq!(result.untested_code_count, 8);
    }

    #[test]
    fn test_regression_detected_ratio_drop() {
        let ctx = RegressionContext {
            coverage_ratio: Some(0.5),
            base_coverage_ratio: Some(0.8),
            ..Default::default()
        };

        let result = analyze(&ctx);
        assert!(result.coverage_regression_detected);
        assert!(result.coverage_delta.unwrap() < -0.01);
    }

    #[test]
    fn test_regression_detected_untested_increased() {
        let ctx = RegressionContext {
            untested_critical_files: vec!["a.rs".into(), "b.rs".into(), "c.rs".into()],
            base_untested_critical_count: Some(1),
            ..Default::default()
        };

        let result = analyze(&ctx);
        assert!(result.coverage_regression_detected); // 3 > base 1
    }

    #[test]
    fn test_no_regression_few_code_files_good_ratio() {
        // 2 untested out of 10 code files → 80% coverage → no regression
        let ctx = RegressionContext {
            untested_critical_files: vec!["a.rs".into(), "b.rs".into()],
            file_stats: (0..10)
                .map(|i| (format!("src/mod_{}.rs", i), 'M', 10, 5))
                .collect(),
            ..Default::default()
        };

        let result = analyze(&ctx);
        assert!(!result.coverage_regression_detected); // 2/10 = 80% coverage, no ratio drop
    }

    // -- Serde --

    #[test]
    fn test_serde_skip_empty_non_code() {
        let result = TestRegression::default();
        let json = serde_json::to_string(&result).unwrap();
        assert!(!json.contains("untested_non_code_files"));
    }

    #[test]
    fn test_serde_includes_non_code_when_present() {
        let result = TestRegression {
            untested_non_code_files: vec!["README.md".into()],
            untested_non_code_count: 1,
            ..Default::default()
        };
        let json = serde_json::to_string(&result).unwrap();
        assert!(json.contains("untested_non_code_files"));
    }

    #[test]
    fn test_round_trip_serde() {
        let ctx = RegressionContext {
            coverage_ratio: Some(0.7),
            base_coverage_ratio: Some(0.8),
            untested_critical_files: vec![
                "src/main.rs".into(),
                "README.md".into(),
                "tests/foo.rs".into(),
            ],
            base_untested_critical_count: Some(0),
            ..Default::default()
        };

        let result = analyze(&ctx);
        let json = serde_json::to_string_pretty(&result).unwrap();
        let deserialized: TestRegression = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.untested_code_count, result.untested_code_count);
        assert_eq!(
            deserialized.untested_non_code_count,
            result.untested_non_code_count
        );
        assert_eq!(
            deserialized.untested_critical_count,
            result.untested_critical_count
        );
        assert_eq!(
            deserialized.coverage_regression_detected,
            result.coverage_regression_detected
        );
        assert_eq!(
            deserialized.coverage_improvement_detected,
            result.coverage_improvement_detected
        );
    }

    // -- Config-like classification tests --

    #[test]
    fn test_config_like_playwright_config() {
        assert!(is_config_like("playwright.config.ts"));
        assert!(is_config_like("src/playwright.config.ts"));
    }

    #[test]
    fn test_config_like_vite_config() {
        assert!(is_config_like("vite.config.ts"));
        assert!(is_config_like("vite.config.js"));
        assert!(is_config_like("vite.config.mjs"));
    }

    #[test]
    fn test_config_like_other_configs() {
        assert!(is_config_like("jest.config.js"));
        assert!(is_config_like("tailwind.config.js"));
        assert!(is_config_like("tailwind.config.cjs"));
        assert!(is_config_like("vitest.config.ts"));
        assert!(is_config_like("webpack.config.js"));
    }

    #[test]
    fn test_config_like_setup_files() {
        assert!(is_config_like("jest.setup.ts"));
        assert!(is_config_like("vitest.setup.js"));
    }

    #[test]
    fn test_config_like_barrel_index() {
        assert!(is_config_like("index.ts"));
        assert!(is_config_like("index.js"));
        assert!(is_config_like("index.tsx"));
        assert!(is_config_like("index.jsx"));
    }

    #[test]
    fn test_config_like_nested_barrel() {
        assert!(is_config_like("src/utils/index.ts"));
        assert!(is_config_like("packages/core/index.js"));
    }

    #[test]
    fn test_config_like_types() {
        assert!(is_config_like("types.ts"));
        assert!(is_config_like("src/types.ts"));
    }

    #[test]
    fn test_config_like_declaration_files() {
        assert!(is_config_like("types.d.ts"));
        assert!(is_config_like("src/env.d.ts"));
        assert!(is_config_like("global.d.ts"));
    }

    #[test]
    fn test_not_config_like_regular_source() {
        assert!(!is_config_like("src/main.ts"));
        assert!(!is_config_like("src/main.rs"));
        assert!(!is_config_like("src/app.tsx"));
        assert!(!is_config_like("src/utils.ts"));
        assert!(!is_config_like("src/handler.js"));
        assert!(!is_config_like("src/config.rs")); // .rs config is real code
    }

    #[test]
    fn test_config_like_devtools() {
        assert!(is_config_like("src/devtools/browserMocks.ts"));
        assert!(is_config_like("devtools/panel.tsx"));
    }

    #[test]
    fn test_config_like_storybook() {
        assert!(is_config_like(".storybook/main.ts"));
        assert!(is_config_like("storybook/preview.ts"));
        assert!(is_config_like("src/stories/Button.stories.tsx"));
    }

    #[test]
    fn test_config_like_mock_files() {
        assert!(is_config_like("src/devtools/browserMocks.ts"));
        assert!(is_config_like("src/utils/mockData.ts"));
        assert!(is_config_like("src/__mocks__/apiMock.ts")); // also caught by is_test_file
        // Non-mock files with "mock" substring should NOT match unless basename has it
        assert!(!is_config_like("src/hammock/utils.ts"));
    }

    #[test]
    fn test_config_like_routed_to_non_code() {
        let ctx = RegressionContext {
            untested_critical_files: vec![
                "playwright.config.ts".into(),
                "vite.config.ts".into(),
                "src/index.ts".into(),
                "src/types.ts".into(),
                "src/main.ts".into(), // real code
            ],
            file_stats: vec![
                ("playwright.config.ts".into(), 'M', 5, 2),
                ("vite.config.ts".into(), 'M', 3, 1),
                ("src/index.ts".into(), 'M', 2, 0),
                ("src/types.ts".into(), 'M', 10, 3),
                ("src/main.ts".into(), 'M', 20, 5),
                // Additional tested code files for good ratio
                ("src/app.ts".into(), 'M', 10, 2),
                ("src/utils.ts".into(), 'M', 5, 1),
            ],
            ..Default::default()
        };

        let result = analyze(&ctx);
        // Only main.ts is real untested code; configs/barrel/types → non-code
        assert_eq!(result.untested_code_count, 1);
        assert_eq!(result.untested_non_code_count, 4);
        assert!(result.untested_code_files.contains(&"src/main.ts".into()));
    }

    // -- Adaptive threshold tests --

    #[test]
    fn test_adaptive_threshold_good_ratio_not_detected() {
        // 6 untested out of 15 code files → 60% coverage → NOT detected
        let untested: Vec<String> = (0..6).map(|i| format!("src/mod_{}.rs", i)).collect();
        let file_stats: Vec<(String, char, usize, usize)> = (0..15)
            .map(|i| (format!("src/mod_{}.rs", i), 'M', 10, 5))
            .collect();

        let ctx = RegressionContext {
            untested_critical_files: untested,
            file_stats,
            ..Default::default()
        };

        let result = analyze(&ctx);
        assert!(!result.coverage_regression_detected);
        assert_eq!(result.untested_code_count, 6);
    }

    #[test]
    fn test_adaptive_threshold_low_ratio_detected() {
        // 6 untested out of 8 code files → 25% coverage → detected (< 40%)
        let untested: Vec<String> = (0..6).map(|i| format!("src/mod_{}.rs", i)).collect();
        let file_stats: Vec<(String, char, usize, usize)> = (0..8)
            .map(|i| (format!("src/mod_{}.rs", i), 'M', 10, 5))
            .collect();

        let ctx = RegressionContext {
            untested_critical_files: untested,
            file_stats,
            ..Default::default()
        };

        let result = analyze(&ctx);
        assert!(result.coverage_regression_detected);
        assert_eq!(result.untested_code_count, 6);
    }

    #[test]
    fn test_adaptive_threshold_hard_cap_with_good_ratio() {
        // 12 untested out of 30 code files → 60% coverage → hard_cap requires < 50%
        let untested: Vec<String> = (0..12).map(|i| format!("src/mod_{}.rs", i)).collect();
        let file_stats: Vec<(String, char, usize, usize)> = (0..30)
            .map(|i| (format!("src/mod_{}.rs", i), 'M', 10, 5))
            .collect();

        let ctx = RegressionContext {
            untested_critical_files: untested,
            file_stats,
            ..Default::default()
        };

        let result = analyze(&ctx);
        assert!(!result.coverage_regression_detected); // 60% > 50% threshold, no hard_cap
        assert!(!result.coverage_improvement_detected);
    }

    #[test]
    fn test_adaptive_threshold_hard_cap_with_low_ratio() {
        // 12 untested out of 15 code files → 20% coverage → hard_cap fires
        let untested: Vec<String> = (0..12).map(|i| format!("src/mod_{}.rs", i)).collect();
        let file_stats: Vec<(String, char, usize, usize)> = (0..15)
            .map(|i| (format!("src/mod_{}.rs", i), 'M', 10, 5))
            .collect();

        let ctx = RegressionContext {
            untested_critical_files: untested,
            file_stats,
            ..Default::default()
        };

        let result = analyze(&ctx);
        assert!(result.coverage_regression_detected); // 20% < 50%, hard_cap fires
        assert!(!result.coverage_improvement_detected);
    }

    #[test]
    fn test_adaptive_threshold_coverage_delta_drop() {
        // Good code ratio but coverage_delta dropped > 5%
        let ctx = RegressionContext {
            coverage_ratio: Some(0.5),
            base_coverage_ratio: Some(0.6),
            file_stats: vec![("src/main.rs".into(), 'M', 10, 5)],
            ..Default::default()
        };

        let result = analyze(&ctx);
        assert!(result.coverage_regression_detected); // delta = -0.1 < -0.05
        assert!(!result.coverage_improvement_detected);
    }

    #[test]
    fn test_adaptive_threshold_large_pr_high_ratio_not_detected() {
        // Real-world scenario: 87-file PR, 12 code files untested but 60% ratio
        let untested: Vec<String> = (0..12).map(|i| format!("src/feature_{}.ts", i)).collect();
        // 30 code files total → 12/30 untested = 60% coverage
        let mut file_stats: Vec<(String, char, usize, usize)> = (0..30)
            .map(|i| (format!("src/feature_{}.ts", i), 'M', 10, 5))
            .collect();
        // Plus 57 non-code files (configs, docs, etc.)
        for i in 0..57 {
            file_stats.push((format!("docs/page_{}.md", i), 'M', 5, 2));
        }

        let ctx = RegressionContext {
            untested_critical_files: untested,
            file_stats,
            ..Default::default()
        };

        let result = analyze(&ctx);
        // 12/30 = 60% coverage > 50% → hard_cap doesn't fire, no regression
        assert!(!result.coverage_regression_detected);
        assert!(!result.coverage_improvement_detected);
    }

    #[test]
    fn test_adaptive_threshold_boundary_at_ten() {
        // Exactly 10 untested (not > 10) with good ratio → NOT detected
        let untested: Vec<String> = (0..10).map(|i| format!("src/mod_{}.rs", i)).collect();
        let file_stats: Vec<(String, char, usize, usize)> = (0..25)
            .map(|i| (format!("src/mod_{}.rs", i), 'M', 10, 5))
            .collect();

        let ctx = RegressionContext {
            untested_critical_files: untested,
            file_stats,
            ..Default::default()
        };

        let result = analyze(&ctx);
        assert!(!result.coverage_regression_detected); // 10/25 = 60% coverage, 10 not > 10
        assert!(!result.coverage_improvement_detected);
    }

    #[test]
    fn test_coverage_improved_suppresses_soft_signals() {
        // Real scenario: PR adds many files, coverage improves from 12% to 21%
        // Despite many untested files, this is NOT a regression.
        let untested: Vec<String> = (0..30).map(|i| format!("src/new_{}.rs", i)).collect();
        let file_stats: Vec<(String, char, usize, usize)> = (0..40)
            .map(|i| (format!("src/new_{}.rs", i), 'M', 50, 20))
            .collect();

        let ctx = RegressionContext {
            untested_critical_files: untested,
            base_untested_critical_count: Some(12),
            coverage_ratio: Some(0.213),
            base_coverage_ratio: Some(0.116),
            file_stats,
            ..Default::default()
        };

        let result = analyze(&ctx);
        // Coverage improved by ~10pp — soft signals (low_coverage, hard_cap,
        // untested_increased) are suppressed because this is not a regression.
        assert!(!result.coverage_regression_detected);
        assert!(result.coverage_improvement_detected);
    }

    #[test]
    fn test_coverage_improvement_detected_with_higher_ratio_and_more_untested_files() {
        let untested: Vec<String> = (0..12).map(|i| format!("src/new_{}.rs", i)).collect();
        let file_stats: Vec<(String, char, usize, usize)> = (0..47)
            .map(|i| (format!("src/new_{}.rs", i), 'M', 25, 5))
            .collect();

        let ctx = RegressionContext {
            untested_critical_files: untested,
            base_untested_critical_count: Some(5),
            coverage_ratio: Some(0.213),
            base_coverage_ratio: Some(0.116),
            file_stats,
            ..Default::default()
        };

        let result = analyze(&ctx);
        assert!(
            result
                .coverage_delta
                .is_some_and(|delta| (delta - 0.097).abs() < 1e-9)
        );
        assert!(!result.coverage_regression_detected);
        assert!(result.coverage_improvement_detected);
    }
}