worktrunk 0.41.0

A CLI for Git worktree management, designed for parallel AI agent workflows
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
//! Tests for the shell integration first-run prompt
//!
//! These tests verify that `prompt_shell_integration` behaves correctly across scenarios:
//! - Skips when shell integration is active (WORKTRUNK_DIRECTIVE_CD_FILE set)
//! - Skips when already prompted (config flag true)
//! - Skips when already installed (config line exists in shell config)
//! - Shows hint when not a TTY (non-interactive)
//! - Prompts and respects user's choice in interactive mode

use crate::common::{TestRepo, repo};
use rstest::rstest;
use std::fs;
use worktrunk::config::UserConfig;

///
/// When WORKTRUNK_DIRECTIVE_CD_FILE is set (shell integration active), we should:
/// 1. Never call prompt_shell_integration()
/// 2. Have zero overhead from the prompt feature
#[rstest]
fn test_switch_with_active_shell_integration_no_prompt(repo: TestRepo) {
    // Create a worktree first
    let create_output = repo
        .wt_command()
        .args(["switch", "--create", "feature"])
        .output()
        .unwrap();
    assert!(
        create_output.status.success(),
        "First switch should succeed: {}",
        String::from_utf8_lossy(&create_output.stderr)
    );

    // Now switch with shell integration "active" (CD directive file set)
    // The file must exist (shell wrapper creates it before calling wt)
    let cd_file = repo.root_path().join("directive_cd.txt");
    let exec_file = repo.root_path().join("directive_exec.txt");
    fs::write(&cd_file, "").unwrap();
    fs::write(&exec_file, "").unwrap();
    let mut cmd = repo.wt_command();
    cmd.env("WORKTRUNK_DIRECTIVE_CD_FILE", &cd_file);
    cmd.env("WORKTRUNK_DIRECTIVE_EXEC_FILE", &exec_file);

    let output = cmd.args(["switch", "feature"]).output().unwrap();

    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        output.status.success(),
        "Switch should succeed.\nstderr: {stderr}\nstdout: {stdout}"
    );

    // The CD file should have a path (shell integration active)
    let cd_content = fs::read_to_string(&cd_file).unwrap_or_default();
    assert!(
        !cd_content.trim().is_empty(),
        "CD file should contain a path when shell integration active"
    );

    // No install prompt in output (would contain "Install shell integration")
    assert!(
        !stderr.contains("Install shell integration"),
        "Should not show install prompt when shell integration active: {stderr}"
    );
}

#[rstest]
fn test_switch_with_skip_prompt_flag(repo: TestRepo) {
    // Set the skip flag in config
    let config_path = repo.test_config_path();
    let config = UserConfig {
        skip_shell_integration_prompt: true,
        ..Default::default()
    };
    config.save_to(config_path).unwrap();

    let output = repo
        .wt_command()
        .args(["switch", "--create", "feature"])
        .output()
        .unwrap();

    assert!(output.status.success(), "Switch should succeed");

    // No install prompt in output
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        !stderr.contains("Install shell integration"),
        "Should not show install prompt when already prompted: {stderr}"
    );
}

///
/// When stdin is not a TTY (e.g., piped input), we should:
/// - Skip the prompt (can't interact)
/// - Always show the hint (not just first run)
/// - NOT mark as prompted (hints are not prompts)
#[rstest]
fn test_switch_non_tty_shows_hint(repo: TestRepo) {
    use std::process::Stdio;

    // Run with piped stdin (not a TTY)
    let output = repo
        .wt_command()
        .args(["switch", "--create", "feature"])
        .stdin(Stdio::piped())
        .output()
        .unwrap();

    assert!(output.status.success(), "Switch should succeed");

    // Verify the switch succeeded without prompting
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("Created branch") && stderr.contains("and worktree"),
        "Should create worktree: {stderr}"
    );

    // Should show hint
    assert!(
        stderr.contains("wt config shell install"),
        "Should show install hint: {stderr}"
    );

    // Config should NOT have skip_shell_integration_prompt set (hints are not prompts)
    let config_content = fs::read_to_string(repo.test_config_path()).unwrap_or_default();
    assert!(
        !config_content.contains("skip-shell-integration-prompt"),
        "Should not mark as prompted for non-TTY (hints are not prompts): {config_content}"
    );

    // Second non-TTY run should also show hint
    let output2 = repo
        .wt_command()
        .args(["switch", "--create", "feature2"])
        .stdin(Stdio::piped())
        .output()
        .unwrap();

    let stderr2 = String::from_utf8_lossy(&output2.stderr);
    assert!(
        stderr2.contains("wt config shell install"),
        "Should show hint on every non-TTY run: {stderr2}"
    );
}

