bmux_env 0.0.1-alpha.1

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

#![cfg_attr(feature = "fail-on-warnings", deny(warnings))]

use std::collections::BTreeMap;
use std::io::{IsTerminal, Write};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, anyhow, bail};
use bmux_slots::{
    NewSlotBlock, SLOT_NAME_ENV, SLOTS_BIN_DIR_ENV, Slot, SlotManifest, default_bin_dir,
    default_manifest_path, is_read_only_manifest, remove_slot_block, render_slot_block_toml,
    validate_slot_name, write_slot_block,
};

/// Shell flavors supported by [`cmd_shell`].
#[derive(Copy, Clone, Debug)]
pub enum ShellKind {
    Auto,
    Bash,
    Zsh,
    Fish,
    Nushell,
    Powershell,
    Posix,
}

/// Output format for [`cmd_print`].
#[derive(Copy, Clone, Debug)]
pub enum PrintFormat {
    Shell,
    Json,
    Nix,
    Fish,
}

/// Output format for structured slot subcommands (list/show/install).
#[derive(Copy, Clone, Debug)]
pub enum SlotOutputFormat {
    Toml,
    Json,
    Nix,
}

/// How to place the per-slot binary when installing.
#[derive(Copy, Clone, Debug, Default)]
pub enum InstallMode {
    /// Create a symlink at `<bin_dir>/bmux-<name>` pointing at the source binary.
    #[default]
    Symlink,
    /// Copy the source binary to `<bin_dir>/bmux-<name>`.
    Copy,
}

/// Parameters for [`cmd_install`].
#[derive(Debug, Clone)]
pub struct InstallParams {
    /// New slot name.
    pub name: String,
    /// Path to the source `bmux` binary. Relative paths are resolved against
    /// the current working directory before being written to the manifest.
    pub binary: PathBuf,
    /// Whether the slot should inherit the shared base config.
    pub inherit_base: bool,
    /// Symlink or copy at the destination.
    pub mode: InstallMode,
    /// Destination directory; `None` means use `default_bin_dir()`.
    pub bin_dir: Option<PathBuf>,
    /// Structured output format used when printing the would-be block
    /// (read-only manifest or `--dry-run`).
    pub format: SlotOutputFormat,
    /// When true, never touch disk; just print what would happen.
    pub dry_run: bool,
    /// Allow replacing an existing slot with the same name. Without this,
    /// duplicates are refused (after an interactive confirmation prompt if
    /// a TTY is attached).
    pub overwrite: bool,
    /// Skip interactive confirmation prompts. When a slot with the same
    /// name already exists, `overwrite` must also be set to actually
    /// replace it.
    pub yes: bool,
}

// ---------------------------------------------------------------------------
// shell / exec / print
// ---------------------------------------------------------------------------

/// Resolve a [`ShellKind`] (possibly `Auto`) to a concrete dialect.
#[must_use]
pub fn resolve_shell(kind: ShellKind) -> ShellKind {
    if !matches!(kind, ShellKind::Auto) {
        return kind;
    }
    let raw = std::env::var("SHELL").unwrap_or_default();
    let basename = std::path::Path::new(&raw)
        .file_name()
        .and_then(|s| s.to_str())
        .unwrap_or("");
    match basename {
        "bash" => ShellKind::Bash,
        "zsh" => ShellKind::Zsh,
        "fish" => ShellKind::Fish,
        "nu" | "nushell" => ShellKind::Nushell,
        "pwsh" | "powershell" => ShellKind::Powershell,
        _ => ShellKind::Posix,
    }
}

/// Print shell code that idempotently prepends `$BMUX_SLOTS_BIN_DIR` to `PATH`.
///
/// # Errors
///
/// Returns errors for I/O failures on the output stream.
pub fn cmd_shell<W: Write>(w: &mut W, shell: ShellKind) -> Result<()> {
    let resolved = resolve_shell(shell);
    let bin_dir = default_bin_dir();
    let bin_str = bin_dir.to_string_lossy();
    let out = match resolved {
        ShellKind::Bash | ShellKind::Zsh => bash_zsh(&bin_str),
        ShellKind::Fish => fish(&bin_str),
        ShellKind::Nushell => nushell(&bin_str),
        ShellKind::Powershell => powershell(&bin_str),
        ShellKind::Posix | ShellKind::Auto => posix(&bin_str),
    };
    w.write_all(out.as_bytes())?;
    Ok(())
}

/// `exec` the given command with `BMUX_SLOT_NAME=<slot>` and
/// `$BMUX_SLOTS_BIN_DIR` prepended to `PATH`.
///
/// On Unix this `execvp`s (never returns on success). On other platforms it
/// spawns, waits, and exits with the child's exit code.
///
/// # Errors
///
/// - When the command vector is empty.
/// - When the slot is not declared in the manifest.
/// - When `exec`/spawn fails.
pub fn cmd_exec(slot_name: &str, argv: &[String]) -> Result<()> {
    if argv.is_empty() {
        bail!("exec: missing command");
    }
    let manifest = SlotManifest::load_default().context("load slot manifest")?;
    let _slot = manifest
        .get(slot_name)
        .map_err(|e| anyhow!("{e}"))
        .context("resolve slot")?;

    // Safety: CLI-style env mutation before exec/spawn; no threads yet.
    unsafe { std::env::set_var(SLOT_NAME_ENV, slot_name) };
    let bin_dir = default_bin_dir();
    prepend_path_env(&bin_dir)?;

    exec_replace(argv)
}

