kimun 0.31.2

Code metrics tool — health score, complexity, duplication, hotspots, ownership
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
use super::*;
use crate::cli::OutputMode;
use crate::util::find_test_block_start;
use crate::walk::{ExcludeFilter, WalkConfig};
use git2::Repository;
use std::fs;
use std::path::Path;

#[test]
fn run_on_empty_dir() {
    let dir = tempfile::tempdir().unwrap();
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), false, &filter);
    run(&cfg, OutputMode::Table, 10, 6, "cogcom").unwrap();
}

#[test]
fn run_on_rust_file() {
    let dir = tempfile::tempdir().unwrap();
    fs::write(
        dir.path().join("main.rs"),
        "// a module\nfn main() {\n    let x = 1;\n    let y = x + 2;\n    println!(\"{}\", y);\n}\n",
    )
    .unwrap();
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), false, &filter);
    let model = ScoringModel::Cognitive;
    let score = compute_score(&cfg, 10, 6, &model).unwrap();
    assert!(
        score.score > 50.0,
        "simple code should score well, got {}",
        score.score
    );
    assert_eq!(score.files_analyzed, 1);
    assert!(score.total_loc > 0);
    assert_eq!(score.dimensions.len(), 5);
}

#[test]
fn run_on_rust_file_legacy_model() {
    let dir = tempfile::tempdir().unwrap();
    fs::write(
        dir.path().join("main.rs"),
        "// a module\nfn main() {\n    let x = 1;\n    let y = x + 2;\n    println!(\"{}\", y);\n}\n",
    )
    .unwrap();
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), false, &filter);
    let model = ScoringModel::Legacy;
    let score = compute_score(&cfg, 10, 6, &model).unwrap();
    assert!(
        score.score > 50.0,
        "simple code should score well with legacy model, got {}",
        score.score
    );
    assert_eq!(score.files_analyzed, 1);
    assert!(score.total_loc > 0);
    assert_eq!(score.dimensions.len(), 6);
    assert_eq!(score.dimensions[0].name, "Maintainability Index");
    assert_eq!(score.dimensions[1].name, "Cyclomatic Complexity");
}

#[test]
fn run_json_output() {
    let dir = tempfile::tempdir().unwrap();
    fs::write(
        dir.path().join("main.rs"),
        "fn main() {\n    let x = 1;\n}\n",
    )
    .unwrap();
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), false, &filter);
    run(&cfg, OutputMode::Json, 10, 6, "cogcom").unwrap();
}

#[test]
fn run_includes_tests_with_flag() {
    let dir = tempfile::tempdir().unwrap();
    fs::create_dir(dir.path().join("tests")).unwrap();
    fs::write(
        dir.path().join("tests/integration.rs"),
        "fn test() {\n    assert!(true);\n}\n",
    )
    .unwrap();
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), true, &filter);
    let model = ScoringModel::Cognitive;
    let score = compute_score(&cfg, 10, 6, &model).unwrap();
    assert_eq!(score.files_analyzed, 1);
}

#[test]
fn run_excludes_tests_by_default() {
    let dir = tempfile::tempdir().unwrap();
    fs::create_dir(dir.path().join("tests")).unwrap();
    fs::write(
        dir.path().join("tests/integration.rs"),
        "fn test() {\n    assert!(true);\n}\n",
    )
    .unwrap();
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), false, &filter);
    let model = ScoringModel::Cognitive;
    let score = compute_score(&cfg, 10, 6, &model).unwrap();
    assert_eq!(score.files_analyzed, 0);
}

#[test]
fn run_on_current_repo() {
    // Smoke test on the actual repo
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(Path::new("."), false, &filter);
    run(&cfg, OutputMode::Table, 5, 6, "cogcom").unwrap();
}

#[test]
fn run_on_current_repo_legacy() {
    // Smoke test on the actual repo with legacy model
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(Path::new("."), false, &filter);
    run(&cfg, OutputMode::Table, 5, 6, "legacy").unwrap();
}

