batless 0.7.0

A fast, non-blocking code and text viewer inspired by bat
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
use std::io::Write;
use std::process::Command;
use tempfile::NamedTempFile;

fn create_test_file(content: &str, extension: &str) -> NamedTempFile {
    let mut file = tempfile::Builder::new()
        .suffix(extension)
        .tempfile()
        .unwrap();
    file.write_all(content.as_bytes()).unwrap();
    file
}

fn run_batless(args: &[&str]) -> std::process::Output {
    Command::new(env!("CARGO_BIN_EXE_batless"))
        .args(args)
        .output()
        .expect("Failed to execute batless")
}

#[test]
fn test_help_command() {
    let output = run_batless(&["--help"]);
    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("A fast, non-blocking code and text viewer"));
    assert!(stdout.contains("--mode <MODE>"));
    assert!(stdout.contains("--max-lines"));
}

#[test]
fn test_version_command() {
    let output = run_batless(&["--version"]);
    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("batless"));
}

#[test]
fn test_version_json_command() {
    let output = run_batless(&["--version-json"]);
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let json: serde_json::Value = serde_json::from_str(&stdout).expect("valid json");
    assert_eq!(json["name"], "batless");
    assert_eq!(json["version"], env!("CARGO_PKG_VERSION"));
    assert!(json["git_hash"].is_string());
    assert_ne!(json["git_hash"].as_str().unwrap_or("unknown"), "unknown");
    assert!(json["build_timestamp"].is_string());
    assert_ne!(
        json["build_timestamp"].as_str().unwrap_or("unknown"),
        "unknown"
    );
}

#[test]
fn test_plain_mode() {
    let content = "fn main() {\n    println!(\"Hello, world!\");\n}\n";
    let file = create_test_file(content, ".rs");

    let output = run_batless(&[file.path().to_str().unwrap(), "--mode=plain"]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("fn main()"));
    assert!(stdout.contains("println!"));
}

#[test]
fn test_plain_output() {
    let content = "fn main() {\n    println!(\"Hello, world!\");\n}\n";
    let file = create_test_file(content, ".rs");

    let output = run_batless(&[file.path().to_str().unwrap(), "--mode=plain"]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("main"));
    assert!(stdout.contains("println"));
}