#[cfg(unix)]
fn exec_replace(argv: &[String]) -> Result<()> {
    use std::os::unix::process::CommandExt;
    let err = std::process::Command::new(&argv[0]).args(&argv[1..]).exec();
    Err(anyhow!("exec {:?} failed: {err}", argv[0]))
}

#[cfg(not(unix))]
fn exec_replace(argv: &[String]) -> Result<()> {
    let status = std::process::Command::new(&argv[0])
        .args(&argv[1..])
        .status()
        .with_context(|| format!("spawn {:?}", argv[0]))?;
    let code = status.code().unwrap_or(1);
    std::process::exit(code);
}

fn prepend_path_env(dir: &Path) -> Result<()> {
    let current = std::env::var_os("PATH").unwrap_or_default();
    let mut parts: Vec<PathBuf> = std::env::split_paths(&current).collect();
    if parts.iter().any(|p| p == dir) {
        return Ok(());
    }
    parts.insert(0, dir.to_path_buf());
    let joined = std::env::join_paths(parts).context("join PATH")?;
    unsafe { std::env::set_var("PATH", &joined) };
    Ok(())
}

/// Print the resolved env-var set as structured data.
///
/// # Errors
///
/// Returns errors for I/O failures on the output stream.
pub fn cmd_print<W: Write>(w: &mut W, format: PrintFormat) -> Result<()> {
    let env = resolve_env_map();
    match format {
        PrintFormat::Shell => {
            for (k, v) in &env {
                writeln!(w, "{k}={}", shell_single_quote(v))?;
            }
        }
        PrintFormat::Json => {
            let map: serde_json::Map<_, _> = env
                .iter()
                .map(|(k, v)| (k.clone(), serde_json::Value::String(v.clone())))
                .collect();
            serde_json::to_writer_pretty(&mut *w, &map)?;
            writeln!(w)?;
        }
        PrintFormat::Nix => {
            writeln!(w, "{{")?;
            for (k, v) in &env {
                writeln!(w, "  {k} = {};", nix_string(v))?;
            }
            writeln!(w, "}}")?;
        }
        PrintFormat::Fish => {
            for (k, v) in &env {
                writeln!(w, "set -gx {k} {}", shell_single_quote(v))?;
            }
        }
    }
    Ok(())
}

fn resolve_env_map() -> BTreeMap<String, String> {
    let mut out = BTreeMap::new();
    let bin_dir = default_bin_dir();
    out.insert(
        SLOTS_BIN_DIR_ENV.to_string(),
        bin_dir.to_string_lossy().into_owned(),
    );
    if let Ok(m) = SlotManifest::load_default() {
        if let Some(ref src) = m.source {
            out.insert(
                "BMUX_SLOTS_MANIFEST_RESOLVED".to_string(),
                src.to_string_lossy().into_owned(),
            );
        }
        if let Some(d) = m.resolved_default() {
            out.insert("BMUX_DEFAULT_SLOT".to_string(), d.name.clone());
        }
    }
    out
}

// ---------------------------------------------------------------------------
// slot read commands (list / show / paths / doctor)
// ---------------------------------------------------------------------------

/// List all declared slots.
///
/// # Errors
///
/// Returns errors for manifest-load or I/O failures.
pub fn cmd_slot_list<W: Write>(w: &mut W, format: SlotOutputFormat) -> Result<()> {
    let manifest = load_manifest()?;
    match format {
        SlotOutputFormat::Toml => emit_toml_list(w, &manifest)?,
        SlotOutputFormat::Json => emit_json_list(w, &manifest)?,
        SlotOutputFormat::Nix => emit_nix_list(w, &manifest)?,
    }
    Ok(())
}

/// Show a single slot's detail.
///
/// `default_name` is consulted when `name` is `None` (typically passed as the
/// active slot name by bmux_cli, or `None` by the standalone bmux-env binary).
///
/// # Errors
///
/// Returns errors for manifest-load or resolution failures.
pub fn cmd_slot_show<W: Write>(
    w: &mut W,
    name: Option<&str>,
    default_name: Option<&str>,
    format: SlotOutputFormat,
) -> Result<()> {
    let manifest = load_manifest()?;
    let slot = resolve_slot_name(&manifest, name, default_name)?;
    match format {
        SlotOutputFormat::Toml => {
            writeln!(w, "[slots.{}]", slot.name)?;
            emit_slot_toml_body(w, slot)?;
        }
        SlotOutputFormat::Json => {
            let value = slot_to_json(slot);
            serde_json::to_writer_pretty(&mut *w, &value)?;
            writeln!(w)?;
        }
        SlotOutputFormat::Nix => {
            emit_slot_nix(w, slot)?;
        }
    }
    Ok(())
}

/// Print a slot's resolved paths.
///
/// # Errors
///
/// Returns errors for manifest-load or resolution failures.
pub fn cmd_slot_paths<W: Write>(
    w: &mut W,
    name: Option<&str>,
    default_name: Option<&str>,
) -> Result<()> {
    let manifest = load_manifest()?;
    let slot = resolve_slot_name(&manifest, name, default_name)?;
    writeln!(w, "slot         = {}", slot.name)?;
    writeln!(w, "binary       = {}", slot.binary.display())?;
    writeln!(w, "config_dir   = {}", slot.config_dir.display())?;
    writeln!(w, "runtime_dir  = {}", slot.runtime_dir.display())?;
    writeln!(w, "data_dir     = {}", slot.data_dir.display())?;
    writeln!(w, "state_dir    = {}", slot.state_dir.display())?;
    writeln!(w, "log_dir      = {}", slot.log_dir.display())?;
    writeln!(w, "inherit_base = {}", slot.inherit_base)?;
    Ok(())
}