#[test]
fn find_test_block_start_finds_cfg_test() {
    let lines = vec![
        "fn foo() {}".to_string(),
        "#[cfg(test)]".to_string(),
        "mod tests {}".to_string(),
    ];
    assert_eq!(find_test_block_start(&lines), 1);
}

#[test]
fn find_test_block_start_with_leading_spaces() {
    let lines = vec![
        "fn foo() {}".to_string(),
        "  #[cfg(test)]  ".to_string(),
        "mod tests {}".to_string(),
    ];
    assert_eq!(find_test_block_start(&lines), 1);
}

#[test]
fn find_test_block_start_no_match() {
    let lines = vec!["fn foo() {}".to_string(), "fn bar() {}".to_string()];
    assert_eq!(find_test_block_start(&lines), 2);
}

#[test]
fn find_test_block_start_empty() {
    let lines: Vec<String> = vec![];
    assert_eq!(find_test_block_start(&lines), 0);
}

#[test]
fn excludes_markdown_files() {
    let dir = tempfile::tempdir().unwrap();
    fs::write(dir.path().join("README.md"), "# Hello\n\nWorld\n").unwrap();
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), false, &filter);
    let model = ScoringModel::Cognitive;
    let score = compute_score(&cfg, 10, 6, &model).unwrap();
    assert_eq!(score.files_analyzed, 0, "Markdown should be excluded");
}

#[test]
fn excludes_toml_files() {
    let dir = tempfile::tempdir().unwrap();
    fs::write(
        dir.path().join("Cargo.toml"),
        "[package]\nname = \"test\"\n",
    )
    .unwrap();
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), false, &filter);
    let model = ScoringModel::Cognitive;
    let score = compute_score(&cfg, 10, 6, &model).unwrap();
    assert_eq!(score.files_analyzed, 0, "TOML should be excluded");
}

#[test]
fn excludes_json_files() {
    let dir = tempfile::tempdir().unwrap();
    fs::write(dir.path().join("data.json"), "{\"key\": \"value\"}\n").unwrap();
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), false, &filter);
    let model = ScoringModel::Cognitive;
    let score = compute_score(&cfg, 10, 6, &model).unwrap();
    assert_eq!(score.files_analyzed, 0, "JSON should be excluded");
}

#[test]
fn run_on_single_file() {
    let dir = tempfile::tempdir().unwrap();
    let file = dir.path().join("lib.rs");
    fs::write(
        &file,
        "/// Docs\nfn add(a: i32, b: i32) -> i32 {\n    a + b\n}\n",
    )
    .unwrap();
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(&file, false, &filter);
    let model = ScoringModel::Cognitive;
    let score = compute_score(&cfg, 10, 6, &model).unwrap();
    assert_eq!(score.files_analyzed, 1);
    assert!(score.total_loc > 0);
}

#[test]
fn dimensions_sum_to_100_percent() {
    let dir = tempfile::tempdir().unwrap();
    fs::write(
        dir.path().join("main.rs"),
        "fn main() {\n    let x = 1;\n}\n",
    )
    .unwrap();
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), false, &filter);
    let model = ScoringModel::Cognitive;
    let score = compute_score(&cfg, 10, 6, &model).unwrap();
    let total_weight: f64 = score.dimensions.iter().map(|d| d.weight).sum();
    assert!(
        (total_weight - 1.0).abs() < 0.001,
        "weights should sum to 1.0, got {total_weight}"
    );
}

#[test]
fn legacy_dimensions_sum_to_100_percent() {
    let dir = tempfile::tempdir().unwrap();
    fs::write(
        dir.path().join("main.rs"),
        "fn main() {\n    let x = 1;\n}\n",
    )
    .unwrap();
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), false, &filter);
    let model = ScoringModel::Legacy;
    let score = compute_score(&cfg, 10, 6, &model).unwrap();
    let total_weight: f64 = score.dimensions.iter().map(|d| d.weight).sum();
    assert!(
        (total_weight - 1.0).abs() < 0.001,
        "legacy weights should sum to 1.0, got {total_weight}"
    );
}

// ── ScoreGate error message format ──────────────────────────────────────