#[test]
fn test_json_mode() {
    let content = "fn main() {\n    println!(\"Hello, world!\");\n}\n";
    let file = create_test_file(content, ".rs");

    let output = run_batless(&[file.path().to_str().unwrap(), "--mode=json"]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();

    // Parse as JSON to verify structure
    let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert_eq!(json["mode"], "json");
    assert_eq!(json["language"], "Rust");
    assert_eq!(
        json["processed_lines"]
            .as_u64()
            .expect("processed_lines should be numeric"),
        json["lines"]
            .as_array()
            .expect("lines should be an array")
            .len() as u64
    );
    assert!(json["lines"].is_array());
    assert!(json["total_lines"].is_number());
    assert!(json["total_lines_exact"].is_boolean());
    assert!(json["total_bytes"].is_number());
}

#[test]
fn test_json_pretty_flag_changes_formatting() {
    let content = "fn main() { println!(\"Hello\"); }\n";
    let file = create_test_file(content, ".rs");

    let compact = run_batless(&[file.path().to_str().unwrap(), "--mode=json"]);
    assert!(compact.status.success());
    let compact_out = String::from_utf8(compact.stdout).unwrap();
    // Compact should not contain multiple leading spaces on new lines (minified)
    assert!(!compact_out.contains("\n  \"file\""));

    let pretty = run_batless(&[
        file.path().to_str().unwrap(),
        "--mode=json",
        "--json-pretty",
    ]);
    assert!(pretty.status.success());
    let pretty_out = String::from_utf8(pretty.stdout).unwrap();
    // Pretty output should contain indentation
    assert!(pretty_out.contains("\n  \"file\""));
    assert!(pretty_out.len() > compact_out.len());
}

#[test]
fn test_max_lines_limit() {
    let content = "line 1\nline 2\nline 3\nline 4\nline 5\n";
    let file = create_test_file(content, ".txt");

    let output = run_batless(&[
        file.path().to_str().unwrap(),
        "--mode=plain",
        "--max-lines=3",
    ]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let lines: Vec<&str> = stdout.lines().collect();

    // Should have 3 content lines + 1 truncation message
    assert_eq!(lines.len(), 4);
    assert!(lines[3].contains("Output truncated after 3 lines"));
}

#[test]
fn test_max_bytes_limit() {
    let content = "Short line 1\nShort line 2\nShort line 3\nShort line 4\n";
    let file = create_test_file(content, ".txt");

    let output = run_batless(&[
        file.path().to_str().unwrap(),
        "--mode=plain",
        "--max-bytes=25",
        "--max-lines=100", // Large line limit so bytes limit takes effect
    ]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    // Should be truncated since the content is longer than 25 bytes
    assert!(stdout.contains("Output truncated after") && stdout.contains("bytes"));
}

#[test]
fn test_language_detection() {
    let python_content = "def hello():\n    print('Hello, world!')\n";
    let file = create_test_file(python_content, ".py");

    let output = run_batless(&[file.path().to_str().unwrap(), "--mode=json"]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert_eq!(json["language"], "Python");
}

#[test]
fn test_explicit_language() {
    let content = "function hello() {\n    console.log('Hello');\n}\n";
    let file = create_test_file(content, ".unknown");

    let output = run_batless(&[
        file.path().to_str().unwrap(),
        "--language=javascript",
        "--mode=json",
    ]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert_eq!(json["language"], "javascript");
}

#[test]
fn test_plain_no_ansi() {
    let content = "fn main() {\n    println!(\"Hello\");\n}\n";
    let file = create_test_file(content, ".rs");

    let output = run_batless(&[file.path().to_str().unwrap(), "--mode=plain"]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    // Plain mode never produces ANSI escape sequences
    assert!(!stdout.contains("\x1b["));
}

#[test]
fn test_strip_ansi() {
    let content = "fn main() {\n    println!(\"Hello\");\n}\n";
    let file = create_test_file(content, ".rs");

    let output = run_batless(&[
        file.path().to_str().unwrap(),
        "--mode=plain",
        "--strip-ansi",
    ]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("fn main()"));
}

#[test]
fn test_empty_file() {
    let file = create_test_file("", ".txt");

    let output = run_batless(&[file.path().to_str().unwrap(), "--mode=json"]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert_eq!(json["total_lines"], 0);
    assert_eq!(json["total_bytes"], 0);
    assert_eq!(json["truncated"], false);
}

#[test]
fn test_nonexistent_file() {
    let output = run_batless(&["/nonexistent/file.txt", "--mode=plain"]);

    assert!(!output.status.success());
    let stderr = String::from_utf8(output.stderr).unwrap();
    // Windows may have different error messages
    assert!(
        stderr.contains("No such file")
            || stderr.contains("not found")
            || stderr.contains("cannot find")
            || stderr.contains("system cannot find")
    );
}

#[test]
fn test_multiple_options_combined() {
    let content = "def func1():\n    pass\n\ndef func2():\n    pass\n\ndef func3():\n    pass\n";
    let file = create_test_file(content, ".py");

    let output = run_batless(&[
        file.path().to_str().unwrap(),
        "--mode=json",
        "--max-lines=2",
        "--language=python",
    ]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();

    assert_eq!(json["language"], "python");
    assert_eq!(json["processed_lines"], 2);
    assert!(json["total_lines"].as_i64().unwrap() >= json["processed_lines"].as_i64().unwrap());
    assert_eq!(json["truncated"], true);
    assert_eq!(json["lines"].as_array().unwrap().len(), 2);
}

#[test]
fn test_list_languages() {
    let output = run_batless(&["--list-languages"]);
    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("Rust"));
    assert!(stdout.contains("Python"));
    assert!(stdout.contains("JavaScript"));
}

#[test]
fn test_total_lines_exact_flag() {
    let content = "line1\nline2\nline3\nline4\nline5\n";
    let file = create_test_file(content, ".txt");

    let output = run_batless(&[
        file.path().to_str().unwrap(),
        "--mode=json",
        "--max-lines=2",
    ]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();

    assert_eq!(json["truncated_by_lines"], true);
    assert_eq!(json["total_lines_exact"], false);
}

#[test]
fn test_shell_completion_generation_bash() {
    let output = run_batless(&["--generate-completions", "bash"]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("_batless()"));
    assert!(stdout.contains("COMPREPLY=()"));
}

#[test]
fn test_shell_completion_generation_zsh() {
    let output = run_batless(&["--generate-completions", "zsh"]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("#compdef batless"));
}

#[test]
fn test_shell_completion_generation_fish() {
    let output = run_batless(&["--generate-completions", "fish"]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("complete -c batless"));
}

#[test]
fn test_shell_completion_generation_powershell() {
    let output = run_batless(&["--generate-completions", "power-shell"]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("Register-ArgumentCompleter"));
}

#[test]
fn test_stdin_processing() {
    // Test that stdin input is properly processed by using echo and pipe
    // This is more reliable than trying to capture cargo run output
    let output = Command::new("sh")
        .arg("-c")
        .arg("echo 'test line 1\\ntest line 2' | cargo run -- --mode=json")
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());

    // The output goes to stdout when using shell pipe
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("test line 1"));
    assert!(stdout.contains("test line 2"));
    assert!(
        stdout.contains("file\": \"-") || stdout.contains("file\":\"-"),
        "Expected file path '-' marker in JSON output, got: {stdout}"
    );
}

#[test]
fn test_stdin_processing_with_max_lines() {
    // Pipe many lines but limit to 3 — verifies incremental truncation
    let output = Command::new("sh")
        .arg("-c")
        .arg("printf 'line1\\nline2\\nline3\\nline4\\nline5\\n' | cargo run -- --mode=json --max-lines=3")
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("line1"));
    assert!(stdout.contains("line3"));
    assert!(!stdout.contains("line4"), "line4 should be truncated");

    // Verify truncation is reported in JSON
    assert!(
        stdout.contains("\"truncated\": true") || stdout.contains("\"truncated\":true"),
        "Expected truncated flag in JSON output"
    );
}

#[test]
fn test_stdin_processing_with_max_bytes() {
    // Pipe content but limit bytes — verifies byte-based truncation
    // Use --max-lines to keep it balanced with max-bytes (avoids validation error)
    let output = Command::new("sh")
        .arg("-c")
        .arg("printf 'short\\nthis is a longer line\\nanother line\\n' | cargo run -- --mode=json --max-bytes=100 --max-lines=1")
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    // Only the first line should be included due to --max-lines=1
    assert!(stdout.contains("short"));
    assert!(
        !stdout.contains("another line"),
        "Content beyond max-lines should be truncated"
    );
    assert!(
        stdout.contains("\"truncated\": true") || stdout.contains("\"truncated\":true"),
        "Expected truncated flag in JSON output"
    );
}

#[test]
fn test_invalid_language_error() {
    let output = run_batless(&["src/main.rs", "--language=invalid-language"]);
    assert!(!output.status.success());
    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(stderr.contains("Language 'invalid-language' not found"));
    assert!(stderr.contains("E203"));
}

// Enhanced integration tests for technical debt resolution
#[test]
fn test_once_lock_performance() {
    // Test that OnceLock initialization doesn't cause performance regression
    let content = "fn main() { println!(\"test\"); }";
    let file = create_test_file(content, ".rs");

    // Run multiple times to ensure OnceLock initialization is cached
    for _ in 0..3 {
        let output = run_batless(&[file.path().to_str().unwrap(), "--mode=plain"]);
        assert!(output.status.success());
        let stdout = String::from_utf8(output.stdout).unwrap();
        assert!(stdout.contains("fn main()"));
    }
}

#[test]
fn test_error_handling_with_suggestions() {
    // Test file not found with suggestions
    let output = run_batless(&["nonexistent_file.txt"]);
    assert!(!output.status.success());
    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(stderr.contains("File not found"));
    assert!(stderr.contains("E101"));
}

#[test]
fn test_large_file_processing() {
    // Test processing of larger files to verify no performance regression
    let large_content = "fn test_function() {\n    println!(\"line\");\n}\n".repeat(100);
    let file = create_test_file(&large_content, ".rs");

    let output = run_batless(&[
        file.path().to_str().unwrap(),
        "--mode=json",
        "--max-lines=50",
    ]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    assert!(json["truncated"].as_bool().unwrap_or(false));
}

#[test]
fn test_configuration_validation_edge_cases() {
    // Test zero max lines
    let content = "test";
    let file = create_test_file(content, ".txt");

    let output = run_batless(&[file.path().to_str().unwrap(), "--max-lines=0"]);

    // Should fail with configuration error
    assert!(!output.status.success());
    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(stderr.contains("E302") || stderr.contains("Configuration error"));
}

#[test]
fn test_language_validation() {
    let output = run_batless(&["--list-languages"]);
    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("Rust"));
    assert!(stdout.contains("Python"));
}

#[test]
fn test_compatibility_flags() {
    let content = "line1\nline2\nline3\n";
    let file = create_test_file(content, ".txt");

    // Test --plain flag
    let output = run_batless(&[file.path().to_str().unwrap(), "--plain"]);
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert_eq!(stdout.trim(), content.trim());

    // Test --number flag with plain mode
    let output = run_batless(&[file.path().to_str().unwrap(), "--number", "--mode=plain"]);
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    // Line numbering should be present in some form
    assert!(stdout.contains("line1") && stdout.contains("line2") && stdout.contains("line3"));
}

#[test]
fn test_encoding_detection_robustness() {
    // Test with various content types
    let test_cases = vec![
        ("ASCII content", ".txt"),
        ("UTF-8 content with émojis 🦀", ".txt"),
        ("Binary-like content \x00\x01\x02", ".bin"),
    ];

    for (content, ext) in test_cases {
        let file = create_test_file(content, ext);
        let output = run_batless(&[file.path().to_str().unwrap(), "--mode=json"]);

        // Should handle gracefully without crashing
        if output.status.success() {
            let stdout = String::from_utf8(output.stdout).unwrap();
            let json: serde_json::Value = serde_json::from_str(&stdout).unwrap();
            assert!(json["encoding"].is_string());
        }
        // Binary content might fail, which is acceptable
    }
}

// =========================================================================
// ME-3: --strip-blank-lines and --strip-comments tests
// =========================================================================

#[test]
fn test_strip_blank_lines() {
    let content = "def foo():\n    pass\n\n\ndef bar():\n    pass\n";
    let file = create_test_file(content, ".py");

    let output = run_batless(&[
        file.path().to_str().unwrap(),
        "--strip-blank-lines",
        "--mode=plain",
    ]);

    assert!(
        output.status.success(),
        "batless --strip-blank-lines should succeed"
    );
    let stdout = String::from_utf8(output.stdout).unwrap();

    // Blank lines should be stripped — no consecutive empty lines
    let has_consecutive_blanks = stdout.contains("\n\n");
    assert!(
        !has_consecutive_blanks,
        "Output should not contain consecutive blank lines after --strip-blank-lines"
    );

    // Code lines should still be present
    assert!(
        stdout.contains("def foo():"),
        "def foo(): should be retained"
    );
    assert!(
        stdout.contains("def bar():"),
        "def bar(): should be retained"
    );
}

#[test]
fn test_strip_comments() {
    let content = "# this is a comment\ndef foo():\n    # inline comment\n    pass\n";
    let file = create_test_file(content, ".py");

    let output = run_batless(&[
        file.path().to_str().unwrap(),
        "--strip-comments",
        "--mode=plain",
    ]);

    assert!(
        output.status.success(),
        "batless --strip-comments should succeed"
    );
    let stdout = String::from_utf8(output.stdout).unwrap();

    assert!(
        !stdout.contains("# this is a comment"),
        "Top-level comment should be stripped"
    );
    assert!(
        !stdout.contains("# inline comment"),
        "Inline comment line should be stripped"
    );
    assert!(
        stdout.contains("def foo():"),
        "Function definition should be retained"
    );
}

#[test]
fn test_strip_compression_ratio_in_json() {
    let content = "# comment\ndef foo():\n    pass\n\n# comment2\n";
    let file = create_test_file(content, ".py");

    let output = run_batless(&[
        file.path().to_str().unwrap(),
        "--strip-comments",
        "--strip-blank-lines",
        "--mode=json",
    ]);

    assert!(
        output.status.success(),
        "batless --strip-comments --strip-blank-lines --mode=json should succeed"
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    let json: serde_json::Value =
        serde_json::from_str(&stdout).expect("Output should be valid JSON");

    assert!(
        json.get("compression_ratio").is_some(),
        "JSON output should contain compression_ratio field"
    );
    let ratio = json["compression_ratio"]
        .as_f64()
        .expect("compression_ratio should be a float");
    assert!(
        ratio > 1.0,
        "compression_ratio should be > 1.0 when content was stripped, got {ratio}"
    );
}

// =========================================================================
// SF-1: --mode=index tests
// =========================================================================

#[test]
fn test_mode_index_rust() {
    let content = "pub fn foo() -> i32 { 1 }\nstruct Bar { x: i32 }\npub fn baz() {}\n";
    let file = create_test_file(content, ".rs");

    let output = run_batless(&[file.path().to_str().unwrap(), "--mode=index"]);

    assert!(
        output.status.success(),
        "batless --mode=index should succeed for a Rust file"
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    let json: serde_json::Value =
        serde_json::from_str(&stdout).expect("--mode=index output should be valid JSON");

    assert!(
        json["symbols"].is_array(),
        "JSON output should contain a symbols array"
    );
    let symbol_count = json["symbol_count"]
        .as_u64()
        .expect("symbol_count should be a number");
    assert!(symbol_count > 0, "symbol_count should be > 0");

    let symbols = json["symbols"].as_array().unwrap();
    assert!(
        symbols
            .iter()
            .any(|s| s["kind"].as_str().is_some_and(|k| k == "function")),
        "At least one symbol should have kind == function"
    );
    assert!(
        symbols.iter().any(|s| s["name"].as_str() == Some("foo")),
        "At least one symbol should have name == foo"
    );
    assert!(
        symbols.iter().all(|s| s.get("line_start").is_some()),
        "All symbols should have a line_start field"
    );
}

#[test]
fn test_mode_index_has_file_metadata() {
    let content = "pub fn main() {}\nstruct Config { value: i32 }\n";
    let file = create_test_file(content, ".rs");

    let output = run_batless(&[file.path().to_str().unwrap(), "--mode=index"]);

    assert!(
        output.status.success(),
        "batless --mode=index should succeed"
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    let json: serde_json::Value =
        serde_json::from_str(&stdout).expect("--mode=index output should be valid JSON");

    assert!(
        json.get("file").is_some(),
        "JSON output should contain file field"
    );
    assert!(
        json.get("language").is_some(),
        "JSON output should contain language field"
    );
    assert!(
        json["total_lines"].is_number(),
        "JSON output should contain total_lines field"
    );
    assert!(
        json["symbol_count"].is_number(),
        "JSON output should contain symbol_count field"
    );
    assert_eq!(
        json["mode"].as_str(),
        Some("index"),
        "mode field should equal index"
    );
}