/// Run the manifest doctor, printing advisory messages.
///
/// Returns `true` when everything is healthy, `false` otherwise.
///
/// # Errors
///
/// Returns errors for I/O failures on the output stream.
pub fn cmd_slot_doctor<W: Write>(w: &mut W) -> Result<bool> {
    let manifest = match SlotManifest::load_default() {
        Ok(m) => m,
        Err(e) => {
            writeln!(w, "doctor: failed to load manifest: {e}")?;
            return Ok(false);
        }
    };
    if manifest.slots.is_empty() {
        writeln!(w, "doctor: no slots declared (legacy single-install mode)")?;
        return Ok(true);
    }
    writeln!(w, "doctor: {} slot(s) declared", manifest.slots.len())?;
    let mut any_fail = false;
    for (name, slot) in &manifest.slots {
        writeln!(w, "  slot {name}:")?;
        if validate_slot_name(name).is_err() {
            writeln!(w, "    ✗ invalid name")?;
            any_fail = true;
        } else {
            writeln!(w, "    ✓ name valid")?;
        }
        if slot.binary.exists() {
            writeln!(w, "    ✓ binary present")?;
        } else {
            writeln!(w, "    ✗ binary {} does not exist", slot.binary.display())?;
            any_fail = true;
        }
        if slot.inherit_base {
            let base = bmux_slots::default_base_config_path();
            if base.exists() {
                writeln!(w, "    ✓ base.toml present at {}", base.display())?;
            } else {
                writeln!(
                    w,
                    "    âš  inherit_base = true but base.toml missing at {}",
                    base.display()
                )?;
            }
        }
    }
    Ok(!any_fail)
}

// ---------------------------------------------------------------------------
// slot write commands (install / uninstall)
// ---------------------------------------------------------------------------

/// Outcome of [`cmd_install`], for callers that want to distinguish success
/// from a read-only refusal.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InstallOutcome {
    /// The slot binary + manifest block were written successfully.
    Written,
    /// Manifest was detected as read-only; nothing was written.
    RefusedReadOnly,
    /// `--dry-run` was specified; nothing was written.
    DryRun,
    /// A slot with the same name already exists and the caller did not
    /// pass `overwrite` (and either is non-interactive or `yes` was set).
    RefusedDuplicate,
    /// A slot with the same name already exists and the user declined the
    /// overwrite confirmation prompt.
    RefusedCancelled,
}

/// Install a new slot.
///
/// On success (or in dry-run / read-only refusal), emits the would-be manifest
/// block to `w` in the requested format. When writing, also creates
/// `<bin_dir>/bmux-<name>` as a symlink or copy and appends the block to the
/// manifest.
///
/// # Errors
///
/// - Invalid slot name.
/// - Binary path missing.
/// - Filesystem failures while creating symlink or copy.
/// - Manifest write failures (distinct from read-only refusal, which is
///   signaled via [`InstallOutcome::RefusedReadOnly`] rather than an Err).
pub fn cmd_install<W: Write>(w: &mut W, params: &InstallParams) -> Result<InstallOutcome> {
    validate_slot_name(&params.name).map_err(|e| anyhow!("{e}"))?;

    // Resolve the source binary (to absolute) before storing in manifest.
    let source_binary = if params.binary.is_absolute() {
        params.binary.clone()
    } else {
        std::env::current_dir()
            .context("resolve relative --binary path")?
            .join(&params.binary)
    };
    if !source_binary.exists() {
        bail!("source binary {} does not exist", source_binary.display());
    }

    let block = NewSlotBlock {
        name: params.name.clone(),
        binary: source_binary.clone(),
        inherit_base: params.inherit_base,
    };

    // Always emit the block for the user to see.
    emit_install_block(w, &block, params.format)?;

    if params.dry_run {
        return Ok(InstallOutcome::DryRun);
    }

    let manifest_path = default_manifest_path();
    if manifest_path.exists() && is_read_only_manifest(&manifest_path) {
        writeln!(
            w,
            "note: {} is managed declaratively (read-only); block above is for manual insertion.",
            manifest_path.display()
        )?;
        return Ok(InstallOutcome::RefusedReadOnly);
    }

    // Detect a pre-existing `[slots.<name>]` block in the target manifest file
    // (we intentionally do not consider `extend` files — we only edit this
    // file). If found, either refuse, prompt, or remove it depending on
    // `overwrite` / `yes` / TTY.
    let existing = manifest_has_slot(&manifest_path, &params.name)?;
    if existing {
        match resolve_overwrite_decision(w, &params.name, params.overwrite, params.yes)? {
            OverwriteDecision::Proceed => {
                writeln!(w, "overwriting existing slot '{}'", params.name)?;
                // Drop the old block. Ignore `UnknownSlot` since we already
                // know it exists; surface any other error.
                remove_slot_block(&manifest_path, &params.name).map_err(|e| anyhow!("{e}"))?;
            }
            OverwriteDecision::RefusedDuplicate => {
                writeln!(
                    w,
                    "slot '{}' already exists; pass --overwrite to replace it",
                    params.name
                )?;
                return Ok(InstallOutcome::RefusedDuplicate);
            }
            OverwriteDecision::RefusedCancelled => {
                writeln!(w, "aborted; slot '{}' left unchanged", params.name)?;
                return Ok(InstallOutcome::RefusedCancelled);
            }
        }
    }

    // Place the binary first so a partial failure is easier to reason about
    // (user can retry the manifest write).
    let bin_dir = params.bin_dir.clone().unwrap_or_else(default_bin_dir);
    std::fs::create_dir_all(&bin_dir)
        .with_context(|| format!("create bin dir {}", bin_dir.display()))?;
    let target_path = bin_dir.join(format!("bmux-{}", params.name));
    if target_path.exists() || target_path.is_symlink() {
        // Replace existing link/file; callers are expected to run uninstall
        // first if they want to preserve the old link.
        std::fs::remove_file(&target_path)
            .with_context(|| format!("remove pre-existing {}", target_path.display()))?;
    }
    match params.mode {
        InstallMode::Symlink => {
            #[cfg(unix)]
            {
                std::os::unix::fs::symlink(&source_binary, &target_path).with_context(|| {
                    format!(
                        "symlink {} -> {}",
                        target_path.display(),
                        source_binary.display()
                    )
                })?;
            }
            #[cfg(windows)]
            {
                // File symlinks on Windows need special perms; fall back to copy
                // if symlink fails.
                if let Err(e) = std::os::windows::fs::symlink_file(&source_binary, &target_path) {
                    writeln!(w, "note: symlink failed ({e}); falling back to copy")?;
                    std::fs::copy(&source_binary, &target_path)
                        .with_context(|| format!("copy fallback to {}", target_path.display()))?;
                }
            }
        }
        InstallMode::Copy => {
            std::fs::copy(&source_binary, &target_path).with_context(|| {
                format!(
                    "copy {} -> {}",
                    source_binary.display(),
                    target_path.display()
                )
            })?;
        }
    }

    write_slot_block(&manifest_path, &block).map_err(|e| anyhow!("{e}"))?;
    writeln!(
        w,
        "installed slot '{}' at {} (manifest: {})",
        params.name,
        target_path.display(),
        manifest_path.display()
    )?;
    Ok(InstallOutcome::Written)
}