#[test]
fn score_gate_error_message_shows_scores_delta_and_tolerance() {
    let before = 86.93_f64;
    let after = 86.89_f64;
    let msg = format_gate_error(before, after, after - before, 0.01);
    assert!(
        msg.contains("86.93 → 86.89"),
        "scores should show two decimal places, got: {msg}"
    );
    assert!(
        msg.contains("(-0.0400)"),
        "delta should show four decimal places, got: {msg}"
    );
    assert!(
        msg.contains("0.01 tolerance"),
        "message should name the tolerance, got: {msg}"
    );
}

#[test]
fn score_gate_error_message_separates_drop_just_over_tolerance() {
    let msg = format_gate_error(77.7800, 77.7696, 77.7696 - 77.7800, 0.01);
    assert!(
        msg.contains("(-0.0104)"),
        "a drop just over the tolerance must not print as -0.01, got: {msg}"
    );
}

#[test]
fn gate_score_worsened_ignores_drop_within_tolerance() {
    assert!(!gate_score_worsened(87.814, 87.811, 0.01));
    assert!(!gate_score_worsened(90.0, 90.0, 0.01));
}

#[test]
fn gate_score_worsened_ignores_sub_tolerance_drop_across_rounding_boundary() {
    // -0.0078, but rounds to 77.78 → 77.77.
    assert!(!gate_score_worsened(77.77897, 77.77122, 0.01));
}

#[test]
fn gate_score_worsened_catches_drop_over_tolerance() {
    assert!(gate_score_worsened(87.81, 87.79, 0.01));
    assert!(gate_score_worsened(90.0, 89.5, 0.01));
}

#[test]
fn gate_score_worsened_honors_custom_tolerance() {
    assert!(!gate_score_worsened(90.0, 89.96, 0.05));
    assert!(gate_score_worsened(90.0, 89.9, 0.05));
}

#[test]
fn gate_score_worsened_zero_tolerance_catches_any_drop() {
    assert!(gate_score_worsened(90.0, 89.999, 0.0));
    assert!(!gate_score_worsened(90.0, 90.0, 0.0));
}

#[test]
fn gate_score_worsened_allows_improvement() {
    assert!(!gate_score_worsened(87.80, 87.81, 0.01));
    assert!(!gate_score_worsened(89.5, 90.0, 0.0));
}

