codelens-engine 1.9.36

Harness-native Rust MCP server for code intelligence — 90+ tools (+6 semantic), 25 languages, tree-sitter-first, 50-87% fewer tokens
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
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
use super::*;
use crate::db::{IndexDb, NewSymbol};
use std::sync::Mutex;

/// Serialize tests that load the fastembed ONNX model to avoid file lock contention.
static MODEL_LOCK: Mutex<()> = Mutex::new(());

/// Serialize tests that mutate `CODELENS_EMBED_HINT_*` env vars.
/// The v1.6.0 default flip (§8.14) exposed a pre-existing race where
/// parallel env-var mutating tests interfere with each other — the
/// old `unwrap_or(false)` default happened to mask the race most of
/// the time, but `unwrap_or(true)` no longer does. All tests that
/// read or mutate `CODELENS_EMBED_HINT_*` should take this lock.
static ENV_LOCK: Mutex<()> = Mutex::new(());

macro_rules! skip_without_embedding_model {
    () => {
        if !super::embedding_model_assets_available() {
            eprintln!("skipping embedding test: CodeSearchNet model assets unavailable");
            return;
        }
    };
}

/// Helper: create a temp project with seeded symbols.
fn make_project_with_source() -> (tempfile::TempDir, ProjectRoot) {
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path();

    // Write a source file so body extraction works
    let source = "def hello():\n    print('hi')\n\ndef world():\n    return 42\n";
    write_python_file_with_symbols(
        root,
        "main.py",
        source,
        "hash1",
        &[
            ("hello", "def hello():", "hello"),
            ("world", "def world():", "world"),
        ],
    );

    let project = ProjectRoot::new_exact(root).unwrap();
    (dir, project)
}

fn write_python_file_with_symbols(
    root: &std::path::Path,
    relative_path: &str,
    source: &str,
    hash: &str,
    symbols: &[(&str, &str, &str)],
) {
    std::fs::write(root.join(relative_path), source).unwrap();
    let db_path = crate::db::index_db_path(root);
    let db = IndexDb::open(&db_path).unwrap();
    let file_id = db
        .upsert_file(relative_path, 100, hash, source.len() as i64, Some("py"))
        .unwrap();

    let new_symbols: Vec<NewSymbol<'_>> = symbols
        .iter()
        .map(|(name, signature, name_path)| {
            let start = source.find(signature).unwrap() as i64;
            let end = source[start as usize..]
                .find("\n\ndef ")
                .map(|offset| start + offset as i64)
                .unwrap_or(source.len() as i64);
            let line = source[..start as usize]
                .bytes()
                .filter(|&b| b == b'\n')
                .count() as i64
                + 1;
            NewSymbol {
                name,
                kind: "function",
                line,
                column_num: 0,
                start_byte: start,
                end_byte: end,
                signature,
                name_path,
                parent_id: None,
            }
        })
        .collect();
    db.insert_symbols(file_id, &new_symbols).unwrap();
}

fn replace_file_embeddings_with_sentinels(
    engine: &EmbeddingEngine,
    file_path: &str,
    sentinels: &[(&str, f32)],
) {
    let mut chunks = engine.store.embeddings_for_files(&[file_path]).unwrap();
    for chunk in &mut chunks {
        if let Some((_, value)) = sentinels
            .iter()
            .find(|(symbol_name, _)| *symbol_name == chunk.symbol_name)
        {
            chunk.embedding = vec![*value; chunk.embedding.len()];
        }
    }
    engine.store.delete_by_file(&[file_path]).unwrap();
    engine.store.insert(&chunks).unwrap();
}

#[test]
fn build_embedding_text_with_signature() {
    let sym = crate::db::SymbolWithFile {
        name: "hello".into(),
        kind: "function".into(),
        file_path: "main.py".into(),
        line: 1,
        signature: "def hello():".into(),
        name_path: "hello".into(),
        start_byte: 0,
        end_byte: 10,
    };
    let text = build_embedding_text(&sym, Some("def hello(): pass"));
    assert_eq!(text, "function hello in main.py: def hello():");
}

#[test]
fn build_embedding_text_without_source() {
    let sym = crate::db::SymbolWithFile {
        name: "MyClass".into(),
        kind: "class".into(),
        file_path: "app.py".into(),
        line: 5,
        signature: "class MyClass:".into(),
        name_path: "MyClass".into(),
        start_byte: 0,
        end_byte: 50,
    };
    let text = build_embedding_text(&sym, None);
    assert_eq!(text, "class MyClass (My Class) in app.py: class MyClass:");
}

#[test]
fn build_embedding_text_empty_signature() {
    let sym = crate::db::SymbolWithFile {
        name: "CONFIG".into(),
        kind: "variable".into(),
        file_path: "config.py".into(),
        line: 1,
        signature: String::new(),
        name_path: "CONFIG".into(),
        start_byte: 0,
        end_byte: 0,
    };
    let text = build_embedding_text(&sym, None);
    assert_eq!(text, "variable CONFIG in config.py");
}

#[test]
fn filters_direct_test_symbols_from_embedding_index() {
    let source = "#[test]\nfn alias_case() {}\n";
    let sym = crate::db::SymbolWithFile {
        name: "alias_case".into(),
        kind: "function".into(),
        file_path: "src/lib.rs".into(),
        line: 2,
        signature: "fn alias_case() {}".into(),
        name_path: "alias_case".into(),
        start_byte: source.find("fn alias_case").unwrap() as i64,
        end_byte: source.len() as i64,
    };

    assert!(is_test_only_symbol(&sym, Some(source)));
}

#[test]
fn filters_cfg_test_module_symbols_from_embedding_index() {
    let source = "#[cfg(all(test, feature = \"semantic\"))]\nmod semantic_tests {\n    fn helper_case() {}\n}\n";
    let sym = crate::db::SymbolWithFile {
        name: "helper_case".into(),
        kind: "function".into(),
        file_path: "src/lib.rs".into(),
        line: 3,
        signature: "fn helper_case() {}".into(),
        name_path: "helper_case".into(),
        start_byte: source.find("fn helper_case").unwrap() as i64,
        end_byte: source.len() as i64,
    };

    assert!(is_test_only_symbol(&sym, Some(source)));
}

#[test]
fn extract_python_docstring() {
    let source =
        "def greet(name):\n    \"\"\"Say hello to a person.\"\"\"\n    print(f'hi {name}')\n";
    let doc = extract_leading_doc(source, 0, source.len()).unwrap();
    assert!(doc.contains("Say hello to a person"));
}

#[test]
fn extract_rust_doc_comment() {
    let source = "fn dispatch_tool() {\n    /// Route incoming tool requests.\n    /// Handles all MCP methods.\n    let x = 1;\n}\n";
    let doc = extract_leading_doc(source, 0, source.len()).unwrap();
    assert!(doc.contains("Route incoming tool requests"));
    assert!(doc.contains("Handles all MCP methods"));
}

#[test]
fn extract_leading_doc_returns_none_for_no_doc() {
    let source = "def f():\n    return 1\n";
    assert!(extract_leading_doc(source, 0, source.len()).is_none());
}

#[test]
fn extract_body_hint_finds_first_meaningful_line() {
    let source = "pub fn parse_symbols(\n    project: &ProjectRoot,\n) -> Vec<SymbolInfo> {\n    let mut parser = tree_sitter::Parser::new();\n    parser.set_language(lang);\n}\n";
    let hint = extract_body_hint(source, 0, source.len());
    assert!(hint.is_some());
    assert!(hint.unwrap().contains("tree_sitter::Parser"));
}

#[test]
fn extract_body_hint_skips_comments() {
    let source = "fn foo() {\n    // setup\n    let x = bar();\n}\n";
    let hint = extract_body_hint(source, 0, source.len());
    assert_eq!(hint.unwrap(), "let x = bar();");
}