/// Decision produced by [`resolve_overwrite_decision`] for a slot install
/// request where a manifest block with the same name already exists.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum OverwriteDecision {
    /// Remove the existing block and continue with the install.
    Proceed,
    /// Do not overwrite; caller did not opt in (non-interactive or `--yes`
    /// without `--overwrite`).
    RefusedDuplicate,
    /// Do not overwrite; user explicitly declined the confirmation prompt.
    RefusedCancelled,
}

/// Check whether the target manifest file already contains a
/// `[slots.<name>]` block. Mirrors the detection used by
/// [`bmux_slots::write_slot_block`] so both paths agree.
fn manifest_has_slot(manifest_path: &Path, name: &str) -> Result<bool> {
    let contents = match std::fs::read_to_string(manifest_path) {
        Ok(s) => s,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(false),
        Err(e) => {
            return Err(anyhow!(
                "failed reading manifest {}: {e}",
                manifest_path.display()
            ));
        }
    };
    let header = format!("[slots.{name}]");
    Ok(contents.contains(&header))
}

/// Decide what to do when the target slot name already exists. On an
/// interactive TTY the user is prompted (`[y/N]`). Non-interactive callers
/// must pass `overwrite` to proceed.
fn resolve_overwrite_decision<W: Write>(
    w: &mut W,
    name: &str,
    overwrite: bool,
    yes: bool,
) -> Result<OverwriteDecision> {
    let interactive = std::io::stdin().is_terminal();

    // With `--overwrite --yes`, skip the prompt entirely.
    if overwrite && yes {
        return Ok(OverwriteDecision::Proceed);
    }

    // Non-interactive: require `--overwrite` to proceed; no prompting.
    if !interactive {
        if overwrite {
            return Ok(OverwriteDecision::Proceed);
        }
        return Ok(OverwriteDecision::RefusedDuplicate);
    }

    // Interactive TTY: always confirm before replacing (even with
    // `--overwrite`, since the operation is destructive).
    if prompt_overwrite_confirmation(w, name)? {
        Ok(OverwriteDecision::Proceed)
    } else {
        Ok(OverwriteDecision::RefusedCancelled)
    }
}

/// Prompt the user on stdin/stdout for an overwrite confirmation. Returns
/// `true` only when the user typed `y` / `yes` (case-insensitive). Default
/// is No.
fn prompt_overwrite_confirmation<W: Write>(w: &mut W, name: &str) -> Result<bool> {
    writeln!(w, "Slot '{name}' already exists. Overwrite? [y/N]")?;
    w.flush().ok();
    let mut answer = String::new();
    std::io::stdin()
        .read_line(&mut answer)
        .context("failed reading overwrite confirmation")?;
    let trimmed = answer.trim().to_ascii_lowercase();
    Ok(trimmed == "y" || trimmed == "yes")
}

/// Outcome of [`cmd_uninstall`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UninstallOutcome {
    /// Removed from manifest and binary link.
    Removed,
    /// Manifest was read-only; nothing changed.
    RefusedReadOnly,
}