///
/// When SHELL is set to an unsupported shell (like tcsh), we should:
/// - Show a hint that the shell is not supported
/// - List the supported shells
#[rstest]
fn test_switch_unsupported_shell_shows_hint(repo: TestRepo) {
    use std::process::Stdio;

    // Run with an unsupported shell
    let mut cmd = repo.wt_command();
    cmd.env("SHELL", "/bin/tcsh");

    let output = cmd
        .args(["switch", "--create", "feature"])
        .stdin(Stdio::piped())
        .output()
        .unwrap();

    assert!(output.status.success(), "Switch should succeed");

    // Should show unsupported shell message
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("not yet supported for tcsh"),
        "Should show unsupported shell message: {stderr}"
    );
    assert!(
        stderr.contains("bash, zsh, fish, nu, PowerShell"),
        "Should list supported shells: {stderr}"
    );
}

///
/// When SHELL is not set (unusual Unix setup or Windows), we should:
/// - Show the standard install hint
#[rstest]
fn test_switch_no_shell_env_shows_hint(repo: TestRepo) {
    use std::process::Stdio;

    // Run without SHELL set
    let mut cmd = repo.wt_command();
    cmd.env_remove("SHELL");

    let output = cmd
        .args(["switch", "--create", "feature"])
        .stdin(Stdio::piped())
        .output()
        .unwrap();

    assert!(output.status.success(), "Switch should succeed");

    // Should show install hint
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("wt config shell install"),
        "Should show install hint when SHELL not set: {stderr}"
    );
}

// PTY-based tests for interactive scenarios
#[cfg(all(unix, feature = "shell-integration-tests"))]
mod pty_tests {
    use super::*;
    use crate::common::pty::{build_pty_command, exec_cmd_in_pty, exec_cmd_in_pty_prompted};
    use crate::common::{add_pty_filters, setup_snapshot_settings, wt_bin};
    use insta::assert_snapshot;
    use std::path::Path;
    use tempfile::TempDir;

    /// Create insta settings for shell integration prompt PTY tests.
    ///
    /// Combines:
    /// - Standard repo path filters (from setup_snapshot_settings)
    /// - PTY-specific filters (^D, ANSI resets)
    /// - Home directory filter (for isolated temp home)
    fn prompt_pty_settings(repo: &TestRepo, home_dir: &Path) -> insta::Settings {
        let mut settings = setup_snapshot_settings(repo);
        add_pty_filters(&mut settings);

        // Replace temp home directory with [HOME]
        settings.add_filter(&regex::escape(&home_dir.to_string_lossy()), "[HOME]");

        settings
    }

