bashkit 0.9.0

Awesomely fast virtual sandbox with bash and file system
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
//! Tests for awk: parser limits, interpreter limits, end-to-end runs.

use super::parser::AwkParser;
use super::{Awk, csv_split_fields};
use crate::builtins::limits::AWK_MAX_GETLINE_CACHED_FILES as MAX_GETLINE_CACHED_FILES;
use crate::builtins::{Builtin, Context};
use crate::error::Result;
use crate::interpreter::ExecResult;

use crate::fs::{FileSystem, InMemoryFs};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;

async fn run_awk(args: &[&str], stdin: Option<&str>) -> Result<ExecResult> {
    let awk = Awk;
    let fs = Arc::new(InMemoryFs::new());
    let mut vars = HashMap::new();
    let mut cwd = PathBuf::from("/");
    let args: Vec<String> = args.iter().map(|s| s.to_string()).collect();

    let ctx = Context {
        args: &args,
        env: &HashMap::new(),
        variables: &mut vars,
        cwd: &mut cwd,
        fs,
        stdin,
        #[cfg(feature = "http_client")]
        http_client: None,
        #[cfg(feature = "git")]
        git_client: None,
        #[cfg(feature = "ssh")]
        ssh_client: None,
        shell: None,
    };

    awk.execute(ctx).await
}

#[tokio::test]
async fn test_awk_print_all() {
    let result = run_awk(&["{print}"], Some("hello\nworld")).await.unwrap();
    assert_eq!(result.stdout, "hello\nworld\n");
}

