commitbee 0.5.0

AI-powered commit message generator using tree-sitter semantic analysis and local LLMs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
// SPDX-FileCopyrightText: 2026 Sephyi <me@sephy.io>
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial

mod helpers;

use std::path::PathBuf;

use commitbee::config::Config;
use commitbee::domain::{ChangeStatus, CodeSymbol, CommitType, FileCategory, SymbolKind};
use commitbee::services::context::ContextBuilder;
use helpers::{make_file_change, make_renamed_file, make_staged_changes};

// ─── Helpers ─────────────────────────────────────────────────────────────────

fn default_config() -> Config {
    Config::default()
}

fn make_symbol(
    name: &str,
    kind: SymbolKind,
    file: &str,
    is_public: bool,
    is_added: bool,
) -> CodeSymbol {
    CodeSymbol {
        kind,
        name: name.to_string(),
        file: PathBuf::from(file),
        line: 1,
        end_line: 10,
        is_public,
        is_added,
        is_whitespace_only: None,
        signature: None,
    }
}

// ─── CommitType inference ─────────────────────────────────────────────────────

#[test]
fn infer_type_all_docs() {
    let changes = make_staged_changes(vec![
        make_file_change("README.md", ChangeStatus::Modified, "", 5, 2),
        make_file_change("CHANGELOG.md", ChangeStatus::Modified, "", 3, 1),
    ]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert_eq!(
        ctx.suggested_type,
        CommitType::Docs,
        "all .md files should infer Docs"
    );
}

#[test]
fn infer_type_all_tests() {
    let changes = make_staged_changes(vec![
        make_file_change("tests/unit.rs", ChangeStatus::Modified, "", 10, 0),
        make_file_change("tests/integration.rs", ChangeStatus::Added, "", 20, 0),
    ]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert_eq!(
        ctx.suggested_type,
        CommitType::Test,
        "all test files should infer Test"
    );
}

#[test]
fn infer_type_all_config() {
    let changes = make_staged_changes(vec![make_file_change(
        "Cargo.toml",
        ChangeStatus::Modified,
        "",
        3,
        1,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert_eq!(
        ctx.suggested_type,
        CommitType::Chore,
        "Cargo.toml only should infer Chore"
    );
}

#[test]
fn infer_type_all_build() {
    let changes = make_staged_changes(vec![make_file_change(
        ".github/workflows/ci.yml",
        ChangeStatus::Modified,
        "",
        5,
        0,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert_eq!(
        ctx.suggested_type,
        CommitType::Build,
        ".github/workflows/ci.yml should infer Build"
    );
}

#[test]
fn infer_type_new_public_symbols_is_feat() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/services/new_service.rs",
        ChangeStatus::Modified,
        "+pub fn new_service() {}",
        5,
        1,
    )]);
    let symbols = vec![make_symbol(
        "new_service",
        SymbolKind::Function,
        "src/services/new_service.rs",
        true,
        true,
    )];
    let ctx = ContextBuilder::build(&changes, &symbols, &default_config());
    assert_eq!(
        ctx.suggested_type,
        CommitType::Feat,
        "a newly added public function should infer Feat"
    );
}

#[test]
fn infer_type_majority_new_files_is_feat() {
    // 2 Added, 1 Modified → majority new (>50%)
    let changes = make_staged_changes(vec![
        make_file_change("src/services/foo.rs", ChangeStatus::Added, "", 50, 0),
        make_file_change("src/services/bar.rs", ChangeStatus::Added, "", 30, 0),
        make_file_change("src/lib.rs", ChangeStatus::Modified, "", 5, 2),
    ]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert_eq!(
        ctx.suggested_type,
        CommitType::Feat,
        "majority new files (>50%) should infer Feat"
    );
}

#[test]
fn infer_type_small_balanced_change_is_style() {
    // <20 insertions and <20 deletions, balanced, no symbols -> style
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "-let x = 1;\n+let x = 2;",
        1,
        1,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert_eq!(
        ctx.suggested_type,
        CommitType::Style,
        "small balanced change with no symbols should infer Style"
    );
}

#[test]
fn infer_type_small_unbalanced_change_is_refactor() {
    // <20 insertions and <20 deletions, unbalanced (more dels), no symbols -> refactor
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "-removed line\n-another\n+replacement;",
        1,
        10,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert_eq!(
        ctx.suggested_type,
        CommitType::Refactor,
        "small unbalanced change with no symbols should infer Refactor"
    );
}

// ─── Scope inference ─────────────────────────────────────────────────────────

#[test]
fn infer_scope_single_module() {
    let changes = make_staged_changes(vec![
        make_file_change("src/services/context.rs", ChangeStatus::Modified, "", 5, 2),
        make_file_change("src/services/git.rs", ChangeStatus::Modified, "", 3, 1),
    ]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert_eq!(
        ctx.suggested_scope,
        Some("services".to_string()),
        "files in src/services/ should yield scope 'services'"
    );
}

#[test]
fn infer_scope_none_for_mixed_modules() {
    let changes = make_staged_changes(vec![
        make_file_change("src/services/context.rs", ChangeStatus::Modified, "", 5, 2),
        make_file_change("src/domain/change.rs", ChangeStatus::Modified, "", 3, 1),
    ]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert!(
        ctx.suggested_scope.is_none(),
        "files from different modules should yield no scope, got {:?}",
        ctx.suggested_scope
    );
}

// ─── Prompt output ───────────────────────────────────────────────────────────

#[test]
fn prompt_includes_symbols_when_present() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/services/context.rs",
        ChangeStatus::Modified,
        "+pub fn new_func() {}\n-fn old_func() {}",
        5,
        3,
    )]);
    let symbols = vec![
        make_symbol(
            "new_func",
            SymbolKind::Function,
            "src/services/context.rs",
            true,
            true,
        ),
        make_symbol(
            "old_func",
            SymbolKind::Function,
            "src/services/context.rs",
            false,
            false,
        ),
    ];
    let ctx = ContextBuilder::build(&changes, &symbols, &default_config());
    let prompt = ctx.to_prompt();

    assert!(
        prompt.contains("SYMBOLS CHANGED:"),
        "prompt should contain 'SYMBOLS CHANGED:' when symbols are present"
    );
    assert!(
        prompt.contains("new_func"),
        "prompt should contain added symbol name 'new_func'"
    );
    assert!(
        prompt.contains("old_func"),
        "prompt should contain removed symbol name 'old_func'"
    );
}

#[test]
fn prompt_omits_symbols_when_empty() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "-let x = 1;\n+let x = 2;",
        1,
        1,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    let prompt = ctx.to_prompt();

    assert!(
        !prompt.contains("SYMBOLS CHANGED:"),
        "prompt should not contain 'SYMBOLS CHANGED:' when no symbols are present"
    );
}

// ─── Budget management ───────────────────────────────────────────────────────

#[test]
fn prompt_respects_budget() {
    // Generate a huge diff (10 000 lines)
    let huge_diff = "+ added line of code here\n".repeat(10_000);

    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        &huge_diff,
        10_000,
        0,
    )]);

    let mut config = default_config();
    config.max_context_chars = 5_000;

    let ctx = ContextBuilder::build(&changes, &[], &config);
    let prompt = ctx.to_prompt();

    assert!(
        prompt.len() < 10_000,
        "prompt length {} should be less than 10 000 chars when budget is 5 000",
        prompt.len()
    );
}

