batless 0.6.0

A non-blocking, LLM-friendly code 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
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
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 non-blocking, LLM-friendly code 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());
    assert!(json["identifier_count"].is_number());
    assert!(json["identifiers_truncated"].is_boolean());
}

#[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.trim().split('\n').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_summary_mode() {
    let content = "import os\nimport sys\n\ndef main():\n    print('hello')\n    x = 1\n    y = 2\n\nclass Test:\n    def method(self):\n        pass\n";
    let file = create_test_file(content, ".py");

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

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("import os"));
    assert!(stdout.contains("def main():"));
    assert!(stdout.contains("class Test:"));
    assert!(!stdout.contains("x = 1")); // Should not include non-summary lines
}

#[test]
fn test_summary_flag() {
    let content = "fn main() {\n    println!(\"hello\");\n    let x = 1;\n}\n\nstruct Test {\n    name: String,\n}\n";
    let file = create_test_file(content, ".rs");

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

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("fn main()"));
    assert!(stdout.contains("struct Test"));
    assert!(!stdout.contains("let x = 1")); // Should not include non-summary lines
}

#[test]
fn test_json_summary_retains_full_content() {
    let content =
        "import os\n\nclass Example:\n    def method(self):\n        pass\n\nSECRET = 42\n";
    let file = create_test_file(content, ".py");

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

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

    let lines = json["lines"].as_array().expect("lines array");
    assert!(
        lines
            .iter()
            .any(|line| line.as_str() == Some("SECRET = 42")),
        "Original content should remain intact"
    );
    let summary_lines = json["summary_lines"].as_array().expect("summary lines");
    assert!(
        summary_lines.len() <= lines.len(),
        "Summary should not exceed full content"
    );
}

#[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_token_sampling_reports_truncation() {
    let repeated_tokens: Vec<String> = (0..3_000).map(|i| format!("token{}", i)).collect();
    let content = repeated_tokens.join(" ");
    let file = create_test_file(&content, ".rs");

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

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

    let tokens = json["identifiers"].as_array().expect("tokens array");
    let token_count = json["identifier_count"].as_u64().expect("token_count");

    assert!(
        token_count as usize > tokens.len(),
        "Reported count should exceed sampled tokens"
    );
    assert_eq!(json["identifiers_truncated"], true);
}

#[test]
fn test_include_tokens() {
    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=json",
        "--include-tokens",
    ]);

    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["identifiers"].is_array());
    let tokens = json["identifiers"].as_array().unwrap();
    assert!(!tokens.is_empty());
}

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

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

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

    // Check new fields
    assert!(json["encoding"].is_string());
    assert!(json["syntax_errors"].is_array());
    assert!(json["truncated_by_lines"].is_boolean());
    assert!(json["truncated_by_bytes"].is_boolean());
    assert!(json["processed_lines"].is_number());
    assert!(json["total_lines"].is_number());
    assert!(json["identifiers"].is_array());
    assert!(json["identifier_count"].is_number());
    assert!(json["identifiers_truncated"].is_boolean());
}

#[test]
fn test_summary_with_no_important_lines() {
    let content = "// Just comments\n// Nothing important\n// More comments\n";
    let file = create_test_file(content, ".rs");

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

    assert!(output.status.success());
    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(stderr.contains("// No summary-worthy code structures found"));
}

#[test]
fn test_ai_profile_claude() {
    let test_file = create_test_file("fn main() {\n    println!(\"hello\");\n}\n", ".rs");
    let output = run_batless(&["--profile", "claude", test_file.path().to_str().unwrap()]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("=== File Summary ==="));
    assert!(stdout.contains("Language: Rust"));
}

