try-cli 0.1.4

Lightweight, time-sensitive directory navigation for experiments — a fast way to jump between temporary project folders
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
mod cli;
mod error;
mod model;
mod score;
mod selector;
mod storage;
mod tui;
mod util;

use crate::error::Result;
use clap::error::ErrorKind;
use clap::{Parser, Subcommand};
use std::{env, ffi::OsString, io, io::IsTerminal, path::Path, path::PathBuf};

const SHELL_WRAPPED_ENV: &str = "TRY_SHELL_WRAPPED";
const SHELL_INTEGRATION_HINT: &str = r#"Shell integration is not active. Run: eval "$(try init)""#;
const INVALID_SUBCOMMAND_HINT: &str =
    r#"Shell integration is not active. Use `try cd <query>` directly, or run: eval "$(try init)""#;
const TOP_LEVEL_AFTER_HELP: &str = concat!(
    "After `cargo install try-cli`, enable shell integration:\n",
    "  bash/zsh: eval \"$(try init)\"\n",
    "  fish: eval \"$(try init | string collect)\"\n",
    "\n",
    "Direct binary usage without shell integration:\n",
    "  try cd my-experiment\n",
    "  try clone https://github.com/user/repo.git\n",
);
const INIT_AFTER_HELP: &str = concat!(
    "This command prints shell code.\n",
    "Evaluate it from your shell config instead of running it directly:\n",
    "  bash/zsh: eval \"$(try init)\"\n",
    "  fish: eval \"$(try init | string collect)\"\n",
);

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ShellKind {
    Fish,
    Posix,
}

#[derive(Parser, Debug)]
#[command(
    name = "try",
    version,
    about = "Interactive try-dir selector",
    disable_help_subcommand = true,
    after_help = TOP_LEVEL_AFTER_HELP
)]
struct Cli {
    /// Override base tries directory
    #[arg(long, global = true, value_name = "PATH")]
    path: Option<PathBuf>,

    #[command(subcommand)]
    command: Option<Commands>,
}

#[derive(Subcommand, Debug)]
enum Commands {
    /// Print shell function for shell integration
    #[command(after_help = INIT_AFTER_HELP)]
    Init {
        /// Override base tries directory for generated alias
        #[arg(long, value_name = "PATH")]
        path: Option<PathBuf>,
        /// Optional absolute path positional to keep backward behavior
        #[arg(value_name = "PATH")]
        abs_path: Option<PathBuf>,
    },
    /// Interactive selector; prints shell commands for the wrapper
    Cd {
        /// Query terms; use `--` before hyphen-leading terms
        #[arg(value_name = "QUERY", trailing_var_arg = true)]
        query: Vec<String>,
    },
    /// Print clone shell commands into a date-prefixed directory
    Clone {
        /// Git URI (https://... or git@...)
        git_uri: String,
        /// Optional directory name override
        name: Option<String>,
    },
}

fn shell_kind() -> ShellKind {
    if util::is_fish_shell() {
        ShellKind::Fish
    } else {
        ShellKind::Posix
    }
}