// ─── FileCategory classification ─────────────────────────────────────────────

#[test]
fn file_category_source() {
    let cases = [
        ("src/main.rs", FileCategory::Source),
        ("lib/utils.ts", FileCategory::Source),
        ("app/module.py", FileCategory::Source),
        ("cmd/server.go", FileCategory::Source),
    ];

    for (path, expected) in cases {
        let got = FileCategory::from_path(&PathBuf::from(path));
        assert_eq!(got, expected, "{} should be classified as Source", path);
    }
}

#[test]
fn file_category_test() {
    let cases = [
        "tests/unit.rs",
        "src/foo_test.rs",
        "app/app.test.ts",
        "lib/lib_spec.js",
    ];

    for path in cases {
        let got = FileCategory::from_path(&PathBuf::from(path));
        assert_eq!(
            got,
            FileCategory::Test,
            "{} should be classified as Test, got {:?}",
            path,
            got
        );
    }
}

#[test]
fn file_category_docs() {
    let cases = [
        ("README.md", FileCategory::Docs),
        ("docs/guide.rst", FileCategory::Docs),
    ];

    for (path, expected) in cases {
        let got = FileCategory::from_path(&PathBuf::from(path));
        assert_eq!(got, expected, "{} should be classified as Docs", path);
    }
}

#[test]
fn file_category_config() {
    let cases = [
        ("Cargo.toml", FileCategory::Config),
        ("package.json", FileCategory::Config),
        (".gitignore", FileCategory::Config),
    ];

    for (path, expected) in cases {
        let got = FileCategory::from_path(&PathBuf::from(path));
        assert_eq!(got, expected, "{} should be classified as Config", path);
    }
}