#[test]
fn score_gate_fail_if_worse_error_contains_decimal_delta() {
    // Integration: run_diff with fail_if_worse on a repo whose second commit
    // introduces a function with 10 levels of nesting (cognitive complexity ≈ 55,
    // normalizes to ~2/100) against a trivial baseline (~90/100). The ~40-point
    // drop guarantees the gate fires regardless of normalization variance.
    let dir = tempfile::tempdir().unwrap();
    let repo = git2::Repository::init(dir.path()).unwrap();
    let mut config = repo.config().unwrap();
    config.set_str("user.name", "Test").unwrap();
    config.set_str("user.email", "test@test.com").unwrap();
    let sig =
        git2::Signature::new("Test", "test@test.com", &git2::Time::new(1_700_000_000, 0)).unwrap();

    // First commit: minimal clean file → high baseline score (~90).
    let simple = "fn clean() -> i32 { 42 }\n";
    std::fs::write(dir.path().join("lib.rs"), simple).unwrap();
    let oid1 = {
        let mut index = repo.index().unwrap();
        index.add_path(std::path::Path::new("lib.rs")).unwrap();
        index.write().unwrap();
        let tree = repo.find_tree(index.write_tree().unwrap()).unwrap();
        repo.commit(Some("HEAD"), &sig, &sig, "init", &tree, &[])
            .unwrap()
    };

    // Second commit: 10 levels of nesting hits cognitive complexity (~55),
    // indentation stddev, and Halstead simultaneously — guaranteed large drop.
    let complex = r#"fn clean() -> i32 { 42 }
fn deeply_nested(x: i32) -> i32 {
    if x > 0 {
        if x > 1 {
            if x > 2 {
                if x > 3 {
                    if x > 4 {
                        if x > 5 {
                            if x > 6 {
                                if x > 7 {
                                    if x > 8 {
                                        if x > 9 {
                                            return x;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    0
}
"#;
    std::fs::write(dir.path().join("lib.rs"), complex).unwrap();
    {
        let mut index = repo.index().unwrap();
        index.add_path(std::path::Path::new("lib.rs")).unwrap();
        index.write().unwrap();
        let tree = repo.find_tree(index.write_tree().unwrap()).unwrap();
        let parent = repo.find_commit(oid1).unwrap();
        repo.commit(
            Some("HEAD"),
            &sig,
            &sig,
            "add deeply nested fn",
            &tree,
            &[&parent],
        )
        .unwrap();
    }

    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), true, &filter);
    let gate = ScoreGate {
        max_drop: Some(0.01),
        fail_below: None,
        ..ScoreGate::default()
    };
    let result = run_diff(&cfg, "HEAD~1", OutputMode::Table, 10, 6, "cogcom", gate);

    let msg = result
        .expect_err(
            "gate should fire: 10-level nesting must produce a score drop vs trivial baseline",
        )
        .to_string();

    let paren_start = msg.rfind('(').expect("message should contain '('");
    let paren_end = msg.rfind(')').expect("message should contain ')'");
    let delta_str = &msg[paren_start + 1..paren_end];
    let dot_idx = delta_str.find('.').unwrap_or_else(|| {
        panic!("delta '{delta_str}' should contain a decimal point, full msg: {msg}")
    });
    let decimals = &delta_str[dot_idx + 1..];
    assert_eq!(
        decimals.len(),
        4,
        "delta should have exactly 4 decimal places, got '{delta_str}' in: {msg}"
    );
}

// ── ScoringModel::from_arg ──────────────────────────────────────────────

#[test]
fn scoring_model_from_arg_cogcom() {
    assert_eq!(ScoringModel::from_arg("cogcom"), ScoringModel::Cognitive);
}

#[test]
fn scoring_model_from_arg_legacy() {
    assert_eq!(ScoringModel::from_arg("legacy"), ScoringModel::Legacy);
}

// ── run helpers ─────────────────────────────────────────────────────────

fn create_test_repo_with_rust_file() -> (tempfile::TempDir, Repository) {
    let dir = tempfile::tempdir().unwrap();
    let repo = Repository::init(dir.path()).unwrap();
    let mut config = repo.config().unwrap();
    config.set_str("user.name", "Test").unwrap();
    config.set_str("user.email", "test@test.com").unwrap();

    // Write a Rust file and commit it.
    let rs_path = dir.path().join("main.rs");
    fs::write(&rs_path, "fn main() {\n    let x = 1;\n}\n").unwrap();

    let sig =
        git2::Signature::new("Test", "test@test.com", &git2::Time::new(1_700_000_000, 0)).unwrap();
    {
        let mut index = repo.index().unwrap();
        index.add_path(Path::new("main.rs")).unwrap();
        index.write().unwrap();
        let tree_oid = index.write_tree().unwrap();
        let tree = repo.find_tree(tree_oid).unwrap();
        repo.commit(Some("HEAD"), &sig, &sig, "initial", &tree, &[])
            .unwrap();
    }

    (dir, repo)
}

fn make_git_repo_with_file(content: &str) -> tempfile::TempDir {
    make_git_repo_with_files(&[("main.rs", content)])
}

fn make_git_repo_with_files(files: &[(&str, &str)]) -> tempfile::TempDir {
    let dir = tempfile::tempdir().unwrap();
    let repo = git2::Repository::init(dir.path()).unwrap();
    let mut config = repo.config().unwrap();
    config.set_str("user.name", "Test").unwrap();
    config.set_str("user.email", "test@test.com").unwrap();
    let sig =
        git2::Signature::new("Test", "test@test.com", &git2::Time::new(1_700_000_000, 0)).unwrap();
    let mut index = repo.index().unwrap();
    for (name, content) in files {
        fs::write(dir.path().join(name), content).unwrap();
        index.add_path(Path::new(name)).unwrap();
    }
    index.write().unwrap();
    let tree_oid = index.write_tree().unwrap();
    let tree = repo.find_tree(tree_oid).unwrap();
    repo.commit(Some("HEAD"), &sig, &sig, "init", &tree, &[])
        .unwrap();
    drop(tree);
    dir
}

#[test]
fn run_diff_on_git_repo() {
    let (dir, _repo) = create_test_repo_with_rust_file();
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), true, &filter);
    let result = run_diff(
        &cfg,
        "HEAD",
        OutputMode::Table,
        5,
        6,
        "cogcom",
        ScoreGate::default(),
    );
    assert!(result.is_ok(), "run_diff should succeed: {:?}", result);
}

#[test]
fn run_diff_json_on_git_repo() {
    let (dir, _repo) = create_test_repo_with_rust_file();
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), true, &filter);
    let result = run_diff(
        &cfg,
        "HEAD",
        OutputMode::Json,
        5,
        6,
        "cogcom",
        ScoreGate::default(),
    );
    assert!(result.is_ok(), "run_diff JSON should succeed: {:?}", result);
}

#[test]
fn run_diff_legacy_model_on_git_repo() {
    let (dir, _repo) = create_test_repo_with_rust_file();
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), true, &filter);
    let result = run_diff(
        &cfg,
        "HEAD",
        OutputMode::Table,
        5,
        6,
        "legacy",
        ScoreGate::default(),
    );
    assert!(
        result.is_ok(),
        "run_diff legacy should succeed: {:?}",
        result
    );
}

#[test]
fn score_gate_no_flags_succeeds() {
    let dir = make_git_repo_with_file("fn main() {}\n");
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), false, &filter);
    let gate = ScoreGate::default();
    let result = run_diff(&cfg, "HEAD", OutputMode::Table, 10, 6, "cogcom", gate);
    assert!(result.is_ok(), "no gates should always succeed: {result:?}");
}

