leindex 2.1.1

LeIndex MCP and semantic code search engine for AI tools and large codebases
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
//! Structural (AST-pattern) search — end-to-end tests.
//!
//! Covers the four layers a caller can hit:
//! * the engine ([`search`]) over real files on disk,
//! * language resolution (`resolve_language`, `language_for_path`),
//! * the MCP branch's gating (`unavailable_reason`) and its JSON shape,
//! * the `leindex_explore` router entry and the payload trimmer.
//!
//! ## The expando regression
//!
//! `test_metavariables_match_in_dollar_hostile_languages` is the reason the
//! module rewrites `$` to `µ` before parsing: Rust, Python and Go lexers cannot
//! start an identifier with `$`, so a pattern written with literal `$` parses
//! into error nodes and matches nothing. That test fails if anyone "simplifies"
//! the expando away.

#![cfg(feature = "structural-search")]

use leindex::cli::mcp::output::trim::trim_llm_payload;
use leindex::cli::mcp::structural_handler::StructuralHandler;
use leindex::feature_flags::{FeatureFlag, with_flag_override};
use leindex::search::textsearch::{
    CompiledPattern, FileFilter, NO_CAP, StructuralError, StructuralOptions, StructuralOutput,
    language_for_path, resolve_language, search_structural,
};
use serde_json::{Value, json};
use std::fs;
use std::path::Path;

// =============================================================================
// Fixtures
// =============================================================================

/// A small tree with two Rust functions, two Python functions, a Go function
/// and a Rust test file — enough to tell scope, globs and caps apart.
fn fixture() -> tempfile::TempDir {
    let dir = tempfile::tempdir().expect("tempdir");
    write(
        dir.path(),
        "src/alpha.rs",
        "fn alpha() {}\n\nfn beta(x: i32) -> i32 {\n    x + 1\n}\n",
    );
    write(
        dir.path(),
        "src/deep/alpha_extra.rs",
        "fn gamma() {\n    println!(\"gamma\");\n}\n",
    );
    write(dir.path(), "src/alpha_test.rs", "fn delta() {}\n");
    write(
        dir.path(),
        "py/mod.py",
        "def one():\n    return 1\n\ndef two(a, b):\n    return a + b\n",
    );
    write(
        dir.path(),
        "go/main.go",
        "package main\n\nfunc main() {\n}\n",
    );
    write(dir.path(), "README.md", "# not code\n");
    dir
}

fn write(root: &Path, rel: &str, contents: &str) {
    let path = root.join(rel);
    fs::create_dir_all(path.parent().expect("parent")).expect("mkdir");
    fs::write(&path, contents).expect("write fixture");
}

/// Make `root` a git worktree.
///
/// Inventory is `git ls-files` when a repository exists and a filesystem walk
/// otherwise, so a test that cares *which* files were enumerated has to pin the
/// repository state or it silently exercises the other path.
///
/// The two branches differ in what they BOUND, not in whether they honour the
/// deadline — both do, at every stage. The walk additionally takes a work budget
/// (`WORK_BUDGET_PER_ADMITTED_FILE` files seen per admitted file); the git branch
/// takes only `limit`, because `git ls-files` hands over a whole sorted listing
/// in one subprocess. So a test about the work budget must NOT be a repository,
/// and a test about the git path must be one.
fn git_init(root: &Path) {
    std::process::Command::new("git")
        .args(["init", "-q"])
        .current_dir(root)
        .status()
        .expect("git init")
        .success()
        .then_some(())
        .expect("git init failed");
}

fn filter(include: &[&str], exclude: &[&str], scope: Option<&str>) -> FileFilter {
    let owned = |items: &[&str]| items.iter().map(|s| s.to_string()).collect::<Vec<_>>();
    FileFilter::new(&owned(include), &owned(exclude), scope)
}

fn options(limit: usize, per_file_cap: usize) -> StructuralOptions {
    StructuralOptions {
        limit,
        per_file_cap,
        ..StructuralOptions::default()
    }
}

/// Every reported hit as `(path, line)`.
fn hits(output: &StructuralOutput) -> Vec<(String, u32)> {
    let mut all = Vec::new();
    for file in &output.files {
        for hit in &file.hits {
            all.push((file.rel.clone(), hit.line));
        }
    }
    all
}

// =============================================================================
// Language resolution
// =============================================================================

#[test]
fn test_resolve_language_accepts_name_display_name_and_extension() {
    for spelling in ["rust", "Rust", "rs"] {
        assert_eq!(
            resolve_language(spelling),
            Some(leindex::parse::grammar::LanguageId::Rust),
            "{spelling} should resolve to Rust"
        );
    }
    for spelling in ["python", "py", "Python"] {
        assert_eq!(
            resolve_language(spelling),
            Some(leindex::parse::grammar::LanguageId::Python),
            "{spelling} should resolve to Python"
        );
    }
    assert_eq!(
        resolve_language("  go  "),
        Some(leindex::parse::grammar::LanguageId::Go)
    );
    assert_eq!(resolve_language(""), None);
    assert_eq!(resolve_language("not-a-language"), None);
}