#[test]
fn extract_body_hint_returns_none_for_empty() {
    let source = "fn empty() {\n}\n";
    let hint = extract_body_hint(source, 0, source.len());
    assert!(hint.is_none());
}

#[test]
fn extract_body_hint_multi_line_collection_via_env_override() {
    // Default is 1 line / 60 chars (v1.4.0 parity after the v1.5 Phase 2
    // PoC revert). Override the line budget via env to confirm the
    // multi-line path still works — this is the knob future experiments
    // will use without recompiling.
    let previous_lines = std::env::var("CODELENS_EMBED_HINT_LINES").ok();
    let previous_chars = std::env::var("CODELENS_EMBED_HINT_CHARS").ok();
    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_LINES", "3");
        std::env::set_var("CODELENS_EMBED_HINT_CHARS", "200");
    }

    let source = "\
fn route_request() {
    let kind = detect_request_kind();
    let target = dispatch_table.get(&kind);
    return target.handle();
}
";
    let hint = extract_body_hint(source, 0, source.len()).expect("hint present");

    let env_restore = || unsafe {
        match &previous_lines {
            Some(value) => std::env::set_var("CODELENS_EMBED_HINT_LINES", value),
            None => std::env::remove_var("CODELENS_EMBED_HINT_LINES"),
        }
        match &previous_chars {
            Some(value) => std::env::set_var("CODELENS_EMBED_HINT_CHARS", value),
            None => std::env::remove_var("CODELENS_EMBED_HINT_CHARS"),
        }
    };

    let all_three = hint.contains("detect_request_kind")
        && hint.contains("dispatch_table")
        && hint.contains("target.handle");
    let has_separator = hint.contains(" · ");
    env_restore();

    assert!(all_three, "missing one of three body lines: {hint}");
    assert!(has_separator, "missing · separator: {hint}");
}

// Note: we intentionally do NOT have a test that verifies the "default"
// 60-char / 1-line behaviour via `extract_body_hint`. Such a test is
// flaky because cargo test runs tests in parallel and the env-overriding
// tests below (`CODELENS_EMBED_HINT_CHARS`, `CODELENS_EMBED_HINT_LINES`)
// can leak their variables into this one. The default constants
// themselves are compile-time checked and covered by
// `extract_body_hint_finds_first_meaningful_line` /
// `extract_body_hint_skips_comments` which assert on the exact single-line
// shape and implicitly depend on the default budget.

#[test]
fn hint_line_budget_respects_env_override() {
    // SAFETY: test block is serialized by crate-level test harness; we
    // restore the variable on exit.
    let previous = std::env::var("CODELENS_EMBED_HINT_LINES").ok();
    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_LINES", "5");
    }
    let budget = super::hint_line_budget();
    unsafe {
        match previous {
            Some(value) => std::env::set_var("CODELENS_EMBED_HINT_LINES", value),
            None => std::env::remove_var("CODELENS_EMBED_HINT_LINES"),
        }
    }
    assert_eq!(budget, 5);
}

#[test]
fn is_nl_shaped_accepts_multi_word_prose() {
    assert!(super::is_nl_shaped("skip comments and string literals"));
    assert!(super::is_nl_shaped("failed to open database"));
    assert!(super::is_nl_shaped("detect client version"));
}

#[test]
fn is_nl_shaped_rejects_code_and_paths() {
    // Path-like tokens (both slash flavors)
    assert!(!super::is_nl_shaped("crates/codelens-engine/src"));
    assert!(!super::is_nl_shaped("C:\\Users\\foo"));
    // Module-path-like
    assert!(!super::is_nl_shaped("std::sync::Mutex"));
    // Single-word identifier
    assert!(!super::is_nl_shaped("detect_client"));
    // Too short
    assert!(!super::is_nl_shaped("ok"));
    assert!(!super::is_nl_shaped(""));
    // High non-alphabetic ratio
    assert!(!super::is_nl_shaped("1 2 3 4 5"));
}

#[test]
fn extract_comment_body_strips_comment_markers() {
    assert_eq!(
        super::extract_comment_body("/// rust doc comment"),
        Some("rust doc comment".to_string())
    );
    assert_eq!(
        super::extract_comment_body("// regular line comment"),
        Some("regular line comment".to_string())
    );
    assert_eq!(
        super::extract_comment_body("# python line comment"),
        Some("python line comment".to_string())
    );
    assert_eq!(
        super::extract_comment_body("/* inline block */"),
        Some("inline block".to_string())
    );
    assert_eq!(
        super::extract_comment_body("* continuation line"),
        Some("continuation line".to_string())
    );
}

#[test]
fn extract_comment_body_rejects_rust_attributes_and_shebangs() {
    assert!(super::extract_comment_body("#[derive(Debug)]").is_none());
    assert!(super::extract_comment_body("#[test]").is_none());
    assert!(super::extract_comment_body("#!/usr/bin/env python").is_none());
}

#[test]
fn extract_nl_tokens_gated_off_by_default() {
    let _env_guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
    // Default: no env, no NL tokens regardless of body content.
    let previous = std::env::var("CODELENS_EMBED_HINT_INCLUDE_COMMENTS").ok();
    unsafe {
        std::env::remove_var("CODELENS_EMBED_HINT_INCLUDE_COMMENTS");
    }
    let source = "\
fn skip_things() {
    // skip comments and string literals during search
    let lit = \"scan for matching tokens\";
}
";
    let result = extract_nl_tokens(source, 0, source.len());
    unsafe {
        if let Some(value) = previous {
            std::env::set_var("CODELENS_EMBED_HINT_INCLUDE_COMMENTS", value);
        }
    }
    assert!(result.is_none(), "gate leaked: {result:?}");
}

#[test]
fn auto_hint_mode_defaults_on_unless_explicit_off() {
    let _env_guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
    // v1.6.0 flip (§8.14): default-ON semantics.
    //
    // Case 1: env var unset → default ON (the v1.6.0 flip).
    // Case 2: env var="0" (or "false"/"no"/"off") → explicit OFF
    //   (opt-out preserved).
    // Case 3: env var="1" (or "true"/"yes"/"on") → explicit ON
    //   (still works — explicit always wins).
    let previous = std::env::var("CODELENS_EMBED_HINT_AUTO").ok();

    // Case 1: unset → ON (flip)
    unsafe {
        std::env::remove_var("CODELENS_EMBED_HINT_AUTO");
    }
    let default_enabled = super::auto_hint_mode_enabled();
    assert!(
        default_enabled,
        "v1.6.0 default flip: auto hint mode should be ON when env unset"
    );

    // Case 2: explicit OFF
    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_AUTO", "0");
    }
    let explicit_off = super::auto_hint_mode_enabled();
    assert!(
        !explicit_off,
        "explicit CODELENS_EMBED_HINT_AUTO=0 must still disable (opt-out escape hatch)"
    );

    // Case 3: explicit ON
    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_AUTO", "1");
    }
    let explicit_on = super::auto_hint_mode_enabled();
    assert!(
        explicit_on,
        "explicit CODELENS_EMBED_HINT_AUTO=1 must still enable"
    );

    // Restore
    unsafe {
        match previous {
            Some(v) => std::env::set_var("CODELENS_EMBED_HINT_AUTO", v),
            None => std::env::remove_var("CODELENS_EMBED_HINT_AUTO"),
        }
    }
}