#[test]
fn score_gate_fail_below_passes_when_above_threshold() {
    let dir = make_git_repo_with_file("fn main() {}\n");
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), false, &filter);
    // A small clean file should score high — F-- threshold should never trigger
    let gate = ScoreGate {
        max_drop: None,
        fail_below: Some(analyzer::Grade::FMinusMinus),
        ..ScoreGate::default()
    };
    let result = run_diff(&cfg, "HEAD", OutputMode::Table, 10, 6, "cogcom", gate);
    assert!(
        result.is_ok(),
        "F-- threshold should not trigger on clean code"
    );
}

#[test]
fn score_gate_fail_below_unit() {
    // Unit test: verify gate logic directly using ScoreDiff without a full git run.
    let before = analyzer::score_to_grade(80.0); // B
    let after = analyzer::score_to_grade(70.0); // C
    let diff = diff::ScoreDiff {
        git_ref: "HEAD".to_string(),
        overall: diff::ScoreDelta {
            before: 80.0,
            after: 70.0,
            delta: -10.0,
        },
        before_grade: before,
        after_grade: after,
        files_before: 1,
        files_after: 1,
        loc_before: 10,
        loc_after: 10,
        dimensions: vec![],
        changed: None,
    };
    // Gate: fail if below B — current score is C, so should fail
    let threshold = analyzer::Grade::B;
    assert!(
        diff.after_grade.numeric_rank() < threshold.numeric_rank(),
        "C should be below B"
    );
    // Gate: fail if below F-- — current score is C, should pass
    let low_threshold = analyzer::Grade::FMinusMinus;
    assert!(
        diff.after_grade.numeric_rank() >= low_threshold.numeric_rank(),
        "C should not be below F--"
    );
    // Gate: fail-if-worse compares numeric score, not grade
    assert!(diff.overall.delta < 0.0, "80→70 is a negative delta");
}