#[test]
fn test_document_flavours_are_not_structural_languages() {
    // Markdown/Rst/Adoc/PlainText resolve (they are real LanguageIds) but have
    // no AST: matching them would hand ast-grep a stand-in grammar.
    assert!(resolve_language("markdown").is_some());
    assert_eq!(language_for_path(Path::new("README.md")), None);
    assert_eq!(language_for_path(Path::new("notes.txt")), None);
    assert_eq!(
        language_for_path(Path::new("src/lib.rs")),
        Some(leindex::parse::grammar::LanguageId::Rust)
    );
    assert_eq!(language_for_path(Path::new("no-extension")), None);
    // Filename-based languages resolve before the extension probe: an
    // extensionless Makefile has no extension to read, and CMakeLists.txt
    // carries a .txt suffix that would otherwise resolve to a document
    // flavour (and therefore None).
    assert_eq!(
        language_for_path(Path::new("Makefile")),
        Some(leindex::parse::grammar::LanguageId::Make)
    );
    assert_eq!(
        language_for_path(Path::new("build/Makefile")),
        Some(leindex::parse::grammar::LanguageId::Make)
    );
    assert_eq!(
        language_for_path(Path::new("makefile")),
        Some(leindex::parse::grammar::LanguageId::Make)
    );
    assert_eq!(
        language_for_path(Path::new("GNUmakefile")),
        Some(leindex::parse::grammar::LanguageId::Make)
    );
    assert_eq!(
        language_for_path(Path::new("CMakeLists.txt")),
        Some(leindex::parse::grammar::LanguageId::Cmake)
    );
    // ...but an ordinary .txt file must NOT be hijacked by the filename probe.
    assert_eq!(
        language_for_path(Path::new("docs/CMakeLists.txt.bak")),
        None
    );

    let err = CompiledPattern::new("markdown", "# heading").unwrap_err();
    assert!(
        matches!(err, StructuralError::GrammarUnavailable(_)),
        "expected GrammarUnavailable, got {err:?}"
    );
    match err {
        StructuralError::GrammarUnavailable(name) => assert_eq!(name, "Markdown"),
        other => panic!("unexpected error: {other:?}"),
    }
}

// =============================================================================
// Pattern compilation
// =============================================================================

#[test]
fn test_pattern_compiles_for_rust_and_python() {
    let rust = CompiledPattern::new("rust", "fn $NAME() { $$$BODY }").expect("rust pattern");
    assert_eq!(rust.language(), "Rust");

    let python = CompiledPattern::new("py", "def $NAME($$$ARGS): $$$BODY").expect("python pattern");
    assert_eq!(python.language(), "Python");

    // Go needs a receiver-free function to be a single node.
    let go = CompiledPattern::new("go", "func $NAME() { $$$BODY }").expect("go pattern");
    assert_eq!(go.language(), "Go");
}

#[test]
fn test_unsupported_and_unusable_patterns_are_rejected() {
    let err = CompiledPattern::new("brainfuck", "fn $A() {}").unwrap_err();
    assert_eq!(
        err,
        StructuralError::UnsupportedLanguage("brainfuck".to_string())
    );
    assert!(err.to_string().contains("does not support language"));

    let empty = CompiledPattern::new("rust", "   ").unwrap_err();
    assert!(matches!(empty, StructuralError::UnusablePattern(_)));

    let huge = "fn $A() {}".repeat(leindex::search::textsearch::structural::MAX_PATTERN_BYTES);
    let too_big = CompiledPattern::new("rust", &huge).unwrap_err();
    assert!(
        matches!(too_big, StructuralError::UnusablePattern(ref d) if d.contains("maximum")),
        "expected size error, got {too_big:?}"
    );

    // Two top-level nodes: a pattern must name one node to match one node.
    // (Note what is *not* rejected: tree-sitter's error recovery turns
    // syntactically broken source like `fn ( {` into a single ERROR node, so
    // it compiles and then simply matches nothing. That is ast-grep's
    // behaviour, not a gate we can add without re-parsing the pattern.)
    let multi = CompiledPattern::new("rust", "fn a() {}\nfn b() {}").unwrap_err();
    assert!(
        matches!(multi, StructuralError::InvalidPattern { .. }),
        "expected InvalidPattern, got {multi:?}"
    );
    assert!(
        multi.to_string().contains("not valid Rust code"),
        "error should name the language: {multi}"
    );
}

// =============================================================================
// Matching
// =============================================================================

#[test]
fn test_matches_rust_functions_in_fixture_tree() {
    let dir = fixture();
    let pattern = CompiledPattern::new("rust", "fn $NAME() { $$$BODY }").expect("pattern");
    let output = search_structural(
        dir.path(),
        &pattern,
        &FileFilter::default(),
        &options(100, NO_CAP),
    );

    let found = hits(&output);
    // `alpha` and `delta` take no arguments; `beta` does, `gamma` has a body
    // but the pattern's `$$$BODY` covers it, so gamma matches too.
    assert!(
        found.contains(&("src/alpha.rs".to_string(), 1)),
        "line 1 of src/alpha.rs (fn alpha) missing from {found:?}"
    );
    assert!(
        found.iter().any(|(path, _)| path == "src/alpha_test.rs"),
        "test-file function should match: {found:?}"
    );
    assert_eq!(found.len(), 3, "expected alpha, delta and gamma: {found:?}");
    assert_eq!(output.returned, 3);
    assert_eq!(output.stats.total_matches, 3);
    assert_eq!(output.stats.files_matched, 3);
    assert!(!output.has_more);

    let alpha = output
        .files
        .iter()
        .find(|f| f.rel == "src/alpha.rs")
        .expect("src/alpha.rs in results");
    assert_eq!(alpha.lang, "Rust");
    assert_eq!(alpha.hits[0].kind, "function_item");
    assert_eq!(alpha.hits[0].line, 1);
    assert_eq!(alpha.hits[0].col, 1);
    assert_eq!(alpha.hits[0].text, "fn alpha() {}");
    assert_eq!(alpha.hits[0].capture.as_deref(), Some("NAME"));
    assert_eq!(alpha.hits[0].captured.as_deref(), Some("alpha"));
    assert!(alpha.hits[0].start_byte < alpha.hits[0].end_byte);
}

#[test]
fn test_matches_python_functions_in_fixture_tree() {
    let dir = fixture();
    let pattern = CompiledPattern::new("python", "def $NAME($$$ARGS): $$$BODY").expect("pattern");
    let output = search_structural(
        dir.path(),
        &pattern,
        &FileFilter::default(),
        &options(100, NO_CAP),
    );

    let found = hits(&output);
    assert_eq!(found.len(), 2, "one and two: {found:?}");
    let module = output
        .files
        .iter()
        .find(|f| f.rel == "py/mod.py")
        .expect("py/mod.py in results");
    assert_eq!(module.lang, "Python");
    assert_eq!(module.hits[0].kind, "function_definition");
    assert_eq!(module.hits[0].captured.as_deref(), Some("one"));
    assert_eq!(module.hits[1].captured.as_deref(), Some("two"));
}