#[test]
fn test_ai_profile_copilot() {
    let test_file = create_test_file("fn main() {\n    println!(\"hello\");\n}\n", ".rs");
    let output = run_batless(&["--profile", "copilot", test_file.path().to_str().unwrap()]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    // Should be JSON output with tokens
    assert!(
        stdout.contains("\"language\": \"Rust\"") || stdout.contains("\"language\":\"Rust\""),
        "Expected language Rust field in JSON output, got: {}",
        stdout
    );
    assert!(stdout.contains("\"identifiers\":"));
}

#[test]
fn test_ai_profile_chatgpt() {
    let test_file = create_test_file("def hello():\n    print('world')\n", ".py");
    let output = run_batless(&["--profile", "chatgpt", test_file.path().to_str().unwrap()]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    // Should be JSON output with tokens
    assert!(
        stdout.contains("\"language\": \"Python\"") || stdout.contains("\"language\":\"Python\""),
        "Expected language Python field in JSON output, got: {}",
        stdout
    );
    assert!(stdout.contains("\"identifiers\":"));
}

#[test]
fn test_ai_profile_assistant() {
    let test_file = create_test_file("class Test {\n    public void run() {}\n}\n", ".java");
    let output = run_batless(&["--profile", "assistant", test_file.path().to_str().unwrap()]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    // Should be summary output
    assert!(stdout.contains("=== File Summary ==="));
}

#[test]
fn test_explicit_mode_overrides_profile() {
    let test_file = create_test_file("fn main() {}\n", ".rs");
    let output = run_batless(&[
        "--profile",
        "claude",
        "--mode",
        "json", // Explicit --mode wins over profile default
        test_file.path().to_str().unwrap(),
    ]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    // Explicit --mode=json should win over claude profile's summary default
    assert!(stdout.starts_with("{"));
    assert!(!stdout.contains("=== File Summary ==="));
}

#[test]
fn test_profile_default_mode_used_without_explicit_mode() {
    let test_file = create_test_file("fn main() {}\n", ".rs");
    let output = run_batless(&["--profile", "claude", test_file.path().to_str().unwrap()]);

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    // Without explicit --mode, claude profile's summary default applies
    assert!(stdout.contains("=== File Summary ==="));
    assert!(!stdout.starts_with("{"));
}

#[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"));
}

#[test]
fn test_summary_mode_different_languages() {
    // Test JavaScript
    let js_content = "import React from 'react';\n\nfunction Component() {\n    console.log('test');\n    return <div>Hello</div>;\n}\n\nexport default Component;\n";
    let js_file = create_test_file(js_content, ".js");

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

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("import React"));
    assert!(stdout.contains("function Component"));
    assert!(stdout.contains("export default"));
}

// 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_memory_efficiency_string_handling() {
    // Test that string operations are memory efficient
    let content =
        "use std::collections::HashMap;\n\nfn main() {\n    let map = HashMap::new();\n}\n";
    let file = create_test_file(content, ".rs");

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

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

    // Verify tokens are properly included without excessive memory usage
    assert!(json["identifiers"].is_array());
    assert!(!json["identifiers"].as_array().unwrap().is_empty());
}

#[test]
fn test_schema_validation() {
    // Test JSON schema validation functionality
    let output = run_batless(&["--get-schema", "json_output"]);
    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    let schema: serde_json::Value = serde_json::from_str(&stdout).unwrap();

    assert!(schema["type"].as_str().unwrap() == "object");
    let properties = schema["properties"]
        .as_object()
        .expect("properties should be an object");
    assert!(
        properties.contains_key("identifier_count"),
        "schema should expose identifier_count"
    );
    assert!(
        properties.contains_key("identifiers_truncated"),
        "schema should expose identifiers_truncated"
    );
    assert!(
        properties.contains_key("total_lines_exact"),
        "schema should expose total_lines_exact"
    );
}

#[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"
    );
}

#[test]
fn test_mode_index_with_hash() {
    let content = "pub fn answer() -> i32 { 42 }\n";
    let file = create_test_file(content, ".rs");

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

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

    assert!(
        json.get("file_hash").is_some(),
        "JSON output should contain file_hash field when --hash is passed"
    );
    assert!(
        !json["file_hash"].is_null(),
        "file_hash should not be null when --hash is passed"
    );
}