#[test]
fn score_gate_fail_if_worse_same_ref_passes() {
    let dir = make_git_repo_with_file("fn main() {}\n");
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), false, &filter);
    // Comparing HEAD to itself — score cannot be worse
    let gate = ScoreGate {
        max_drop: Some(0.01),
        fail_below: None,
        ..ScoreGate::default()
    };
    let result = run_diff(&cfg, "HEAD", OutputMode::Table, 10, 6, "cogcom", gate);
    assert!(
        result.is_ok(),
        "same ref comparison should not trigger fail-if-worse"
    );
}

#[test]
fn run_on_current_repo_with_target() {
    // Test with an explicit non-"." path to exercise the target display branch
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(Path::new("src"), false, &filter);
    run(&cfg, OutputMode::Table, 5, 6, "cogcom").unwrap();
}

#[test]
fn run_json_with_target_path() {
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(Path::new("src"), false, &filter);
    run(&cfg, OutputMode::Json, 5, 6, "cogcom").unwrap();
}

#[test]
fn run_short_format() {
    let dir = tempfile::tempdir().unwrap();
    fs::write(dir.path().join("main.rs"), "fn main() { let x = 1; }\n").unwrap();
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), false, &filter);
    run(&cfg, OutputMode::Short, 10, 6, "cogcom").unwrap();
}

#[test]
fn run_terse_format() {
    let dir = tempfile::tempdir().unwrap();
    fs::write(dir.path().join("main.rs"), "fn main() { let x = 1; }\n").unwrap();
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), false, &filter);
    run(&cfg, OutputMode::Terse, 10, 6, "cogcom").unwrap();
}

#[test]
fn run_diff_short_format() {
    let dir = make_git_repo_with_file("fn main() { let x = 1; }\n");
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), false, &filter);
    let gate = ScoreGate {
        max_drop: None,
        fail_below: None,
        ..ScoreGate::default()
    };
    run_diff(&cfg, "HEAD", OutputMode::Short, 10, 6, "cogcom", gate).unwrap();
}

#[test]
fn run_diff_terse_format() {
    let dir = make_git_repo_with_file("fn main() { let x = 1; }\n");
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), false, &filter);
    let gate = ScoreGate {
        max_drop: None,
        fail_below: None,
        ..ScoreGate::default()
    };
    run_diff(&cfg, "HEAD", OutputMode::Terse, 10, 6, "cogcom", gate).unwrap();
}

// ── --gate-scope changed ────────────────────────────────────────────────

const SIMPLE_FNS: &str = "fn add(a: i32, b: i32) -> i32 {\n    a + b\n}\n\n\
fn sub(a: i32, b: i32) -> i32 {\n    a - b\n}\n\n\
fn mul(a: i32, b: i32) -> i32 {\n    a * b\n}\n";

const NESTED_FN: &str = "fn tangle(a: i32, b: i32, c: i32) -> i32 {
    let mut r = 0;
    if a > 0 {
        for i in 0..a {
            if i % 2 == 0 {
                while r < b {
                    if c > 3 && b > 2 || a < 1 {
                        match i {
                            0 => { if b > 1 { r += 1; } else { r += 2; } }
                            _ => { r += 3; }
                        }
                    }
                    r += 1;
                }
            }
        }
    }
    r
}
";

fn changed_scope_gate() -> ScoreGate {
    ScoreGate {
        max_drop: Some(GateScope::Changed.default_tolerance()),
        scope: GateScope::Changed,
        ..ScoreGate::default()
    }
}

fn run_gate(dir: &Path, gate: ScoreGate) -> Result<(), Box<dyn std::error::Error>> {
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir, false, &filter);
    run_diff(&cfg, "HEAD", OutputMode::Table, 10, 6, "cogcom", gate)
}

#[test]
fn changed_scope_passes_when_healthy_code_is_deleted() {
    let dir = make_git_repo_with_files(&[("good.rs", SIMPLE_FNS), ("bad.rs", NESTED_FN)]);
    fs::write(
        dir.path().join("good.rs"),
        "fn add(a: i32, b: i32) -> i32 {\n    a + b\n}\n",
    )
    .unwrap();

    let project = ScoreGate {
        max_drop: Some(0.01),
        ..ScoreGate::default()
    };
    assert!(
        run_gate(dir.path(), project).is_err(),
        "the project aggregate drops when healthy code leaves the denominator"
    );
    run_gate(dir.path(), changed_scope_gate()).expect("no changed file got worse");
}