#[test]
fn test_metavariables_match_in_dollar_hostile_languages() {
    // The regression guard for the expando char: `$` alone yields ZERO hits in
    // these grammars because their lexers cannot start an identifier with `$`.
    let dir = tempfile::tempdir().expect("tempdir");
    write(dir.path(), "a.rs", "fn one() {\n    body();\n}\n");
    write(dir.path(), "b.py", "def one():\n    return 1\n");
    write(dir.path(), "c.go", "package main\n\nfunc one() {\n}\n");

    let cases = [
        ("rust", "fn $NAME() { $$$BODY }", "a.rs"),
        ("python", "def $NAME(): $$$BODY", "b.py"),
        ("go", "func $NAME() { $$$BODY }", "c.go"),
    ];
    for (lang, pattern_text, file) in cases {
        let pattern = CompiledPattern::new(lang, pattern_text).expect(pattern_text);
        let output = search_structural(
            dir.path(),
            &pattern,
            &FileFilter::default(),
            &options(100, NO_CAP),
        );
        assert_eq!(
            output.stats.total_matches,
            1,
            "{lang} pattern {pattern_text:?} should match {file} via the expando char, got {:?}",
            hits(&output)
        );
        assert_eq!(
            output.files[0].hits[0].captured.as_deref(),
            Some("one"),
            "{lang}: $NAME should capture the function name"
        );
    }
}

// =============================================================================
// Filtering
// =============================================================================

#[test]
fn test_scope_confines_the_search() {
    let dir = fixture();
    let pattern = CompiledPattern::new("rust", "fn $NAME() { $$$BODY }").expect("pattern");
    let output = search_structural(
        dir.path(),
        &pattern,
        &filter(&[], &[], Some("src/deep")),
        &options(100, NO_CAP),
    );

    assert_eq!(
        hits(&output),
        vec![("src/deep/alpha_extra.rs".to_string(), 1)]
    );
    assert_eq!(output.stats.candidates, 1);
}

#[test]
fn test_include_and_exclude_globs_filter_paths() {
    let dir = fixture();
    let pattern = CompiledPattern::new("rust", "fn $NAME() { $$$BODY }").expect("pattern");

    let included = search_structural(
        dir.path(),
        &pattern,
        &filter(&["src/**/*.rs"], &["*_test.rs"], None),
        &options(100, NO_CAP),
    );
    let found = hits(&included);
    assert!(
        found.iter().all(|(path, _)| !path.ends_with("_test.rs")),
        "exclude glob leaked test files: {found:?}"
    );
    assert!(
        found
            .iter()
            .any(|(path, _)| path == "src/deep/alpha_extra.rs"),
        "include glob should still admit nested sources: {found:?}"
    );
    assert_eq!(found.len(), 2, "alpha + gamma: {found:?}");

    let excluded_dir = search_structural(
        dir.path(),
        &pattern,
        &filter(&[], &["src/deep/"], None),
        &options(100, NO_CAP),
    );
    assert!(
        hits(&excluded_dir)
            .iter()
            .all(|(path, _)| path != "src/deep/alpha_extra.rs"),
        "directory exclude ignored"
    );
}

// =============================================================================
// Caps, paging and limits
// =============================================================================

#[test]
fn test_per_file_cap_withholds_matches_and_reports_them() {
    let dir = tempfile::tempdir().expect("tempdir");
    let body: String = (0..6).map(|i| format!("fn f{i}() {{}}\n")).collect();
    write(dir.path(), "many.rs", &body);
    let pattern = CompiledPattern::new("rust", "fn $NAME() { $$$BODY }").expect("pattern");

    let capped = search_structural(
        dir.path(),
        &pattern,
        &FileFilter::default(),
        &options(100, 2),
    );
    assert_eq!(capped.files[0].hits.len(), 2, "cap limits reported hits");
    assert!(capped.files[0].capped, "file must be flagged as capped");
    assert_eq!(capped.returned, 2);
    // Matches withheld by the cap appear on no page, so they are not "more".
    assert!(
        !capped.has_more,
        "cap-withheld matches must not imply has_more"
    );
    assert_eq!(
        capped.stats.total_matches, 6,
        "totals still count every match"
    );
    assert_eq!(capped.stats.files_capped, 1);

    let uncapped = search_structural(
        dir.path(),
        &pattern,
        &FileFilter::default(),
        &options(100, NO_CAP),
    );
    assert_eq!(uncapped.files[0].hits.len(), 6);
    assert!(!uncapped.files[0].capped);
}

#[test]
fn test_limit_and_offset_page_deterministically() {
    let dir = tempfile::tempdir().expect("tempdir");
    for (i, name) in ["a", "b", "c"].iter().enumerate() {
        write(
            dir.path(),
            &format!("{name}.rs"),
            &format!("fn f{i}() {{}}\n"),
        );
    }
    let pattern = CompiledPattern::new("rust", "fn $NAME() { $$$BODY }").expect("pattern");

    let first = search_structural(
        dir.path(),
        &pattern,
        &FileFilter::default(),
        &options(2, NO_CAP),
    );
    assert_eq!(first.returned, 2);
    assert!(first.has_more, "3 matches with limit 2 means more exist");
    assert_eq!(
        first
            .files
            .iter()
            .map(|f| f.rel.as_str())
            .collect::<Vec<_>>(),
        vec!["a.rs", "b.rs"]
    );

    let second = search_structural(
        dir.path(),
        &pattern,
        &FileFilter::default(),
        &StructuralOptions {
            offset: 2,
            ..options(2, NO_CAP)
        },
    );
    assert_eq!(second.returned, 1);
    assert!(!second.has_more);
    assert_eq!(
        second
            .files
            .iter()
            .map(|f| f.rel.as_str())
            .collect::<Vec<_>>(),
        vec!["c.rs"]
    );
    // Totals describe the whole tree, not just the page.
    assert_eq!(second.stats.total_matches, 3);
}

#[test]
fn test_zero_limit_returns_totals_without_hits() {
    let dir = fixture();
    let pattern = CompiledPattern::new("rust", "fn $NAME() { $$$BODY }").expect("pattern");
    let output = search_structural(
        dir.path(),
        &pattern,
        &FileFilter::default(),
        &options(0, NO_CAP),
    );

    assert_eq!(output.returned, 0);
    assert!(output.files.is_empty());
    assert_eq!(output.stats.total_matches, 3);
    assert!(output.has_more, "everything is still ahead of offset 0");
}