#[test]
fn file_category_build() {
    let cases = [
        (".github/workflows/ci.yml", FileCategory::Build),
        ("Dockerfile", FileCategory::Build),
        ("Makefile", FileCategory::Build),
    ];

    for (path, expected) in cases {
        let got = FileCategory::from_path(&PathBuf::from(path));
        assert_eq!(got, expected, "{} should be classified as Build", path);
    }
}

// ─── Additional CommitType inference ──────────────────────────────────────────

#[test]
fn infer_type_more_deletions_is_refactor() {
    // 50 deletions > 10 insertions * 2 = 20
    let changes = make_staged_changes(vec![make_file_change(
        "src/services/old_module.rs",
        ChangeStatus::Modified,
        "",
        10,
        50,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert_eq!(
        ctx.suggested_type,
        CommitType::Refactor,
        "deletions > insertions*2 should infer Refactor"
    );
}

#[test]
fn infer_type_default_fallback_is_refactor() {
    // 30 insertions, 30 deletions (not small, not deletion-heavy, not special category)
    let changes = make_staged_changes(vec![make_file_change(
        "src/services/module.rs",
        ChangeStatus::Modified,
        "",
        30,
        30,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert_eq!(
        ctx.suggested_type,
        CommitType::Refactor,
        "large non-special change should fallback to Refactor (safer than Feat)"
    );
}

// ─── Additional budget and truncation ─────────────────────────────────────────

#[test]
fn symbols_budget_truncation() {
    // Create 20 symbols, with a very small budget
    let symbols: Vec<CodeSymbol> = (0..20)
        .map(|i| {
            make_symbol(
                &format!("function_{}", i),
                SymbolKind::Function,
                "src/lib.rs",
                true,
                true,
            )
        })
        .collect();

    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "",
        100,
        0,
    )]);

    let mut config = default_config();
    // Very small budget to force truncation
    config.max_context_chars = 500;

    let ctx = ContextBuilder::build(&changes, &symbols, &config);
    let prompt = ctx.to_prompt();

    assert!(
        prompt.contains("more symbols"),
        "prompt should indicate truncated symbols when budget is exceeded"
    );
}

#[test]
fn skip_content_lock_files() {
    let changes = make_staged_changes(vec![make_file_change(
        "Cargo.lock",
        ChangeStatus::Modified,
        "+lots of lock file content\n".repeat(100).as_str(),
        100,
        50,
    )]);

    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert!(
        ctx.truncated_diff.contains("lock file - content skipped"),
        "lock file diff should contain skip message, got: {}",
        &ctx.truncated_diff[..ctx.truncated_diff.len().min(200)]
    );
}

// ─── Additional scope inference ──────────────────────────────────────────────

#[test]
fn scope_from_packages_prefix() {
    let changes = make_staged_changes(vec![make_file_change(
        "packages/foo/src/bar.rs",
        ChangeStatus::Modified,
        "",
        5,
        2,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert_eq!(
        ctx.suggested_scope,
        Some("foo".to_string()),
        "packages/foo/src/bar.rs should yield scope 'foo'"
    );
}

// ─── Additional FileCategory classification ──────────────────────────────────

#[test]
fn file_category_other() {
    let path = PathBuf::from("data/file.xyz");
    let got = FileCategory::from_path(&path);
    assert_eq!(
        got,
        FileCategory::Other,
        "unknown extension .xyz should be classified as Other"
    );
}

// ─── Evidence flags ─────────────────────────────────────────────────────────

#[test]
fn evidence_mechanical_transform_balanced_no_symbols() {
    // Small balanced change, no symbols → mechanical
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "-    old_indent\n+old_indent",
        5,
        5,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert!(
        ctx.is_mechanical,
        "balanced small change with no symbols should be mechanical"
    );
}

#[test]
fn evidence_not_mechanical_with_symbols() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "+pub fn new_func() {}",
        5,
        3,
    )]);
    let symbols = vec![make_symbol(
        "new_func",
        SymbolKind::Function,
        "src/lib.rs",
        true,
        true,
    )];
    let ctx = ContextBuilder::build(&changes, &symbols, &default_config());
    assert!(
        !ctx.is_mechanical,
        "change with new symbols should not be mechanical"
    );
}

#[test]
fn evidence_not_mechanical_large_change() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "",
        50,
        50,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert!(
        !ctx.is_mechanical,
        "large change (100 lines total) should not be mechanical"
    );
}

#[test]
fn evidence_bug_evidence_from_fix_comment() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "+// fix: handle edge case where input is empty\n+if input.is_empty() { return; }",
        2,
        0,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert!(
        ctx.has_bug_evidence,
        "diff with '// fix' comment should have bug evidence"
    );
}