/// Remove a slot's manifest block and binary link.
///
/// When `purge` is true, also removes the slot's config/data/state/log dirs.
/// Otherwise the slot's on-disk data is preserved.
///
/// # Errors
///
/// - Invalid slot name.
/// - Manifest I/O failures.
pub fn cmd_uninstall<W: Write>(
    w: &mut W,
    name: &str,
    purge: bool,
    bin_dir: Option<&Path>,
) -> Result<UninstallOutcome> {
    validate_slot_name(name).map_err(|e| anyhow!("{e}"))?;

    let manifest_path = default_manifest_path();
    if manifest_path.exists() && is_read_only_manifest(&manifest_path) {
        writeln!(
            w,
            "note: {} is managed declaratively (read-only); cannot uninstall.",
            manifest_path.display()
        )?;
        return Ok(UninstallOutcome::RefusedReadOnly);
    }

    // Snapshot slot paths for --purge before we drop the manifest block.
    let purge_paths = if purge {
        SlotManifest::load_default()
            .ok()
            .and_then(|m| m.slots.get(name).cloned())
            .map(|s| vec![s.config_dir, s.data_dir, s.state_dir, s.log_dir])
    } else {
        None
    };

    // Remove manifest block. Missing manifest => warn-only.
    if manifest_path.exists() {
        match remove_slot_block(&manifest_path, name) {
            Ok(()) => {}
            Err(e) => {
                writeln!(w, "warning: removing manifest block: {e}")?;
            }
        }
    }

    // Remove binary link/copy.
    let bin_dir = bin_dir.map_or_else(default_bin_dir, Path::to_path_buf);
    let target = bin_dir.join(format!("bmux-{name}"));
    if (target.exists() || target.is_symlink())
        && let Err(e) = std::fs::remove_file(&target)
    {
        writeln!(w, "warning: removing {}: {e}", target.display())?;
    }

    if let Some(paths) = purge_paths {
        for p in paths {
            if p.exists()
                && let Err(e) = std::fs::remove_dir_all(&p)
            {
                writeln!(w, "warning: purging {}: {e}", p.display())?;
            }
        }
    }

    writeln!(w, "uninstalled slot '{name}'")?;
    Ok(UninstallOutcome::Removed)
}

