agent-file-tools 0.42.0

Agent File Tools — tree-sitter powered code analysis for AI agents
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
//! Integration tests for the outline command through the binary protocol.

use std::fs::{self, File};
use std::path::{Path, PathBuf};

use super::helpers::{fixture_path, AftProcess};

fn write_temp_file(root: &Path, relative: &str, content: &str) -> PathBuf {
    let path = root.join(relative);
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).expect("create parent directories");
    }
    fs::write(&path, content).expect("write temp file");
    path
}

#[test]
fn read_allows_explicit_ranges_from_large_files() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("large.txt");
    let mut content = String::from("first\nsecond\nthird\n");
    content.push_str(&"x".repeat(51 * 1024 * 1024));
    fs::write(&path, content).expect("write large file");

    let mut aft = AftProcess::spawn();
    let resp = aft.send(
        &serde_json::json!({
            "id": "read-large-range",
            "command": "read",
            "file": path,
            "start_line": 1,
            "end_line": 2,
        })
        .to_string(),
    );

    assert_eq!(resp["success"], true, "read should succeed: {resp:?}");
    assert_eq!(resp["content"], "1: first\n2: second\n");
    assert_eq!(resp["complete"], false, "range read is partial: {resp:?}");
    assert_eq!(
        resp["truncated"], true,
        "range read should flag gap: {resp:?}"
    );
    assert!(
        resp.get("total_lines").is_none(),
        "ranged read should not scan to EOF just to report total_lines: {resp:?}"
    );

    assert!(aft.shutdown().success());
}

#[test]
fn read_range_stops_after_one_line_lookahead() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("invalid-after-lookahead.txt");
    let mut bytes = (1..=11)
        .map(|n| format!("line {n}\n"))
        .collect::<String>()
        .into_bytes();
    bytes.extend_from_slice(&[0xff, 0xfe, 0xfd]);
    bytes.extend_from_slice(&vec![b'x'; 1024 * 1024]);
    fs::write(&path, bytes).expect("write ranged fixture");

    let mut aft = AftProcess::spawn();
    let resp = aft.send(
        &serde_json::json!({
            "id": "read-range-lookahead",
            "command": "read",
            "file": path,
            "start_line": 1,
            "end_line": 10,
        })
        .to_string(),
    );

    assert_eq!(resp["success"], true, "read should succeed: {resp:?}");
    assert_eq!(resp["content"].as_str().unwrap().lines().count(), 10);
    assert_eq!(
        resp["truncated"], true,
        "lookahead should detect more lines: {resp:?}"
    );
    assert!(
        resp.get("total_lines").is_none(),
        "lookahead-only ranged read must not report an inexact total: {resp:?}"
    );

    assert!(aft.shutdown().success());
}

#[test]
fn read_range_reports_partial_and_omits_unknown_total_lines() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("hundred.txt");
    let content = (1..=100)
        .map(|n| format!("line {n}"))
        .collect::<Vec<_>>()
        .join("\n");
    fs::write(&path, content).expect("write ranged fixture");

    let mut aft = AftProcess::spawn();
    let resp = aft.send(
        &serde_json::json!({
            "id": "read-range-partial",
            "command": "read",
            "file": path,
            "start_line": 1,
            "end_line": 10,
        })
        .to_string(),
    );

    assert_eq!(resp["success"], true, "read should succeed: {resp:?}");
    assert_eq!(resp["complete"], false, "range read is partial: {resp:?}");
    assert_eq!(
        resp["truncated"], true,
        "range read should flag gap: {resp:?}"
    );
    assert!(
        resp.get("total_lines").is_none(),
        "total_lines is unknown when the ranged read stops after lookahead: {resp:?}"
    );
    assert_eq!(
        resp["lines_read"], 10,
        "should return requested slice: {resp:?}"
    );

    assert!(aft.shutdown().success());
}