/// A multi-variable capture must report what it captured when it is the
/// pattern's first (here: only) capture — `alpha($$$ARGS)`. ast-grep stores
/// multi-capture nodes in a separate map from single captures, so reading
/// only `get_match` reported `capture: "ARGS"` with `captured` permanently
/// `None` even when arguments were matched (Codex review, P2).
#[test]
fn test_multi_capture_reports_the_captured_args() {
    let dir = fixture();
    write(dir.path(), "calls.rs", "fn one() {\n    alpha(1, 2);\n}\n");
    let pattern = CompiledPattern::new("rust", "alpha($$$ARGS)").expect("pattern");
    let output = search_structural(
        dir.path(),
        &pattern,
        &FileFilter::default(),
        &options(100, NO_CAP),
    );

    let file = output
        .files
        .iter()
        .find(|file| file.rel == "calls.rs")
        .expect("calls.rs must be in the results");
    assert_eq!(file.hits.len(), 1);
    let hit = &file.hits[0];
    assert_eq!(hit.capture.as_deref(), Some("ARGS"));
    let captured = hit
        .captured
        .as_deref()
        .expect("$$$ARGS matched arguments, so captured must be present");
    assert_eq!(captured, "1, 2", "captured must hold the matched arguments");
}

/// The complement of the expired-deadline test, and the guard that catches the
/// obvious way to break the fix: enforcing a deadline in the git branch must not
/// cost a live search its candidates. With a deadline that has NOT expired, a
/// repository fixture must still enumerate its Rust files — which is where they
/// came from all along, and what the previous version of the test above quietly
/// depended on.
#[test]
fn test_live_deadline_keeps_git_inventory_candidates() {
    let dir = fixture();
    git_init(dir.path());
    let pattern = CompiledPattern::new("rust", "fn $NAME() { $$$BODY }").expect("pattern");
    let output = search_structural(
        dir.path(),
        &pattern,
        &FileFilter::default(),
        &StructuralOptions {
            deadline: Some(std::time::Instant::now() + std::time::Duration::from_secs(60)),
            ..options(100, NO_CAP)
        },
    );

    assert!(
        output.stats.candidates > 0,
        "an unexpired deadline must not cost the git inventory its listing"
    );
    assert!(
        output.stats.scanned > 0,
        "the candidates must actually be parsed when time remains"
    );
    assert_eq!(output.stats.skipped_deadline, 0);
    assert!(!output.stopped_by_deadline);
}

/// An already-expired deadline must not report a clean scan. The specific
/// `skipped_deadline == candidates` shape is NOT asserted here: with the deadline
/// expired before inventory begins, no candidates are produced at all, so that
/// comparison is `0 == 0` and pins nothing. The scan-loop half of the accounting
/// — a candidate the worker declines to open — needs candidates that survive the
/// inventory, which a deadline this early cannot provide; the inventory module
/// covers the deadline paths directly instead.
#[test]
fn test_expired_deadline_reports_partial_not_complete() {
    let dir = fixture();
    git_init(dir.path());
    let pattern = CompiledPattern::new("rust", "fn $NAME() { $$$BODY }").expect("pattern");
    let output = search_structural(
        dir.path(),
        &pattern,
        &FileFilter::default(),
        &StructuralOptions {
            deadline: Some(std::time::Instant::now()),
            ..options(100, NO_CAP)
        },
    );

    assert_eq!(output.returned, 0);
    assert_eq!(
        output.stats.scanned, 0,
        "nothing was opened, and nothing may be counted as if it were"
    );
    assert!(
        output.has_more,
        "an unexamined file may hold matches, so the result cannot claim to be complete"
    );
    // `stopped_by_deadline` may only claim time pressure, and here it must: the
    // deadline is genuinely expired at every stage. It must never be inferred
    // from a file-count limit — see `test_truncated_inventory_forces_has_more`.
    assert!(output.stopped_by_deadline);
}

/// The scan-loop half of the deadline accounting, which the expired-deadline
/// test above structurally cannot cover: its deadline is already expired when
/// the inventory runs, so no candidate survives to reach a worker. Here the
/// deadline is LIVE at inventory time and tight enough that it can expire
/// while workers are still opening files — the exact shape the accounting
/// exists for.
///
/// Which side of that race a run lands on is machine speed, so the assertion
/// is the INVARIANT rather than a particular outcome: every candidate was
/// either scanned or skipped on the deadline, and a run that did skip must
/// say so through `stopped_by_deadline` and `has_more`. Asserting
/// `skipped_deadline > 0` outright would re-introduce the timing flake this
/// branch just removed from the inventory suite; a loaded CI runner lands on
/// the skip side regularly, and the invariant is checked on both sides.
#[test]
fn test_scan_loop_deadline_accounting_sums_to_candidates() {
    let dir = tempfile::tempdir().expect("tempdir");
    // Enough files that a scan can plausibly outlive the deadline on a busy
    // runner; small enough that creating them stays cheap.
    for i in 0..4000 {
        write(dir.path(), &format!("f{i:04}.rs"), "fn one() {}\n");
    }
    let pattern = CompiledPattern::new("rust", "fn $NAME() { $$$BODY }").expect("pattern");
    let output = search_structural(
        dir.path(),
        &pattern,
        &FileFilter::default(),
        &StructuralOptions {
            deadline: Some(std::time::Instant::now() + std::time::Duration::from_millis(200)),
            ..options(100, NO_CAP)
        },
    );

    assert!(
        output.stats.candidates > 0,
        "the live deadline must not starve the inventory"
    );
    assert_eq!(
        output.stats.scanned + output.stats.skipped_deadline,
        output.stats.candidates,
        "every candidate is accounted for: opened and scanned, or skipped on \
         the deadline — never silently dropped"
    );
    if output.stats.skipped_deadline > 0 {
        assert!(
            output.stopped_by_deadline,
            "files skipped on the deadline are exactly what justifies the claim"
        );
        assert!(
            output.has_more,
            "skipped files may hold matches, so the result cannot claim completeness"
        );
    }
}