#[test]
fn language_supports_nl_stack_classifies_correctly() {
    // Supported — measured or static-typed analogue
    assert!(super::language_supports_nl_stack("rs"));
    assert!(super::language_supports_nl_stack("rust"));
    assert!(super::language_supports_nl_stack("cpp"));
    assert!(super::language_supports_nl_stack("c++"));
    assert!(super::language_supports_nl_stack("c"));
    assert!(super::language_supports_nl_stack("go"));
    assert!(super::language_supports_nl_stack("golang"));
    assert!(super::language_supports_nl_stack("java"));
    assert!(super::language_supports_nl_stack("kt"));
    assert!(super::language_supports_nl_stack("kotlin"));
    assert!(super::language_supports_nl_stack("scala"));
    assert!(super::language_supports_nl_stack("cs"));
    assert!(super::language_supports_nl_stack("csharp"));
    // §8.13 Phase 3c: TypeScript / JavaScript added after
    // facebook/jest external-repo A/B (+7.3 % hybrid MRR).
    assert!(super::language_supports_nl_stack("ts"));
    assert!(super::language_supports_nl_stack("typescript"));
    assert!(super::language_supports_nl_stack("tsx"));
    assert!(super::language_supports_nl_stack("js"));
    assert!(super::language_supports_nl_stack("javascript"));
    assert!(super::language_supports_nl_stack("jsx"));
    // Case-insensitive
    assert!(super::language_supports_nl_stack("Rust"));
    assert!(super::language_supports_nl_stack("RUST"));
    assert!(super::language_supports_nl_stack("TypeScript"));
    // Leading/trailing whitespace is tolerated
    assert!(super::language_supports_nl_stack("  rust  "));
    assert!(super::language_supports_nl_stack("  ts  "));

    // Unsupported — measured regression or untested dynamic
    assert!(!super::language_supports_nl_stack("py"));
    assert!(!super::language_supports_nl_stack("python"));
    assert!(!super::language_supports_nl_stack("rb"));
    assert!(!super::language_supports_nl_stack("ruby"));
    assert!(!super::language_supports_nl_stack("php"));
    assert!(!super::language_supports_nl_stack("lua"));
    assert!(!super::language_supports_nl_stack("sh"));
    // Unknown defaults to unsupported
    assert!(!super::language_supports_nl_stack("klingon"));
    assert!(!super::language_supports_nl_stack(""));
}

#[test]
fn language_supports_sparse_weighting_classifies_correctly() {
    assert!(super::language_supports_sparse_weighting("rs"));
    assert!(super::language_supports_sparse_weighting("rust"));
    assert!(super::language_supports_sparse_weighting("cpp"));
    assert!(super::language_supports_sparse_weighting("go"));
    assert!(super::language_supports_sparse_weighting("java"));
    assert!(super::language_supports_sparse_weighting("kotlin"));
    assert!(super::language_supports_sparse_weighting("csharp"));

    assert!(!super::language_supports_sparse_weighting("ts"));
    assert!(!super::language_supports_sparse_weighting("typescript"));
    assert!(!super::language_supports_sparse_weighting("tsx"));
    assert!(!super::language_supports_sparse_weighting("js"));
    assert!(!super::language_supports_sparse_weighting("javascript"));
    assert!(!super::language_supports_sparse_weighting("jsx"));
    assert!(!super::language_supports_sparse_weighting("py"));
    assert!(!super::language_supports_sparse_weighting("python"));
    assert!(!super::language_supports_sparse_weighting("klingon"));
    assert!(!super::language_supports_sparse_weighting(""));
}

#[test]
fn auto_hint_should_enable_requires_both_gate_and_supported_lang() {
    let _env_guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
    let prev_auto = std::env::var("CODELENS_EMBED_HINT_AUTO").ok();
    let prev_lang = std::env::var("CODELENS_EMBED_HINT_AUTO_LANG").ok();

    // Case 1: gate explicitly off → never enable, regardless of language.
    // v1.6.0 flip (§8.14): `unset` now means default-ON, so to test
    // "gate off" we must set the env var to an explicit "0".
    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_AUTO", "0");
        std::env::set_var("CODELENS_EMBED_HINT_AUTO_LANG", "rust");
    }
    assert!(
        !super::auto_hint_should_enable(),
        "gate-off (explicit =0) with lang=rust must stay disabled"
    );

    // Case 2: gate on, supported language → enable
    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_AUTO", "1");
        std::env::set_var("CODELENS_EMBED_HINT_AUTO_LANG", "rust");
    }
    assert!(
        super::auto_hint_should_enable(),
        "gate-on + lang=rust must enable"
    );

    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_AUTO", "1");
        std::env::set_var("CODELENS_EMBED_HINT_AUTO_LANG", "typescript");
    }
    assert!(
        super::auto_hint_should_enable(),
        "gate-on + lang=typescript must keep Phase 2b/2c enabled"
    );

    // Case 3: gate on, unsupported language → disable
    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_AUTO", "1");
        std::env::set_var("CODELENS_EMBED_HINT_AUTO_LANG", "python");
    }
    assert!(
        !super::auto_hint_should_enable(),
        "gate-on + lang=python must stay disabled"
    );

    // Case 4: gate on, no language tag → conservative disable
    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_AUTO", "1");
        std::env::remove_var("CODELENS_EMBED_HINT_AUTO_LANG");
    }
    assert!(
        !super::auto_hint_should_enable(),
        "gate-on + no lang tag must stay disabled"
    );

    // Restore
    unsafe {
        match prev_auto {
            Some(v) => std::env::set_var("CODELENS_EMBED_HINT_AUTO", v),
            None => std::env::remove_var("CODELENS_EMBED_HINT_AUTO"),
        }
        match prev_lang {
            Some(v) => std::env::set_var("CODELENS_EMBED_HINT_AUTO_LANG", v),
            None => std::env::remove_var("CODELENS_EMBED_HINT_AUTO_LANG"),
        }
    }
}

#[test]
fn auto_sparse_should_enable_requires_both_gate_and_sparse_supported_lang() {
    let _env_guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
    let prev_auto = std::env::var("CODELENS_EMBED_HINT_AUTO").ok();
    let prev_lang = std::env::var("CODELENS_EMBED_HINT_AUTO_LANG").ok();

    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_AUTO", "0");
        std::env::set_var("CODELENS_EMBED_HINT_AUTO_LANG", "rust");
    }
    assert!(
        !super::auto_sparse_should_enable(),
        "gate-off (explicit =0) must disable sparse auto gate"
    );

    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_AUTO", "1");
        std::env::set_var("CODELENS_EMBED_HINT_AUTO_LANG", "rust");
    }
    assert!(
        super::auto_sparse_should_enable(),
        "gate-on + lang=rust must enable sparse auto gate"
    );

    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_AUTO", "1");
        std::env::set_var("CODELENS_EMBED_HINT_AUTO_LANG", "typescript");
    }
    assert!(
        !super::auto_sparse_should_enable(),
        "gate-on + lang=typescript must keep sparse auto gate disabled"
    );

    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_AUTO", "1");
        std::env::set_var("CODELENS_EMBED_HINT_AUTO_LANG", "python");
    }
    assert!(
        !super::auto_sparse_should_enable(),
        "gate-on + lang=python must keep sparse auto gate disabled"
    );

    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_AUTO", "1");
        std::env::remove_var("CODELENS_EMBED_HINT_AUTO_LANG");
    }
    assert!(
        !super::auto_sparse_should_enable(),
        "gate-on + no lang tag must keep sparse auto gate disabled"
    );

    unsafe {
        match prev_auto {
            Some(v) => std::env::set_var("CODELENS_EMBED_HINT_AUTO", v),
            None => std::env::remove_var("CODELENS_EMBED_HINT_AUTO"),
        }
        match prev_lang {
            Some(v) => std::env::set_var("CODELENS_EMBED_HINT_AUTO_LANG", v),
            None => std::env::remove_var("CODELENS_EMBED_HINT_AUTO_LANG"),
        }
    }
}

