dpc-tau-ext-shell 0.2.0

A minimal Unix-first coding agent.
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
use tempfile::TempDir;

use super::*;

/// Ensures the raw integer-second configuration field has the same default and
/// accepted zero/value forms while shell dispatch retains a unit-safe duration.
#[test]
fn user_command_timeout_config_decodes_integer_seconds_to_duration() {
    assert_eq!(
        ShellConfig::default().user_command_timeout,
        Duration::from_secs(60 * 60)
    );

    let configured: ShellConfig = serde_json::from_value(serde_json::json!({
        "user_command_timeout_secs": 17
    }))
    .expect("parse integer-second timeout");
    assert_eq!(configured.user_command_timeout, Duration::from_secs(17));

    let zero: ShellConfig = serde_json::from_value(serde_json::json!({
        "user_command_timeout_secs": 0
    }))
    .expect("parse zero timeout");
    assert_eq!(zero.user_command_timeout, Duration::ZERO);
}

/// Ensures omission preserves unrestricted behavior without requiring the
/// supplied cwd to exist.
#[test]
fn absent_allowlist_does_not_touch_or_restrict_the_workdir() {
    let config = ShellConfig::default();
    assert_eq!(
        config
            .authorize("anything", Path::new("/definitely/missing/tau-cwd"))
            .expect("absent allowlist is unrestricted"),
        None
    );
}

/// Ensures omitted command enforcement preserves the existing shell prompt
/// without an allowlist declaration.
#[test]
fn absent_allowlist_omits_the_prompt_fragment() {
    assert_eq!(ShellConfig::default().allowlist_prompt_fragment(), None);
}

/// Ensures an enabled allowlist renders its typed command selectors and paired
/// canonical-workdir selectors rather than claiming they are literal commands.
#[test]
fn allowlist_prompt_lists_typed_selector_pairs() {
    let config: ShellConfig = serde_json::from_value(serde_json::json!({
        "allowlist": [
            {
                "workdir": "/srv/project/**",
                "command": "cargo *"
            },
            {
                "workdir": "/srv/project",
                "command_regex": "jj (?:log|show)"
            }
        ]
    }))
    .expect("parse allowlist");

    assert_eq!(
        config.allowlist_prompt_fragment(),
        Some(
            "\n\n### Shell command allowlist\n\n\
             Shell command enforcement is enabled. A raw shell command and its \
             canonical effective workdir must both match one selector pair:\n\
             - command_glob: \"cargo *\"; workdir: \"/srv/project/**\"\n\
             - command_regex: \"jj (?:log|show)\"; workdir: \"/srv/project\""
                .to_owned()
        )
    );
}