/// A candidate that cannot be READ is not a candidate that cannot MATCH.
///
/// `has_more` used to count only deadline skips and listing truncations, so a
/// file the process could not open — denied permissions, transient I/O — was
/// dropped from the scan and the search still answered `has_more: false`. A
/// caller reads that as "the tree was searched and holds nothing", which is
/// false: those contents were never examined (Codex P2).
///
/// The same change must NOT count policy refusals, or every tree containing a
/// binary would pin `has_more` forever. Both halves are asserted here on one
/// tree: one readable file with a match, one unreadable `.rs`, one binary `.rs`.
#[test]
#[cfg(unix)]
fn test_unreadable_candidate_makes_the_result_incomplete() {
    use std::os::unix::fs::PermissionsExt;

    let dir = tempfile::tempdir().expect("tempdir");
    write(dir.path(), "src/ok.rs", "fn visible() {}\n");
    // Binary: byte 0 is NUL inside the sniff window, so the engine refuses it by
    // POLICY. It must not contribute to completeness.
    fs::write(dir.path().join("src/blob.rs"), b"\0binary\x01payload").expect("write blob");
    // Readable metadata, unreadable bytes: chmod 000 means `fs::read` fails with
    // EACCES while the walk still enumerates the path as a candidate.
    let denied = dir.path().join("src/denied.rs");
    fs::write(&denied, "fn hidden() {}\n").expect("write denied");
    fs::set_permissions(&denied, fs::Permissions::from_mode(0o000)).expect("chmod");

    // Prove the denial BEFORE running the search, while the mode is still 000:
    // checking after a restore would only prove the file is readable again.
    assert!(
        fs::read(&denied).is_err(),
        "chmod 000 must actually deny the read, or this test is vacuous \
         (a privileged user ignores the permission bits)"
    );

    let pattern = CompiledPattern::new("rust", "fn $NAME() { $$$BODY }").expect("pattern");
    let output = search_structural(
        dir.path(),
        &pattern,
        &FileFilter::default(),
        &options(100, NO_CAP),
    );

    // Restore right after the scan so a failure below cannot leave a file the
    // test harness struggles to remove.
    fs::set_permissions(&denied, fs::Permissions::from_mode(0o644)).expect("restore chmod");

    assert_eq!(
        output.stats.unreadable, 1,
        "the unreadable candidate must be classified as unreadable, not as a \
         policy refusal: stats {:?}",
        output.stats
    );
    assert!(
        output.has_more,
        "a file the search could not open may hold matches, so the result must \
         not claim completeness"
    );
    assert!(
        !output.stopped_by_deadline,
        "an I/O failure is not time pressure: stopped_by_deadline may only cite \
         the deadline"
    );
    assert_eq!(
        output.stats.skipped_deadline, 0,
        "no candidate was skipped on the deadline"
    );
    assert_eq!(
        output.stats.candidates, 3,
        "readable, binary and unreadable were all enumerated"
    );
    // The readable file still contributes normally, so the fix did not turn the
    // whole scan into a failure.
    assert_eq!(output.stats.scanned, 1, "only the readable file was parsed");
    assert_eq!(
        hits(&output),
        vec![("src/ok.rs".to_string(), 1)],
        "the readable match must still be reported"
    );
}

/// The mirror half: policy refusals alone must leave `has_more` false.
///
/// Without this, "count unreadable files" could be implemented as "count
/// everything `read_scannable` refused", which pins `has_more` on any tree that
/// contains a single binary or non-UTF-8 file.
#[test]
fn test_policy_refusals_do_not_claim_incompleteness() {
    let dir = tempfile::tempdir().expect("tempdir");
    write(dir.path(), "src/ok.rs", "fn visible() {}\n");
    fs::write(dir.path().join("src/blob.rs"), b"\0binary\x01payload").expect("write blob");
    // Valid bytes, invalid UTF-8: read fine, but unstoppable by ast-grep.
    fs::write(dir.path().join("src/latin1.rs"), [0xFF, 0xFE, 0x41]).expect("write latin1");

    let pattern = CompiledPattern::new("rust", "fn $NAME() { $$$BODY }").expect("pattern");
    let output = search_structural(
        dir.path(),
        &pattern,
        &FileFilter::default(),
        &options(100, NO_CAP),
    );

    assert_eq!(
        output.stats.unreadable, 0,
        "policy refusals are not read failures: stats {:?}",
        output.stats
    );
    assert!(
        !output.has_more,
        "no pattern can match a binary or non-UTF-8 file, so a clean page is \
         complete — counting them would pin has_more on every such tree"
    );
    assert_eq!(
        hits(&output),
        vec![("src/ok.rs".to_string(), 1)],
        "the readable match is still reported"
    );
}

/// The per-candidate deadline checks run BETWEEN scans, so a worker that starts
/// just before expiry still runs its full read + parse to completion — rayon
/// cannot cancel it and ast-grep exposes no cancellation hook. A collect that
/// FINISHES past the deadline is therefore the only observable proof the
/// advertised wall-clock budget was crossed, and it must be reported even when
/// no candidate happened to land on a skip check after expiry (Codex P2).
///
/// One ~5.4 MiB candidate that parses in seconds (measured ~2.8 s in a debug
/// build) against a 200 ms budget: enumeration, the read and both per-candidate
/// checks finish far inside the budget, and only the parse crosses it. The
/// budget deliberately sits well ABOVE inventory cost — the earlier 10 ms budget
/// could expire during enumeration, which yielded zero candidates and made the
/// candidate-count assertion flake while `stopped_by_deadline` stayed true via
/// the walk, i.e. the test passed without ever reaching the parser it exists to
/// pin.
///
/// `stats.scanned == 1` is what keeps the claim honest: both per-candidate checks
/// saw a live clock, so the file really was parsed and the assertions below can
/// only be satisfied by the finished-late collect. A deadline skip reports
/// `scanned == 0` and fails right there, instead of passing vacuously.
#[test]
fn test_scan_finishing_past_deadline_is_reported() {
    let dir = tempfile::tempdir().expect("tempdir");
    let big = format!("fn seed() {{}}\n{}", "// pad\n".repeat(900_000));
    write(dir.path(), "big.rs", &big);
    let pattern = CompiledPattern::new("rust", "fn $NAME() { $$$BODY }").expect("pattern");
    let output = search_structural(
        dir.path(),
        &pattern,
        &FileFilter::default(),
        &StructuralOptions {
            deadline: Some(std::time::Instant::now() + std::time::Duration::from_millis(200)),
            ..options(100, NO_CAP)
        },
    );

    assert_eq!(
        output.stats.candidates, 1,
        "the budget must not starve the inventory of its single candidate; zero \
         candidates is an inventory truncation, which this test does not cover"
    );
    assert_eq!(
        output.stats.scanned, 1,
        "a deadline skip here means the parse never ran, so nothing below would \
         prove the finished-late collect"
    );
    assert!(
        output.stopped_by_deadline,
        "a scan that crossed the deadline must say so — via skips, or via the \
         finished-late collect when the parse ran unguarded past expiry"
    );
    assert!(output.has_more, "a partial scan cannot claim completeness");
}