#[test]
fn evidence_no_bug_evidence_for_refactor() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "-    if let Some(x) = foo() {\n-        bar();\n-    }\n+    if let Some(x) = foo() { bar(); }",
        1,
        3,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert!(
        !ctx.has_bug_evidence,
        "refactor without fix/bug comments should not have bug evidence"
    );
}

#[test]
fn evidence_dependency_only() {
    let changes = make_staged_changes(vec![
        make_file_change("Cargo.toml", ChangeStatus::Modified, "", 3, 1),
        make_file_change(".github/workflows/ci.yml", ChangeStatus::Modified, "", 2, 1),
    ]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert!(
        ctx.is_dependency_only,
        "all config/build files should be dependency_only"
    );
}

#[test]
fn evidence_not_dependency_only_with_source() {
    let changes = make_staged_changes(vec![
        make_file_change("Cargo.toml", ChangeStatus::Modified, "", 3, 1),
        make_file_change("src/lib.rs", ChangeStatus::Modified, "", 5, 2),
    ]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert!(
        !ctx.is_dependency_only,
        "mix of config and source should not be dependency_only"
    );
}

#[test]
fn evidence_public_api_removed_count() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "-pub fn old_api() {}\n-pub fn another_old() {}",
        0,
        10,
    )]);
    let symbols = vec![
        make_symbol("old_api", SymbolKind::Function, "src/lib.rs", true, false),
        make_symbol(
            "another_old",
            SymbolKind::Function,
            "src/lib.rs",
            true,
            false,
        ),
    ];
    let ctx = ContextBuilder::build(&changes, &symbols, &default_config());
    assert_eq!(
        ctx.public_api_removed_count, 2,
        "should count 2 removed public symbols"
    );
}

#[test]
fn prompt_contains_evidence_section() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "-old\n+new",
        1,
        1,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    let prompt = ctx.to_prompt();
    assert!(
        prompt.contains("EVIDENCE:"),
        "prompt should contain EVIDENCE section"
    );
    assert!(
        prompt.contains("mechanical/formatting change?"),
        "prompt should contain mechanical transform question"
    );
    assert!(
        prompt.contains("bug-fix comments?"),
        "prompt should contain bug-fix question"
    );
}

#[test]
fn prompt_contains_constraints_when_no_bug_evidence() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "-old\n+new",
        1,
        1,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    let prompt = ctx.to_prompt();
    assert!(
        prompt.contains("CONSTRAINTS (must follow):"),
        "prompt should contain CONSTRAINTS section when bug_evidence=no"
    );
    assert!(
        prompt.contains("No bug-fix comments found"),
        "prompt should mention no bug-fix constraint"
    );
}

// ─── API replacement type inference ─────────────────────────────────────────

#[test]
fn api_replacement_infers_refactor() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/services/context.rs",
        ChangeStatus::Modified,
        "+pub fn new_builder()\n-pub fn old_builder()",
        20,
        15,
    )]);
    let symbols = vec![
        make_symbol(
            "new_builder",
            SymbolKind::Function,
            "src/services/context.rs",
            true,
            true,
        ),
        make_symbol(
            "old_builder",
            SymbolKind::Function,
            "src/services/context.rs",
            true,
            false,
        ),
    ];
    let commit_type = ContextBuilder::infer_commit_type(&changes, &symbols, false);
    assert_eq!(
        commit_type,
        CommitType::Refactor,
        "adding new public API while removing old public API should be refactor, not feat"
    );
}

#[test]
fn api_addition_without_removal_infers_feat() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/services/context.rs",
        ChangeStatus::Modified,
        "+pub fn new_feature()",
        20,
        0,
    )]);
    let symbols = vec![make_symbol(
        "new_feature",
        SymbolKind::Function,
        "src/services/context.rs",
        true,
        true,
    )];
    let commit_type = ContextBuilder::infer_commit_type(&changes, &symbols, false);
    assert_eq!(
        commit_type,
        CommitType::Feat,
        "adding new public API without removing old ones should be feat"
    );
}

// ─── Prompt content tests ───────────────────────────────────────────────────

#[test]
fn prompt_includes_subject_budget() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "-old\n+new",
        1,
        1,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    let prompt = ctx.to_prompt();
    // Should contain "under XX chars" for subject budget
    assert!(
        prompt.contains("under ") && prompt.contains(" chars"),
        "prompt should contain subject character budget"
    );
}

#[test]
fn prompt_breaking_constraint_includes_description_guidance() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "-pub fn old()\n+fn new()",
        1,
        1,
    )]);
    let symbols = vec![make_symbol(
        "old",
        SymbolKind::Function,
        "src/lib.rs",
        true,
        false,
    )];
    let ctx = ContextBuilder::build(&changes, &symbols, &default_config());
    let prompt = ctx.to_prompt();
    assert!(
        prompt.contains("describe what was removed"),
        "breaking change constraint should guide the model to describe the removal"
    );
}