fn emit_install_block<W: Write>(
    w: &mut W,
    block: &NewSlotBlock,
    format: SlotOutputFormat,
) -> Result<()> {
    match format {
        SlotOutputFormat::Toml => {
            writeln!(w, "# Slot block:")?;
            write!(w, "{}", render_slot_block_toml(block))?;
        }
        SlotOutputFormat::Json => {
            let value = serde_json::json!({
                "name": block.name,
                "binary": block.binary.to_string_lossy(),
                "inherit_base": block.inherit_base,
            });
            serde_json::to_writer_pretty(&mut *w, &value)?;
            writeln!(w)?;
        }
        SlotOutputFormat::Nix => {
            writeln!(w, "# Home Manager attrset:")?;
            writeln!(w, "slots.{} = {{", block.name)?;
            writeln!(
                w,
                "  binary = {};",
                nix_string(&block.binary.to_string_lossy())
            )?;
            writeln!(w, "  inheritBase = {};", block.inherit_base)?;
            writeln!(w, "}};")?;
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// helpers shared across commands
// ---------------------------------------------------------------------------

fn load_manifest() -> Result<SlotManifest> {
    SlotManifest::load_default().map_err(|e| anyhow::anyhow!("failed loading slot manifest: {e}"))
}

fn resolve_slot_name<'a>(
    manifest: &'a SlotManifest,
    name: Option<&str>,
    default_name: Option<&str>,
) -> Result<&'a Slot> {
    if let Some(n) = name {
        return manifest.get(n).map_err(|e| anyhow!("{e}"));
    }
    if let Some(n) = default_name {
        return manifest.get(n).map_err(|e| anyhow!("{e}"));
    }
    if let Some(d) = manifest.resolved_default() {
        return Ok(d);
    }
    anyhow::bail!(
        "no slot specified and no active or default slot is set (manifest has {} slots)",
        manifest.slots.len()
    )
}

fn emit_toml_list<W: Write>(w: &mut W, m: &SlotManifest) -> std::io::Result<()> {
    if let Some(ref d) = m.default {
        writeln!(w, "default = {}", bmux_slots::toml_string_literal(d))?;
        writeln!(w)?;
    }
    for (name, slot) in &m.slots {
        writeln!(w, "[slots.{name}]")?;
        emit_slot_toml_body(w, slot)?;
        writeln!(w)?;
    }
    Ok(())
}

fn emit_slot_toml_body<W: Write>(w: &mut W, slot: &Slot) -> std::io::Result<()> {
    writeln!(
        w,
        "binary       = {}",
        bmux_slots::toml_string_literal(&slot.binary.to_string_lossy())
    )?;
    writeln!(w, "inherit_base = {}", slot.inherit_base)?;
    writeln!(
        w,
        "config_dir   = {}",
        bmux_slots::toml_string_literal(&slot.config_dir.to_string_lossy())
    )?;
    writeln!(
        w,
        "runtime_dir  = {}",
        bmux_slots::toml_string_literal(&slot.runtime_dir.to_string_lossy())
    )?;
    writeln!(
        w,
        "data_dir     = {}",
        bmux_slots::toml_string_literal(&slot.data_dir.to_string_lossy())
    )?;
    writeln!(
        w,
        "state_dir    = {}",
        bmux_slots::toml_string_literal(&slot.state_dir.to_string_lossy())
    )?;
    writeln!(
        w,
        "log_dir      = {}",
        bmux_slots::toml_string_literal(&slot.log_dir.to_string_lossy())
    )?;
    Ok(())
}

fn emit_json_list<W: Write>(w: &mut W, m: &SlotManifest) -> std::io::Result<()> {
    let value = serde_json::json!({
        "default": m.default,
        "slots": m.slots.values().map(slot_to_json).collect::<Vec<_>>(),
    });
    serde_json::to_writer_pretty(&mut *w, &value)?;
    writeln!(w)
}

fn slot_to_json(slot: &Slot) -> serde_json::Value {
    serde_json::json!({
        "name": slot.name,
        "binary": slot.binary.to_string_lossy(),
        "inherit_base": slot.inherit_base,
        "config_dir": slot.config_dir.to_string_lossy(),
        "runtime_dir": slot.runtime_dir.to_string_lossy(),
        "data_dir": slot.data_dir.to_string_lossy(),
        "state_dir": slot.state_dir.to_string_lossy(),
        "log_dir": slot.log_dir.to_string_lossy(),
    })
}

fn emit_nix_list<W: Write>(w: &mut W, m: &SlotManifest) -> std::io::Result<()> {
    writeln!(w, "{{")?;
    if let Some(ref d) = m.default {
        writeln!(w, "  default = {};", nix_string(d))?;
    }
    writeln!(w, "  slots = {{")?;
    for (name, slot) in &m.slots {
        writeln!(w, "    {name} = {{")?;
        writeln!(
            w,
            "      binary = {};",
            nix_string(&slot.binary.to_string_lossy())
        )?;
        writeln!(w, "      inheritBase = {};", slot.inherit_base)?;
        writeln!(
            w,
            "      configDir = {};",
            nix_string(&slot.config_dir.to_string_lossy())
        )?;
        writeln!(
            w,
            "      runtimeDir = {};",
            nix_string(&slot.runtime_dir.to_string_lossy())
        )?;
        writeln!(
            w,
            "      dataDir = {};",
            nix_string(&slot.data_dir.to_string_lossy())
        )?;
        writeln!(
            w,
            "      stateDir = {};",
            nix_string(&slot.state_dir.to_string_lossy())
        )?;
        writeln!(
            w,
            "      logDir = {};",
            nix_string(&slot.log_dir.to_string_lossy())
        )?;
        writeln!(w, "    }};")?;
    }
    writeln!(w, "  }};")?;
    writeln!(w, "}}")?;
    Ok(())
}

fn emit_slot_nix<W: Write>(w: &mut W, slot: &Slot) -> std::io::Result<()> {
    writeln!(w, "{{")?;
    writeln!(w, "  name = {};", nix_string(&slot.name))?;
    writeln!(
        w,
        "  binary = {};",
        nix_string(&slot.binary.to_string_lossy())
    )?;
    writeln!(w, "  inheritBase = {};", slot.inherit_base)?;
    writeln!(
        w,
        "  configDir = {};",
        nix_string(&slot.config_dir.to_string_lossy())
    )?;
    writeln!(
        w,
        "  runtimeDir = {};",
        nix_string(&slot.runtime_dir.to_string_lossy())
    )?;
    writeln!(
        w,
        "  dataDir = {};",
        nix_string(&slot.data_dir.to_string_lossy())
    )?;
    writeln!(
        w,
        "  stateDir = {};",
        nix_string(&slot.state_dir.to_string_lossy())
    )?;
    writeln!(
        w,
        "  logDir = {};",
        nix_string(&slot.log_dir.to_string_lossy())
    )?;
    writeln!(w, "}}")?;
    Ok(())
}

// ---------------------------------------------------------------------------
// shell dialect helpers
// ---------------------------------------------------------------------------

fn bash_zsh(bin_dir: &str) -> String {
    let quoted = shell_double_quote_body(bin_dir);
    format!(
        r#"# bmux-env shell (bash/zsh)
export BMUX_SLOTS_BIN_DIR="${{BMUX_SLOTS_BIN_DIR:-{quoted}}}"
case ":$PATH:" in
  *":$BMUX_SLOTS_BIN_DIR:"*) ;;
  *) export PATH="$BMUX_SLOTS_BIN_DIR:$PATH" ;;
esac
"#
    )
}

fn posix(bin_dir: &str) -> String {
    let quoted = shell_double_quote_body(bin_dir);
    format!(
        r#"# bmux-env shell (posix)
BMUX_SLOTS_BIN_DIR="${{BMUX_SLOTS_BIN_DIR:-{quoted}}}"
export BMUX_SLOTS_BIN_DIR
case ":$PATH:" in
  *":$BMUX_SLOTS_BIN_DIR:"*) ;;
  *) PATH="$BMUX_SLOTS_BIN_DIR:$PATH"; export PATH ;;
esac
"#
    )
}

fn fish(bin_dir: &str) -> String {
    let quoted = shell_single_quote(bin_dir);
    format!(
        r#"# bmux-env shell (fish)
if not set -q BMUX_SLOTS_BIN_DIR
    set -gx BMUX_SLOTS_BIN_DIR {quoted}
end
if not contains -- $BMUX_SLOTS_BIN_DIR $PATH
    set -gx PATH $BMUX_SLOTS_BIN_DIR $PATH
end
"#
    )
}