/// A deadline that expires while the *inventory* is being walked must also
/// report a partial result. `walk_inventory` stops at the deadline and returns
/// whatever it had, so a non-git tree can yield zero candidates and
/// `has_more: false` — a clean-looking "nothing matched" for a scan that never
/// ran. `list_files_reporting` carries that truncation out so `search` can
/// refuse to claim completeness (Codex P2).
#[test]
fn test_truncated_inventory_forces_has_more() {
    let dir = tempfile::tempdir().expect("tempdir");
    write(dir.path(), "src/one.rs", "fn one() {}\n");
    // Deliberately NOT a repository: this is the walk path.
    let pattern = CompiledPattern::new("rust", "fn $NAME() { $$$BODY }").expect("pattern");
    let output = search_structural(
        dir.path(),
        &pattern,
        &FileFilter::default(),
        &StructuralOptions {
            deadline: Some(std::time::Instant::now()),
            ..options(100, NO_CAP)
        },
    );

    assert_eq!(
        output.stats.candidates, 0,
        "the expired deadline stops the walk before it enumerates anything"
    );
    assert_eq!(output.returned, 0);
    assert!(output.stopped_by_deadline);
    assert!(
        output.has_more,
        "an inventory that was cut short cannot be reported as a complete scan"
    );
}

#[test]
fn test_no_candidates_is_an_empty_success() {
    let dir = tempfile::tempdir().expect("tempdir");
    write(dir.path(), "only.py", "def one():\n    return 1\n");
    let pattern = CompiledPattern::new("rust", "fn $NAME() { $$$BODY }").expect("pattern");
    let output = search_structural(
        dir.path(),
        &pattern,
        &FileFilter::default(),
        &options(100, NO_CAP),
    );

    assert_eq!(output.returned, 0);
    assert_eq!(
        output.stats.scanned, 0,
        "no .rs files, so nothing was parsed"
    );
    assert_eq!(output.stats.total_matches, 0);
    assert!(!output.has_more);
    assert!(!output.stopped_by_deadline);
}

#[test]
fn test_scanned_counts_successful_reads_including_files_without_matches() {
    let dir = tempfile::tempdir().expect("tempdir");
    write(dir.path(), "hit.rs", "fn found() {}\n");
    write(dir.path(), "miss.rs", "struct NoMatch;\n");
    std::fs::write(dir.path().join("binary.rs"), b"\0binary").unwrap();
    let pattern = CompiledPattern::new("rust", "fn $NAME() { $$$BODY }").expect("pattern");
    let output = search_structural(
        dir.path(),
        &pattern,
        &FileFilter::default(),
        &options(100, NO_CAP),
    );
    assert_eq!(output.stats.candidates, 3);
    assert_eq!(output.stats.scanned, 2);
    assert_eq!(output.returned, 1);
    assert!(!output.has_more);
    assert!(!output.stopped_by_deadline);
}

// =============================================================================
// MCP gate and shape (through the real `execute` path)
// =============================================================================

fn registry() -> std::sync::Arc<leindex::cli::registry::ProjectRegistry> {
    std::sync::Arc::new(leindex::cli::registry::ProjectRegistry::new(4))
}

/// The gate is a hard error that says *why* and that nothing ran — never an
/// empty result set the caller could mistake for "the code does not exist".
#[test]
fn test_disabled_flag_fails_with_actionable_error() {
    with_flag_override(FeatureFlag::StructuralSearch, false, || {
        let err = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(2)
            .enable_all()
            .build()
            .expect("runtime")
            .block_on(StructuralHandler.execute(
                &registry(),
                json!({
                    "pattern": "fn $NAME() { $$$BODY }",
                    "project_path": "/tmp"
                }),
            ))
            .expect_err("flag off must fail, not return an empty result");

        assert_eq!(err.code, -32602, "invalid_params");
        assert!(
            err.message.contains("unavailable"),
            "message must say the tool is unavailable: {}",
            err.message
        );
        assert!(
            err.message.contains("LEINDEX_FEATURE_STRUCTURAL_SEARCH=1"),
            "message must name the env var to set: {}",
            err.message
        );
        let hint = err.suggestion().expect("gate error carries a hint");
        assert!(
            hint.contains("NOT searched"),
            "hint must state the pattern was not searched: {hint}"
        );
        assert!(
            hint.contains("no file was scanned"),
            "hint must state nothing was scanned: {hint}"
        );
        assert!(
            hint.contains("fn $NAME() { $$$BODY }"),
            "hint must echo the pattern back: {hint}"
        );
    });
}