fn render_init_script(script_path: &Path, tries_path: &Path, shell_kind: ShellKind) -> String {
    let path_arg = format!(r#" --path "{}""#, tries_path.display());
    match shell_kind {
        ShellKind::Fish => format!(
            r#"function try
  set -l script_path "{}"
  switch "$argv[1]"
    case -h --help -V --version init
      /usr/bin/env TRY_SHELL_WRAPPED=1 "$script_path" $argv 2>/dev/tty
      return
    case cd clone
      switch "$argv[2]"
        case -h --help -V --version
          /usr/bin/env TRY_SHELL_WRAPPED=1 "$script_path" $argv 2>/dev/tty
          return
      end
  end
  switch "$argv[1]"
    case cd
      set argv cd{} $argv[2..-1]
    case clone
      set argv clone{} $argv[2..-1]
    case '*'
      set argv cd{} $argv
  end
  set -l cmd (/usr/bin/env TRY_SHELL_WRAPPED=1 "$script_path" $argv 2>/dev/tty | string collect)
  set -l cmd_status $status
  test $cmd_status -eq 0 && eval $cmd || echo $cmd
end"#,
            script_path.display(),
            path_arg,
            path_arg,
            path_arg
        ),
        ShellKind::Posix => format!(
            r#"try() {{
  script_path='{}'
  case "$1" in
    -h|--help|-V|--version|init)
      /usr/bin/env TRY_SHELL_WRAPPED=1 "$script_path" "$@" 2>/dev/tty
      return;;
    cd|clone)
      case "$2" in
        -h|--help|-V|--version)
          /usr/bin/env TRY_SHELL_WRAPPED=1 "$script_path" "$@" 2>/dev/tty
          return;;
      esac;;
  esac
  case "$1" in
    cd)
      shift
      set -- cd{} "$@";;
    clone)
      shift
      set -- clone{} "$@";;
    *)
      set -- cd{} "$@";;
  esac
  tmp=$(mktemp 2>/dev/null || echo "/tmp/try-cmd-$$")
  /usr/bin/env TRY_SHELL_WRAPPED=1 "$script_path" "$@" > "$tmp" 2>/dev/tty
  cmd_status=$?
  cmd=$(cat "$tmp" 2>/dev/null)
  rm -f "$tmp" 2>/dev/null
  if [ $cmd_status -eq 0 ] && [ -n "$cmd" ]; then
    eval "$cmd"
  else
    [ -n "$cmd" ] && echo "$cmd"
  fi
}}"#,
            script_path.display(),
            path_arg,
            path_arg,
            path_arg
        ),
    }
}

fn shell_integration_hint_needed(
    is_wrapped: bool,
    stdout_is_terminal: bool,
    stderr_is_terminal: bool,
) -> bool {
    !is_wrapped && stdout_is_terminal && stderr_is_terminal
}

fn should_show_shell_integration_hint() -> bool {
    shell_integration_hint_needed(
        env::var_os(SHELL_WRAPPED_ENV).is_some(),
        io::stdout().is_terminal(),
        io::stderr().is_terminal(),
    )
}

fn print_shell_integration_hint(message: &str) {
    let mut err = io::stderr();
    let _ = crate::tui::warn(&mut err, message);
}

fn maybe_print_shell_integration_hint() {
    if should_show_shell_integration_hint() {
        print_shell_integration_hint(SHELL_INTEGRATION_HINT);
    }
}