#[test]
fn nl_tokens_enabled_explicit_env_wins_over_auto() {
    let _env_guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
    let prev_explicit = std::env::var("CODELENS_EMBED_HINT_INCLUDE_COMMENTS").ok();
    let prev_auto = std::env::var("CODELENS_EMBED_HINT_AUTO").ok();
    let prev_lang = std::env::var("CODELENS_EMBED_HINT_AUTO_LANG").ok();

    // Explicit ON beats auto-OFF-for-python
    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_INCLUDE_COMMENTS", "1");
        std::env::set_var("CODELENS_EMBED_HINT_AUTO", "1");
        std::env::set_var("CODELENS_EMBED_HINT_AUTO_LANG", "python");
    }
    assert!(
        super::nl_tokens_enabled(),
        "explicit=1 must win over auto+python=off"
    );

    // Explicit OFF beats auto-ON-for-rust
    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_INCLUDE_COMMENTS", "0");
        std::env::set_var("CODELENS_EMBED_HINT_AUTO", "1");
        std::env::set_var("CODELENS_EMBED_HINT_AUTO_LANG", "rust");
    }
    assert!(
        !super::nl_tokens_enabled(),
        "explicit=0 must win over auto+rust=on"
    );

    // No explicit, auto+rust → on via fallback
    unsafe {
        std::env::remove_var("CODELENS_EMBED_HINT_INCLUDE_COMMENTS");
        std::env::set_var("CODELENS_EMBED_HINT_AUTO", "1");
        std::env::set_var("CODELENS_EMBED_HINT_AUTO_LANG", "rust");
    }
    assert!(
        super::nl_tokens_enabled(),
        "no explicit + auto+rust must enable"
    );

    // No explicit, auto+python → off via fallback
    unsafe {
        std::env::remove_var("CODELENS_EMBED_HINT_INCLUDE_COMMENTS");
        std::env::set_var("CODELENS_EMBED_HINT_AUTO", "1");
        std::env::set_var("CODELENS_EMBED_HINT_AUTO_LANG", "python");
    }
    assert!(
        !super::nl_tokens_enabled(),
        "no explicit + auto+python must stay disabled"
    );

    // Restore
    unsafe {
        match prev_explicit {
            Some(v) => std::env::set_var("CODELENS_EMBED_HINT_INCLUDE_COMMENTS", v),
            None => std::env::remove_var("CODELENS_EMBED_HINT_INCLUDE_COMMENTS"),
        }
        match prev_auto {
            Some(v) => std::env::set_var("CODELENS_EMBED_HINT_AUTO", v),
            None => std::env::remove_var("CODELENS_EMBED_HINT_AUTO"),
        }
        match prev_lang {
            Some(v) => std::env::set_var("CODELENS_EMBED_HINT_AUTO_LANG", v),
            None => std::env::remove_var("CODELENS_EMBED_HINT_AUTO_LANG"),
        }
    }
}

#[test]
fn strict_comments_gated_off_by_default() {
    let _env_guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
    let previous = std::env::var("CODELENS_EMBED_HINT_STRICT_COMMENTS").ok();
    unsafe {
        std::env::remove_var("CODELENS_EMBED_HINT_STRICT_COMMENTS");
    }
    let enabled = super::strict_comments_enabled();
    unsafe {
        if let Some(value) = previous {
            std::env::set_var("CODELENS_EMBED_HINT_STRICT_COMMENTS", value);
        }
    }
    assert!(!enabled, "strict comments gate leaked");
}

#[test]
fn looks_like_meta_annotation_detects_rejected_prefixes() {
    // All case variants of the rejected prefix list must match.
    assert!(super::looks_like_meta_annotation("TODO: fix later"));
    assert!(super::looks_like_meta_annotation("todo handle edge case"));
    assert!(super::looks_like_meta_annotation("FIXME this is broken"));
    assert!(super::looks_like_meta_annotation(
        "HACK: workaround for bug"
    ));
    assert!(super::looks_like_meta_annotation("XXX not implemented yet"));
    assert!(super::looks_like_meta_annotation(
        "BUG in the upstream crate"
    ));
    assert!(super::looks_like_meta_annotation("REVIEW before merging"));
    assert!(super::looks_like_meta_annotation(
        "REFACTOR this block later"
    ));
    assert!(super::looks_like_meta_annotation("TEMP: remove before v2"));
    assert!(super::looks_like_meta_annotation(
        "DEPRECATED use new_api instead"
    ));
    // Leading whitespace inside the comment body is handled.
    assert!(super::looks_like_meta_annotation(
        "   TODO: with leading ws"
    ));
}

#[test]
fn looks_like_meta_annotation_preserves_behaviour_prefixes() {
    // Explicitly-excluded prefixes — kept as behaviour signal.
    assert!(!super::looks_like_meta_annotation(
        "NOTE: this branch handles empty input"
    ));
    assert!(!super::looks_like_meta_annotation(
        "WARN: overflow is possible"
    ));
    assert!(!super::looks_like_meta_annotation(
        "SAFETY: caller must hold the lock"
    ));
    assert!(!super::looks_like_meta_annotation(
        "PANIC: unreachable by construction"
    ));
    // Behaviour-descriptive prose must pass through.
    assert!(!super::looks_like_meta_annotation(
        "parse json body from request"
    ));
    assert!(!super::looks_like_meta_annotation(
        "walk directory respecting gitignore"
    ));
    assert!(!super::looks_like_meta_annotation(
        "compute cosine similarity between vectors"
    ));
    // Empty / edge inputs
    assert!(!super::looks_like_meta_annotation(""));
    assert!(!super::looks_like_meta_annotation("   "));
    assert!(!super::looks_like_meta_annotation("123 numeric prefix"));
}

#[test]
fn strict_comments_filters_meta_annotations_during_extraction() {
    let _env_guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
    let previous = std::env::var("CODELENS_EMBED_HINT_STRICT_COMMENTS").ok();
    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_STRICT_COMMENTS", "1");
    }
    let source = "\
fn handle_request() {
    // TODO: handle the error path properly
    // parse json body from the incoming request
    // FIXME: this can panic on empty input
    // walk directory respecting the gitignore rules
    let _x = 1;
}
";
    let result = super::extract_nl_tokens_inner(source, 0, source.len());
    unsafe {
        match previous {
            Some(value) => std::env::set_var("CODELENS_EMBED_HINT_STRICT_COMMENTS", value),
            None => std::env::remove_var("CODELENS_EMBED_HINT_STRICT_COMMENTS"),
        }
    }
    let hint = result.expect("behaviour comments must survive");
    // The first real behaviour comment must appear. The hint is capped
    // by the default 60-char budget, so we only assert on a short
    // substring that's guaranteed to fit.
    assert!(
        hint.contains("parse json body"),
        "behaviour comment dropped: {hint}"
    );
    // TODO / FIXME must NOT appear anywhere in the hint (they were
    // rejected before join, so they cannot be there even partially).
    assert!(!hint.contains("TODO"), "TODO annotation leaked: {hint}");
    assert!(!hint.contains("FIXME"), "FIXME annotation leaked: {hint}");
}