fn nushell(bin_dir: &str) -> String {
    let quoted = nu_double_quote(bin_dir);
    format!(
        r#"# bmux-env shell (nushell)
let-env BMUX_SLOTS_BIN_DIR = ($env.BMUX_SLOTS_BIN_DIR? | default {quoted})
let-env PATH = (
    $env.PATH
    | split row (char esep)
    | prepend $env.BMUX_SLOTS_BIN_DIR
    | uniq
    | str collect (char esep)
)
"#
    )
}

fn powershell(bin_dir: &str) -> String {
    let quoted = ps_double_quote(bin_dir);
    format!(
        r#"# bmux-env shell (powershell)
if (-not $env:BMUX_SLOTS_BIN_DIR) {{ $env:BMUX_SLOTS_BIN_DIR = {quoted} }}
$sep = [System.IO.Path]::PathSeparator
$parts = $env:PATH.Split($sep)
if ($parts -notcontains $env:BMUX_SLOTS_BIN_DIR) {{
    $env:PATH = $env:BMUX_SLOTS_BIN_DIR + $sep + $env:PATH
}}
"#
    )
}

fn shell_single_quote(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('\'');
    for ch in s.chars() {
        if ch == '\'' {
            out.push_str("'\\''");
        } else {
            out.push(ch);
        }
    }
    out.push('\'');
    out
}

fn shell_double_quote_body(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for ch in s.chars() {
        if matches!(ch, '"' | '\\' | '$' | '`') {
            out.push('\\');
        }
        out.push(ch);
    }
    out
}

fn nu_double_quote(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('"');
    for ch in s.chars() {
        if ch == '"' || ch == '\\' {
            out.push('\\');
        }
        out.push(ch);
    }
    out.push('"');
    out
}

fn ps_double_quote(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('"');
    for ch in s.chars() {
        if ch == '"' {
            out.push_str("`\"");
        } else if ch == '`' {
            out.push_str("``");
        } else if ch == '$' {
            out.push_str("`$");
        } else {
            out.push(ch);
        }
    }
    out.push('"');
    out
}