fn main() -> Result<()> {
    // Use try_parse so we can route help/version to stderr, keeping stdout clean
    // for shell-evaluable output.
    let cli = match Cli::try_parse() {
        Ok(c) => c,
        Err(e) => {
            match e.kind() {
                ErrorKind::DisplayHelp | ErrorKind::DisplayVersion => {
                    // Force printing to stderr so shell wrapper doesn't eval help text.
                    eprintln!("{e}");
                    std::process::exit(0);
                }
                ErrorKind::InvalidSubcommand => {
                    let _ = e.print();
                    if should_show_shell_integration_hint() {
                        print_shell_integration_hint(INVALID_SUBCOMMAND_HINT);
                    }
                    std::process::exit(e.exit_code());
                }
                _ => {
                    // Let clap print the error to stderr and set appropriate exit code.
                    e.exit();
                }
            }
        }
    };

    let base_path = cli
        .path
        .clone()
        .unwrap_or_else(selector::TrySelector::default_base_path);

    match cli.command {
        None => {
            // Default to interactive selector, equivalent to `try cd` with empty query
            maybe_print_shell_integration_hint();
            cli::run_cd_flow(String::new(), &base_path)
        }
        Some(Commands::Init { path, abs_path }) => {
            let script_path = env::current_exe()
                .ok()
                .and_then(|p| p.canonicalize().ok())
                .unwrap_or_else(|| PathBuf::from("try"));
            let mut tries_path = path
                .or(abs_path.filter(|p| p.is_absolute()))
                .unwrap_or(base_path.clone());
            // Normalize ~ if passed via clap as a plain string previously; keep as PathBuf otherwise
            if let Some(s) = tries_path.to_str()
                && s.starts_with("~/")
            {
                tries_path = util::shellexpand_home(s);
            }
            println!(
                "{}",
                render_init_script(&script_path, &tries_path, shell_kind())
            );
            Ok(())
        }
        Some(Commands::Cd { query }) => {
            maybe_print_shell_integration_hint();
            let query_os: Vec<OsString> = query.into_iter().map(OsString::from).collect();
            let query_str = cli::build_cd_query(&query_os);
            cli::run_cd_flow(query_str, &base_path)
        }
        Some(Commands::Clone { git_uri, name }) => {
            maybe_print_shell_integration_hint();
            let dir_name = util::generate_clone_directory_name(&git_uri, name.as_deref());
            if dir_name.is_none() {
                let mut err = io::stderr();
                let _ = crate::tui::error(&mut err, &format!("Unable to parse git URI: {git_uri}"));
                std::process::exit(1);
            }
            let full = base_path.join(dir_name.unwrap());
            let mut parts: Vec<String> = Vec::new();
            parts.push(util::dir_assign_for_shell(&full));
            parts.push("mkdir -p \"$dir\"".into());
            parts.push(format!("git clone '{git_uri}' \"$dir\""));
            parts.push("touch \"$dir\"".into());
            parts.push("cd \"$dir\"".into());
            println!("{}", util::join_shell(&parts));
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use clap::CommandFactory;
    use std::ffi::OsString;
    use std::fs;
    use std::io;
    // use std::io::Write; // removed; no tests require direct Write now
    use std::path::Path;
    use std::path::PathBuf;
    use std::time::{Duration, SystemTime};

    #[test]
    fn test_sanitize_query_filters_disallowed() {
        let input = "Hello,_World-!@$ 42.";
        let out = crate::util::sanitize_query(input);
        assert_eq!(out, "Hello_World- 42.");
    }

    #[test]
    fn test_is_printable() {
        assert!(crate::util::is_printable('a'));
        assert!(crate::util::is_printable('-'));
        assert!(crate::util::is_printable('_'));
        assert!(crate::util::is_printable('.'));
        assert!(crate::util::is_printable(' '));
        assert!(!crate::util::is_printable('\n'));
        assert!(!crate::util::is_printable('!'));
    }

    #[test]
    fn test_split_date_prefixed() {
        let s = "2025-08-26-hello";
        let p = crate::util::split_date_prefixed(s);
        assert_eq!(p, Some(("2025-08-26", "hello")));
        assert_eq!(crate::util::split_date_prefixed("foo"), None);
        assert_eq!(
            crate::util::split_date_prefixed("2025-08-2x-hello"),
            Some(("2025-08-2x", "hello"))
        );
    }

    #[test]
    fn test_display_width_ascii() {
        assert_eq!(crate::tui::display_width("hello"), 5);
        assert_eq!(crate::tui::display_width(""), 0);
    }

    #[test]
    fn test_civil_from_days_epoch() {
        // 1970-01-01 is day 0 in our usage
        let (y, m, d) = crate::util::civil_from_days(0);
        assert_eq!((y, m, d), (1970, 1, 1));
    }

    #[test]
    fn test_calculate_score_basic() {
        // Empty query -> date-prefixed gets a positive boost; non-date stays 0 without recency
        let s1 = crate::score::calculate_score("2025-08-26-test", "", None, None);
        let s2 = crate::score::calculate_score("foo", "", None, None);
        assert!(s1 > s2);
        assert_eq!(s2, 0.0);

        // Non-matching query => 0
        assert_eq!(crate::score::calculate_score("abc", "zz", None, None), 0.0);

        // Simple positive fuzzy match
        assert!(crate::score::calculate_score("foo-test", "ft", None, None) > 0.0);
    }

    #[test]
    fn test_format_relative_time_none() {
        assert_eq!(crate::tui::format_relative_time(None), "?");
    }

    #[test]
    fn test_extract_option_with_value_removes_and_last_wins() {
        // Case: flag separate arg form
        let mut args = vec![
            OsString::from("--path"),
            OsString::from("/tmp/x"),
            OsString::from("cd"),
        ];
        let val = crate::util::extract_option_with_value(&mut args, "--path");
        assert_eq!(val.as_deref(), Some("/tmp/x"));
        assert_eq!(args, vec![OsString::from("cd")]);

        // Case: mix of = and separate; rightmost wins
        let mut args = vec![
            OsString::from("--path=/a"),
            OsString::from("--path"),
            OsString::from("/b"),
            OsString::from("init"),
        ];
        let val = crate::util::extract_option_with_value(&mut args, "--path");
        assert_eq!(val.as_deref(), Some("/b"));
        assert_eq!(args, vec![OsString::from("init")]);
    }

    #[test]
    fn test_shell_escape_single_quotes() {
        let path = PathBuf::from("/tmp/it's ok");
        let escaped = crate::util::shell_escape(path);
        assert_eq!(escaped, "'/tmp/it'\\''s ok'");
    }

    #[test]
    fn test_normalize_query_for_match_spaces_to_dash_and_sanitize() {
        let q = "Hello,  World!!";
        let norm = crate::storage::normalize_query_for_match(q);
        assert_eq!(norm, "Hello-World");
    }

    #[test]
    fn test_fast_create_skips_when_exact_exists_ignoring_date() -> io::Result<()> {
        let tmp_root = std::env::temp_dir().join(format!("tryrs-test-{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp_root);
        fs::create_dir_all(&tmp_root)?;
        // existing dir: date-prefixed version of "foo-bar"
        let existing = tmp_root.join("2025-08-26-foo-bar");
        fs::create_dir_all(&existing)?;
        // exact match by stripping date and normalizing spaces -> '-'
        let res = crate::storage::fast_create_target_if_no_exact(&tmp_root, "foo bar")?;
        assert!(res.is_none());
        let _ = fs::remove_dir_all(&tmp_root);
        Ok(())
    }

    #[test]
    fn test_fast_create_returns_target_when_no_match() -> io::Result<()> {
        let tmp_root = std::env::temp_dir().join(format!("tryrs-test2-{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp_root);
        fs::create_dir_all(&tmp_root)?;
        let q = "new thing";
        let res = crate::storage::fast_create_target_if_no_exact(&tmp_root, q)?;
        assert!(res.is_some());
        let p = res.unwrap();
        let bn = p.file_name().unwrap().to_string_lossy().to_string();
        assert!(bn.starts_with(&crate::util::today_prefix()));
        assert!(bn.ends_with("-new-thing"));
        let _ = fs::remove_dir_all(&tmp_root);
        Ok(())
    }

    #[test]
    fn test_fast_create_sanitizes_query() -> io::Result<()> {
        let tmp_root = std::env::temp_dir().join(format!("tryrs-test3-{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp_root);
        fs::create_dir_all(&tmp_root)?;
        let q = "Hello,_World-!@$ 42."; // sanitize removes !@$ but keeps underscore, dash, space, dot
        let res = crate::storage::fast_create_target_if_no_exact(&tmp_root, q)?;
        let p = res.unwrap();
        let bn = p.file_name().unwrap().to_string_lossy().to_string();
        assert!(bn.contains("Hello_World-"));
        assert!(bn.ends_with("-42."));
        let _ = fs::remove_dir_all(&tmp_root);
        Ok(())
    }

    #[test]
    fn test_compute_viewport_no_scroll_needed() {
        // total 5 items, max_visible 3, starting at top
        let (s, e) = crate::tui::compute_viewport(0, 0, 3, 5);
        assert_eq!((s, e), (0, 3));
        let (s, e) = crate::tui::compute_viewport(1, 0, 3, 5);
        assert_eq!((s, e), (0, 3));
        let (s, e) = crate::tui::compute_viewport(2, 0, 3, 5);
        assert_eq!((s, e), (0, 3));
    }

    #[test]
    fn test_compute_viewport_scrolls_down() {
        // When cursor moves past the last visible index, scroll advances
        let (s, e) = crate::tui::compute_viewport(3, 0, 3, 6);
        assert_eq!((s, e), (1, 4));
        let (s, e) = crate::tui::compute_viewport(4, 1, 3, 6);
        assert_eq!((s, e), (2, 5));
    }

    #[test]
    fn test_compute_viewport_scrolls_up() {
        // If cursor moves above current scroll, scroll follows cursor
        let (s, e) = crate::tui::compute_viewport(1, 2, 3, 6);
        assert_eq!((s, e), (1, 4));
        let (s, e) = crate::tui::compute_viewport(0, 1, 3, 6);
        assert_eq!((s, e), (0, 3));
    }

    #[test]
    fn test_compute_viewport_clamps_end_to_total() {
        // Near the end, end should not exceed total
        let (s, e) = crate::tui::compute_viewport(5, 3, 3, 6);
        assert_eq!((s, e), (3, 6));
    }

    #[test]
    fn test_build_cd_query_strips_duplicate_cd() {
        let args = vec![
            OsString::from("cd"),
            OsString::from("foo"),
            OsString::from("bar"),
        ];
        let q = crate::cli::build_cd_query(&args);
        assert_eq!(q, "foo bar");
    }

    #[test]
    fn test_build_cd_query_no_change_when_no_dup() {
        let args = vec![OsString::from("notes"), OsString::from("proj")];
        let q = crate::cli::build_cd_query(&args);
        assert_eq!(q, "notes proj");
    }

    #[test]
    fn test_git_uri_parsing_and_dirname() {
        use crate::util::{generate_clone_directory_name, parse_git_uri};
        let p1 = parse_git_uri("https://github.com/user/repo.git").unwrap();
        assert_eq!(p1.user, "user");
        assert_eq!(p1.repo, "repo");
        let dn = generate_clone_directory_name("git@github.com:user/repo.git", None).unwrap();
        assert!(dn.ends_with("-user-repo"));
        // custom name wins
        let dn2 = generate_clone_directory_name("https://gitlab.com/u/r", Some("my-fork"));
        assert_eq!(dn2.unwrap(), "my-fork");
    }

    #[test]
    fn test_score_recency_mtime_and_ctime() {
        let now = SystemTime::now();
        let older = now - Duration::from_secs(10 * 86_400); // ~10 days ago
        let recent = now - Duration::from_secs(2 * 3_600); // 2 hours ago

        // With empty query and non-date-prefixed text, score is only recency-based
        let s_old_m = crate::score::calculate_score("hello", "", None, Some(older));
        let s_new_m = crate::score::calculate_score("hello", "", None, Some(recent));
        assert!(s_new_m > s_old_m);
        assert!(s_new_m > 0.0);

        let s_old_c = crate::score::calculate_score("hello", "", Some(older), None);
        let s_new_c = crate::score::calculate_score("hello", "", Some(recent), None);
        assert!(s_new_c > s_old_c);
        assert!(s_new_c > 0.0);
    }

    #[test]
    fn test_format_relative_time_buckets() {
        let now = SystemTime::now();
        // just now (<= 9s)
        assert_eq!(crate::tui::format_relative_time(Some(now)), "just now");
        // minutes
        assert_eq!(
            crate::tui::format_relative_time(Some(now - Duration::from_secs(2 * 60))),
            "2m ago"
        );
        // hours
        assert_eq!(
            crate::tui::format_relative_time(Some(now - Duration::from_secs(3 * 3_600))),
            "3h ago"
        );
        // days
        assert_eq!(
            crate::tui::format_relative_time(Some(now - Duration::from_secs(5 * 86_400))),
            "5d ago"
        );
        // months (30-day blocks)
        assert_eq!(
            crate::tui::format_relative_time(Some(now - Duration::from_secs(2 * 2_592_000))),
            "2mo ago"
        );
        // years
        assert_eq!(
            crate::tui::format_relative_time(Some(now - Duration::from_secs(3 * 31_536_000))),
            "3y ago"
        );
        // future timestamp gracefully treated as "just now"
        assert_eq!(
            crate::tui::format_relative_time(Some(now + Duration::from_secs(60))),
            "just now"
        );
    }

    #[test]
    fn test_display_width_wide_chars() {
        // Each CJK char typically counts as width 2
        assert_eq!(crate::tui::display_width("你好"), 4);
        assert_eq!(crate::tui::display_width("ab"), 2);
    }

    #[test]
    fn test_is_git_uri_matrix() {
        use crate::util::is_git_uri;
        assert!(is_git_uri("https://github.com/user/repo"));
        assert!(is_git_uri("git@github.com:user/repo.git"));
        assert!(is_git_uri("https://gitlab.com/u/r"));
        // permissive detection: contains host or .git suffix
        assert!(is_git_uri("ssh://git@github.com/user/repo.git"));
        assert!(!is_git_uri("notes/proj"));
        assert!(!is_git_uri("foo"));
        assert!(is_git_uri("bar.git"));
    }

    #[test]
    fn test_shellexpand_home_and_join_shell() {
        use crate::util::shellexpand_home;
        // shellexpand_home expands ~/
        if let Some(home) = dirs::home_dir() {
            let p = shellexpand_home("~/abc");
            assert!(p.starts_with(&home));
            assert!(p.ends_with("abc"));
        }
        // Non-tilde path unchanged
        assert_eq!(shellexpand_home("/tmp/x").to_string_lossy(), "/tmp/x");
        // join_shell trivial
        assert_eq!(crate::util::join_shell(&["a".into(), "b".into()]), "a && b");
    }

    #[test]
    fn test_shell_integration_hint_needed_matrix() {
        assert!(crate::shell_integration_hint_needed(false, true, true));
        assert!(!crate::shell_integration_hint_needed(true, true, true));
        assert!(!crate::shell_integration_hint_needed(false, false, true));
        assert!(!crate::shell_integration_hint_needed(false, true, false));
    }

    #[test]
    fn test_render_init_script_posix_uses_wrapped_env_and_routes_subcommands() {
        let stdout = crate::render_init_script(
            Path::new("/tmp/try"),
            Path::new("/tmp/tries"),
            crate::ShellKind::Posix,
        );

        assert!(
            stdout.contains("TRY_SHELL_WRAPPED=1"),
            "Init script should set the wrapper marker, got: {stdout}"
        );
        assert!(
            stdout.contains("cmd_status=$?"),
            "Init script should use cmd_status=$?, got: {stdout}"
        );
        assert!(
            stdout.contains("set -- clone --path \"/tmp/tries\" \"$@\""),
            "Init script should route clone through the clone subcommand, got: {stdout}"
        );
        assert!(
            stdout.contains("-h|--help|-V|--version|init"),
            "Init script should pass init/help/version through directly, got: {stdout}"
        );
        let has_standalone_status = stdout
            .lines()
            .any(|line| line.trim().starts_with("status=$?") || line.contains(" status=$?"));
        assert!(
            !has_standalone_status,
            "Init script should not use status=$? (read-only in zsh), got: {stdout}"
        );
    }

    #[test]
    fn test_render_init_script_fish_uses_wrapped_env_and_routes_subcommands() {
        let stdout = crate::render_init_script(
            Path::new("/tmp/try"),
            Path::new("/tmp/tries"),
            crate::ShellKind::Fish,
        );

        assert!(
            stdout.contains("TRY_SHELL_WRAPPED=1"),
            "Init script should set the wrapper marker, got: {stdout}"
        );
        assert!(
            stdout.contains("set -l cmd_status $status"),
            "Init script should use cmd_status in fish, got: {stdout}"
        );
        assert!(
            stdout.contains("case -h --help -V --version init"),
            "Init script should pass init/help/version through directly, got: {stdout}"
        );
        assert!(
            stdout.contains("set argv clone --path \"/tmp/tries\" $argv[2..-1]"),
            "Init script should route clone through the clone subcommand, got: {stdout}"
        );
    }

    #[test]
    fn test_top_level_help_mentions_shell_integration_setup() {
        let help = crate::Cli::command().render_help().to_string();

        assert!(
            help.contains("eval \"$(try init)\""),
            "Top-level help should mention shell integration setup, got: {help}"
        );
        assert!(
            help.contains("try cd my-experiment"),
            "Top-level help should mention direct binary usage, got: {help}"
        );
    }

    #[test]
    fn test_init_help_explains_eval_usage() {
        let mut cmd = crate::Cli::command();
        let help = cmd
            .find_subcommand_mut("init")
            .expect("missing init subcommand")
            .render_help()
            .to_string();

        assert!(
            help.contains("prints shell code"),
            "Init help should explain what the command emits, got: {help}"
        );
        assert!(
            help.contains("eval \"$(try init)\""),
            "Init help should mention eval usage, got: {help}"
        );
    }
}