#[test]
fn test_enabled_handler_searches_a_real_project() {
    let dir = fixture();
    with_flag_override(FeatureFlag::StructuralSearch, true, || {
        let value = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(2)
            .enable_all()
            .build()
            .expect("runtime")
            .block_on(StructuralHandler.execute(
                &registry(),
                json!({
                    "pattern": "fn $NAME() { $$$BODY }",
                    "lang": "rust",
                    "project_path": dir.path()
                }),
            ))
            .expect("structural search should succeed with the gate open");

        assert_eq!(value["mode"], "structural");
        assert_eq!(value["pattern"], "fn $NAME() { $$$BODY }");
        assert_eq!(value["lang"], "Rust");
        assert_eq!(value["returned"], 3);
        assert_eq!(value["total_matches"], 3);
        assert_eq!(value["has_more"], false);
        assert!(value["stats"]["scanned"].as_u64().unwrap() >= 3);

        let files = value["files"].as_array().expect("files array");
        let alpha = files
            .iter()
            .find(|f| f["path"] == "src/alpha.rs")
            .expect("src/alpha.rs");
        assert_eq!(alpha["lang"], "Rust");
        assert_eq!(alpha["capped"], false);
        assert_eq!(alpha["hits"][0]["line"], 1);
        assert_eq!(alpha["hits"][0]["kind"], "function_item");
        assert_eq!(alpha["hits"][0]["capture"], "NAME");
        assert_eq!(alpha["hits"][0]["captured"], "alpha");
    });
}

/// `offset` is a pagination cursor, not a budget. Silently clamping it returns
/// a page from the wrong position, which a `offset += returned` loop cannot
/// detect. The schema advertises `maximum: MAX_LIMIT`, so an out-of-range value
/// is rejected rather than reshaped.
#[test]
fn test_offset_above_max_limit_is_rejected() {
    let dir = fixture();
    with_flag_override(FeatureFlag::StructuralSearch, true, || {
        let err = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(2)
            .enable_all()
            .build()
            .expect("runtime")
            .block_on(StructuralHandler.execute(
                &registry(),
                json!({
                    "pattern": "fn $NAME() { $$$BODY }",
                    "lang": "rust",
                    "offset": 10_001u64,
                    "project_path": dir.path()
                }),
            ))
            .expect_err("an out-of-range cursor must not be silently rewritten");

        assert_eq!(err.code, -32602, "invalid_params");
        assert!(
            err.message.contains("offset"),
            "message must name the offending argument: {}",
            err.message
        );
        assert!(
            err.message.contains("10000"),
            "message must name the ceiling: {}",
            err.message
        );
        assert!(
            err.suggestion().is_some_and(|hint| hint.contains("offset")),
            "the hint must tell the caller how to page"
        );
    });
}

/// The boundary itself is inside the advertised range and must be accepted.
#[test]
fn test_offset_at_max_limit_is_accepted() {
    let dir = fixture();
    with_flag_override(FeatureFlag::StructuralSearch, true, || {
        let value = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(2)
            .enable_all()
            .build()
            .expect("runtime")
            .block_on(StructuralHandler.execute(
                &registry(),
                json!({
                    "pattern": "fn $NAME() { $$$BODY }",
                    "lang": "rust",
                    "offset": 10_000u64,
                    "project_path": dir.path()
                }),
            ))
            .expect("offset == MAX_LIMIT is within the advertised range");

        // Skipping past 3 matches yields an empty page, but a well-defined one.
        assert_eq!(value["returned"], 0);
        assert_eq!(value["total_matches"], 3);
    });
}

#[test]
fn test_handler_infers_language_and_refuses_ambiguity() {
    with_flag_override(FeatureFlag::StructuralSearch, true, || {
        let runtime = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(2)
            .enable_all()
            .build()
            .expect("runtime");

        // Single-language scope: no `lang` needed.
        let rust_only = tempfile::tempdir().expect("tempdir");
        write(rust_only.path(), "lib.rs", "fn only() {}\n");
        runtime
            .block_on(StructuralHandler.execute(
                &registry(),
                json!({
                    "pattern": "fn $NAME() { $$$BODY }",
                    "project_path": rust_only.path()
                }),
            ))
            .expect("unambiguous scope should infer Rust");

        // Mixed scope: refusing beats parsing Rust patterns as Python.
        let mixed = fixture();
        let err = runtime
            .block_on(StructuralHandler.execute(
                &registry(),
                json!({
                    "pattern": "fn $NAME() { $$$BODY }",
                    "project_path": mixed.path()
                }),
            ))
            .expect_err("two languages in scope must be refused");
        assert!(
            err.message.contains("several languages") && err.message.contains("refusing to guess"),
            "error should explain the ambiguity: {}",
            err.message
        );

        // Nothing parseable at all: say so instead of scanning nothing.
        let empty = tempfile::tempdir().expect("tempdir");
        write(empty.path(), "notes.md", "# docs\n");
        let err = runtime
            .block_on(StructuralHandler.execute(
                &registry(),
                json!({
                    "pattern": "fn $NAME() { $$$BODY }",
                    "project_path": empty.path()
                }),
            ))
            .expect_err("no language in scope must be an error");
        assert!(
            err.message.contains("No structural-search language"),
            "error should explain the empty scope: {}",
            err.message
        );
    });
}

#[test]
fn test_missing_pattern_is_rejected() {
    with_flag_override(FeatureFlag::StructuralSearch, true, || {
        let err = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(2)
            .enable_all()
            .build()
            .expect("runtime")
            .block_on(StructuralHandler.execute(&registry(), json!({ "lang": "rust" })))
            .expect_err("pattern is required");
        assert!(
            err.message.contains("pattern"),
            "error must name the missing argument: {}",
            err.message
        );
    });
}

#[test]
fn test_default_tool_listing_exposes_structural() {
    let handler = StructuralHandler;
    assert_eq!(handler.name(), "leindex_structural");
    assert_eq!(handler.title(), "LeIndex [Structural]");

    let schema = handler.argument_schema();
    let required = schema["required"].as_array().expect("required array");
    assert_eq!(required, &vec![Value::String("pattern".to_string())]);
    let props = schema["properties"].as_object().expect("properties");
    for arg in [
        "pattern",
        "lang",
        "scope",
        "include_globs",
        "exclude_globs",
        "limit",
        "offset",
        "per_file_cap",
        "timeout_ms",
        "project_path",
    ] {
        assert!(props.contains_key(arg), "schema is missing `{arg}`");
    }
}

// =============================================================================
// Trim
// =============================================================================