#[test]
fn read_directory_truncation_reports_partial() {
    let dir = tempfile::tempdir().unwrap();
    for i in 0..1001 {
        fs::write(dir.path().join(format!("entry-{i:04}.txt")), "x").expect("write entry");
    }

    let mut aft = AftProcess::spawn();
    let resp = aft.send(
        &serde_json::json!({
            "id": "read-dir-partial",
            "command": "read",
            "file": dir.path(),
        })
        .to_string(),
    );

    assert_eq!(
        resp["success"], true,
        "directory read should succeed: {resp:?}"
    );
    assert_eq!(
        resp["complete"], false,
        "truncated directory is partial: {resp:?}"
    );
    assert_eq!(
        resp["truncated"], true,
        "directory should flag truncation: {resp:?}"
    );
    assert_eq!(
        resp["total_entries"], 1001,
        "must include total entries: {resp:?}"
    );

    assert!(aft.shutdown().success());
}

#[test]
fn test_outline_typescript_nested_structure() {
    let mut aft = AftProcess::spawn();
    let file = fixture_path("sample.ts");

    let resp = aft.send(&format!(
        r#"{{"id":"ol-1","command":"outline","file":{}}}"#,
        crate::helpers::json_string(&file.display())
    ));

    assert_eq!(resp["id"], "ol-1");
    assert_eq!(resp["success"], true, "outline should succeed");

    let text = resp["text"]
        .as_str()
        .expect("text field should be a string");

    // UserService should be at top level (no "." prefix in its line)
    let user_service_line = text
        .lines()
        .find(|l| l.contains("UserService"))
        .expect("UserService should be in outline");
    assert!(
        !user_service_line.contains('.'),
        "UserService should be at top level (no '.' prefix), got: {:?}",
        user_service_line
    );

    // getUser should be a nested member (its line has a "." prefix)
    let get_user_line = text
        .lines()
        .find(|l| l.contains("getUser"))
        .expect("getUser should be in outline");
    assert!(
        get_user_line.contains('.'),
        "getUser should be a nested member (has '.' prefix), got: {:?}",
        get_user_line
    );

    // addUser should be a nested member (its line has a "." prefix)
    let add_user_line = text
        .lines()
        .find(|l| l.contains("addUser"))
        .expect("addUser should be in outline");
    assert!(
        add_user_line.contains('.'),
        "addUser should be a nested member (has '.' prefix), got: {:?}",
        add_user_line
    );

    // Verify all expected symbol kind abbreviations are present
    assert!(text.contains(" fn "), "should have fn (function) kind");
    assert!(text.contains(" cls "), "should have cls (class) kind");
    assert!(text.contains(" ifc "), "should have ifc (interface) kind");
    assert!(text.contains(" enum "), "should have enum kind");
    assert!(
        text.contains(" type "),
        "should have type (type_alias) kind"
    );

    // greet should be exported: its line's first non-space chars are "E "
    let greet_line = text
        .lines()
        .find(|l| l.contains("greet") && !l.contains("UserService"))
        .expect("greet line should be in outline");
    assert!(
        greet_line.trim_start().starts_with("E "),
        "greet should be exported, got: {:?}",
        greet_line
    );

    // internalHelper should not be exported: its line's first non-space chars are "- "
    let internal_line = text
        .lines()
        .find(|l| l.contains("internalHelper"))
        .expect("internalHelper line should be in outline");
    assert!(
        internal_line.trim_start().starts_with("- "),
        "internalHelper should not be exported, got: {:?}",
        internal_line
    );

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn test_outline_python_multi_level_nesting() {
    let mut aft = AftProcess::spawn();
    let file = fixture_path("sample.py");

    let resp = aft.send(&format!(
        r#"{{"id":"ol-py","command":"outline","file":{}}}"#,
        crate::helpers::json_string(&file.display())
    ));

    assert_eq!(resp["success"], true, "outline should succeed for Python");

    let text = resp["text"]
        .as_str()
        .expect("text field should be a string");

    // OuterClass should be present at top level (2-space indent: starts with "  E" or "  -")
    let outer_class_line = text
        .lines()
        .find(|l| l.contains("OuterClass"))
        .expect("OuterClass should be in outline");
    assert!(
        outer_class_line.starts_with("  E") || outer_class_line.starts_with("  -"),
        "OuterClass should be at top level (2-space indent), got: {:?}",
        outer_class_line
    );

    // InnerClass should be nested, NOT at top level
    let inner_class_line = text
        .lines()
        .find(|l| l.contains("InnerClass"))
        .expect("InnerClass should be in outline");
    assert!(
        !(inner_class_line.starts_with("  E") || inner_class_line.starts_with("  -")),
        "InnerClass should be nested, not at top level, got: {:?}",
        inner_class_line
    );

    // inner_method should be nested, NOT at top level
    let inner_method_line = text
        .lines()
        .find(|l| l.contains("inner_method"))
        .expect("inner_method should be in outline");
    assert!(
        !(inner_method_line.starts_with("  E") || inner_method_line.starts_with("  -")),
        "inner_method should be nested, not at top level, got: {:?}",
        inner_method_line
    );

    // outer_method should be present as a member of OuterClass
    assert!(
        text.contains("outer_method"),
        "outer_method should be in outline"
    );

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn test_outline_missing_file() {
    let mut aft = AftProcess::spawn();

    let resp =
        aft.send(r#"{"id":"ol-miss","command":"outline","file":"/nonexistent/path/to/file.ts"}"#);

    assert_eq!(resp["success"], false);
    assert_eq!(resp["code"], "file_not_found");
    assert!(resp["message"].as_str().unwrap().contains("file not found"));

    // Process should still be alive
    let resp = aft.send(r#"{"id":"alive","command":"ping"}"#);
    assert_eq!(resp["success"], true);

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn test_outline_missing_param() {
    let mut aft = AftProcess::spawn();

    let resp = aft.send(r#"{"id":"ol-nop","command":"outline"}"#);

    assert_eq!(resp["success"], false);
    assert_eq!(resp["code"], "invalid_request");
    assert!(resp["message"].as_str().unwrap().contains("file"));

    let status = aft.shutdown();
    assert!(status.success());
}

// ============================================================================
// Zoom command integration tests
// ============================================================================

#[test]
fn test_zoom_success_with_annotations() {
    let mut aft = AftProcess::spawn();
    let file = fixture_path("calls.ts");

    let resp = aft.send(&format!(
        r#"{{"id":"z-1","command":"zoom","file":{},"symbol":"compute","callgraph":true}}"#,
        crate::helpers::json_string(&file.display())
    ));

    assert_eq!(resp["id"], "z-1");
    assert_eq!(resp["success"], true, "zoom should succeed: {:?}", resp);
    assert_eq!(resp["name"], "compute");
    assert_eq!(resp["kind"], "function");

    // Content should contain the function body
    let content = resp["content"].as_str().expect("content string");
    assert!(
        content.contains("function compute"),
        "content should have function declaration: {}",
        content
    );
    assert!(
        content.contains("helper(a)"),
        "content should contain call to helper: {}",
        content
    );

    // Range should be present
    assert!(resp["range"]["start_line"].is_number());
    assert!(resp["range"]["end_line"].is_number());

    // Annotations
    let calls_out = resp["annotations"]["calls_out"]
        .as_array()
        .expect("calls_out array");
    let out_names: Vec<&str> = calls_out
        .iter()
        .map(|c| c["name"].as_str().unwrap())
        .collect();
    assert!(
        out_names.contains(&"helper"),
        "compute should call helper: {:?}",
        out_names
    );

    let called_by = resp["annotations"]["called_by"]
        .as_array()
        .expect("called_by array");
    let by_names: Vec<&str> = called_by
        .iter()
        .map(|c| c["name"].as_str().unwrap())
        .collect();
    assert!(
        by_names.contains(&"orchestrate"),
        "orchestrate should call compute: {:?}",
        by_names
    );

    // Each CallRef should have a line number
    for cr in calls_out {
        assert!(cr["line"].is_number(), "CallRef should have line: {:?}", cr);
        assert!(
            cr["line"].as_u64().unwrap_or(0) >= 1,
            "calls_out line should be 1-based: {:?}",
            cr
        );
    }
    for cr in called_by {
        assert!(cr["line"].is_number(), "CallRef should have line: {:?}", cr);
        assert!(
            cr["line"].as_u64().unwrap_or(0) >= 1,
            "called_by line should be 1-based: {:?}",
            cr
        );
    }

    let helper_call = calls_out
        .iter()
        .find(|cr| cr["name"] == "helper")
        .expect("compute should call helper");
    assert_eq!(helper_call["line"], 8, "helper call should be 1-based");

    let orchestrate_caller = called_by
        .iter()
        .find(|cr| cr["name"] == "orchestrate")
        .expect("orchestrate should call compute");
    assert_eq!(
        orchestrate_caller["line"], 13,
        "caller annotation should be 1-based"
    );

    // Context lines
    let ctx_before = resp["context_before"]
        .as_array()
        .expect("context_before array");
    let ctx_after = resp["context_after"]
        .as_array()
        .expect("context_after array");
    assert!(ctx_before.len() <= 3, "default context_lines is 3");
    assert!(ctx_after.len() <= 3, "default context_lines is 3");

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn test_read_range_after_binary_sample_rewind() {
    let temp_dir = tempfile::tempdir().expect("create temp dir");
    let file = temp_dir.path().join("sample.txt");
    let mut content = "a".repeat(4096);
    content.push_str("\nline-two\nline-three\n");
    std::fs::write(&file, content).expect("write sample");

    let mut aft = AftProcess::spawn();
    let cfg = aft.configure(temp_dir.path());
    assert_eq!(cfg["success"], true, "configure should succeed: {:?}", cfg);

    let resp = aft.send(&format!(
        r#"{{"id":"read-range-rewind","command":"read","file":{},"start_line":2,"end_line":3}}"#,
        crate::helpers::json_string(&file.display())
    ));

    assert_eq!(resp["success"], true, "read should succeed: {resp:?}");
    assert_eq!(resp["lines_read"], 2);
    let content = resp["content"].as_str().expect("content string");
    assert!(content.contains("2: line-two"), "content: {content}");
    assert!(content.contains("3: line-three"), "content: {content}");

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn test_read_binary_detection_after_single_open_refactor() {
    let temp_dir = tempfile::tempdir().expect("create temp dir");
    let file = temp_dir.path().join("binary.bin");
    std::fs::write(&file, [b'a', b'b', 0, b'c']).expect("write binary");

    let mut aft = AftProcess::spawn();
    let cfg = aft.configure(temp_dir.path());
    assert_eq!(cfg["success"], true, "configure should succeed: {:?}", cfg);

    let resp = aft.send(&format!(
        r#"{{"id":"read-binary-single-open","command":"read","file":{},"start_line":1,"end_line":1}}"#,
        crate::helpers::json_string(&file.display())
    ));

    assert_eq!(
        resp["success"], true,
        "binary read should succeed: {resp:?}"
    );
    assert_eq!(resp["binary"], true);
    assert!(resp["message"]
        .as_str()
        .is_some_and(|message| message.contains("Binary file")));

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn test_read_rejects_files_larger_than_50mb() {
    let temp_dir = tempfile::tempdir().expect("create temp dir");
    let file = temp_dir.path().join("large.txt");
    let handle = File::create(&file).expect("create large file");
    handle
        .set_len((50 * 1024 * 1024 + 1) as u64)
        .expect("set sparse file length");

    let mut aft = AftProcess::spawn();
    let cfg = aft.configure(temp_dir.path());
    assert_eq!(cfg["success"], true, "configure should succeed: {:?}", cfg);

    let resp = aft.send(&format!(
        r#"{{"id":"read-large","command":"read","file":{}}}"#,
        crate::helpers::json_string(&file.display())
    ));

    assert_eq!(resp["success"], false, "read should fail: {:?}", resp);
    assert_eq!(resp["code"], "invalid_request");
    assert!(resp["message"]
        .as_str()
        .expect("message")
        .contains("Use start_line/end_line to read sections"));

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn test_read_handles_inverted_line_range() {
    // Regression for audit #1: read with start_line > end_line previously
    // panicked at `lines[start_idx..end_idx]` because end_idx was clamped
    // independently of start_idx. Must now return success with zero lines.
    let temp_dir = tempfile::tempdir().expect("create temp dir");
    let file = temp_dir.path().join("sample.txt");
    std::fs::write(&file, "one\ntwo\nthree\nfour\nfive\n").expect("write sample");

    let mut aft = AftProcess::spawn();
    let cfg = aft.configure(temp_dir.path());
    assert_eq!(cfg["success"], true, "configure should succeed: {:?}", cfg);

    let resp = aft.send(&format!(
        r#"{{"id":"read-inv","command":"read","file":{},"start_line":8,"end_line":3}}"#,
        crate::helpers::json_string(&file.display())
    ));

    assert_eq!(
        resp["success"], true,
        "inverted range should not crash: {:?}",
        resp
    );
    assert_eq!(resp["lines_read"], 0);

    // Process must still be alive (would be dead on the old panic path).
    let ping = aft.send(r#"{"id":"alive","command":"ping"}"#);
    assert_eq!(ping["success"], true);

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn test_read_directory_caps_entries_at_one_thousand() {
    let temp_dir = tempfile::tempdir().expect("create temp dir");
    for index in 0..1005 {
        std::fs::write(temp_dir.path().join(format!("entry_{index:04}.txt")), "x")
            .expect("write directory entry");
    }

    let mut aft = AftProcess::spawn();
    let cfg = aft.configure(temp_dir.path());
    assert_eq!(cfg["success"], true, "configure should succeed: {cfg:?}");

    let resp = aft.send(&format!(
        r#"{{"id":"read-dir-cap","command":"read","file":{}}}"#,
        crate::helpers::json_string(&temp_dir.path().display())
    ));

    assert_eq!(
        resp["success"], true,
        "read directory should succeed: {resp:?}"
    );
    assert_eq!(resp["total_entries"], 1005);
    let entries = resp["entries"].as_array().expect("entries array");
    assert_eq!(entries.len(), 1001);
    assert_eq!(entries[0], "entry_0000.txt");
    assert_eq!(entries[999], "entry_0999.txt");
    assert_eq!(
        entries[1000],
        "\n... and 5 more entries (truncated, showing first 1000)"
    );

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn test_zoom_symbol_not_found() {
    let mut aft = AftProcess::spawn();
    let file = fixture_path("calls.ts");

    let resp = aft.send(&format!(
        r#"{{"id":"z-nf","command":"zoom","file":{},"symbol":"nonexistent_fn"}}"#,
        crate::helpers::json_string(&file.display())
    ));

    assert_eq!(resp["success"], false);
    assert_eq!(resp["code"], "symbol_not_found");
    assert!(resp["message"].as_str().unwrap().contains("nonexistent_fn"));

    // Process should still be alive
    let resp = aft.send(r#"{"id":"alive","command":"ping"}"#);
    assert_eq!(resp["success"], true);

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn test_zoom_code_symbol_lookup_keeps_hash_prefix_exact() {
    let dir = tempfile::tempdir().unwrap();
    let file = write_temp_file(
        dir.path(),
        "lib.rs",
        r#"#[derive(Debug)]
pub struct Thing;
"#,
    );

    let mut aft = AftProcess::spawn();
    aft.configure(dir.path());

    let resp = aft.send(&format!(
        r##"{{"id":"z-rust-hash","command":"zoom","file":{},"symbol":"#[derive(Debug)]"}}"##,
        crate::helpers::json_string(&file.display())
    ));

    assert_eq!(
        resp["success"], false,
        "attribute text should not be normalized as a code symbol: {resp:?}"
    );
    assert_eq!(resp["code"], "symbol_not_found");
    assert!(resp["message"]
        .as_str()
        .unwrap()
        .contains("#[derive(Debug)]"));

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn test_zoom_context_lines_param() {
    let mut aft = AftProcess::spawn();
    let file = fixture_path("calls.ts");

    // Use context_lines=1
    let resp = aft.send(&format!(
        r#"{{"id":"z-cl","command":"zoom","file":{},"symbol":"compute","context_lines":1}}"#,
        crate::helpers::json_string(&file.display())
    ));

    assert_eq!(resp["success"], true);

    let ctx_before = resp["context_before"].as_array().unwrap();
    let ctx_after = resp["context_after"].as_array().unwrap();
    assert!(
        ctx_before.len() <= 1,
        "context_before should be ≤1 with context_lines=1: {:?}",
        ctx_before
    );
    assert!(
        ctx_after.len() <= 1,
        "context_after should be ≤1 with context_lines=1: {:?}",
        ctx_after
    );

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn test_zoom_empty_annotations_arrays() {
    let mut aft = AftProcess::spawn();
    let file = fixture_path("calls.ts");

    // `unused` has no known callers and calls no known symbols
    let resp = aft.send(&format!(
        r#"{{"id":"z-empty","command":"zoom","file":{},"symbol":"unused"}}"#,
        crate::helpers::json_string(&file.display())
    ));

    assert_eq!(resp["success"], true);

    // called_by must be empty array, not null
    let called_by = resp["annotations"]["called_by"]
        .as_array()
        .expect("called_by should be array, not null");
    assert!(called_by.is_empty());

    // calls_out must be an array (possibly empty depending on known symbols)
    let calls_out = resp["annotations"]["calls_out"]
        .as_array()
        .expect("calls_out should be array, not null");
    // It's fine if calls_out has items — what matters is it's an array
    let _ = calls_out;

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn test_zoom_supports_c_symbols() {
    let dir = tempfile::tempdir().expect("create temp dir");
    let file = write_temp_file(
        dir.path(),
        "src/sample.c",
        "int compute(int value) {\n    return value + 1;\n}\n",
    );

    let mut aft = AftProcess::spawn();
    let cfg = aft.configure(dir.path());
    assert_eq!(cfg["success"], true, "configure should succeed: {cfg:?}");

    let resp = aft.send(&format!(
        r#"{{"id":"zoom-c","command":"zoom","file":{},"symbol":"compute"}}"#,
        crate::helpers::json_string(&file.display())
    ));

    assert_eq!(resp["success"], true, "zoom should succeed: {resp:?}");
    let content = resp["content"].as_str().expect("content string");
    assert!(content.contains("int compute(int value)"));
    assert!(content.contains("return value + 1;"));

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn test_zoom_supports_cpp_symbols() {
    let dir = tempfile::tempdir().expect("create temp dir");
    let file = write_temp_file(
        dir.path(),
        "src/sample.cpp",
        "class Worker {\npublic:\n    void run() {}\n};\n",
    );

    let mut aft = AftProcess::spawn();
    let cfg = aft.configure(dir.path());
    assert_eq!(cfg["success"], true, "configure should succeed: {cfg:?}");

    let resp = aft.send(&format!(
        r#"{{"id":"zoom-cpp","command":"zoom","file":{},"symbol":"Worker"}}"#,
        crate::helpers::json_string(&file.display())
    ));

    assert_eq!(resp["success"], true, "zoom should succeed: {resp:?}");
    let content = resp["content"].as_str().expect("content string");
    assert!(content.contains("class Worker"));
    assert!(content.contains("void run()"));

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn test_zoom_supports_zig_symbols() {
    let dir = tempfile::tempdir().expect("create temp dir");
    let file = write_temp_file(
        dir.path(),
        "src/sample.zig",
        "fn greet(name: []const u8) void {\n    _ = name;\n}\n",
    );

    let mut aft = AftProcess::spawn();
    let cfg = aft.configure(dir.path());
    assert_eq!(cfg["success"], true, "configure should succeed: {cfg:?}");

    let resp = aft.send(&format!(
        r#"{{"id":"zoom-zig","command":"zoom","file":{},"symbol":"greet"}}"#,
        crate::helpers::json_string(&file.display())
    ));

    assert_eq!(resp["success"], true, "zoom should succeed: {resp:?}");
    let content = resp["content"].as_str().expect("content string");
    assert!(content.contains("fn greet(name: []const u8) void"));
    assert!(content.contains("_ = name;"));

    let status = aft.shutdown();
    assert!(status.success());
}

#[test]
fn test_zoom_supports_csharp_symbols() {
    let dir = tempfile::tempdir().expect("create temp dir");
    let file = write_temp_file(
        dir.path(),
        "src/Sample.cs",
        "public class Worker\n{\n    public void Run()\n    {\n    }\n}\n",
    );

    let mut aft = AftProcess::spawn();
    let cfg = aft.configure(dir.path());
    assert_eq!(cfg["success"], true, "configure should succeed: {cfg:?}");

    let resp = aft.send(&format!(
        r#"{{"id":"zoom-csharp","command":"zoom","file":{},"symbol":"Worker"}}"#,
        crate::helpers::json_string(&file.display())
    ));

    assert_eq!(resp["success"], true, "zoom should succeed: {resp:?}");
    let content = resp["content"].as_str().expect("content string");
    assert!(content.contains("public class Worker"));
    assert!(content.contains("public void Run()"));

    let status = aft.shutdown();
    assert!(status.success());
}