#[test]
fn strict_comments_is_orthogonal_to_strict_literals() {
    let _env_guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
    // Enabling strict_comments must NOT affect the Pass-2 literal path.
    // A format-specifier literal should still pass through Pass 2
    // when the literal filter is off, regardless of the comment gate.
    let prev_c = std::env::var("CODELENS_EMBED_HINT_STRICT_COMMENTS").ok();
    let prev_l = std::env::var("CODELENS_EMBED_HINT_STRICT_LITERALS").ok();
    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_STRICT_COMMENTS", "1");
        std::env::remove_var("CODELENS_EMBED_HINT_STRICT_LITERALS");
    }
    // Source kept short so the 60-char hint budget does not truncate
    // either of the two substrings we assert on.
    let source = "\
fn handle() {
    // handles real behaviour
    let fmt = \"format error string\";
}
";
    let result = super::extract_nl_tokens_inner(source, 0, source.len());
    unsafe {
        match prev_c {
            Some(v) => std::env::set_var("CODELENS_EMBED_HINT_STRICT_COMMENTS", v),
            None => std::env::remove_var("CODELENS_EMBED_HINT_STRICT_COMMENTS"),
        }
        match prev_l {
            Some(v) => std::env::set_var("CODELENS_EMBED_HINT_STRICT_LITERALS", v),
            None => std::env::remove_var("CODELENS_EMBED_HINT_STRICT_LITERALS"),
        }
    }
    let hint = result.expect("tokens must exist");
    // Comment survives (not a meta-annotation).
    assert!(hint.contains("handles real"), "comment dropped: {hint}");
    // String literal still appears — strict_literals was OFF, so the
    // Pass-2 filter is inactive for this test.
    assert!(
        hint.contains("format error string"),
        "literal dropped: {hint}"
    );
}

#[test]
fn strict_literal_filter_gated_off_by_default() {
    let _env_guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
    let previous = std::env::var("CODELENS_EMBED_HINT_STRICT_LITERALS").ok();
    unsafe {
        std::env::remove_var("CODELENS_EMBED_HINT_STRICT_LITERALS");
    }
    let enabled = super::strict_literal_filter_enabled();
    unsafe {
        if let Some(value) = previous {
            std::env::set_var("CODELENS_EMBED_HINT_STRICT_LITERALS", value);
        }
    }
    assert!(!enabled, "strict literal filter gate leaked");
}

#[test]
fn contains_format_specifier_detects_c_and_python_style() {
    // C / Python `%` style
    assert!(super::contains_format_specifier("Invalid URL %s"));
    assert!(super::contains_format_specifier("got %d matches"));
    assert!(super::contains_format_specifier("value=%r"));
    assert!(super::contains_format_specifier("size=%f"));
    // Python `.format` / f-string / Rust `format!` style
    assert!(super::contains_format_specifier("sending request to {url}"));
    assert!(super::contains_format_specifier("got {0} items"));
    assert!(super::contains_format_specifier("{:?}"));
    assert!(super::contains_format_specifier("value: {x:.2f}"));
    assert!(super::contains_format_specifier("{}"));
    // Plain prose with no format specifier
    assert!(!super::contains_format_specifier(
        "skip comments and string literals"
    ));
    assert!(!super::contains_format_specifier("failed to open database"));
    // JSON-like brace content should not count as a format specifier
    // (multi-word content inside braces)
    assert!(!super::contains_format_specifier("{name: foo, id: 1}"));
}

#[test]
fn looks_like_error_or_log_prefix_rejects_common_patterns() {
    assert!(super::looks_like_error_or_log_prefix("Invalid URL format"));
    assert!(super::looks_like_error_or_log_prefix(
        "Cannot decode response"
    ));
    assert!(super::looks_like_error_or_log_prefix("could not open file"));
    assert!(super::looks_like_error_or_log_prefix(
        "Failed to send request"
    ));
    assert!(super::looks_like_error_or_log_prefix(
        "Expected int, got str"
    ));
    assert!(super::looks_like_error_or_log_prefix(
        "sending request to server"
    ));
    assert!(super::looks_like_error_or_log_prefix(
        "received response headers"
    ));
    assert!(super::looks_like_error_or_log_prefix(
        "starting worker pool"
    ));
    // Real behaviour strings must pass
    assert!(!super::looks_like_error_or_log_prefix(
        "parse json body from request"
    ));
    assert!(!super::looks_like_error_or_log_prefix(
        "compute cosine similarity between vectors"
    ));
    assert!(!super::looks_like_error_or_log_prefix(
        "walk directory tree respecting gitignore"
    ));
}

#[test]
fn strict_mode_rejects_format_and_error_literals_during_extraction() {
    let _env_guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
    // The env gate is bypassed by calling the inner function directly,
    // BUT the inner function still reads the strict-literal env var.
    // So we have to set it explicitly for this test.
    let previous = std::env::var("CODELENS_EMBED_HINT_STRICT_LITERALS").ok();
    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_STRICT_LITERALS", "1");
    }
    let source = "\
fn handle_request() {
    let err = \"Invalid URL %s\";
    let log = \"sending request to the upstream server\";
    let fmt = \"received {count} items in batch\";
    let real = \"parse json body from the incoming request\";
}
";
    let result = super::extract_nl_tokens_inner(source, 0, source.len());
    unsafe {
        match previous {
            Some(value) => std::env::set_var("CODELENS_EMBED_HINT_STRICT_LITERALS", value),
            None => std::env::remove_var("CODELENS_EMBED_HINT_STRICT_LITERALS"),
        }
    }
    let hint = result.expect("some token should survive");
    // The one real behaviour-descriptive literal must land in the hint.
    assert!(
        hint.contains("parse json body"),
        "real literal was filtered out: {hint}"
    );
    // None of the three low-value literals should appear.
    assert!(
        !hint.contains("Invalid URL"),
        "format-specifier literal leaked: {hint}"
    );
    assert!(
        !hint.contains("sending request"),
        "log-prefix literal leaked: {hint}"
    );
    assert!(
        !hint.contains("received {count}"),
        "python fstring literal leaked: {hint}"
    );
}

#[test]
fn strict_mode_leaves_comments_untouched() {
    let _env_guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
    // Comments (Pass 1) should NOT be filtered by the strict flag —
    // the §8.8 post-mortem identified string literals as the
    // regression source, not comments.
    let previous = std::env::var("CODELENS_EMBED_HINT_STRICT_LITERALS").ok();
    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_STRICT_LITERALS", "1");
    }
    let source = "\
fn do_work() {
    // Invalid inputs are rejected by this guard clause.
    // sending requests in parallel across worker threads.
    let _lit = \"format spec %s\";
}
";
    let result = super::extract_nl_tokens_inner(source, 0, source.len());
    unsafe {
        match previous {
            Some(value) => std::env::set_var("CODELENS_EMBED_HINT_STRICT_LITERALS", value),
            None => std::env::remove_var("CODELENS_EMBED_HINT_STRICT_LITERALS"),
        }
    }
    let hint = result.expect("comments should survive strict mode");
    // Both comments should land in the hint even though they start with
    // error/log-style prefixes — the filter only touches string literals.
    assert!(
        hint.contains("Invalid inputs") || hint.contains("rejected by this guard"),
        "strict mode swallowed a comment: {hint}"
    );
    // And the low-value string literal should NOT be in the hint.
    assert!(
        !hint.contains("format spec"),
        "format-specifier literal leaked under strict mode: {hint}"
    );
}

#[test]
fn should_reject_literal_strict_composes_format_and_prefix() {
    // The test-only helper must mirror the production filter logic:
    // a literal is rejected iff it is a format specifier OR an error/log
    // prefix (the production filter uses exactly this disjunction).
    assert!(super::should_reject_literal_strict("Invalid URL %s"));
    assert!(super::should_reject_literal_strict(
        "sending request to server"
    ));
    assert!(super::should_reject_literal_strict("value: {x:.2f}"));
    // Real behaviour strings pass through.
    assert!(!super::should_reject_literal_strict(
        "parse json body from the incoming request"
    ));
    assert!(!super::should_reject_literal_strict(
        "compute cosine similarity between vectors"
    ));
}