/// Ensures optional rule descriptions use the same JSON-safe presentation on
/// denials and brace-safe presentation in the model prompt.
#[test]
fn allowlist_description_renders_safely_on_both_surfaces() {
    let workdir = TempDir::new().expect("workdir");
    let description = "Use \"jj log\"\\only.\n\t\u{1f} {{literal}} café";
    let config: ShellConfig = serde_json::from_value(serde_json::json!({
        "allowlist": [{
            "workdir": workdir.path().display().to_string(),
            "command_regex": "jj log",
            "description": description
        }]
    }))
    .expect("parse described allowlist");

    let prompt = config
        .allowlist_prompt_fragment()
        .expect("enabled allowlist has a prompt fragment");
    assert!(prompt.contains(
        r#"description: "Use \"jj log\"\\only.\n\t\u001f \u007b\u007bliteral\u007d\u007d café""#
    ));
    assert!(!prompt.contains("{{literal}}"));

    let error = config
        .authorize("denied-command-sentinel", workdir.path())
        .expect_err("command must be denied");
    assert!(error.contains(r#"description: "Use \"jj log\"\\only.\n\t\u001f {{literal}} café""#));
    assert!(!error.contains("denied-command-sentinel"));
}

/// Ensures an explicit empty allowlist states the total command denial instead
/// of making an enabled guardrail appear unrestricted.
#[test]
fn empty_allowlist_prompt_states_that_all_commands_are_denied() {
    let config: ShellConfig =
        serde_json::from_value(serde_json::json!({ "allowlist": [] })).expect("parse allowlist");

    assert_eq!(
        config.allowlist_prompt_fragment(),
        Some(
            "\n\n### Shell command allowlist\n\n\
             Shell command enforcement is enabled. A raw shell command and its \
             canonical effective workdir must both match one selector pair:\n\
             - none (all shell commands are denied)"
                .to_owned()
        )
    );
}

/// Ensures prompt presentation has stable set ordering while execution retains
/// the configured authored-rule order and duplicates.
#[test]
fn allowlist_prompt_sorts_and_deduplicates_selector_pairs() {
    let config: ShellConfig = serde_json::from_value(serde_json::json!({
        "allowlist": [
            {
                "workdir": "/srv/z",
                "command_regex": "z"
            },
            {
                "workdir": "/srv/a",
                "command": "a"
            },
            {
                "workdir": "/srv/z",
                "command_regex": "z"
            }
        ]
    }))
    .expect("parse allowlist");

    let prompt = config
        .allowlist_prompt_fragment()
        .expect("enabled allowlist has a prompt fragment");
    assert_eq!(prompt.matches("command_regex: \"z\"").count(), 1);
    assert!(
        prompt.find("command_glob: \"a\"").expect("glob selector")
            < prompt.find("command_regex: \"z\"").expect("regex selector")
    );
}

/// Ensures prompt de-duplication compares the complete rendered rule while
/// denial diagnostics retain authored order and duplicates.
#[test]
fn allowlist_description_participates_in_prompt_identity_only() {
    let workdir = TempDir::new().expect("workdir");
    let workdir_text = workdir.path().display().to_string();
    let config: ShellConfig = serde_json::from_value(serde_json::json!({
        "allowlist": [
            { "workdir": workdir_text, "command": "jj *", "description": "first" },
            { "workdir": workdir_text, "command": "jj *", "description": "second" },
            { "workdir": workdir_text, "command": "jj *", "description": "first" }
        ]
    }))
    .expect("parse duplicate described rules");

    let prompt = config
        .allowlist_prompt_fragment()
        .expect("enabled allowlist has a prompt fragment");
    assert_eq!(prompt.matches(r#"description: "first""#).count(), 1);
    assert_eq!(prompt.matches(r#"description: "second""#).count(), 1);

    let error = config
        .authorize("denied", workdir.path())
        .expect_err("command must be denied");
    assert_eq!(error.matches(r#"description: "first""#).count(), 2);
    assert_eq!(error.matches(r#"description: "second""#).count(), 1);
    assert!(
        error.find(r#"description: "first""#).expect("first")
            < error.find(r#"description: "second""#).expect("second")
    );
}

/// Ensures valid glob braces remain the exact JSON-decoded selector while
/// escaping Handlebars delimiters before the selector reaches prompt assembly.
#[test]
fn allowlist_prompt_escapes_handlebars_delimiters_in_glob_selectors() {
    let config: ShellConfig = serde_json::from_value(serde_json::json!({
        "allowlist": [{
            "workdir": "/srv/project",
            "command": "{{cargo,jj}} *"
        }]
    }))
    .expect("parse nested-brace glob");

    let prompt = config
        .allowlist_prompt_fragment()
        .expect("enabled allowlist has a prompt fragment");
    let selector = r#""\u007b\u007bcargo,jj\u007d\u007d *""#;
    assert!(prompt.contains(&format!("command_glob: {selector}")));
    assert_eq!(
        serde_json::from_str::<String>(selector).expect("selector JSON"),
        "{{cargo,jj}} *"
    );
    assert!(
        !prompt.contains("{{"),
        "selectors must stay literal when appended to a Handlebars template"
    );
}

/// Ensures command and workdir matchers bind as one conjunctive rule rather
/// than combining halves from different entries.
#[test]
fn allowlist_rules_bind_command_and_workdir_as_pairs() {
    let temp = TempDir::new().expect("tempdir");
    let other = TempDir::new().expect("other tempdir");
    let config: ShellConfig = serde_json::from_value(serde_json::json!({
        "allowlist": [
            {
                "workdir": temp.path().display().to_string(),
                "command": "cargo *"
            },
            {
                "workdir": other.path().display().to_string(),
                "command": "jj status"
            }
        ]
    }))
    .expect("parse allowlist");

    assert!(
        config
            .authorize("cargo test", temp.path())
            .expect("matching pair")
            .is_some()
    );
    let error = config
        .authorize("jj status", temp.path())
        .expect_err("split pair must not authorize");
    assert!(error.contains("allowed command/workdir rule pairs:"));
    assert!(error.contains(r#"command_glob: "cargo *""#));
    assert!(error.contains(&format!(r#"workdir: "{}""#, temp.path().display())));
}

/// Ensures the documented `jj` regular expression accepts only the intended
/// raw command language at each length and shell-syntax boundary.
#[test]
fn command_regex_matches_only_the_documented_jj_language() {
    let temp = TempDir::new().expect("tempdir");
    let config: ShellConfig = serde_json::from_value(serde_json::json!({
        "allowlist": [{
            "workdir": temp.path().display().to_string(),
            "command_regex": "jj (?:log|show [a-z]{6,32})"
        }]
    }))
    .expect("parse regex allowlist");

    for command in [
        "jj log",
        "jj show abcdef",
        "jj show abcdefghijklmnopqrstuvwxyzabcdef",
    ] {
        assert!(
            config
                .authorize(command, temp.path())
                .expect("permitted command")
                .is_some(),
            "{command:?} must match"
        );
    }
    for command in [
        "jj show abcde",
        "jj show abcdefghijklmnopqrstuvwxyzabcdefg",
        "jj show ABCDEF",
        "jj show abcde1",
        "jj show abcdéf",
        " jj log",
        "jj  log",
        "jj log ",
        "jj log\n",
        "jj log --no-pager",
        "jj show abcdef extra",
        "jj log | cat",
        "jj log >output",
        "jj log $(true)",
        "jj log `true`",
        "jj log; true",
        "jj log && true",
        "jj log || true",
    ] {
        assert!(
            config.authorize(command, temp.path()).is_err(),
            "{command:?} must be denied"
        );
    }
}

/// Ensures regex rules implicitly use absolute anchors even when multiline mode
/// would make ordinary line anchors accept a substring.
#[test]
fn command_regex_uses_implicit_absolute_whole_string_anchors() {
    let temp = TempDir::new().expect("tempdir");
    let config: ShellConfig = serde_json::from_value(serde_json::json!({
        "allowlist": [{
            "workdir": temp.path().display().to_string(),
            "command_regex": "(?m)^printf allowed$"
        }]
    }))
    .expect("parse regex allowlist");

    assert!(
        config
            .authorize("printf allowed", temp.path())
            .expect("whole command matches")
            .is_some()
    );
    assert!(
        config
            .authorize("printf allowed\nprintf denied", temp.path())
            .is_err(),
        "multiline mode must not weaken implicit absolute anchors"
    );
}

/// Ensures regex configuration stays case-sensitive even if an authored inline
/// flag attempts to weaken that stated allowlist invariant.
#[test]
fn command_regex_rejects_case_insensitive_inline_flags() {
    let error = serde_json::from_value::<ShellConfig>(serde_json::json!({
        "allowlist": [{
            "workdir": "/tmp/**",
            "command_regex": "(?i)jj log"
        }]
    }))
    .expect_err("case-insensitive regex must fail configuration");
    assert!(
        error
            .to_string()
            .contains("shell allowlist command regex must remain case-sensitive")
    );

    for pattern in [r"(?-i:jj log)", "(?x)# (?i)\njj\\x20log"] {
        let config: ShellConfig = serde_json::from_value(serde_json::json!({
            "allowlist": [{
                "workdir": "/tmp",
                "command_regex": pattern
            }]
        }))
        .expect("syntax-aware validation must accept case-sensitive regex");
        assert!(
            config
                .authorize("jj log", Path::new("/tmp"))
                .expect("accepted case-sensitive regex")
                .is_some()
        );
    }
}

/// Ensures command matching is anchored over the raw string while treating
/// separators and newlines as ordinary characters.
#[test]
fn command_globs_match_the_whole_raw_multiline_string() {
    let temp = TempDir::new().expect("tempdir");
    let config: ShellConfig = serde_json::from_value(serde_json::json!({
        "allowlist": [{
            "workdir": temp.path().display().to_string(),
            "command": "printf *"
        }]
    }))
    .expect("parse allowlist");

    assert!(
        config
            .authorize("printf a/b\nprintf second", temp.path())
            .expect("star spans separators and newline")
            .is_some()
    );
    assert!(
        config
            .authorize("x printf value", temp.path())
            .expect_err("glob is whole-string anchored")
            .contains("denied by configured allowlist")
    );
    assert!(
        config
            .authorize("Printf value", temp.path())
            .expect_err("matching is case-sensitive")
            .contains("denied by configured allowlist")
    );
}

/// Ensures workdir stars remain component-aware while double stars cross
/// multiple canonical path components.
#[test]
fn workdir_globs_distinguish_single_and_recursive_stars() {
    let temp = TempDir::new().expect("tempdir");
    let nested = temp.path().join("one/two");
    std::fs::create_dir_all(&nested).expect("create nested cwd");
    let single: ShellConfig = serde_json::from_value(serde_json::json!({
        "allowlist": [{
            "workdir": format!("{}/*", temp.path().display()),
            "command": "*"
        }]
    }))
    .expect("single-star config");
    let recursive: ShellConfig = serde_json::from_value(serde_json::json!({
        "allowlist": [{
            "workdir": format!("{}/**", temp.path().display()),
            "command": "*"
        }]
    }))
    .expect("recursive config");

    assert!(
        single
            .authorize("true", &nested)
            .expect_err("single star cannot cross components")
            .contains("denied")
    );
    assert!(
        recursive
            .authorize("true", &nested)
            .expect("double star crosses components")
            .is_some()
    );
}

/// Ensures an explicitly empty allowlist denies every command and discloses
/// that no paired rules are configured.
#[test]
fn empty_allowlist_denies_all_with_stable_diagnostic() {
    let temp = TempDir::new().expect("tempdir");
    let config: ShellConfig =
        serde_json::from_value(serde_json::json!({ "allowlist": [] })).expect("parse allowlist");
    let error = config
        .authorize("echo denied", temp.path())
        .expect_err("empty allowlist denies all");
    assert_eq!(
        error,
        format!(
            "shell command denied by configured allowlist: no rule matched workdir {} and command\nallowed command/workdir rule pairs:\n- none",
            temp.path().canonicalize().expect("canonical").display()
        )
    );
}

/// Ensures malformed, incomplete, and relative allowlist rules fail during
/// configuration instead of silently widening execution.
#[test]
fn malformed_allowlist_rules_fail_configuration() {
    for (value, expected) in [
        (serde_json::json!({ "allowlist": null }), "sequence"),
        (
            serde_json::json!({ "allowlist": [{ "workdir": "/tmp/**" }] }),
            "command",
        ),
        (
            serde_json::json!({ "allowlist": [{ "workdir": "relative/**", "command": "*" }] }),
            "must be absolute",
        ),
        (
            serde_json::json!({ "allowlist": [{ "workdir": "/tmp/[", "command": "*" }] }),
            "invalid shell allowlist workdir glob",
        ),
        (
            serde_json::json!({ "allowlist": [{ "workdir": "/tmp/**", "command": "[" }] }),
            "invalid shell allowlist command glob",
        ),
        (
            serde_json::json!({ "allowlist": [{ "workdir": "/tmp/**", "command": "*", "extra": true }] }),
            "unknown field",
        ),
    ] {
        let error = serde_json::from_value::<ShellConfig>(value).expect_err("malformed rule");
        assert!(error.to_string().contains(expected), "{error}");
    }
}

/// Ensures exactly one command matcher is required, invalid regular expressions
/// fail closed, and resource diagnostics remain stable at configuration time.
#[test]
fn command_matcher_choice_and_resource_bounds_fail_configuration() {
    let at_rule_limit = (0..MAX_SHELL_ALLOWLIST_RULES)
        .map(|_| serde_json::json!({ "workdir": "/tmp/**", "command": "*" }))
        .collect::<Vec<_>>();
    serde_json::from_value::<ShellConfig>(serde_json::json!({ "allowlist": at_rule_limit }))
        .expect("exact rule-count limit must be accepted");

    for description in [
        "x".repeat(MAX_SHELL_ALLOWLIST_DESCRIPTION_BYTES),
        "é".repeat(MAX_SHELL_ALLOWLIST_DESCRIPTION_BYTES / 2),
    ] {
        serde_json::from_value::<ShellConfig>(serde_json::json!({
            "allowlist": [{
                "workdir": "/tmp/**",
                "command": "*",
                "description": description
            }]
        }))
        .expect("exact authored-description limit must be accepted");
    }

    let mut too_many_rules = (0..MAX_SHELL_ALLOWLIST_RULES)
        .map(|_| serde_json::json!({ "workdir": "/tmp/**", "command": "*" }))
        .collect::<Vec<_>>();
    too_many_rules.push(serde_json::json!({
        "workdir": "/tmp/**",
        "command_regex": format!("a{{{MAX_SHELL_ALLOWLIST_COMPILE_BYTES}}}")
    }));

    let exact_workdir_pattern = format!("/{}", "x".repeat(MAX_SHELL_ALLOWLIST_PATTERN_BYTES - 1));
    for (field, pattern) in [
        ("workdir", exact_workdir_pattern),
        ("command", "x".repeat(MAX_SHELL_ALLOWLIST_PATTERN_BYTES)),
        (
            "command_regex",
            "x".repeat(MAX_SHELL_ALLOWLIST_PATTERN_BYTES),
        ),
    ] {
        let mut rule = serde_json::Map::new();
        rule.insert("workdir".to_owned(), serde_json::json!("/tmp/**"));
        rule.insert(field.to_owned(), serde_json::Value::String(pattern));
        if field == "workdir" {
            rule.insert("command".to_owned(), serde_json::json!("*"));
        }
        serde_json::from_value::<ShellConfig>(serde_json::json!({
            "allowlist": [rule]
        }))
        .expect("exact authored-pattern limit must be accepted");
    }

    let oversized_pattern = "x".repeat(MAX_SHELL_ALLOWLIST_PATTERN_BYTES + 1);
    let compiled_too_large = format!("a{{{}}}", MAX_SHELL_ALLOWLIST_COMPILE_BYTES);
    for (value, expected) in [
        (
            serde_json::json!({ "allowlist": [{ "workdir": "/tmp/**" }] }),
            "shell allowlist rule requires exactly one of `command` or `command_regex`",
        ),
        (
            serde_json::json!({
                "allowlist": [{
                    "workdir": "/tmp/**",
                    "command": "*",
                    "command_regex": ".*"
                }]
            }),
            "shell allowlist rule requires exactly one of `command` or `command_regex`",
        ),
        (
            serde_json::json!({
                "allowlist": [{ "workdir": "/tmp/**", "command_regex": "(" }]
            }),
            "invalid shell allowlist command regex",
        ),
        (
            serde_json::json!({
                "allowlist": [{ "workdir": "/tmp/**", "command": oversized_pattern }]
            }),
            "shell allowlist `command` must not exceed 2048 authored UTF-8 bytes",
        ),
        (
            serde_json::json!({
                "allowlist": [{
                    "workdir": format!("/{}", "x".repeat(MAX_SHELL_ALLOWLIST_PATTERN_BYTES)),
                    "command": "*"
                }]
            }),
            "shell allowlist `workdir` must not exceed 2048 authored UTF-8 bytes",
        ),
        (
            serde_json::json!({
                "allowlist": [{
                    "workdir": "/tmp/**",
                    "command_regex": oversized_pattern
                }]
            }),
            "shell allowlist `command_regex` must not exceed 2048 authored UTF-8 bytes",
        ),
        (
            serde_json::json!({
                "allowlist": [{
                    "workdir": "/tmp/**",
                    "command_regex": compiled_too_large
                }]
            }),
            "shell allowlist command regex compilation must not exceed 262144 bytes",
        ),
        (
            serde_json::json!({
                "allowlist": [{
                    "workdir": "/tmp/**",
                    "command": "*",
                    "description": format!(
                        "{}é",
                        "x".repeat(MAX_SHELL_ALLOWLIST_DESCRIPTION_BYTES - 1)
                    )
                }]
            }),
            "shell allowlist description must not exceed 1024 authored UTF-8 bytes",
        ),
        (
            serde_json::json!({ "allowlist": too_many_rules }),
            "shell allowlist permits at most 32 rules",
        ),
    ] {
        let error = serde_json::from_value::<ShellConfig>(value).expect_err("invalid allowlist");
        assert_eq!(error.to_string(), expected);
    }
}

/// Ensures YAML's recommended single-quoted scalar preserves regular-expression
/// escapes while strict command matcher selection remains active.
#[test]
fn command_regex_accepts_yaml_single_quoted_patterns() {
    let config: ShellConfig = serde_yaml_ng::from_str(
        r#"
allowlist:
  - workdir: /tmp
    command_regex: 'jj show [a-z]{6,32}\.json'
"#,
    )
    .expect("parse YAML regex allowlist");
    assert!(
        config
            .authorize("jj show abcdef.json", Path::new("/tmp"))
            .expect("matching YAML regex")
            .is_some()
    );
}

/// Ensures empty extra_env values implement the documented clear-variable
/// semantics instead of passing an empty string through to the child.
#[test]
fn empty_extra_env_removes_child_variable() {
    let mut extra_env = BTreeMap::new();
    extra_env.insert("HOME".to_owned(), String::new());
    let config = ShellConfig {
        extra_env,
        ..Default::default()
    };

    let output = config
        .command_for("printf \"${HOME+set}\"")
        .env_remove("HOME")
        .output()
        .expect("spawn shell");
    assert!(output.status.success());
    assert_eq!(String::from_utf8_lossy(&output.stdout), "");

    let output = config
        .spawn_isolated("printf \"${HOME+set}\"", None, false, false)
        .expect("spawn isolated shell")
        .child
        .wait_with_output()
        .expect("wait shell");
    assert!(output.status.success());
    assert_eq!(String::from_utf8_lossy(&output.stdout), "");
}

/// Ensures the protected overlay wins over both inherited and ordinary
/// configured values while preserving TERM and unrelated pager variables.
#[test]
fn non_interactive_pager_overlay_has_final_precedence_and_narrow_scope() {
    let config: ShellConfig = serde_json::from_value(serde_json::json!({
        "extra_env": {
            "PAGER": "configured-pager",
            "GIT_PAGER": "configured-git-pager",
            "GH_PAGER": "configured-gh-pager",
            "SYSTEMD_PAGER": "configured-systemd-pager",
            "TERM": "tau-term",
            "JJ_PAGER": "configured-jj-pager",
            "MANPAGER": "configured-man-pager",
            "BAT_PAGER": "configured-bat-pager"
        }
    }))
    .expect("parse shell config");
    let mut command = config.command_for(
        "printf '%s\\n' \"$PAGER\" \"$GIT_PAGER\" \"$GH_PAGER\" \
         \"$SYSTEMD_PAGER\" \"$TERM\" \"$JJ_PAGER\" \"$MANPAGER\" \"$BAT_PAGER\"",
    );
    command
        .env("PAGER", "inherited-pager")
        .env("GIT_PAGER", "inherited-git-pager");
    config.apply_environment(&mut command);

    let output = command.output().expect("run environment probe");
    assert!(output.status.success());
    assert_eq!(
        String::from_utf8_lossy(&output.stdout),
        "cat\ncat\ncat\ncat\ntau-term\ncat\nconfigured-man-pager\nconfigured-bat-pager\n"
    );
}

/// Ensures the full shared preparation sequence preserves an inherited TERM
/// even when ordinary shell configuration does not mention TERM.
#[test]
fn shell_isolation_preserves_inherited_term_by_default() {
    let config = ShellConfig::default();
    let mut command = config.command_for("printf '%s' \"$TERM\"");
    command.env("TERM", "inherited-test-term");
    apply_command_isolation(&mut command);
    config.apply_environment(&mut command);

    let output = command.output().expect("run inherited TERM probe");
    assert!(output.status.success());
    assert_eq!(
        String::from_utf8_lossy(&output.stdout),
        "inherited-test-term"
    );
}

/// Ensures the documented opt-out is explicit and leaves ordinary
/// `extra_env` pager and TERM choices intact.
#[test]
fn non_interactive_pager_opt_out_preserves_configured_environment() {
    let config: ShellConfig = serde_json::from_value(serde_json::json!({
        "non_interactive_pager": false,
        "extra_env": {
            "PAGER": "custom-pager",
            "GIT_PAGER": "custom-git-pager",
            "TERM": "custom-term"
        }
    }))
    .expect("parse shell config");
    let mut command = config.command_for("printf '%s\\n' \"$PAGER\" \"$GIT_PAGER\" \"$TERM\"");
    config.apply_environment(&mut command);

    let output = command.output().expect("run opt-out probe");
    assert!(output.status.success());
    assert_eq!(
        String::from_utf8_lossy(&output.stdout),
        "custom-pager\ncustom-git-pager\ncustom-term\n"
    );
}

/// Ensures directory-lock backend config keeps memory as the default while
/// accepting the opt-in filesystem backend and state directory.
#[test]
fn dir_lock_backend_config_defaults_memory_and_parses_filesystem() {
    assert_eq!(
        ExtConfig::default().dir_lock.backend,
        DirLockBackendConfig::Memory
    );

    let config: ExtConfig = serde_json::from_value(serde_json::json!({
        "dir_lock": {
            "enable": true,
            "backend": "filesystem",
            "state_dir": "/tmp/tau-dir-locks"
        }
    }))
    .expect("parse dir_lock backend config");

    assert_eq!(config.dir_lock.backend, DirLockBackendConfig::Filesystem);
    assert_eq!(
        config.dir_lock.state_dir,
        Some(PathBuf::from("/tmp/tau-dir-locks"))
    );
}