    /// Test: Already installed (config line exists) → skip prompt
    ///
    /// This covers the "installed but shell not restarted" scenario where:
    /// - Shell integration is not active (no directive env vars)
    /// - But the config line is already in shell config files
    /// - We should detect this and skip the prompt (not show interactive prompt)
    /// - We should NOT mark as prompted (no interactive prompt shown)
    ///
    /// Note: Since tests run via `cargo test`, argv[0] contains a path (`target/debug/wt`),
    /// so the "restart shell" hint is suppressed. Shell integration won't intercept explicit
    /// paths, so restarting wouldn't help. In production (PATH lookup), users see a restart hint.
    #[rstest]
    fn test_already_installed_skips_prompt(repo: TestRepo) {
        // Create isolated HOME with shell config that already has integration
        let temp_home = TempDir::new().unwrap();
        let bashrc = temp_home.path().join(".bashrc");
        let config_line = "if command -v wt >/dev/null 2>&1; then eval \"$(command wt config shell init bash)\"; fi";
        fs::write(&bashrc, format!("{config_line}\n")).unwrap();

        let mut env_vars = repo.test_env_vars();
        // Remove directive env vars (ensure shell integration not active)
        env_vars.retain(|(k, _)| {
            k != "WORKTRUNK_DIRECTIVE_CD_FILE"
                && k != "WORKTRUNK_DIRECTIVE_EXEC_FILE"
                && k != "WORKTRUNK_DIRECTIVE_FILE"
        });
        // Set SHELL to bash since we're testing with .bashrc
        env_vars.push(("SHELL".to_string(), "/bin/bash".to_string()));

        let cmd = build_pty_command(
            wt_bin().to_str().unwrap(),
            &["switch", "--create", "feature"],
            repo.root_path(),
            &env_vars,
            Some(temp_home.path()),
        );
        let (output, exit_code) = exec_cmd_in_pty(cmd, "");

        assert_eq!(exit_code, 0);

        // Should NOT contain prompt (detected already installed)
        assert!(
            !output.contains("Install shell integration"),
            "Should not prompt when already installed: {output}"
        );

        // Should have created the worktree
        assert!(
            output.contains("Created branch") && output.contains("and worktree"),
            "Should create worktree: {output}"
        );

        // Config should NOT have skip-shell-integration-prompt = true
        // (no interactive prompt shown, just a hint)
        let config_content = fs::read_to_string(repo.test_config_path()).unwrap_or_default();
        assert!(
            !config_content.contains("skip-shell-integration-prompt"),
            "Should NOT mark as prompted when just showing hint: {config_content}"
        );
    }

    /// Test: Not installed, user declines → mark prompted, no install
    #[rstest]
    fn test_user_declines_prompt(repo: TestRepo) {
        // Create isolated HOME with empty shell config
        let temp_home = TempDir::new().unwrap();
        let bashrc = temp_home.path().join(".bashrc");
        fs::write(&bashrc, "# empty bashrc\n").unwrap();

        let mut env_vars = repo.test_env_vars();
        env_vars.retain(|(k, _)| k != "WORKTRUNK_DIRECTIVE_FILE");
        // Set SHELL to bash since we're testing with .bashrc
        env_vars.push(("SHELL".to_string(), "/bin/bash".to_string()));

        let cmd = build_pty_command(
            wt_bin().to_str().unwrap(),
            &["switch", "--create", "feature"],
            repo.root_path(),
            &env_vars,
            Some(temp_home.path()),
        );
        let (output, exit_code) = exec_cmd_in_pty_prompted(cmd, &["n\n"], "[y/N");

        assert_eq!(exit_code, 0);

        // Should contain the prompt
        assert!(
            output.contains("Install shell integration"),
            "Should show prompt: {output}"
        );

        // Should have created the worktree
        assert!(
            output.contains("Created branch") && output.contains("and worktree"),
            "Should create worktree: {output}"
        );

        // Config should have skip-shell-integration-prompt = true
        let config_content = fs::read_to_string(repo.test_config_path()).unwrap_or_default();
        assert!(
            config_content.contains("skip-shell-integration-prompt = true"),
            "Should mark as prompted after decline: {config_content}"
        );

        // Shell config should NOT have the integration line
        let bashrc_content = fs::read_to_string(&bashrc).unwrap();
        assert!(
            !bashrc_content.contains("eval \"$(command wt"),
            "Should not install when declined: {bashrc_content}"
        );

        // Snapshot the output (filters applied via settings)
        prompt_pty_settings(&repo, temp_home.path()).bind(|| {
            assert_snapshot!("prompt_decline", &output);
        });
    }