#[test]
fn prompt_evidence_uses_natural_language() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "-old\n+new",
        1,
        1,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    let prompt = ctx.to_prompt();
    // Should NOT contain snake_case internal identifiers
    assert!(
        !prompt.contains("mechanical_transform:"),
        "prompt should not use snake_case field names (mechanical_transform)"
    );
    assert!(
        !prompt.contains("bug_evidence:"),
        "prompt should not use snake_case field names (bug_evidence)"
    );
    assert!(
        !prompt.contains("- public_api_removed:"),
        "prompt should not use snake_case field names (public_api_removed)"
    );
}

// ─── Cross-project file categorization ──────────────────────────────────────

#[test]
fn file_category_csharp_is_source() {
    assert_eq!(
        FileCategory::from_path(std::path::Path::new("src/Models/User.cs")),
        FileCategory::Source
    );
}

#[test]
fn file_category_ruby_is_source() {
    assert_eq!(
        FileCategory::from_path(std::path::Path::new("app/models/user.rb")),
        FileCategory::Source
    );
}

#[test]
fn file_category_biome_is_config() {
    assert_eq!(
        FileCategory::from_path(std::path::Path::new("biome.json")),
        FileCategory::Config
    );
}

#[test]
fn file_category_dotfile_config() {
    assert_eq!(
        FileCategory::from_path(std::path::Path::new(".rustfmt.toml")),
        FileCategory::Config
    );
}

#[test]
fn file_category_jenkins_is_build() {
    assert_eq!(
        FileCategory::from_path(std::path::Path::new("Jenkinsfile")),
        FileCategory::Build
    );
}

#[test]
fn file_category_containerfile_is_build() {
    assert_eq!(
        FileCategory::from_path(std::path::Path::new("Containerfile")),
        FileCategory::Build
    );
    assert_eq!(
        FileCategory::from_path(std::path::Path::new("podman-compose.yml")),
        FileCategory::Build
    );
    assert_eq!(
        FileCategory::from_path(std::path::Path::new("compose.yaml")),
        FileCategory::Build
    );
}

// ─── Whitespace-only modified symbol detection ──────────────────────────────

#[test]
fn modified_symbol_whitespace_only_detected() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "@@ -1,3 +1,3 @@\n fn foo() {\n-    bar()\n+  bar()\n }",
        1,
        1,
    )]);
    let sym_old = make_symbol("foo", SymbolKind::Function, "src/lib.rs", true, false);
    let sym_new = make_symbol("foo", SymbolKind::Function, "src/lib.rs", true, true);
    let ctx = ContextBuilder::build(&changes, &[sym_old, sym_new], &default_config());
    assert!(
        !ctx.symbols_modified.contains("foo"),
        "whitespace-only modified symbol should not appear in symbols_modified: {}",
        ctx.symbols_modified
    );
}

#[test]
fn modified_symbol_semantic_change_shown() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "@@ -1,3 +1,3 @@\n fn foo() {\n-    bar()\n+    baz()\n }",
        1,
        1,
    )]);
    let sym_old = make_symbol("foo", SymbolKind::Function, "src/lib.rs", true, false);
    let sym_new = make_symbol("foo", SymbolKind::Function, "src/lib.rs", true, true);
    let ctx = ContextBuilder::build(&changes, &[sym_old, sym_new], &default_config());
    assert!(
        ctx.symbols_modified.contains("foo"),
        "semantic modified symbol should appear in symbols_modified: {}",
        ctx.symbols_modified
    );
}

// ─── Whitespace-only formatting detection ────────────────────────────────────

#[test]
fn all_symbols_whitespace_only_suggests_style() {
    // Diff only changes indentation inside `foo` — no semantic content change.
    // Both old and new symbol share the same name/kind/file → classified as modified.
    // classify_span_change should detect whitespace-only → Style inferred.
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "@@ -1,3 +1,3 @@\n fn foo() {\n-    bar()\n+  bar()\n }",
        1,
        1,
    )]);
    let sym_old = make_symbol("foo", SymbolKind::Function, "src/lib.rs", true, false);
    let sym_new = make_symbol("foo", SymbolKind::Function, "src/lib.rs", true, true);
    let ctx = ContextBuilder::build(&changes, &[sym_old, sym_new], &default_config());
    assert_eq!(
        ctx.suggested_type,
        CommitType::Style,
        "all whitespace-only modified symbols with no added/removed symbols should suggest Style"
    );
}