#[test]
fn changed_scope_ignores_deleted_files() {
    let dir = make_git_repo_with_files(&[("good.rs", SIMPLE_FNS), ("bad.rs", NESTED_FN)]);
    fs::remove_file(dir.path().join("good.rs")).unwrap();
    run_gate(dir.path(), changed_scope_gate()).expect("deleting a file never fails");
}

#[test]
fn changed_scope_fails_naming_the_file_that_got_worse() {
    let dir = make_git_repo_with_files(&[("good.rs", SIMPLE_FNS), ("other.rs", SIMPLE_FNS)]);
    fs::write(
        dir.path().join("good.rs"),
        format!("{SIMPLE_FNS}\n{NESTED_FN}"),
    )
    .unwrap();

    let msg = run_gate(dir.path(), changed_scope_gate())
        .expect_err("good.rs gained a deeply nested function")
        .to_string();
    assert!(msg.contains("good.rs dropped"), "{msg}");
    assert!(!msg.contains("other.rs"), "{msg}");
}

#[test]
fn changed_scope_does_not_fail_on_new_files() {
    let dir = make_git_repo_with_files(&[("good.rs", SIMPLE_FNS)]);
    fs::write(dir.path().join("bad.rs"), NESTED_FN).unwrap();
    run_gate(dir.path(), changed_scope_gate()).expect("new files have no score to fall from");
}

#[test]
fn changed_scope_fails_when_duplicated_lines_grow() {
    let dir = make_git_repo_with_files(&[("a.rs", NESTED_FN)]);
    fs::write(
        dir.path().join("b.rs"),
        NESTED_FN.replace("tangle", "tangle_copy"),
    )
    .unwrap();

    let msg = run_gate(dir.path(), changed_scope_gate())
        .expect_err("b.rs duplicates a.rs")
        .to_string();
    assert!(msg.contains("duplicated lines grew"), "{msg}");
}

#[test]
fn changed_scope_json_includes_changed_files() {
    let dir = make_git_repo_with_files(&[("good.rs", SIMPLE_FNS)]);
    fs::write(dir.path().join("new.rs"), "fn new() {}\n").unwrap();
    let filter = ExcludeFilter::default();
    let cfg = WalkConfig::new(dir.path(), false, &filter);
    run_diff(
        &cfg,
        "HEAD",
        OutputMode::Json,
        10,
        6,
        "cogcom",
        changed_scope_gate(),
    )
    .unwrap();
}

#[test]
fn changed_scope_maps_paths_when_analyzing_a_subdirectory() {
    let dir = make_git_repo_with_files(&[("root.rs", SIMPLE_FNS)]);
    let lib = dir.path().join("lib");
    fs::create_dir(&lib).unwrap();
    fs::write(lib.join("good.rs"), SIMPLE_FNS).unwrap();
    let repo = Repository::open(dir.path()).unwrap();
    let mut index = repo.index().unwrap();
    index.add_path(Path::new("lib/good.rs")).unwrap();
    index.write().unwrap();
    let tree = repo.find_tree(index.write_tree().unwrap()).unwrap();
    let head = repo.head().unwrap().peel_to_commit().unwrap();
    let sig = git2::Signature::now("Test", "test@test.com").unwrap();
    repo.commit(Some("HEAD"), &sig, &sig, "lib", &tree, &[&head])
        .unwrap();

    fs::write(lib.join("good.rs"), format!("{SIMPLE_FNS}\n{NESTED_FN}")).unwrap();
    fs::write(dir.path().join("root.rs"), NESTED_FN).unwrap();

    let msg = run_gate(&lib, changed_scope_gate())
        .expect_err("lib/good.rs got worse")
        .to_string();
    assert!(msg.contains("lib/good.rs dropped"), "{msg}");
    assert!(!msg.contains("root.rs"), "{msg}");
}