    /// Test: Not installed, user accepts → install and show success
    #[rstest]
    fn test_user_accepts_prompt(repo: TestRepo) {
        // Create isolated HOME with empty shell config
        let temp_home = TempDir::new().unwrap();
        let bashrc = temp_home.path().join(".bashrc");
        fs::write(&bashrc, "# empty bashrc\n").unwrap();

        let mut env_vars = repo.test_env_vars();
        env_vars.retain(|(k, _)| k != "WORKTRUNK_DIRECTIVE_FILE");
        // Set SHELL to bash since we're testing with .bashrc
        env_vars.push(("SHELL".to_string(), "/bin/bash".to_string()));

        let cmd = build_pty_command(
            wt_bin().to_str().unwrap(),
            &["switch", "--create", "feature"],
            repo.root_path(),
            &env_vars,
            Some(temp_home.path()),
        );
        let (output, exit_code) = exec_cmd_in_pty_prompted(cmd, &["y\n"], "[y/N");

        assert_eq!(exit_code, 0);

        // Should contain the prompt
        assert!(
            output.contains("Install shell integration"),
            "Should show prompt: {output}"
        );

        // Should show success message for configuration
        assert!(
            output.contains("Configured") && output.contains("bash"),
            "Should show configured message: {output}"
        );

        // Config should NOT have skip-shell-integration-prompt after accept
        // (only set after explicit decline - if they uninstall, they can be prompted again)
        let config_content = fs::read_to_string(repo.test_config_path()).unwrap_or_default();
        assert!(
            !config_content.contains("skip-shell-integration-prompt = true"),
            "Should not set skip flag after accept (installation itself prevents future prompts): {config_content}"
        );

        // Shell config SHOULD have the integration line
        let bashrc_content = fs::read_to_string(&bashrc).unwrap();
        assert!(
            bashrc_content.contains("eval \"$(command wt"),
            "Should install when accepted: {bashrc_content}"
        );

        // Snapshot the output (filters applied via settings)
        prompt_pty_settings(&repo, temp_home.path()).bind(|| {
            assert_snapshot!("prompt_accept", &output);
        });
    }

    /// Test: User requests preview with ? then declines
    #[rstest]
    fn test_user_requests_preview_then_declines(repo: TestRepo) {
        // Create isolated HOME with empty shell config
        let temp_home = TempDir::new().unwrap();
        let bashrc = temp_home.path().join(".bashrc");
        fs::write(&bashrc, "# empty bashrc\n").unwrap();

        let mut env_vars = repo.test_env_vars();
        env_vars.retain(|(k, _)| k != "WORKTRUNK_DIRECTIVE_FILE");
        // Set SHELL to bash since we're testing with .bashrc
        env_vars.push(("SHELL".to_string(), "/bin/bash".to_string()));

        let cmd = build_pty_command(
            wt_bin().to_str().unwrap(),
            &["switch", "--create", "feature"],
            repo.root_path(),
            &env_vars,
            Some(temp_home.path()),
        );
        // User requests preview, then declines
        let (output, exit_code) = exec_cmd_in_pty_prompted(cmd, &["?\n", "n\n"], "[y/N");

        assert_eq!(exit_code, 0);

        // Should contain the prompt (shown twice - before and after preview)
        assert!(
            output.contains("Install shell integration"),
            "Should show prompt: {output}"
        );

        // Should show preview content (gutter with config line)
        assert!(
            output.contains("Will add") && output.contains("bash"),
            "Should show preview: {output}"
        );

        // Should show the config line in preview
        assert!(
            output.contains("eval") && output.contains("wt config shell init"),
            "Should show config line in preview: {output}"
        );

        // Shell config should NOT have the integration line (user declined)
        let bashrc_content = fs::read_to_string(&bashrc).unwrap();
        assert!(
            !bashrc_content.contains("eval \"$(command wt"),
            "Should not install when declined after preview: {bashrc_content}"
        );

        // Snapshot the output (filters applied via settings)
        prompt_pty_settings(&repo, temp_home.path()).bind(|| {
            assert_snapshot!("prompt_preview_decline", &output);
        });
    }