#[test]
fn whitespace_detection_works_with_shifted_lines() {
    // Symbol `process` was at line 2 in HEAD, now at line 5 in staged
    // (3 lines added above it). The whitespace change is inside the function.
    // classify_span_change must use separate old/new spans to detect this correctly.
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "@@ -2,3 +5,3 @@\n fn process() {\n-    do_thing()\n+  do_thing()\n }",
        1,
        1,
    )]);
    let mut sym_old = make_symbol("process", SymbolKind::Function, "src/lib.rs", true, false);
    sym_old.line = 2;
    sym_old.end_line = 4;
    let mut sym_new = make_symbol("process", SymbolKind::Function, "src/lib.rs", true, true);
    sym_new.line = 5;
    sym_new.end_line = 7;
    let ctx = ContextBuilder::build(&changes, &[sym_old, sym_new], &default_config());
    // Should detect as whitespace-only despite line shift
    assert!(
        !ctx.symbols_modified.contains("process"),
        "whitespace-only change with shifted lines should not appear in symbols_modified: {}",
        ctx.symbols_modified
    );
}

// ─── Multi-file diff truncation ──────────────────────────────────────────────

// ─── Locale support ──────────────────────────────────────────────────────────

#[test]
fn locale_instruction_appears_in_prompt_when_set() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "-old\n+new",
        1,
        1,
    )]);
    let mut config = default_config();
    config.locale = Some("de".to_string());

    let ctx = ContextBuilder::build(&changes, &[], &config);
    let prompt = ctx.to_prompt();

    assert!(
        prompt.contains("LANGUAGE:"),
        "prompt should contain LANGUAGE instruction when locale is set"
    );
    assert!(
        prompt.contains("Write the subject and body in de"),
        "prompt should instruct writing in the specified language"
    );
    assert!(
        prompt.contains("JSON keys must remain in English"),
        "prompt should instruct keeping JSON keys in English"
    );
}

#[test]
fn no_locale_instruction_when_none() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "-old\n+new",
        1,
        1,
    )]);
    let config = default_config();

    let ctx = ContextBuilder::build(&changes, &[], &config);
    let prompt = ctx.to_prompt();

    assert!(
        !prompt.contains("LANGUAGE:"),
        "prompt should not contain LANGUAGE instruction when locale is None"
    );
}

#[test]
fn locale_config_defaults_to_none() {
    let config = Config::default();
    assert!(config.locale.is_none(), "locale should default to None");
}

#[test]
fn locale_deserialized_from_toml() {
    let toml_str = r#"locale = "ja""#;
    let config: Config = toml::from_str(toml_str).unwrap();
    assert_eq!(
        config.locale,
        Some("ja".to_string()),
        "locale should be deserialized from TOML"
    );
}

#[test]
fn locale_absent_in_toml_defaults_to_none() {
    let toml_str = r#"model = "llama3:8b""#;
    let config: Config = toml::from_str(toml_str).unwrap();
    assert!(
        config.locale.is_none(),
        "locale should default to None when absent from TOML"
    );
}

// ─── Multi-file diff truncation ──────────────────────────────────────────────

#[test]
fn diff_truncation_multiple_files() {
    let huge_diff = "+line of code\n".repeat(500);
    let files: Vec<_> = (0..10)
        .map(|i| {
            make_file_change(
                &format!("src/module_{}.rs", i),
                ChangeStatus::Modified,
                &huge_diff,
                500,
                0,
            )
        })
        .collect();

    let changes = make_staged_changes(files);

    let mut config = default_config();
    config.max_context_chars = 3_000;

    let ctx = ContextBuilder::build(&changes, &[], &config);
    assert!(
        ctx.truncated_diff.contains("files not shown due to budget")
            || ctx.truncated_diff.contains("budget exceeded")
            || ctx.truncated_diff.contains("lines truncated"),
        "huge multi-file diff should show truncation indicators"
    );
}

// ─── Cross-file symbol connections ───────────────────────────────────────────

#[test]
fn prompt_shows_connections_between_modified_symbols() {
    let changes = make_staged_changes(vec![
        make_file_change(
            "src/services/validator.rs",
            ChangeStatus::Modified,
            "+    let result = parse(input);",
            1,
            0,
        ),
        make_file_change(
            "src/services/parser.rs",
            ChangeStatus::Modified,
            "-pub fn parse(s: &str) -> Ast {\n+pub fn parse(s: &str, strict: bool) -> Ast {",
            1,
            1,
        ),
    ]);
    let symbols = vec![
        make_symbol(
            "parse",
            SymbolKind::Function,
            "src/services/parser.rs",
            true,
            true,
        ),
        make_symbol(
            "parse",
            SymbolKind::Function,
            "src/services/parser.rs",
            true,
            false,
        ),
    ];
    let ctx = ContextBuilder::build(&changes, &symbols, &default_config());
    assert!(
        !ctx.connections.is_empty(),
        "should detect that validator.rs calls modified symbol parse()"
    );
}