#[test]
fn is_static_method_ident_accepts_pascal_and_rejects_snake() {
    assert!(super::is_static_method_ident("HashMap"));
    assert!(super::is_static_method_ident("Parser"));
    assert!(super::is_static_method_ident("A"));
    // snake_case / module-like — the filter must reject these so
    // `std::fs::read_to_string` does not leak into API hints.
    assert!(!super::is_static_method_ident("std"));
    assert!(!super::is_static_method_ident("fs"));
    assert!(!super::is_static_method_ident("_private"));
    assert!(!super::is_static_method_ident(""));
}

#[test]
fn extract_api_calls_gated_off_by_default() {
    let _env_guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
    // Default: no env, no API-call hint regardless of body content.
    let previous = std::env::var("CODELENS_EMBED_HINT_INCLUDE_API_CALLS").ok();
    unsafe {
        std::env::remove_var("CODELENS_EMBED_HINT_INCLUDE_API_CALLS");
    }
    let source = "\
fn make_parser() {
    let p = Parser::new();
    let _ = HashMap::with_capacity(8);
}
";
    let result = extract_api_calls(source, 0, source.len());
    unsafe {
        if let Some(value) = previous {
            std::env::set_var("CODELENS_EMBED_HINT_INCLUDE_API_CALLS", value);
        }
    }
    assert!(result.is_none(), "gate leaked: {result:?}");
}

#[test]
fn extract_api_calls_captures_type_method_patterns() {
    // Uses the env-independent inner to avoid racing with other tests.
    let source = "\
fn open_db() {
    let p = Parser::new();
    let map = HashMap::with_capacity(16);
    let _ = tree_sitter::Parser::new();
}
";
    let hint = super::extract_api_calls_inner(source, 0, source.len())
        .expect("api calls should be produced");
    assert!(hint.contains("Parser::new"), "missing Parser::new: {hint}");
    assert!(
        hint.contains("HashMap::with_capacity"),
        "missing HashMap::with_capacity: {hint}"
    );
}

#[test]
fn extract_api_calls_rejects_module_prefixed_free_functions() {
    // Pure module paths must not surface as Type hints — the whole
    // point of `is_static_method_ident` is to drop these.
    let source = "\
fn read_config() {
    let _ = std::fs::read_to_string(\"foo\");
    let _ = crate::util::parse();
}
";
    let hint = super::extract_api_calls_inner(source, 0, source.len());
    // If any API hint is produced, it must not contain the snake_case
    // module prefixes; otherwise `None` is acceptable too.
    if let Some(hint) = hint {
        assert!(!hint.contains("std::fs"), "lowercase module leaked: {hint}");
        assert!(
            !hint.contains("fs::read_to_string"),
            "module-prefixed free function leaked: {hint}"
        );
        assert!(!hint.contains("crate::util"), "crate path leaked: {hint}");
    }
}

#[test]
fn extract_api_calls_deduplicates_repeated_calls() {
    let source = "\
fn hot_loop() {
    for _ in 0..10 {
        let _ = Parser::new();
        let _ = Parser::new();
    }
    let _ = Parser::new();
}
";
    let hint = super::extract_api_calls_inner(source, 0, source.len())
        .expect("api calls should be produced");
    let first = hint.find("Parser::new").expect("hit");
    let rest = &hint[first + "Parser::new".len()..];
    assert!(
        !rest.contains("Parser::new"),
        "duplicate not deduplicated: {hint}"
    );
}

#[test]
fn extract_api_calls_returns_none_when_body_has_no_type_calls() {
    let source = "\
fn plain() {
    let x = 1;
    let y = x + 2;
}
";
    assert!(super::extract_api_calls_inner(source, 0, source.len()).is_none());
}

#[test]
fn extract_nl_tokens_collects_comments_and_string_literals() {
    // Calls the env-independent inner to avoid racing with other tests
    // that mutate `CODELENS_EMBED_HINT_INCLUDE_COMMENTS`. The gate is
    // covered by `extract_nl_tokens_gated_off_by_default` above.
    let source = "\
fn search_for_matches() {
    // skip comments and string literals during search
    let error = \"failed to open database\";
    let single = \"tok\";
    let path = \"src/foo/bar\";
    let keyword = match kind {
        Kind::Ident => \"detect client version\",
        _ => \"\",
    };
}
";
    // Override the char budget locally so long hints are not truncated
    // before the assertions read them. We use the inner function which
    // still reads `CODELENS_EMBED_HINT_CHARS`, but we do NOT set it —
    // the default 60-char budget is enough for at least the first
    // discriminator to land in the output.
    let hint = super::extract_nl_tokens_inner(source, 0, source.len())
        .expect("nl tokens should be produced");
    // At least one NL-shaped token must land in the hint. The default
    // 60-char budget may truncate later ones; we assert on the first
    // few discriminators only.
    let has_first_nl_signal = hint.contains("skip comments")
        || hint.contains("failed to open")
        || hint.contains("detect client");
    assert!(has_first_nl_signal, "no NL signal produced: {hint}");
    // Short single-token literals must never leak in.
    assert!(!hint.contains(" tok "), "short literal leaked: {hint}");
    // Path literals must never leak in.
    assert!(!hint.contains("src/foo/bar"), "path literal leaked: {hint}");
}

#[test]
fn hint_char_budget_respects_env_override() {
    let previous = std::env::var("CODELENS_EMBED_HINT_CHARS").ok();
    unsafe {
        std::env::set_var("CODELENS_EMBED_HINT_CHARS", "120");
    }
    let budget = super::hint_char_budget();
    unsafe {
        match previous {
            Some(value) => std::env::set_var("CODELENS_EMBED_HINT_CHARS", value),
            None => std::env::remove_var("CODELENS_EMBED_HINT_CHARS"),
        }
    }
    assert_eq!(budget, 120);
}

#[test]
fn embedding_to_bytes_roundtrip() {
    let floats = vec![1.0f32, -0.5, 0.0, 3.25];
    let bytes = embedding_to_bytes(&floats);
    assert_eq!(bytes.len(), 4 * 4);
    // Verify roundtrip
    let recovered: Vec<f32> = bytes
        .chunks_exact(4)
        .map(|chunk| f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]))
        .collect();
    assert_eq!(floats, recovered);
}

#[test]
fn duplicate_pair_key_is_order_independent() {
    let a = duplicate_pair_key("a.py", "foo", "b.py", "bar");
    let b = duplicate_pair_key("b.py", "bar", "a.py", "foo");
    assert_eq!(a, b);
}

#[test]
fn text_embedding_cache_updates_recency() {
    let mut cache = TextEmbeddingCache::new(2);
    cache.insert("a".into(), vec![1.0]);
    cache.insert("b".into(), vec![2.0]);
    assert_eq!(cache.get("a"), Some(vec![1.0]));
    cache.insert("c".into(), vec![3.0]);

    assert_eq!(cache.get("a"), Some(vec![1.0]));
    assert_eq!(cache.get("b"), None);
    assert_eq!(cache.get("c"), Some(vec![3.0]));
}

#[test]
fn text_embedding_cache_can_be_disabled() {
    let mut cache = TextEmbeddingCache::new(0);
    cache.insert("a".into(), vec![1.0]);
    assert_eq!(cache.get("a"), None);
}

#[test]
fn engine_new_and_index() {
    let _lock = MODEL_LOCK.lock().unwrap();
    skip_without_embedding_model!();
    let (_dir, project) = make_project_with_source();
    let engine = EmbeddingEngine::new(&project).expect("engine should load");
    assert!(!engine.is_indexed());

    let count = engine.index_from_project(&project).unwrap();
    assert_eq!(count, 2, "should index 2 symbols");
    assert!(engine.is_indexed());
}