    /// Test: Second switch after first prompt → no prompt
    #[rstest]
    fn test_no_prompt_after_first_prompt(repo: TestRepo) {
        // Create isolated HOME with empty shell config
        let temp_home = TempDir::new().unwrap();
        let bashrc = temp_home.path().join(".bashrc");
        fs::write(&bashrc, "# empty bashrc\n").unwrap();

        let mut env_vars = repo.test_env_vars();
        env_vars.retain(|(k, _)| k != "WORKTRUNK_DIRECTIVE_FILE");
        // Set SHELL to bash since we're testing with .bashrc
        env_vars.push(("SHELL".to_string(), "/bin/bash".to_string()));

        // First switch - decline the prompt
        let cmd = build_pty_command(
            wt_bin().to_str().unwrap(),
            &["switch", "--create", "feature1"],
            repo.root_path(),
            &env_vars,
            Some(temp_home.path()),
        );
        let (_, _) = exec_cmd_in_pty_prompted(cmd, &["n\n"], "[y/N");

        // Second switch - should NOT prompt again
        let cmd = build_pty_command(
            wt_bin().to_str().unwrap(),
            &["switch", "--create", "feature2"],
            repo.root_path(),
            &env_vars,
            Some(temp_home.path()),
        );
        let (output, exit_code) = exec_cmd_in_pty(cmd, "");

        assert_eq!(exit_code, 0);

        assert!(
            !output.contains("Install shell integration"),
            "Should not prompt on second switch: {output}"
        );
    }
}

/// Tests for commit generation prompt (similar to shell integration prompt)
#[cfg(all(unix, feature = "shell-integration-tests"))]
mod commit_generation_prompt_tests {
    use super::*;
    use crate::common::pty::{build_pty_command, exec_cmd_in_pty, exec_cmd_in_pty_prompted};
    use crate::common::wt_bin;
    use std::os::unix::fs::PermissionsExt;
    use std::path::{Path, PathBuf};
    use tempfile::TempDir;