// ─── Signature display ───────────────────────────────────────────────────────

#[test]
fn prompt_shows_signature_diff_for_modified_symbols() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "@@ -1,3 +1,3 @@\n-pub fn validate(input: &str) -> bool {\n+pub fn validate(input: &str, strict: bool) -> Result<()> {\n     // body\n }",
        1,
        1,
    )]);
    let mut sym_old = make_symbol("validate", SymbolKind::Function, "src/lib.rs", true, false);
    sym_old.signature = Some("pub fn validate(input: &str) -> bool".to_string());
    let mut sym_new = make_symbol("validate", SymbolKind::Function, "src/lib.rs", true, true);
    sym_new.signature =
        Some("pub fn validate(input: &str, strict: bool) -> Result<()>".to_string());
    let ctx = ContextBuilder::build(&changes, &[sym_old, sym_new], &default_config());
    assert!(
        ctx.symbols_modified.contains('\u{2192}') || ctx.symbols_modified.contains("->"),
        "modified symbols should show signature transition: {}",
        ctx.symbols_modified
    );
}

#[test]
fn prompt_shows_signatures_when_available() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "+pub fn connect(host: &str) -> Result<()> {\n+    Ok(())\n+}",
        3,
        0,
    )]);
    let mut sym = make_symbol("connect", SymbolKind::Function, "src/lib.rs", true, true);
    sym.signature = Some("pub fn connect(host: &str) -> Result<()>".to_string());
    let ctx = ContextBuilder::build(&changes, &[sym], &default_config());
    let prompt = ctx.to_prompt();
    assert!(
        prompt.contains("pub fn connect(host: &str) -> Result<()>"),
        "prompt should contain the full signature, got symbols_added: {}",
        ctx.symbols_added
    );
}

// ─── Test Coverage: detect_primary_change (#35) ───────────────────────────────

#[test]
fn primary_change_prefers_new_public_api() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "+pub fn new_api() {}",
        1,
        0,
    )]);
    let sym = make_symbol("new_api", SymbolKind::Function, "src/lib.rs", true, true);
    let ctx = ContextBuilder::build(&changes, &[sym], &default_config());
    assert!(
        ctx.primary_change.as_ref().unwrap().contains("new_api"),
        "should mention new public API: {:?}",
        ctx.primary_change
    );
}

#[test]
fn primary_change_falls_back_to_removed_public() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "-pub fn old_api() {}",
        0,
        1,
    )]);
    let sym = make_symbol("old_api", SymbolKind::Function, "src/lib.rs", true, false);
    let ctx = ContextBuilder::build(&changes, &[sym], &default_config());
    assert!(
        ctx.primary_change.as_ref().unwrap().contains("old_api"),
        "should mention removed public API: {:?}",
        ctx.primary_change
    );
}

#[test]
fn primary_change_falls_back_to_largest_file() {
    let changes = make_staged_changes(vec![
        make_file_change("src/a.rs", ChangeStatus::Modified, "+x", 1, 0),
        make_file_change("src/b.rs", ChangeStatus::Modified, "+large change", 50, 10),
    ]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert!(
        ctx.primary_change.as_ref().unwrap().contains("b"),
        "should mention largest file: {:?}",
        ctx.primary_change
    );
}

// ─── Test Coverage: detect_metadata_breaking (#36) ────────────────────────────

#[test]
fn metadata_breaking_detects_msrv_change() {
    let changes = make_staged_changes(vec![make_file_change(
        "Cargo.toml",
        ChangeStatus::Modified,
        "+rust-version = \"1.75\"",
        1,
        0,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert!(
        !ctx.metadata_breaking_signals.is_empty(),
        "should detect MSRV change"
    );
}

#[test]
fn metadata_breaking_detects_pub_use_removal() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "-pub use crate::old_api::*;",
        0,
        1,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert!(
        ctx.metadata_breaking_signals
            .iter()
            .any(|s| s.contains("pub use")),
        "should detect removed pub use: {:?}",
        ctx.metadata_breaking_signals
    );
}

// ─── Test Coverage: detect_bug_evidence all patterns (#38) ────────────────────

#[test]
fn bug_evidence_detects_hash_fix() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.py",
        ChangeStatus::Modified,
        "+# fix: off by one",
        1,
        0,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert!(ctx.has_bug_evidence, "should detect '# fix' pattern");
}

#[test]
fn bug_evidence_detects_c_style_fix() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.c",
        ChangeStatus::Modified,
        "+/* fix: memory leak */",
        1,
        0,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert!(ctx.has_bug_evidence, "should detect '/* fix' pattern");
}

#[test]
fn bug_evidence_detects_bug_keyword() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "+// bug: incorrect index",
        1,
        0,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert!(ctx.has_bug_evidence, "should detect '// bug' pattern");
}