fn nix_string(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('"');
    for ch in s.chars() {
        if ch == '"' || ch == '\\' {
            out.push('\\');
        }
        out.push(ch);
    }
    out.push('"');
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn bash_zsh_output_is_idempotent_shape() {
        let out = bash_zsh("/some/bin");
        assert!(out.contains("export BMUX_SLOTS_BIN_DIR=\"${BMUX_SLOTS_BIN_DIR:-/some/bin}\""));
        assert!(out.contains(r#"case ":$PATH:""#));
        assert!(out.contains("export PATH=\"$BMUX_SLOTS_BIN_DIR:$PATH\""));
    }

    #[test]
    fn shell_double_quote_body_escapes_metachars() {
        assert_eq!(shell_double_quote_body("a b"), "a b");
        assert_eq!(shell_double_quote_body(r#"say "hi""#), r#"say \"hi\""#);
        assert_eq!(shell_double_quote_body("$var"), r"\$var");
        assert_eq!(shell_double_quote_body("a`b"), r"a\`b");
    }

    #[test]
    fn nix_string_escapes_quotes_and_backslashes() {
        assert_eq!(nix_string(r#"a"b"#), r#""a\"b""#);
        assert_eq!(nix_string(r"a\b"), r#""a\\b""#);
    }

    #[test]
    fn cmd_install_dry_run_does_not_touch_disk() {
        let tmp = tempfile::tempdir().unwrap();
        let binary = tmp.path().join("fake-bmux");
        std::fs::write(&binary, b"#!/bin/sh\n").unwrap();
        let bin_dir = tmp.path().join("bin");

        let params = InstallParams {
            name: "cursor".into(),
            binary: binary.clone(),
            inherit_base: false,
            mode: InstallMode::Symlink,
            bin_dir: Some(bin_dir.clone()),
            format: SlotOutputFormat::Toml,
            dry_run: true,
            overwrite: false,
            yes: false,
        };
        let mut out = Vec::new();
        let outcome = cmd_install(&mut out, &params).unwrap();
        assert!(matches!(outcome, InstallOutcome::DryRun));
        assert!(!bin_dir.join("bmux-cursor").exists());
        let text = String::from_utf8(out).unwrap();
        assert!(text.contains("[slots.cursor]"));
    }

    #[test]
    fn manifest_has_slot_detects_existing_block() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("slots.toml");
        std::fs::write(
            &path,
            "[slots.dev]\nbinary = \"/tmp/bmux\"\ninherit_base = true\n",
        )
        .unwrap();
        assert!(manifest_has_slot(&path, "dev").unwrap());
        assert!(!manifest_has_slot(&path, "prod").unwrap());
    }

    #[test]
    fn manifest_has_slot_returns_false_for_missing_file() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("does-not-exist.toml");
        assert!(!manifest_has_slot(&path, "dev").unwrap());
    }

    // Global mutex serialising tests that mutate BMUX_SLOTS_MANIFEST /
    // BMUX_SLOTS_BIN_DIR. Tests are otherwise racy since env is process-wide.
    fn env_lock() -> &'static std::sync::Mutex<()> {
        static LOCK: std::sync::OnceLock<std::sync::Mutex<()>> = std::sync::OnceLock::new();
        LOCK.get_or_init(|| std::sync::Mutex::new(()))
    }

    struct EnvGuard {
        key: &'static str,
        prev: Option<std::ffi::OsString>,
    }
    impl EnvGuard {
        fn set(key: &'static str, value: &std::path::Path) -> Self {
            let prev = std::env::var_os(key);
            // Safety: tests are serialised via env_lock() to avoid races.
            unsafe { std::env::set_var(key, value) };
            Self { key, prev }
        }
    }
    impl Drop for EnvGuard {
        fn drop(&mut self) {
            // Safety: tests are serialised via env_lock() to avoid races.
            unsafe {
                match &self.prev {
                    Some(v) => std::env::set_var(self.key, v),
                    None => std::env::remove_var(self.key),
                }
            }
        }
    }

    fn make_install_params(
        name: &str,
        binary: PathBuf,
        bin_dir: PathBuf,
        overwrite: bool,
        yes: bool,
    ) -> InstallParams {
        InstallParams {
            name: name.to_string(),
            binary,
            inherit_base: false,
            mode: InstallMode::Symlink,
            bin_dir: Some(bin_dir),
            format: SlotOutputFormat::Toml,
            dry_run: false,
            overwrite,
            yes,
        }
    }

    #[test]
    fn cmd_install_refuses_duplicate_without_overwrite_non_interactive() {
        let _g = env_lock().lock().unwrap();
        let tmp = tempfile::tempdir().unwrap();
        let manifest = tmp.path().join("slots.toml");
        let bin_dir = tmp.path().join("bin");
        let binary = tmp.path().join("fake-bmux");
        std::fs::write(&binary, b"#!/bin/sh\n").unwrap();
        // Seed an existing slot block.
        std::fs::write(
            &manifest,
            "[slots.dev]\nbinary = \"/tmp/old-bmux\"\ninherit_base = true\n",
        )
        .unwrap();

        let _manifest_env = EnvGuard::set(bmux_slots::SLOTS_MANIFEST_ENV, &manifest);

        let params = make_install_params("dev", binary, bin_dir, false, false);
        let mut out = Vec::new();
        let outcome = cmd_install(&mut out, &params).unwrap();
        assert!(matches!(outcome, InstallOutcome::RefusedDuplicate));

        // Manifest untouched.
        let contents = std::fs::read_to_string(&manifest).unwrap();
        assert!(contents.contains("/tmp/old-bmux"));

        let text = String::from_utf8(out).unwrap();
        assert!(text.contains("already exists"));
        assert!(text.contains("--overwrite"));
    }

    #[test]
    fn cmd_install_overwrite_yes_replaces_existing_slot() {
        let _g = env_lock().lock().unwrap();
        let tmp = tempfile::tempdir().unwrap();
        let manifest = tmp.path().join("slots.toml");
        let bin_dir = tmp.path().join("bin");
        let binary = tmp.path().join("new-bmux");
        std::fs::write(&binary, b"#!/bin/sh\n# new\n").unwrap();
        std::fs::write(
            &manifest,
            "[slots.dev]\nbinary = \"/tmp/old-bmux\"\ninherit_base = true\n",
        )
        .unwrap();

        let _manifest_env = EnvGuard::set(bmux_slots::SLOTS_MANIFEST_ENV, &manifest);

        let params = make_install_params("dev", binary.clone(), bin_dir.clone(), true, true);
        let mut out = Vec::new();
        let outcome = cmd_install(&mut out, &params).unwrap();
        assert!(matches!(outcome, InstallOutcome::Written));

        // The old block is gone; the new one points at the new binary.
        let contents = std::fs::read_to_string(&manifest).unwrap();
        assert!(!contents.contains("/tmp/old-bmux"));
        assert!(contents.contains(binary.to_string_lossy().as_ref()));

        // The bin-dir symlink/file exists for the slot.
        assert!(bin_dir.join("bmux-dev").exists() || bin_dir.join("bmux-dev").is_symlink());

        let text = String::from_utf8(out).unwrap();
        assert!(text.contains("overwriting existing slot 'dev'"));
        assert!(text.contains("installed slot 'dev'"));
    }

    #[test]
    fn cmd_install_dry_run_with_existing_slot_does_not_touch_disk() {
        let _g = env_lock().lock().unwrap();
        let tmp = tempfile::tempdir().unwrap();
        let manifest = tmp.path().join("slots.toml");
        let bin_dir = tmp.path().join("bin");
        let binary = tmp.path().join("new-bmux");
        std::fs::write(&binary, b"#!/bin/sh\n").unwrap();
        let original = "[slots.dev]\nbinary = \"/tmp/old-bmux\"\ninherit_base = true\n";
        std::fs::write(&manifest, original).unwrap();

        let _manifest_env = EnvGuard::set(bmux_slots::SLOTS_MANIFEST_ENV, &manifest);

        let mut params = make_install_params("dev", binary, bin_dir.clone(), false, false);
        params.dry_run = true;
        let mut out = Vec::new();
        let outcome = cmd_install(&mut out, &params).unwrap();
        assert!(matches!(outcome, InstallOutcome::DryRun));

        // Manifest untouched; bin-dir not even created.
        assert_eq!(std::fs::read_to_string(&manifest).unwrap(), original);
        assert!(!bin_dir.exists());
    }

    // tempfile is a dev-dependency of bmux_slots; surface it here via
    // bmux_slots' re-export would be cleaner long-term, but we can pull it
    // directly via the workspace dev-deps for now. This test crate already
    // re-uses tempfile through bmux_slots' dev-deps chain.
}