#[test]
fn engine_search_returns_results() {
    let _lock = MODEL_LOCK.lock().unwrap();
    skip_without_embedding_model!();
    let (_dir, project) = make_project_with_source();
    let engine = EmbeddingEngine::new(&project).unwrap();
    engine.index_from_project(&project).unwrap();

    let results = engine.search("hello function", 10).unwrap();
    assert!(!results.is_empty(), "search should return results");
    for r in &results {
        assert!(
            r.score >= -1.0 && r.score <= 1.0,
            "score should be in [-1,1]: {}",
            r.score
        );
    }
}

#[test]
fn engine_incremental_index() {
    let _lock = MODEL_LOCK.lock().unwrap();
    skip_without_embedding_model!();
    let (_dir, project) = make_project_with_source();
    let engine = EmbeddingEngine::new(&project).unwrap();
    engine.index_from_project(&project).unwrap();
    assert_eq!(engine.store.count().unwrap(), 2);

    // Re-index only main.py — should replace its embeddings
    let count = engine.index_changed_files(&project, &["main.py"]).unwrap();
    assert_eq!(count, 2);
    assert_eq!(engine.store.count().unwrap(), 2);
}

#[test]
fn engine_reindex_preserves_symbol_count() {
    let _lock = MODEL_LOCK.lock().unwrap();
    skip_without_embedding_model!();
    let (_dir, project) = make_project_with_source();
    let engine = EmbeddingEngine::new(&project).unwrap();
    engine.index_from_project(&project).unwrap();
    assert_eq!(engine.store.count().unwrap(), 2);

    let count = engine.index_from_project(&project).unwrap();
    assert_eq!(count, 2);
    assert_eq!(engine.store.count().unwrap(), 2);
}

#[test]
fn full_reindex_reuses_unchanged_embeddings() {
    let _lock = MODEL_LOCK.lock().unwrap();
    skip_without_embedding_model!();
    let (_dir, project) = make_project_with_source();
    let engine = EmbeddingEngine::new(&project).unwrap();
    engine.index_from_project(&project).unwrap();

    replace_file_embeddings_with_sentinels(&engine, "main.py", &[("hello", 11.0), ("world", 22.0)]);

    let count = engine.index_from_project(&project).unwrap();
    assert_eq!(count, 2);

    let hello = engine
        .store
        .get_embedding("main.py", "hello")
        .unwrap()
        .expect("hello should exist");
    let world = engine
        .store
        .get_embedding("main.py", "world")
        .unwrap()
        .expect("world should exist");
    assert!(hello.embedding.iter().all(|value| *value == 11.0));
    assert!(world.embedding.iter().all(|value| *value == 22.0));
}

#[test]
fn full_reindex_reuses_unchanged_sibling_after_edit() {
    let _lock = MODEL_LOCK.lock().unwrap();
    skip_without_embedding_model!();
    let (dir, project) = make_project_with_source();
    let engine = EmbeddingEngine::new(&project).unwrap();
    engine.index_from_project(&project).unwrap();

    replace_file_embeddings_with_sentinels(&engine, "main.py", &[("hello", 11.0), ("world", 22.0)]);

    let updated_source =
        "def hello():\n    print('hi')\n\ndef world(name):\n    return name.upper()\n";
    write_python_file_with_symbols(
        dir.path(),
        "main.py",
        updated_source,
        "hash2",
        &[
            ("hello", "def hello():", "hello"),
            ("world", "def world(name):", "world"),
        ],
    );

    let count = engine.index_from_project(&project).unwrap();
    assert_eq!(count, 2);

    let hello = engine
        .store
        .get_embedding("main.py", "hello")
        .unwrap()
        .expect("hello should exist");
    let world = engine
        .store
        .get_embedding("main.py", "world")
        .unwrap()
        .expect("world should exist");
    assert!(hello.embedding.iter().all(|value| *value == 11.0));
    assert!(world.embedding.iter().any(|value| *value != 22.0));
    assert_eq!(engine.store.count().unwrap(), 2);
}

#[test]
fn full_reindex_removes_deleted_files() {
    let _lock = MODEL_LOCK.lock().unwrap();
    skip_without_embedding_model!();
    let (dir, project) = make_project_with_source();
    write_python_file_with_symbols(
        dir.path(),
        "extra.py",
        "def bonus():\n    return 7\n",
        "hash-extra",
        &[("bonus", "def bonus():", "bonus")],
    );

    let engine = EmbeddingEngine::new(&project).unwrap();
    engine.index_from_project(&project).unwrap();
    assert_eq!(engine.store.count().unwrap(), 3);

    std::fs::remove_file(dir.path().join("extra.py")).unwrap();
    let db_path = crate::db::index_db_path(dir.path());
    let db = IndexDb::open(&db_path).unwrap();
    db.delete_file("extra.py").unwrap();

    let count = engine.index_from_project(&project).unwrap();
    assert_eq!(count, 2);
    assert_eq!(engine.store.count().unwrap(), 2);
    assert!(engine
        .store
        .embeddings_for_files(&["extra.py"])
        .unwrap()
        .is_empty());
}

#[test]
fn engine_model_change_recreates_db() {
    let _lock = MODEL_LOCK.lock().unwrap();
    skip_without_embedding_model!();
    let (_dir, project) = make_project_with_source();

    // First engine with default model
    let engine1 = EmbeddingEngine::new(&project).unwrap();
    engine1.index_from_project(&project).unwrap();
    assert_eq!(engine1.store.count().unwrap(), 2);
    drop(engine1);

    // Second engine with same model should preserve data
    let engine2 = EmbeddingEngine::new(&project).unwrap();
    assert!(engine2.store.count().unwrap() >= 2);
}

#[test]
fn inspect_existing_index_returns_model_and_count() {
    let _lock = MODEL_LOCK.lock().unwrap();
    skip_without_embedding_model!();
    let (_dir, project) = make_project_with_source();
    let engine = EmbeddingEngine::new(&project).unwrap();
    engine.index_from_project(&project).unwrap();

    let info = EmbeddingEngine::inspect_existing_index(&project)
        .unwrap()
        .expect("index info should exist");
    assert_eq!(info.model_name, engine.model_name());
    assert_eq!(info.indexed_symbols, 2);
}

#[test]
fn inspect_existing_index_recovers_from_corrupt_db() {
    let (_dir, project) = make_project_with_source();
    let index_dir = project.as_path().join(".codelens/index");
    let db_path = index_dir.join("embeddings.db");
    let wal_path = index_dir.join("embeddings.db-wal");
    let shm_path = index_dir.join("embeddings.db-shm");

    std::fs::write(&db_path, b"not a sqlite database").unwrap();
    std::fs::write(&wal_path, b"bad wal").unwrap();
    std::fs::write(&shm_path, b"bad shm").unwrap();

    let info = EmbeddingEngine::inspect_existing_index(&project).unwrap();
    assert!(info.is_none());

    assert!(db_path.is_file());

    let backup_names: Vec<String> = std::fs::read_dir(&index_dir)
        .unwrap()
        .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
        .filter(|name| name.contains(".corrupt-"))
        .collect();

    assert!(
        backup_names
            .iter()
            .any(|name| name.starts_with("embeddings.db.corrupt-")),
        "expected quarantined embedding db, found {backup_names:?}"
    );
}

#[test]
fn store_can_fetch_single_embedding_without_loading_all() {
    let _lock = MODEL_LOCK.lock().unwrap();
    skip_without_embedding_model!();
    let (_dir, project) = make_project_with_source();
    let engine = EmbeddingEngine::new(&project).unwrap();
    engine.index_from_project(&project).unwrap();

    let chunk = engine
        .store
        .get_embedding("main.py", "hello")
        .unwrap()
        .expect("embedding should exist");
    assert_eq!(chunk.file_path, "main.py");
    assert_eq!(chunk.symbol_name, "hello");
    assert!(!chunk.embedding.is_empty());
}