#[test]
fn bug_evidence_detects_fixme() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "+// FIXME this is broken",
        1,
        0,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert!(ctx.has_bug_evidence, "should detect 'fixme' pattern");
}

#[test]
fn bug_evidence_detects_hotfix() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "+// hotfix for prod issue",
        1,
        0,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert!(ctx.has_bug_evidence, "should detect 'hotfix' pattern");
}

// ─── Test Coverage: connection content assertion (#41) ────────────────────────

#[test]
fn connection_content_mentions_symbol_name() {
    let changes = make_staged_changes(vec![
        make_file_change(
            "src/services/validator.rs",
            ChangeStatus::Modified,
            "+    let result = parse(input);",
            1,
            0,
        ),
        make_file_change(
            "src/services/parser.rs",
            ChangeStatus::Modified,
            "-pub fn parse(s: &str) -> Ast {\n+pub fn parse(s: &str, strict: bool) -> Ast {",
            1,
            1,
        ),
    ]);
    let symbols = vec![
        make_symbol(
            "parse",
            SymbolKind::Function,
            "src/services/parser.rs",
            true,
            true,
        ),
        make_symbol(
            "parse",
            SymbolKind::Function,
            "src/services/parser.rs",
            true,
            false,
        ),
    ];
    let ctx = ContextBuilder::build(&changes, &symbols, &default_config());
    assert!(
        ctx.connections.iter().any(|c| c.contains("parse")),
        "connection should mention symbol name 'parse': {:?}",
        ctx.connections
    );
}

// ─── Test Coverage: Deleted/Renamed status (#37) ──────────────────────────────

#[test]
fn format_files_shows_deleted_marker() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/old.rs",
        ChangeStatus::Deleted,
        "-pub fn removed() {}",
        0,
        1,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert!(
        ctx.file_breakdown.contains("[-]"),
        "deleted file should show [-] marker: {}",
        ctx.file_breakdown
    );
}

#[test]
fn format_files_shows_renamed_marker() {
    let changes = make_staged_changes(vec![make_renamed_file(
        "src/old_name.rs",
        "src/new_name.rs",
        95,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    assert!(
        ctx.file_breakdown.contains("[R]"),
        "renamed file should show [R] marker: {}",
        ctx.file_breakdown
    );
    assert!(
        ctx.file_breakdown.contains("95% similar"),
        "renamed file should show similarity: {}",
        ctx.file_breakdown
    );
}

// ─── Test Coverage: classify_span_change None path (#39) ──────────────────────

#[test]
fn whitespace_detection_returns_none_when_span_has_no_changes() {
    // Symbol at lines 50-60 but diff hunk is at lines 1-3 — no overlap
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "@@ -1,3 +1,3 @@\n fn other() {\n-    old()\n+    new()\n }",
        1,
        1,
    )]);
    let mut sym_old = make_symbol("distant", SymbolKind::Function, "src/lib.rs", true, false);
    sym_old.line = 50;
    sym_old.end_line = 60;
    let mut sym_new = make_symbol("distant", SymbolKind::Function, "src/lib.rs", true, true);
    sym_new.line = 50;
    sym_new.end_line = 60;
    let ctx = ContextBuilder::build(&changes, &[sym_old, sym_new], &default_config());
    // Symbol is outside the hunk — classify_span_change returns None (no changes in span).
    // The symbol still appears as "modified" (name+kind+file match) but with no
    // whitespace classification. This is expected: it won't be filtered as whitespace-only.
    // This test verifies the None path doesn't crash or produce false positives.
    assert!(
        ctx.symbols_modified.contains("distant"),
        "modified symbol outside hunk should still appear (with no ws classification): {}",
        ctx.symbols_modified
    );
}

// ─── Test Coverage: HARD LIMIT dedup check (#28) ─────────────────────────────

#[test]
fn prompt_hard_limit_includes_char_budget() {
    let changes = make_staged_changes(vec![make_file_change(
        "src/lib.rs",
        ChangeStatus::Modified,
        "+fn foo() {}",
        1,
        0,
    )]);
    let ctx = ContextBuilder::build(&changes, &[], &default_config());
    let prompt = ctx.to_prompt();
    assert!(
        prompt.contains("HARD LIMIT"),
        "prompt should contain HARD LIMIT section"
    );
    assert!(
        prompt.contains("chars"),
        "HARD LIMIT should mention char budget"
    );
}