#[test]
fn test_trim_keeps_actionable_fields_drops_offsets() {
    let data = json!({
        "mode": "structural",
        "pattern": "fn $NAME() { $$$BODY }",
        "lang": "Rust",
        "returned": 1,
        "total_matches": 1,
        "has_more": false,
        "stats": { "scanned": 3, "millis": 1 },
        "files": [{
            "path": "src/alpha.rs",
            "lang": "Rust",
            "total_matches": 1,
            "capped": false,
            "hits": [{
                "line": 1,
                "col": 1,
                "kind": "function_item",
                "text": "fn alpha() {}",
                "start_byte": 0,
                "end_byte": 14,
                "capture": "NAME",
                "captured": "alpha"
            }]
        }]
    });

    let trimmed = trim_llm_payload("leindex_structural", &data);
    assert_eq!(trimmed["pattern"], "fn $NAME() { $$$BODY }");
    assert_eq!(trimmed["lang"], "Rust");
    assert_eq!(trimmed["total_matches"], 1);
    assert_eq!(trimmed["stats"]["scanned"], 3);

    let hit = &trimmed["files"][0]["hits"][0];
    assert_eq!(hit["line"], 1);
    assert_eq!(hit["kind"], "function_item");
    assert_eq!(hit["captured"], "alpha");
    assert!(hit.get("start_byte").is_none(), "byte offsets are noise");
    assert!(
        hit.get("col").is_none(),
        "line+kind is enough to open the file"
    );
}

#[test]
fn test_trim_preserves_stale_index_warning() {
    let data = json!({
        "mode": "structural",
        "pattern": "fn $A() {}",
        "lang": "Rust",
        "returned": 0,
        "total_matches": 0,
        "has_more": false,
        "files": [],
        "_warning": "index is stale"
    });
    let trimmed = trim_llm_payload("leindex_structural", &data);
    assert_eq!(trimmed["_warning"], "index is stale");
}

// =============================================================================
// CLI rendering
// =============================================================================

#[test]
fn test_render_structural_prints_paths_lines_and_captures() {
    let data = json!({
        "mode": "structural",
        "pattern": "fn $NAME() { $$$BODY }",
        "lang": "Rust",
        "returned": 2,
        "total_matches": 2,
        "has_more": false,
        "files": [
            {
                "path": "src/alpha.rs",
                "lang": "Rust",
                "total_matches": 2,
                "capped": false,
                "hits": [
                    {
                        "line": 1,
                        "kind": "function_item",
                        // A real match spans lines; the renderer must not
                        // print a raw newline mid-row.
                        "text": "fn alpha() {\n    body();\n}",
                        "capture": "NAME",
                        "captured": "alpha"
                    },
                    { "line": 9, "kind": "function_item", "text": "fn beta() {}" }
                ]
            }
        ]
    });

    let out = leindex::cli::mcp::output::render::render_tool_output_plain(
        "leindex_structural",
        &data,
        &json!({}),
    );

    assert!(
        out.contains("2 structural match(es)"),
        "header should carry the totals: {out}"
    );
    assert!(out.contains("src/alpha.rs"), "path line missing: {out}");
    assert!(
        out.contains("1: [function_item]"),
        "first hit missing: {out}"
    );
    assert!(
        out.contains("fn alpha() { body(); }"),
        "multi-line match should collapse onto one row: {out}"
    );
    assert!(out.contains("alpha"), "captured name missing: {out}");
    assert!(
        out.contains("9: [function_item]"),
        "second hit missing: {out}"
    );
    assert!(
        !out.contains("more matches"),
        "no pagination hint expected: {out}"
    );
}

#[test]
fn test_render_structural_reports_empty_results_with_the_note() {
    let data = json!({
        "mode": "structural",
        "pattern": "fn $NAME() { $$$BODY }",
        "lang": "Rust",
        "returned": 0,
        "total_matches": 0,
        "has_more": false,
        "files": [],
        "note": "3 file(s) held more matches than per_file_cap"
    });

    let out = leindex::cli::mcp::output::render::render_tool_output_plain(
        "leindex_structural",
        &data,
        &json!({}),
    );

    assert!(
        out.contains("No structural matches"),
        "expected empty header: {out}"
    );
    assert!(
        out.contains("per_file_cap"),
        "the note must survive an empty page so the caller can widen the cap: {out}"
    );
}

// =============================================================================
// Router
// =============================================================================

#[test]
fn test_explore_router_advertises_and_resolves_structural() {
    let group =
        leindex::cli::mcp::grouped::group_by_name("leindex_explore").expect("explore group");
    let branch = group
        .branches
        .iter()
        .find(|b| b.branch == "structural")
        .expect("structural branch must be advertised");

    assert_eq!(branch.tool, "leindex_structural");
    for alias in ["ast", "ast_grep", "astgrep", "pattern"] {
        assert!(
            branch.aliases.contains(&alias),
            "alias `{alias}` should route to structural"
        );
    }
    assert!(
        group.description.contains("structural"),
        "router description must mention the branch: {}",
        group.description
    );

    let (tool, args) = leindex::cli::mcp::grouped::resolve_call(
        "leindex_explore",
        json!({
            "mode": "structural",
            "pattern": "fn $NAME() { $$$BODY }"
        }),
    )
    .expect("structural mode must resolve");
    assert_eq!(tool, "leindex_structural");
    assert_eq!(args["pattern"], "fn $NAME() { $$$BODY }");
    assert!(args.get("mode").is_none(), "the discriminator is consumed");
}

#[test]
fn test_every_router_branch_names_a_real_handler() {
    // The invariant behind adding a branch: `tools/list` must never name a tool
    // that cannot be dispatched.
    let registered: Vec<String> = leindex::cli::mcp::handlers::all_tool_handlers()
        .iter()
        .map(|h| h.name().to_string())
        .collect();
    for group in leindex::cli::mcp::grouped::GROUPS.iter() {
        for branch in group.branches {
            assert!(
                registered.iter().any(|name| name == branch.tool),
                "{} branch `{}` targets unregistered tool `{}`",
                group.name,
                branch.branch,
                branch.tool
            );
        }
    }
}