#[tokio::test]
async fn test_awk_print_field() {
    let result = run_awk(&["{print $1}"], Some("hello world\nfoo bar"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "hello\nfoo\n");
}

#[tokio::test]
async fn test_awk_print_multiple_fields() {
    let result = run_awk(&["{print $2, $1}"], Some("hello world"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "world hello\n");
}

#[tokio::test]
async fn test_awk_field_separator() {
    let result = run_awk(&["-F:", "{print $1}"], Some("root:x:0:0"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "root\n");
}

#[tokio::test]
async fn test_awk_nr() {
    let result = run_awk(&["{print NR, $0}"], Some("a\nb\nc")).await.unwrap();
    assert_eq!(result.stdout, "1 a\n2 b\n3 c\n");
}

#[tokio::test]
async fn test_awk_nf() {
    let result = run_awk(&["{print NF}"], Some("a b c\nd e")).await.unwrap();
    assert_eq!(result.stdout, "3\n2\n");
}

#[tokio::test]
async fn test_awk_begin_end() {
    let result = run_awk(
        &["BEGIN{print \"start\"} {print} END{print \"end\"}"],
        Some("middle"),
    )
    .await
    .unwrap();
    assert_eq!(result.stdout, "start\nmiddle\nend\n");
}

#[tokio::test]
async fn test_awk_pattern() {
    let result = run_awk(&["/hello/{print}"], Some("hello\nworld\nhello again"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "hello\nhello again\n");
}

#[tokio::test]
async fn test_awk_condition() {
    let result = run_awk(&["NR==2{print}"], Some("line1\nline2\nline3"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "line2\n");
}

#[tokio::test]
async fn test_awk_arithmetic() {
    let result = run_awk(&["{print $1 + $2}"], Some("1 2\n3 4"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "3\n7\n");
}

#[tokio::test]
async fn test_awk_variables() {
    let result = run_awk(&["{sum += $1} END{print sum}"], Some("1\n2\n3\n4"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "10\n");
}

#[tokio::test]
async fn test_awk_unicode_identifier_no_panic() {
    let result = run_awk(&["BEGIN{café=7; print café}"], None).await.unwrap();
    assert_eq!(result.stdout, "7\n");
}

#[tokio::test]
async fn test_awk_length() {
    let result = run_awk(&["{print length($0)}"], Some("hello\nhi"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "5\n2\n");
}

#[tokio::test]
async fn test_awk_substr() {
    let result = run_awk(&["{print substr($0, 2, 3)}"], Some("hello"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "ell\n");
}

#[tokio::test]
async fn test_awk_toupper() {
    let result = run_awk(&["{print toupper($0)}"], Some("hello"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "HELLO\n");
}

#[tokio::test]
async fn test_awk_multi_statement() {
    // Test multiple statements separated by semicolon
    let result = run_awk(&["{x=1; print x}"], Some("test")).await.unwrap();
    assert_eq!(result.stdout, "1\n");
}

#[tokio::test]
async fn test_awk_gsub_with_print() {
    // gsub with regex literal followed by print
    let result = run_awk(
        &[r#"{gsub(/hello/, "hi"); print}"#],
        Some("hello hello hello"),
    )
    .await
    .unwrap();
    assert_eq!(result.stdout, "hi hi hi\n");
}

#[tokio::test]
async fn test_awk_split_with_array_access() {
    // split with array indexing
    let result = run_awk(
        &[r#"{n = split($0, arr, ":"); print arr[2]}"#],
        Some("a:b:c"),
    )
    .await
    .unwrap();
    assert_eq!(result.stdout, "b\n");
}

/// TM-DOS-027: Deeply nested parenthesized expressions must be rejected
#[test]
fn test_awk_parser_depth_limit_parens() {
    // Build expression with 150 nested parens: (((((...(1)...))))
    let depth = 150;
    let open = "(".repeat(depth);
    let close = ")".repeat(depth);
    let program = format!("{{print {open}1{close}}}");

    let mut parser = AwkParser::new(&program);
    let result = parser.parse();
    assert!(result.is_err(), "deeply nested parens must be rejected");
    let err = result.unwrap_err().to_string();
    assert!(
        err.contains("nesting too deep"),
        "error should mention nesting: {err}"
    );
}

/// TM-DOS-027: Deeply chained unary operators must be rejected
#[test]
fn test_awk_parser_depth_limit_unary() {
    // Build expression with 200 chained negations: - - - ... - 1
    let depth = 200;
    let prefix = "- ".repeat(depth);
    let program = format!("{{print {prefix}1}}");

    let mut parser = AwkParser::new(&program);
    let result = parser.parse();
    assert!(result.is_err(), "deeply chained unary ops must be rejected");
    let err = result.unwrap_err().to_string();
    assert!(
        err.contains("nesting too deep"),
        "error should mention nesting: {err}"
    );
}

/// TM-DOS-027: Range patterns must not recursively parse comma chains.
#[test]
fn test_awk_parser_rejects_chained_range_pattern() {
    let operands = std::iter::repeat_n("1", 150).collect::<Vec<_>>().join(",");
    let program = format!("{operands}{{print}}");

    let mut parser = AwkParser::new(&program);
    let result = parser.parse();
    assert!(result.is_err(), "comma-chained ranges must be rejected");
    let err = result.unwrap_err().to_string();
    assert!(
        err.contains("unexpected ',' after range pattern"),
        "error should mention unexpected comma after range: {err}"
    );
}

/// TM-DOS-027: Moderate nesting within limit still works
#[test]
fn test_awk_parser_moderate_nesting_ok() {
    // 10 levels of parens should be fine
    let depth = 10;
    let open = "(".repeat(depth);
    let close = ")".repeat(depth);
    let program = format!("{{print {open}1{close}}}");

    let mut parser = AwkParser::new(&program);
    let result = parser.parse();
    assert!(
        result.is_ok(),
        "moderate nesting should succeed: {:?}", // debug-ok: assert-failure message
        result.err()
    );
}

// === New tests for added features ===

#[tokio::test]
async fn test_awk_for_c_style() {
    let result = run_awk(&["BEGIN{for(i=1;i<=5;i++) print i}"], None)
        .await
        .unwrap();
    assert_eq!(result.stdout, "1\n2\n3\n4\n5\n");
}

#[tokio::test]
async fn test_awk_for_with_body_block() {
    let result = run_awk(&["BEGIN{for(i=0;i<3;i++){print i}}"], None)
        .await
        .unwrap();
    assert_eq!(result.stdout, "0\n1\n2\n");
}

#[tokio::test]
async fn test_awk_while_loop() {
    let result = run_awk(&["BEGIN{i=1; while(i<=3){print i; i++}}"], None)
        .await
        .unwrap();
    assert_eq!(result.stdout, "1\n2\n3\n");
}

#[tokio::test]
async fn test_awk_do_while() {
    let result = run_awk(&["BEGIN{i=1; do{print i; i++}while(i<=3)}"], None)
        .await
        .unwrap();
    assert_eq!(result.stdout, "1\n2\n3\n");
}

#[tokio::test]
async fn test_awk_post_increment() {
    let result = run_awk(&["{print i++}"], Some("a\nb\nc")).await.unwrap();
    assert_eq!(result.stdout, "0\n1\n2\n");
}

#[tokio::test]
async fn test_awk_pre_increment() {
    let result = run_awk(&["{print ++i}"], Some("a\nb\nc")).await.unwrap();
    assert_eq!(result.stdout, "1\n2\n3\n");
}

#[tokio::test]
async fn test_awk_post_decrement() {
    let result = run_awk(&["BEGIN{x=3; print x--; print x}"], None)
        .await
        .unwrap();
    assert_eq!(result.stdout, "3\n2\n");
}

#[tokio::test]
async fn test_awk_array_assign() {
    let result = run_awk(&[r#"BEGIN{a[1]="x"; a[2]="y"; print a[1], a[2]}"#], None)
        .await
        .unwrap();
    assert_eq!(result.stdout, "x y\n");
}

#[tokio::test]
async fn test_awk_array_in_operator() {
    let result = run_awk(
        &[r#"BEGIN{a["foo"]=1; if("foo" in a) print "yes"; if("bar" in a) print "no"}"#],
        None,
    )
    .await
    .unwrap();
    assert_eq!(result.stdout, "yes\n");
}

#[tokio::test]
async fn test_awk_for_in_loop() {
    let result = run_awk(
        &[r#"BEGIN{a[1]="x"; a[2]="y"; for(k in a) count++; print count}"#],
        None,
    )
    .await
    .unwrap();
    assert_eq!(result.stdout, "2\n");
}

#[tokio::test]
async fn test_awk_delete_array_element() {
    let result = run_awk(
        &[r#"BEGIN{a[1]=1; a[2]=2; delete a[1]; for(k in a) print k, a[k]}"#],
        None,
    )
    .await
    .unwrap();
    assert_eq!(result.stdout, "2 2\n");
}

#[tokio::test]
async fn test_awk_v_flag() {
    let result = run_awk(&["-v", "x=hello", "BEGIN{print x}"], None)
        .await
        .unwrap();
    assert_eq!(result.stdout, "hello\n");
}

#[tokio::test]
async fn test_awk_v_flag_numeric() {
    let result = run_awk(&["-v", "n=42", "BEGIN{print n+1}"], None)
        .await
        .unwrap();
    assert_eq!(result.stdout, "43\n");
}

#[tokio::test]
async fn test_awk_break_in_for() {
    let result = run_awk(&["BEGIN{for(i=1;i<=10;i++){if(i>3) break; print i}}"], None)
        .await
        .unwrap();
    assert_eq!(result.stdout, "1\n2\n3\n");
}

#[tokio::test]
async fn test_awk_continue_in_for() {
    let result = run_awk(
        &["BEGIN{for(i=1;i<=5;i++){if(i==3) continue; print i}}"],
        None,
    )
    .await
    .unwrap();
    assert_eq!(result.stdout, "1\n2\n4\n5\n");
}

#[tokio::test]
async fn test_awk_ternary() {
    let result = run_awk(&[r#"{print ($1>2 ? "big" : "small")}"#], Some("1\n3\n2"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "small\nbig\nsmall\n");
}

#[tokio::test]
async fn test_awk_field_assignment() {
    let result = run_awk(&[r#"{$2="new"; print}"#], Some("one two three"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "one new three\n");
}

#[tokio::test]
async fn test_awk_csv_to_json_pattern() {
    // This is the pattern LLMs use for CSV→JSON conversion
    let result = run_awk(
        &[
            "-F,",
            r#"NR==1{for(i=1;i<=NF;i++) h[i]=$i; next} {for(i=1;i<=NF;i++) printf "%s=%s ", h[i], $i; print ""}"#,
        ],
        Some("name,age\nalice,30\nbob,25"),
    )
    .await
    .unwrap();
    assert!(result.stdout.contains("name=alice"));
    assert!(result.stdout.contains("age=30"));
    assert!(result.stdout.contains("name=bob"));
}

#[tokio::test]
async fn test_awk_compound_array_assign() {
    let result = run_awk(
        &[r#"{count[$1]++} END{for(k in count) print k, count[k]}"#],
        Some("a\nb\na\nc\nb\na"),
    )
    .await
    .unwrap();
    // Order may vary, so check contents
    assert!(result.stdout.contains("a 3"));
    assert!(result.stdout.contains("b 2"));
    assert!(result.stdout.contains("c 1"));
}

#[tokio::test]
async fn test_awk_next_statement() {
    let result = run_awk(&["NR==2{next} {print}"], Some("line1\nline2\nline3"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "line1\nline3\n");
}

#[tokio::test]
async fn test_awk_exit_statement() {
    let result = run_awk(&["NR==2{exit} {print}"], Some("line1\nline2\nline3"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "line1\n");
}

#[tokio::test]
async fn test_awk_getline_basic() {
    let result = run_awk(&["{getline; print}"], Some("line1\nline2"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "line2\n");
}

#[tokio::test]
async fn test_awk_getline_updates_fields() {
    let result = run_awk(&["{getline; print $1}"], Some("a b\nc d"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "c\n");
}

#[tokio::test]
async fn test_awk_getline_at_eof() {
    // getline at EOF should keep current $0
    let result = run_awk(&["{getline; print}"], Some("only")).await.unwrap();
    assert_eq!(result.stdout, "only\n");
}

#[tokio::test]
async fn test_awk_revenue_calculation() {
    // This is the exact eval task pattern
    let result = run_awk(
        &["-F,", "NR>1{total+=$2*$3} END{print total}"],
        Some("product,price,quantity\nwidget,10,5\ngadget,25,3\ndoohickey,7,12\nsprocket,15,8"),
    )
    .await
    .unwrap();
    assert_eq!(result.stdout, "329\n");
}

#[tokio::test]
async fn test_awk_printf_parens() {
    // printf with parenthesized syntax: printf("format", args)
    let result = run_awk(
        &[r#"BEGIN{printf("["); printf("%s", "x"); printf("]"); print ""}"#],
        Some(""),
    )
    .await
    .unwrap();
    assert_eq!(result.stdout, "[x]\n");
}

#[tokio::test]
async fn test_awk_printf_parens_csv() {
    // CSV to JSON pattern using printf with parens
    let result = run_awk(
        &[
            "-F,",
            r#"NR==1{for(i=1;i<=NF;i++) h[i]=$i; next} {printf("%s{", (NR>2?",":"")); for(i=1;i<=NF;i++){printf("%s\"%s\":\"%s\"", (i>1?",":""), h[i], $i)}; printf("}")} END{print ""}"#,
        ],
        Some("name,age\nalice,30\nbob,25\n"),
    )
    .await
    .unwrap();
    assert!(result.stdout.contains("alice"));
    assert!(result.stdout.contains("bob"));
}

#[tokio::test]
async fn test_awk_recursive_function_depth_limit() {
    // Recursive function should be limited, not stack overflow
    let result = run_awk(
        &[r#"function r(n) { return r(n+1) } BEGIN { print r(0) }"#],
        Some(""),
    )
    .await
    .unwrap();
    // Should complete without crashing (returns Uninitialized -> empty string)
    assert_eq!(result.exit_code, 0);
}

#[tokio::test]
async fn test_awk_while_loop_limited() {
    // Infinite while loop should terminate via default max_loop_iterations
    let result = run_awk(
        &[r#"BEGIN { i=0; while(1) { i++; if(i>200000) break } print i }"#],
        Some(""),
    )
    .await
    .unwrap();
    assert_eq!(result.exit_code, 0);
    let count: usize = result.stdout.trim().parse().unwrap();
    // Should be capped at default max_loop_iterations (10_000), not 200_000
    assert!(
        count <= 10_001,
        "loop ran {} times, expected <= 10001",
        count
    );
}

#[tokio::test]
async fn test_awk_unicode_in_comment() {
    // Issue #395: multi-byte Unicode chars in comments should not panic
    let result = run_awk(&["# ── header ──────\n{ print $1 }"], Some("hello world\n"))
        .await
        .unwrap();
    assert_eq!(result.exit_code, 0);
    assert_eq!(result.stdout.trim(), "hello");
}

#[tokio::test]
async fn test_awk_unicode_in_string() {
    // Multi-byte chars in string literals should not panic
    let result = run_awk(&[r#"BEGIN { print "café" }"#], Some(""))
        .await
        .unwrap();
    assert_eq!(result.exit_code, 0);
    assert_eq!(result.stdout.trim(), "café");
}

#[tokio::test]
async fn test_awk_array_assign_field_ref_subscript() {
    // Issue #396.1: arr[$1] = $3 should work with field refs as subscripts
    let result = run_awk(
        &["{ arr[$1] = $2 } END { print arr[\"hello\"] }"],
        Some("hello world\n"),
    )
    .await
    .unwrap();
    assert_eq!(result.exit_code, 0);
    assert_eq!(result.stdout.trim(), "world");
}

#[tokio::test]
async fn test_awk_multi_subscript() {
    // Issue #396.2: a["x","y"] multi-subscript with SUBSEP
    let result = run_awk(&[r#"BEGIN { a["x","y"] = 1; print a["x","y"] }"#], Some(""))
        .await
        .unwrap();
    assert_eq!(result.exit_code, 0);
    assert_eq!(result.stdout.trim(), "1");
}

#[tokio::test]
async fn test_awk_subsep_defined() {
    // Issue #396.3: SUBSEP should be defined as \034
    let result = run_awk(&[r#"BEGIN { print length(SUBSEP) }"#], Some(""))
        .await
        .unwrap();
    assert_eq!(result.exit_code, 0);
    assert_eq!(result.stdout.trim(), "1");
}

#[tokio::test]
async fn test_awk_preincrement_array() {
    // Issue #396.4: ++arr[key] should work
    let result = run_awk(
        &["{ ++count[$1] } END { for (k in count) print k, count[k] }"],
        Some("a\nb\na\n"),
    )
    .await
    .unwrap();
    assert_eq!(result.exit_code, 0);
    assert!(result.stdout.contains("a 2"));
    assert!(result.stdout.contains("b 1"));
}

/// Helper that returns the VFS alongside the result for testing file output.
async fn run_awk_with_fs(
    args: &[&str],
    stdin: Option<&str>,
) -> (Result<ExecResult>, Arc<InMemoryFs>) {
    let awk = Awk;
    let fs = Arc::new(InMemoryFs::new());
    let mut vars = HashMap::new();
    let mut cwd = PathBuf::from("/");
    let args: Vec<String> = args.iter().map(|s| s.to_string()).collect();

    let ctx = Context {
        args: &args,
        env: &HashMap::new(),
        variables: &mut vars,
        cwd: &mut cwd,
        fs: fs.clone(),
        stdin,
        #[cfg(feature = "http_client")]
        http_client: None,
        #[cfg(feature = "git")]
        git_client: None,
        #[cfg(feature = "ssh")]
        ssh_client: None,
        shell: None,
    };

    let result = awk.execute(ctx).await;
    (result, fs)
}

#[tokio::test]
async fn test_awk_print_redirect_truncate() {
    // Issue #607: print ... > file should create file with content
    let (result, fs) = run_awk_with_fs(&[r#"BEGIN{print "hello" > "/tmp/out"}"#], None).await;
    let result = result.unwrap();
    assert_eq!(result.exit_code, 0);
    // stdout should be empty (output went to file)
    assert_eq!(result.stdout, "");
    let content = fs
        .read_file(std::path::Path::new("/tmp/out"))
        .await
        .unwrap();
    assert_eq!(String::from_utf8_lossy(&content), "hello\n");
}

#[tokio::test]
async fn test_awk_printf_redirect_truncate() {
    // Issue #607: printf ... > file should create file with content
    let (result, fs) = run_awk_with_fs(&[r#"BEGIN{printf "hello" > "/tmp/out"}"#], None).await;
    let result = result.unwrap();
    assert_eq!(result.exit_code, 0);
    assert_eq!(result.stdout, "");
    let content = fs
        .read_file(std::path::Path::new("/tmp/out"))
        .await
        .unwrap();
    assert_eq!(String::from_utf8_lossy(&content), "hello");
}

#[tokio::test]
async fn test_awk_print_redirect_append() {
    // Issue #607: print ... >> file should append
    let (result, fs) = run_awk_with_fs(
        &[r#"BEGIN{print "a" > "/tmp/out"; print "b" >> "/tmp/out"}"#],
        None,
    )
    .await;
    let result = result.unwrap();
    assert_eq!(result.exit_code, 0);
    let content = fs
        .read_file(std::path::Path::new("/tmp/out"))
        .await
        .unwrap();
    assert_eq!(String::from_utf8_lossy(&content), "a\nb\n");
}

#[tokio::test]
async fn test_awk_print_redirect_multiple_to_same_file() {
    // Multiple prints to same file with > should accumulate (AWK keeps file open)
    let (result, fs) = run_awk_with_fs(
        &[r#"BEGIN{print "line1" > "/tmp/out"; print "line2" > "/tmp/out"}"#],
        None,
    )
    .await;
    let result = result.unwrap();
    assert_eq!(result.exit_code, 0);
    let content = fs
        .read_file(std::path::Path::new("/tmp/out"))
        .await
        .unwrap();
    assert_eq!(String::from_utf8_lossy(&content), "line1\nline2\n");
}

#[tokio::test]
async fn test_awk_print_redirect_pipe_unsupported() {
    // Pipe output should return clear error
    let (result, _fs) = run_awk_with_fs(&[r#"BEGIN{print "hello" | "cat"}"#], None).await;
    assert!(result.is_err());
    let err_msg = result.unwrap_err().to_string();
    assert!(
        err_msg.contains("pipe"),
        "expected pipe error, got: {err_msg}"
    );
}

// ========================================================================
// --csv flag (issues #617, #618)
// ========================================================================

#[tokio::test]
async fn test_awk_csv_basic() {
    let result = run_awk(&["--csv", "{print $2}"], Some("a,b,c\nd,e,f"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "b\ne\n");
}

#[tokio::test]
async fn test_awk_csv_quoted_fields() {
    // Quoted field with embedded comma
    let result = run_awk(&["--csv", "{print $2}"], Some(r#"1,"hello, world",3"#))
        .await
        .unwrap();
    assert_eq!(result.stdout, "hello, world\n");
}

#[tokio::test]
async fn test_awk_csv_escaped_quotes() {
    // Double-quote escaping per RFC 4180
    let result = run_awk(&["--csv", "{print $2}"], Some(r#"1,"she said ""hi""",3"#))
        .await
        .unwrap();
    assert_eq!(result.stdout, "she said \"hi\"\n");
}

#[tokio::test]
async fn test_awk_csv_nf() {
    let result = run_awk(&["--csv", "{print NF}"], Some("a,b,c"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "3\n");
}

#[tokio::test]
async fn test_awk_csv_ofs() {
    // In CSV mode, OFS defaults to comma
    let result = run_awk(&["--csv", "{print $1, $3}"], Some("a,b,c"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "a,c\n");
}

#[tokio::test]
async fn test_awk_csv_empty_fields() {
    let result = run_awk(&["--csv", "{print NF}"], Some("a,,c,"))
        .await
        .unwrap();
    assert_eq!(result.stdout, "4\n");
}

#[tokio::test]
async fn test_awk_csv_k_flag_alias() {
    // -k is an alias for --csv
    let result = run_awk(&["-k", "{print $2}"], Some("a,b,c")).await.unwrap();
    assert_eq!(result.stdout, "b\n");
}

#[test]
fn test_csv_split_fields_unit() {
    assert_eq!(csv_split_fields("a,b,c"), vec!["a", "b", "c"]);
    assert_eq!(
        csv_split_fields(r#"1,"hello, world",3"#),
        vec!["1", "hello, world", "3"]
    );
    assert_eq!(csv_split_fields(r#""a""b",c"#), vec!["a\"b", "c"]);
    assert_eq!(csv_split_fields("a,,c,"), vec!["a", "", "c", ""]);
}

// ========================================================================
// gawk 5.3+ Unicode escape sequences (issue #617)
// ========================================================================

#[tokio::test]
async fn test_awk_unicode_escape_basic() {
    // \u followed by hex digits → Unicode character
    let result = run_awk(&[r#"BEGIN{print "\u0041"}"#], None).await.unwrap();
    assert_eq!(result.stdout, "A\n");
}

#[tokio::test]
async fn test_awk_unicode_escape_multibyte() {
    // \u00E9 → é (Latin small e with acute)
    let result = run_awk(&[r#"BEGIN{print "\u00E9"}"#], None).await.unwrap();
    assert_eq!(result.stdout, "é\n");
}

#[tokio::test]
async fn test_awk_unicode_escape_emoji() {
    // \u1F600 → 😀 (grinning face, 5 hex digits)
    let result = run_awk(&[r#"BEGIN{print "\u1F600"}"#], None).await.unwrap();
    assert_eq!(result.stdout, "😀\n");
}

#[tokio::test]
async fn test_awk_unicode_escape_bare_u() {
    // \u with no hex digits → literal \u
    let result = run_awk(&[r#"BEGIN{print "\u "}"#], None).await.unwrap();
    assert_eq!(result.stdout, "\\u \n");
}

#[tokio::test]
async fn test_awk_unicode_escape_mixed() {
    // Mix of Unicode escapes and regular text
    let result = run_awk(&[r#"BEGIN{print "caf\u00E9"}"#], None)
        .await
        .unwrap();
    assert_eq!(result.stdout, "café\n");
}

// ========================================================================
// getline from file (issue #796)
// ========================================================================

/// Helper: create an in-memory FS with a file, then run awk.
async fn run_awk_with_file(
    args: &[&str],
    stdin: Option<&str>,
    file_path: &str,
    file_content: &str,
) -> Result<ExecResult> {
    let awk = Awk;
    let fs = Arc::new(InMemoryFs::new());
    fs.write_file(std::path::Path::new(file_path), file_content.as_bytes())
        .await
        .unwrap();
    let mut vars = HashMap::new();
    let mut cwd = PathBuf::from("/");
    let args: Vec<String> = args.iter().map(|s| s.to_string()).collect();

    let ctx = Context {
        args: &args,
        env: &HashMap::new(),
        variables: &mut vars,
        cwd: &mut cwd,
        fs,
        stdin,
        #[cfg(feature = "http_client")]
        http_client: None,
        #[cfg(feature = "git")]
        git_client: None,
        #[cfg(feature = "ssh")]
        ssh_client: None,
        shell: None,
    };

    awk.execute(ctx).await
}

#[tokio::test]
async fn test_awk_getline_file_into_var() {
    // getline var < "file" reads lines one at a time
    let result = run_awk_with_file(
        &[r#"BEGIN{while((getline line < "/tmp/data.txt") > 0) print line}"#],
        None,
        "/tmp/data.txt",
        "alpha\nbeta\ngamma",
    )
    .await
    .unwrap();
    assert_eq!(result.stdout, "alpha\nbeta\ngamma\n");
}

#[tokio::test]
async fn test_awk_getline_file_no_var() {
    // getline < "file" without variable updates $0
    let result = run_awk_with_file(
        &[r#"BEGIN{getline < "/tmp/data.txt"; print}"#],
        None,
        "/tmp/data.txt",
        "first\nsecond",
    )
    .await
    .unwrap();
    assert_eq!(result.stdout, "first\n");
}

#[tokio::test]
async fn test_awk_getline_file_eof() {
    // getline at EOF keeps variable unchanged
    let result = run_awk_with_file(
        &[r#"BEGIN{getline x < "/tmp/f.txt"; getline x < "/tmp/f.txt"; print x}"#],
        None,
        "/tmp/f.txt",
        "only",
    )
    .await
    .unwrap();
    assert_eq!(result.stdout, "only\n");
}

#[tokio::test]
async fn test_awk_getline_file_missing() {
    // getline from non-existent file should not crash (returns -1 / empty)
    let result = run_awk(&[r#"BEGIN{getline x < "/no/such/file"; print "ok"}"#], None)
        .await
        .unwrap();
    assert_eq!(result.stdout, "ok\n");
}

#[tokio::test]
async fn test_awk_output_limit_exceeded() {
    // Each iteration prints a 1000-char line. 100k iters = ~100MB >> 10MB limit.
    let result = run_awk(
        &[r#"BEGIN { s = sprintf("%1000s", "x"); for(i=0;i<100000;i++) print s }"#],
        None,
    )
    .await
    .unwrap();
    assert_eq!(result.exit_code, 2);
    assert!(
        result.stderr.contains("output limit exceeded"),
        "stderr should mention output limit: {}",
        result.stderr
    );
    assert!(
        result.stdout.len() <= 11_000_000,
        "stdout should be bounded: {} bytes",
        result.stdout.len()
    );
}

#[tokio::test]
async fn test_awk_output_under_limit_ok() {
    // Small output well under 10MB should succeed normally
    let result = run_awk(&[r#"BEGIN { for(i=0;i<100;i++) print "hello" }"#], None)
        .await
        .unwrap();
    assert_eq!(result.exit_code, 0);
    let lines: Vec<&str> = result.stdout.trim().split('\n').collect();
    assert_eq!(lines.len(), 100);
}

#[tokio::test]
async fn test_awk_file_redirect_output_limit() {
    // File redirect output should also be bounded
    let result = run_awk(
        &[r#"BEGIN { s = sprintf("%1000s", "x"); for(i=0;i<100000;i++) print s > "/tmp/out" }"#],
        None,
    )
    .await
    .unwrap();
    assert_eq!(result.exit_code, 2);
    assert!(
        result.stderr.contains("output limit exceeded"),
        "stderr should mention output limit: {}",
        result.stderr
    );
}

/// Helper: run AWK with a caller-provided VFS.
async fn run_awk_with_custom_fs(
    args: &[&str],
    stdin: Option<&str>,
    fs: Arc<InMemoryFs>,
) -> Result<ExecResult> {
    let awk = Awk;
    let mut vars = HashMap::new();
    let mut cwd = PathBuf::from("/");
    let args: Vec<String> = args.iter().map(|s| s.to_string()).collect();

    let ctx = Context {
        args: &args,
        env: &HashMap::new(),
        variables: &mut vars,
        cwd: &mut cwd,
        fs,
        stdin,
        #[cfg(feature = "http_client")]
        http_client: None,
        #[cfg(feature = "git")]
        git_client: None,
        #[cfg(feature = "ssh")]
        ssh_client: None,
        shell: None,
    };

    awk.execute(ctx).await
}

#[tokio::test]
async fn test_awk_getline_file_cache_limit_exceeded() {
    // Opening more than MAX_GETLINE_CACHED_FILES distinct files must fail
    // gracefully (getline returns -1 for new files beyond the limit).
    use crate::fs::FsLimits;

    let limits = FsLimits {
        max_file_count: 200_000,
        max_total_bytes: 200_000_000,
        ..FsLimits::default()
    };
    let fs = Arc::new(InMemoryFs::with_limits(limits));
    let count = MAX_GETLINE_CACHED_FILES + 5;
    for i in 0..count {
        fs.write_file(
            std::path::Path::new(&format!("/tmp/f{i}.txt")),
            format!("line{i}").as_bytes(),
        )
        .await
        .unwrap();
    }

    // AWK program: read one line from each file, count successes
    let prog = format!(
        r#"BEGIN{{ ok=0; for(i=0;i<{count};i++) {{ f="/tmp/f"i".txt"; if((getline x < f)>0) ok++ }} print ok }}"#,
    );
    let result = run_awk_with_custom_fs(&[&prog], None, fs).await.unwrap();
    let ok: usize = result.stdout.trim().parse().unwrap();
    // Exactly MAX_GETLINE_CACHED_FILES should succeed, rest should fail
    assert_eq!(ok, MAX_GETLINE_CACHED_FILES);
}

#[tokio::test]
async fn test_awk_getline_file_cache_within_limit() {
    // Opening a reasonable number of files should all succeed.
    let fs = Arc::new(InMemoryFs::new());
    let count = 10;
    for i in 0..count {
        fs.write_file(
            std::path::Path::new(&format!("/tmp/f{i}.txt")),
            format!("data{i}").as_bytes(),
        )
        .await
        .unwrap();
    }

    let prog = format!(
        r#"BEGIN{{ ok=0; for(i=0;i<{count};i++) {{ f="/tmp/f"i".txt"; if((getline x < f)>0) ok++ }} print ok }}"#,
    );
    let result = run_awk_with_custom_fs(&[&prog], None, fs).await.unwrap();
    let ok: usize = result.stdout.trim().parse().unwrap();
    assert_eq!(ok, count);
}

#[tokio::test]
async fn test_awk_getline_file_size_limit() {
    // A file exceeding FsLimits::max_file_size is rejected by getline.
    // Defense-in-depth: VFS also enforces limits, so a file at exactly
    // the boundary is accepted while one over is rejected at VFS level.
    use crate::fs::FsLimits;

    let limits = FsLimits {
        max_file_size: 100,
        ..FsLimits::unlimited()
    };
    let fs = Arc::new(InMemoryFs::with_limits(limits));
    // Write a file within limits -- should be readable via getline.
    fs.write_file(std::path::Path::new("/tmp/ok.txt"), &[b'a'; 100])
        .await
        .unwrap();
    // Attempt to write an oversized file -- VFS rejects it, so getline
    // returns -1 (file not found).
    let _ = fs
        .write_file(std::path::Path::new("/tmp/big.txt"), &[b'x'; 101])
        .await;

    // Within-limit file succeeds
    let result = run_awk_with_custom_fs(
        &[r#"BEGIN{r=(getline x < "/tmp/ok.txt"); print r}"#],
        None,
        fs.clone(),
    )
    .await
    .unwrap();
    assert_eq!(result.stdout, "1\n");

    // Over-limit file fails (not stored by VFS)
    let result = run_awk_with_custom_fs(
        &[r#"BEGIN{r=(getline x < "/tmp/big.txt"); print r}"#],
        None,
        fs,
    )
    .await
    .unwrap();
    assert_eq!(result.stdout, "-1\n");
}

// TM-INF-022: malformed-input corpus must not leak Debug shapes.
const AWK_BANNED: &[&str] = &["AwkError::", "ParseError {", "Token::"];

#[tokio::test]
async fn no_leak_unbalanced_brace() {
    let r = crate::builtins::debug_leak_check::run("echo 1 | awk 'BEGIN { print'").await;
    crate::builtins::debug_leak_check::assert_no_leak(&r, "awk_unbalanced_brace", AWK_BANNED);
}

#[tokio::test]
async fn no_leak_invalid_regex() {
    let r = crate::builtins::debug_leak_check::run("echo 1 | awk '/[/'").await;
    crate::builtins::debug_leak_check::assert_no_leak(&r, "awk_invalid_regex", AWK_BANNED);
}

#[tokio::test]
async fn no_leak_undefined_function_call() {
    let r =
        crate::builtins::debug_leak_check::run("echo 1 | awk 'BEGIN { totally_undefined_fn() }'")
            .await;
    crate::builtins::debug_leak_check::assert_no_leak(
        &r,
        "awk_undefined_function_call",
        AWK_BANNED,
    );
}