    fn setup_fake_claude(temp_home: &Path) -> PathBuf {
        // Create a fake claude executable that does nothing
        let bin_dir = temp_home.join("bin");
        fs::create_dir_all(&bin_dir).unwrap();
        let claude_path = bin_dir.join("claude");
        fs::write(&claude_path, "#!/bin/sh\nexit 0\n").unwrap();
        // Make executable
        let mut perms = fs::metadata(&claude_path).unwrap().permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&claude_path, perms).unwrap();
        bin_dir
    }

    /// Test: No LLM tool available, prompt is skipped and skip flag is set
    #[rstest]
    fn test_no_llm_tool_sets_skip_flag(repo: TestRepo) {
        let temp_home = TempDir::new().unwrap();

        // Stage a change so commit has something to do
        let test_file = repo.root_path().join("test.txt");
        fs::write(&test_file, "test content\n").unwrap();
        repo.run_git(&["add", "test.txt"]);

        let mut env_vars = repo.test_env_vars();
        // Use minimal PATH to ensure claude/codex aren't found
        env_vars.push(("PATH".to_string(), "/usr/bin:/bin".to_string()));

        let cmd = build_pty_command(
            wt_bin().to_str().unwrap(),
            &["step", "commit"],
            repo.root_path(),
            &env_vars,
            Some(temp_home.path()),
        );
        let (output, exit_code) = exec_cmd_in_pty(cmd, "");

        // Should succeed (using fallback commit message)
        assert_eq!(exit_code, 0, "Command should succeed: {output}");

        // Config should have skip-commit-generation-prompt = true (no tool found)
        let config_content = fs::read_to_string(repo.test_config_path()).unwrap_or_default();
        assert!(
            config_content.contains("skip-commit-generation-prompt = true"),
            "Should set skip flag when no tool found: {config_content}"
        );
    }

    /// Test: LLM tool available, user declines prompt
    #[rstest]
    fn test_user_declines_llm_prompt(repo: TestRepo) {
        let temp_home = TempDir::new().unwrap();
        let bin_dir = setup_fake_claude(temp_home.path());

        // Stage a change
        let test_file = repo.root_path().join("test.txt");
        fs::write(&test_file, "test content\n").unwrap();
        repo.run_git(&["add", "test.txt"]);

        let mut env_vars = repo.test_env_vars();
        // Add our fake claude to PATH
        let path = format!("{}:/usr/bin:/bin", bin_dir.display());
        env_vars.push(("PATH".to_string(), path));

        let cmd = build_pty_command(
            wt_bin().to_str().unwrap(),
            &["step", "commit"],
            repo.root_path(),
            &env_vars,
            Some(temp_home.path()),
        );
        let (output, exit_code) = exec_cmd_in_pty_prompted(cmd, &["n\n"], "[y/N");

        assert_eq!(exit_code, 0, "Command should succeed: {output}");

        // Should show the prompt
        assert!(
            output.contains("Configure") && output.contains("claude"),
            "Should show LLM config prompt: {output}"
        );

        // Config should have skip-commit-generation-prompt = true
        let config_content = fs::read_to_string(repo.test_config_path()).unwrap_or_default();
        assert!(
            config_content.contains("skip-commit-generation-prompt = true"),
            "Should set skip flag when declined: {config_content}"
        );
    }

    /// Test: LLM tool available, user accepts prompt
    #[rstest]
    fn test_user_accepts_llm_prompt(repo: TestRepo) {
        let temp_home = TempDir::new().unwrap();
        let bin_dir = setup_fake_claude(temp_home.path());

        // Stage a change
        let test_file = repo.root_path().join("test.txt");
        fs::write(&test_file, "test content\n").unwrap();
        repo.run_git(&["add", "test.txt"]);

        let mut env_vars = repo.test_env_vars();
        let path = format!("{}:/usr/bin:/bin", bin_dir.display());
        env_vars.push(("PATH".to_string(), path));

        let cmd = build_pty_command(
            wt_bin().to_str().unwrap(),
            &["step", "commit"],
            repo.root_path(),
            &env_vars,
            Some(temp_home.path()),
        );
        let (output, _exit_code) = exec_cmd_in_pty_prompted(cmd, &["y\n"], "[y/N");

        // Note: exit_code may be non-zero because our fake claude doesn't generate
        // a real commit message. We're testing the prompt flow, not the LLM result.

        // Should show success message for config save
        assert!(
            output.contains("Added to user config"),
            "Should show config added message: {output}"
        );

        // Config should have the command configured
        let config_content = fs::read_to_string(repo.test_config_path()).unwrap_or_default();
        assert!(
            config_content.contains("[commit.generation]") && config_content.contains("command"),
            "Should add commit generation config: {config_content}"
        );
    }

    /// Test: User requests preview (?)
    #[rstest]
    fn test_user_requests_preview(repo: TestRepo) {
        let temp_home = TempDir::new().unwrap();
        let bin_dir = setup_fake_claude(temp_home.path());

        // Stage a change
        let test_file = repo.root_path().join("test.txt");
        fs::write(&test_file, "test content\n").unwrap();
        repo.run_git(&["add", "test.txt"]);

        let mut env_vars = repo.test_env_vars();
        let path = format!("{}:/usr/bin:/bin", bin_dir.display());
        env_vars.push(("PATH".to_string(), path));

        let cmd = build_pty_command(
            wt_bin().to_str().unwrap(),
            &["step", "commit"],
            repo.root_path(),
            &env_vars,
            Some(temp_home.path()),
        );
        // Request preview, then decline
        let (output, exit_code) = exec_cmd_in_pty_prompted(cmd, &["?\n", "n\n"], "[y/N");

        assert_eq!(exit_code, 0, "Command should succeed: {output}");

        // Should show the preview
        assert!(
            output.contains("Would add to") && output.contains("[commit.generation]"),
            "Should show preview: {output}"
        );
    }
}