#[test]
fn find_similar_code_uses_index_and_excludes_target_symbol() {
    let _lock = MODEL_LOCK.lock().unwrap();
    skip_without_embedding_model!();
    let (_dir, project) = make_project_with_source();
    let engine = EmbeddingEngine::new(&project).unwrap();
    engine.index_from_project(&project).unwrap();

    let matches = engine.find_similar_code("main.py", "hello", 5).unwrap();
    assert!(!matches.is_empty());
    assert!(matches
        .iter()
        .all(|m| !(m.file_path == "main.py" && m.symbol_name == "hello")));
}

#[test]
fn delete_by_file_removes_rows_in_one_batch() {
    let _lock = MODEL_LOCK.lock().unwrap();
    skip_without_embedding_model!();
    let (_dir, project) = make_project_with_source();
    let engine = EmbeddingEngine::new(&project).unwrap();
    engine.index_from_project(&project).unwrap();

    let deleted = engine.store.delete_by_file(&["main.py"]).unwrap();
    assert_eq!(deleted, 2);
    assert_eq!(engine.store.count().unwrap(), 0);
}

#[test]
fn store_streams_embeddings_grouped_by_file() {
    let _lock = MODEL_LOCK.lock().unwrap();
    skip_without_embedding_model!();
    let (_dir, project) = make_project_with_source();
    let engine = EmbeddingEngine::new(&project).unwrap();
    engine.index_from_project(&project).unwrap();

    let mut groups = Vec::new();
    engine
        .store
        .for_each_file_embeddings(&mut |file_path, chunks| {
            groups.push((file_path, chunks.len()));
            Ok(())
        })
        .unwrap();

    assert_eq!(groups, vec![("main.py".to_string(), 2)]);
}

#[test]
fn store_fetches_embeddings_for_specific_files() {
    let _lock = MODEL_LOCK.lock().unwrap();
    skip_without_embedding_model!();
    let (_dir, project) = make_project_with_source();
    let engine = EmbeddingEngine::new(&project).unwrap();
    engine.index_from_project(&project).unwrap();

    let chunks = engine.store.embeddings_for_files(&["main.py"]).unwrap();
    assert_eq!(chunks.len(), 2);
    assert!(chunks.iter().all(|chunk| chunk.file_path == "main.py"));
}

#[test]
fn store_fetches_embeddings_for_scored_chunks() {
    let _lock = MODEL_LOCK.lock().unwrap();
    skip_without_embedding_model!();
    let (_dir, project) = make_project_with_source();
    let engine = EmbeddingEngine::new(&project).unwrap();
    engine.index_from_project(&project).unwrap();

    let scored = engine.search_scored("hello world function", 2).unwrap();
    let chunks = engine.store.embeddings_for_scored_chunks(&scored).unwrap();

    assert_eq!(chunks.len(), scored.len());
    assert!(scored.iter().all(|candidate| chunks.iter().any(|chunk| {
        chunk.file_path == candidate.file_path
            && chunk.symbol_name == candidate.symbol_name
            && chunk.line == candidate.line
            && chunk.signature == candidate.signature
            && chunk.name_path == candidate.name_path
    })));
}

#[test]
fn find_misplaced_code_returns_per_file_outliers() {
    let _lock = MODEL_LOCK.lock().unwrap();
    skip_without_embedding_model!();
    let (_dir, project) = make_project_with_source();
    let engine = EmbeddingEngine::new(&project).unwrap();
    engine.index_from_project(&project).unwrap();

    let outliers = engine.find_misplaced_code(5).unwrap();
    assert_eq!(outliers.len(), 2);
    assert!(outliers.iter().all(|item| item.file_path == "main.py"));
}

#[test]
fn find_duplicates_uses_batched_candidate_embeddings() {
    let _lock = MODEL_LOCK.lock().unwrap();
    skip_without_embedding_model!();
    let (_dir, project) = make_project_with_source();
    let engine = EmbeddingEngine::new(&project).unwrap();
    engine.index_from_project(&project).unwrap();

    replace_file_embeddings_with_sentinels(&engine, "main.py", &[("hello", 5.0), ("world", 5.0)]);

    let duplicates = engine.find_duplicates(0.99, 4).unwrap();
    assert!(!duplicates.is_empty());
    assert!(duplicates.iter().any(|pair| {
        (pair.symbol_a == "main.py:hello" && pair.symbol_b == "main.py:world")
            || (pair.symbol_a == "main.py:world" && pair.symbol_b == "main.py:hello")
    }));
}

#[test]
fn search_scored_returns_raw_chunks() {
    let _lock = MODEL_LOCK.lock().unwrap();
    skip_without_embedding_model!();
    let (_dir, project) = make_project_with_source();
    let engine = EmbeddingEngine::new(&project).unwrap();
    engine.index_from_project(&project).unwrap();

    let chunks = engine.search_scored("world function", 5).unwrap();
    assert!(!chunks.is_empty());
    for c in &chunks {
        assert!(!c.file_path.is_empty());
        assert!(!c.symbol_name.is_empty());
    }
}

#[test]
fn configured_embedding_model_name_defaults_to_codesearchnet() {
    assert_eq!(configured_embedding_model_name(), CODESEARCH_MODEL_NAME);
}

#[test]
fn requested_embedding_model_override_ignores_default_model_name() {
    let _lock = MODEL_LOCK.lock().unwrap();
    let previous = std::env::var("CODELENS_EMBED_MODEL").ok();
    unsafe {
        std::env::set_var("CODELENS_EMBED_MODEL", CODESEARCH_MODEL_NAME);
    }

    let result = requested_embedding_model_override().unwrap();

    unsafe {
        match previous {
            Some(value) => std::env::set_var("CODELENS_EMBED_MODEL", value),
            None => std::env::remove_var("CODELENS_EMBED_MODEL"),
        }
    }

    assert_eq!(result, None);
}

#[cfg(not(feature = "model-bakeoff"))]
#[test]
fn requested_embedding_model_override_requires_bakeoff_feature() {
    let _lock = MODEL_LOCK.lock().unwrap();
    let previous = std::env::var("CODELENS_EMBED_MODEL").ok();
    unsafe {
        std::env::set_var("CODELENS_EMBED_MODEL", "all-MiniLM-L12-v2");
    }

    let err = requested_embedding_model_override().unwrap_err();

    unsafe {
        match previous {
            Some(value) => std::env::set_var("CODELENS_EMBED_MODEL", value),
            None => std::env::remove_var("CODELENS_EMBED_MODEL"),
        }
    }

    assert!(err.to_string().contains("model-bakeoff"));
}

#[cfg(feature = "model-bakeoff")]
#[test]
fn requested_embedding_model_override_accepts_alternative_model() {
    let _lock = MODEL_LOCK.lock().unwrap();
    let previous = std::env::var("CODELENS_EMBED_MODEL").ok();
    unsafe {
        std::env::set_var("CODELENS_EMBED_MODEL", "all-MiniLM-L12-v2");
    }

    let result = requested_embedding_model_override().unwrap();

    unsafe {
        match previous {
            Some(value) => std::env::set_var("CODELENS_EMBED_MODEL", value),
            None => std::env::remove_var("CODELENS_EMBED_MODEL"),
        }
    }

    assert_eq!(result.as_deref(), Some("all-MiniLM-L12-v2"));
}

#[test]
fn recommended_embed_threads_caps_macos_style_load() {
    let threads = recommended_embed_threads();
    assert!(threads >= 1);
    assert!(threads <= 8);
}

#[test]
fn embed_batch_size_has_safe_default_floor() {
    assert!(embed_batch_size() >= 1);
    if cfg!(target_os = "macos") {
        assert!(embed_batch_size() <= DEFAULT_MACOS_EMBED_BATCH_SIZE);
    }
}