zenops 0.18.0

Declarative system configuration management for shell config and dotfiles.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
//! Loading, parsing, and resolving `~/.config/zenops/config.toml`.
//!
//! [`Config`] is the in-memory view of a parsed config. Submodules
//! ([`shell`], [`pkg`], [`ssh`], [`user`], [`git`], …) own the
//! `Stored*` deserialize shapes that map onto each TOML section.
//!
//! Callers go through [`Config::load`], then drive the loaded config:
//! [`Config::update_config_files`] populates the materialiser,
//! [`Config::push_pkg_health`] emits package status events, and
//! [`Config::check_own_status`] reports git state for the zenops repo
//! itself.
//!
//! `Config::load` also builds a small map of system inputs
//! (`brew_prefix`, `os`, `user.name`, `user.email`, …) used to
//! [`zenops_expand`]-expand `${...}` placeholders inside generated
//! config bodies.

pub(crate) mod condition;
mod error;
mod git;
pub(crate) mod pkg;
mod pkg_config_files;
pub(crate) mod shell;
pub(crate) mod ssh;
mod stored_relative_path;
mod user;

pub use error::Error as ConfigError;

use std::{
    path::{Path, PathBuf},
    sync::Arc,
};

use indexmap::IndexMap;
use smol_str::SmolStr;
use xshell::cmd;
use zenops_safe_relative_path::srpath;

pub use crate::config::pkg::PkgConfig;

use crate::{
    config::{
        condition::{Condition, Conditions, HostContext},
        git::StoredGitConfig,
        pkg::{Shell, ShellInitAction},
        shell::StoredShellEnvironment,
        ssh::{CurlGithubKeyFetcher, StoredSshConfig},
        user::StoredUserConfig,
    },
    config_files::{ConfigFileDirs, ConfigFilePath, ConfigFiles},
    error::Error,
    git::Git,
    os::Os,
    output::{Event, Output, PkgStatus, ResolvedConfigFilePath, Status},
    pkg_manager,
};

#[derive(serde::Deserialize, schemars::JsonSchema, Debug, Clone, PartialEq, Default)]
#[serde(deny_unknown_fields, default)]
pub(crate) struct StoredConfig {
    shell: StoredShellEnvironment,
    pkg: IndexMap<SmolStr, PkgConfig>,
    ssh: StoredSshConfig,
    user: StoredUserConfig,
    git: StoredGitConfig,
    conditions: IndexMap<SmolStr, Condition>,
}

pub struct Config<'dirs> {
    dirs: &'dirs ConfigFileDirs,
    zenops_repo: ResolvedConfigFilePath,
    stored: StoredConfig,
    system_inputs: IndexMap<SmolStr, SmolStr>,
    conditions: Conditions,
    hostname: String,
}

fn detect_brew_prefix() -> Result<Option<PathBuf>, Error> {
    const CANDIDATES: &[&str] = &["/opt/homebrew", "/usr/local", "/home/linuxbrew/.linuxbrew"];
    for prefix in CANDIDATES.iter().map(Path::new) {
        let brew = prefix.join("bin/brew");
        if brew
            .try_exists()
            .map_err(|e| ConfigError::BrewProbeFailed(brew.clone(), e))?
        {
            return Ok(Some(prefix.to_path_buf()));
        }
    }
    Ok(None)
}

fn build_system_inputs(
    brew_prefix: Option<&Path>,
    user: &StoredUserConfig,
) -> IndexMap<SmolStr, SmolStr> {
    let mut m = IndexMap::new();
    if let Some(p) = brew_prefix {
        m.insert(
            SmolStr::new_static("brew_prefix"),
            SmolStr::new(p.to_string_lossy()),
        );
    }
    m.insert(
        SmolStr::new_static("os"),
        SmolStr::new_static(std::env::consts::OS),
    );
    if let Some(name) = &user.name {
        m.insert(SmolStr::new_static("user.name"), name.clone());
    }
    if let Some(email) = &user.email {
        m.insert(SmolStr::new_static("user.email"), email.clone());
    }
    m
}

static DEFAULT_PKGS: &[(&str, &str)] = &[
    ("brew-macos", include_str!("pkgs/brew-macos.toml")),
    ("brew-linux", include_str!("pkgs/brew-linux.toml")),
    ("bashrc-chain", include_str!("pkgs/bashrc-chain.toml")),
    ("local-bin", include_str!("pkgs/local-bin.toml")),
    ("brew-python", include_str!("pkgs/brew-python.toml")),
    ("cargo", include_str!("pkgs/cargo.toml")),
    ("bash-completion", include_str!("pkgs/bash-completion.toml")),
    ("zsh-completions", include_str!("pkgs/zsh-completions.toml")),
    ("sk", include_str!("pkgs/sk.toml")),
    ("starship", include_str!("pkgs/starship.toml")),
    ("zenops", include_str!("pkgs/zenops.toml")),
    ("llvm", include_str!("pkgs/llvm.toml")),
];

static BUILTIN_CONDITIONS: &str = include_str!("condition_builtins.toml");

fn deep_merge(base: &mut toml::Value, overlay: toml::Value) {
    match (base, overlay) {
        (toml::Value::Table(b), toml::Value::Table(o)) => {
            for (k, v) in o {
                deep_merge(
                    b.entry(k).or_insert(toml::Value::Table(Default::default())),
                    v,
                );
            }
        }
        (base, overlay) => *base = overlay,
    }
}

impl<'dirs> Config<'dirs> {
    pub fn load(
        dirs: &'dirs ConfigFileDirs,
        sh: &xshell::Shell,
        update_self: bool,
    ) -> Result<Self, Error> {
        if update_self {
            let zenops_dir = dirs.zenops();
            cmd!(sh, "git -C {zenops_dir} pull --rebase").run()?;
        }

        let zenops_repo =
            ResolvedConfigFilePath::resolve(ConfigFilePath::Zenops(Arc::from(srpath!(""))), dirs);

        let path = dirs.zenops().join("config.toml");

        let mut merged = toml::Value::Table(Default::default());
        let builtin_conditions: toml::Value = toml::from_str(BUILTIN_CONDITIONS).map_err(|e| {
            ConfigError::ParseDb(std::path::PathBuf::from("<defaults:conditions>"), e)
        })?;
        deep_merge(&mut merged, builtin_conditions);
        for (name, src) in DEFAULT_PKGS {
            let v: toml::Value = toml::from_str(src).map_err(|e| {
                ConfigError::ParseDb(std::path::PathBuf::from(format!("<defaults:{name}>")), e)
            })?;
            deep_merge(&mut merged, v);
        }

        let user_bytes = std::fs::read(&path).map_err(|e| ConfigError::OpenDb(path.clone(), e))?;
        let user_val: toml::Value = toml::from_slice(&user_bytes)
            .map_err(|e| ConfigError::ParseDb(path.to_path_buf(), e))?;

        deep_merge(&mut merged, user_val);

        let stored: StoredConfig = merged
            .try_into()
            .map_err(|e| ConfigError::ParseDb(path.to_path_buf(), e))?;

        let conditions = Conditions::compile(stored.conditions.clone())
            .map_err(ConfigError::CompileConditions)?;
        let hostname = gethostname::gethostname().to_string_lossy().into_owned();

        let brew_prefix = detect_brew_prefix()?;
        let system_inputs = build_system_inputs(brew_prefix.as_deref(), &stored.user);

        Ok(Self {
            dirs,
            zenops_repo,
            stored,
            system_inputs,
            conditions,
            hostname,
        })
    }

    pub fn pkgs(&self) -> &IndexMap<SmolStr, PkgConfig> {
        &self.stored.pkg
    }

    pub fn home(&self) -> &Path {
        self.dirs.home()
    }

    pub fn system_inputs(&self) -> &IndexMap<SmolStr, SmolStr> {
        &self.system_inputs
    }

    pub(crate) fn conditions(&self) -> &Conditions {
        &self.conditions
    }

    pub(crate) fn shell(&self) -> Option<Shell> {
        self.stored.shell.shell()
    }

    /// Build a [`HostContext`] keyed to this `Config`'s host. The optional
    /// `shell` override lets callers (e.g. shell-init emitters that need
    /// per-shell evaluation) supply a shell different from `self.shell()`;
    /// passing `None` falls back to the configured shell.
    pub(crate) fn host_context(&self, shell: Option<Shell>) -> Result<HostContext<'_>, Error> {
        Ok(HostContext {
            os: Os::current()?,
            shell: shell.or_else(|| self.shell()),
            hostname: &self.hostname,
            home: self.dirs.home(),
            system_inputs: &self.system_inputs,
        })
    }

    pub(crate) fn env_pkg_inits(
        &self,
        shell: Shell,
    ) -> Result<Vec<(&SmolStr, &PkgConfig, &ShellInitAction)>, Error> {
        let ctx = self.host_context(Some(shell))?;
        let mut inits = Vec::new();
        for (name, p) in &self.stored.pkg {
            if p.is_installed(&self.conditions, &ctx)? {
                for a in p.shell.env_init.for_shell(shell).iter() {
                    inits.push((name, p, a));
                }
            }
        }
        Ok(inits)
    }

    pub(crate) fn login_pkg_inits(
        &self,
        shell: Shell,
    ) -> Result<Vec<(&SmolStr, &PkgConfig, &ShellInitAction)>, Error> {
        let ctx = self.host_context(Some(shell))?;
        let mut inits = Vec::new();
        for (name, p) in &self.stored.pkg {
            if p.is_installed(&self.conditions, &ctx)? {
                for a in p.shell.login_init.for_shell(shell).iter() {
                    inits.push((name, p, a));
                }
            }
        }
        Ok(inits)
    }

    pub(crate) fn interactive_pkg_inits(
        &self,
        shell: Shell,
    ) -> Result<Vec<(&SmolStr, &PkgConfig, &ShellInitAction)>, Error> {
        let ctx = self.host_context(Some(shell))?;
        let mut inits = Vec::new();
        for (name, p) in &self.stored.pkg {
            if p.is_installed(&self.conditions, &ctx)? {
                for a in p.shell.interactive_init.for_shell(shell).iter() {
                    inits.push((name, p, a));
                }
            }
        }
        Ok(inits)
    }

    pub fn update_config_files(
        &self,
        _sh: &xshell::Shell,
        config_files: &mut ConfigFiles<'_>,
    ) -> Result<(), Error> {
        self.stored.shell.update_config_files(self, config_files)?;
        self.stored
            .ssh
            .update_config_files(config_files, &CurlGithubKeyFetcher)?;
        self.stored.git.update_config_files(
            &self.stored.user,
            !self.stored.ssh.allowed_signers.is_empty(),
            config_files,
        )?;
        let ctx = self.host_context(None)?;
        for (pkg_key, pkg) in &self.stored.pkg {
            if !pkg.is_installed(&self.conditions, &ctx)? {
                continue;
            }
            for cfg in pkg.configs() {
                cfg.update_config_files(pkg_key, self, config_files)?;
            }
        }
        Ok(())
    }

    pub fn check_own_status(
        &self,
        sh: &xshell::Shell,
        output: &mut dyn Output,
    ) -> Result<(), Error> {
        let git = Git::new(self.dirs.zenops(), sh);
        if git.is_git_repo()? {
            let statuses = git.status()?;
            if statuses.is_empty() {
                output.push(Event::Status(Status::GitRepoClean {
                    repo: self.zenops_repo.clone(),
                }))?;
            } else {
                for status in statuses {
                    output.push(Event::Status(Status::Git {
                        repo: self.zenops_repo.clone(),
                        status,
                    }))?;
                }
            }
        }
        Ok(())
    }

    /// Emit a `Status::Pkg` event for every pkg under the `enable = "on"`
    /// contract: `PkgStatus::Missing` when detect doesn't match on this
    /// host, `PkgStatus::Ok` when it does (or when there's no detect to
    /// check). No-op for `detect`/`disabled` pkgs — silence on miss is the
    /// defining behavior of `detect`, and `--all` keeps that invariant
    /// (detect pkgs live in `zenops pkg --all`'s column instead). Called
    /// from the apply/status entry points, not from `Config::load` — a
    /// load isn't an event, and these observations should only surface
    /// from commands the user runs.
    pub fn push_pkg_health(&self, output: &mut dyn Output) -> Result<(), Error> {
        let ctx = self.host_context(None)?;
        let manager = pkg_manager::detect(&ctx.os)?;
        for (key, pkg) in &self.stored.pkg {
            let label = pkg.name.clone().unwrap_or_else(|| key.clone());
            if pkg.enable_on_but_detect_missing(&self.conditions, &ctx)? {
                let install_command = manager.and_then(|m| {
                    let pkgs = m.packages_for(&pkg.install_hint);
                    (!pkgs.is_empty()).then(|| m.install_command(pkgs))
                });
                output.push(Event::Status(Status::Pkg {
                    pkg: label,
                    status: PkgStatus::Missing { install_command },
                }))?;
            } else if pkg.enable_on_and_detect_matches(&self.conditions, &ctx)? {
                output.push(Event::Status(Status::Pkg {
                    pkg: label,
                    status: PkgStatus::Ok,
                }))?;
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod readme_tests {
    use super::StoredConfig;
    use std::path::{Path, PathBuf};

    /// Every ```toml block in README.md and under docs/ must deserialize as a
    /// full [`StoredConfig`]. Guards against docs silently drifting away from
    /// the real config shape (e.g. after a breaking rename like `[[configs]]`
    /// → `[[pkg.x.configs]]`).
    #[test]
    fn doc_toml_blocks_parse_as_stored_config() {
        let root = Path::new(env!("CARGO_MANIFEST_DIR"));
        let mut files: Vec<PathBuf> = vec![root.join("README.md")];
        let docs_dir = root.join("docs");
        if docs_dir.is_dir() {
            for entry in std::fs::read_dir(&docs_dir).expect("read docs/") {
                let path = entry.expect("docs/ entry").path();
                if path.extension().is_some_and(|e| e == "md") {
                    files.push(path);
                }
            }
        }
        files.sort();

        let mut total_blocks = 0usize;
        for file in &files {
            let body = std::fs::read_to_string(file)
                .unwrap_or_else(|e| panic!("read {}: {e}", file.display()));
            let blocks = extract_toml_blocks(&body);
            for (i, block) in blocks.iter().enumerate() {
                toml::from_str::<StoredConfig>(block).unwrap_or_else(|e| {
                    panic!(
                        "{} ```toml block #{i} failed to parse: {e}\n---\n{block}---",
                        file.display()
                    )
                });
            }
            total_blocks += blocks.len();
        }

        assert!(
            total_blocks > 0,
            "no ```toml blocks found across README.md + docs/*.md"
        );
    }

    fn extract_toml_blocks(body: &str) -> Vec<String> {
        let mut blocks = Vec::new();
        let mut in_toml = false;
        let mut current = String::new();
        for line in body.lines() {
            if in_toml {
                if line.trim_start().starts_with("```") {
                    blocks.push(std::mem::take(&mut current));
                    in_toml = false;
                } else {
                    current.push_str(line);
                    current.push('\n');
                }
            } else if line.trim_start().starts_with("```toml") {
                in_toml = true;
            }
        }
        blocks
    }
}

/// Fedora-host simulation tests. Construct a synthetic Fedora 42 `Os` and
/// load the bundled default pkgs through the same deep-merge path
/// `Config::load` uses on a real machine, then assert the install
/// commands a Fedora user would see. Locked-in so the dnf5 research
/// behind each default doesn't silently drift.
#[cfg(test)]
mod fedora_defaults_tests {
    use super::*;
    use crate::os::{Distro, Fedora, FedoraVersion, Linux, Os};
    use crate::pkg_manager::{self, DetectedPackageManager};

    fn fedora42() -> Os {
        Os::Linux(Linux {
            distro: Distro::Fedora(Fedora {
                version: FedoraVersion::F42,
            }),
        })
    }

    /// Replicates the deep-merge in `Config::load` for just the builtin
    /// conditions + DEFAULT_PKGS — no on-disk config.toml, no IO.
    fn load_defaults() -> StoredConfig {
        let mut merged = toml::Value::Table(Default::default());
        let builtin_conditions: toml::Value = toml::from_str(BUILTIN_CONDITIONS).unwrap();
        deep_merge(&mut merged, builtin_conditions);
        for (_name, src) in DEFAULT_PKGS {
            let v: toml::Value = toml::from_str(src).unwrap();
            deep_merge(&mut merged, v);
        }
        merged.try_into().unwrap()
    }

    fn install_cmd_for(pkg: &PkgConfig, mgr: DetectedPackageManager) -> Option<String> {
        let pkgs = mgr.packages_for(&pkg.install_hint);
        (!pkgs.is_empty()).then(|| mgr.install_command(pkgs))
    }

    #[test]
    fn detect_native_on_fedora_42_is_dnf5() {
        assert_eq!(
            pkg_manager::detect_native(&fedora42()),
            Some(DetectedPackageManager::Dnf5),
        );
    }

    #[test]
    fn starship_dnf_install_command_matches_research() {
        let cfg = load_defaults();
        let pkg = cfg.pkg.get("starship").expect("starship default present");
        assert_eq!(
            install_cmd_for(pkg, DetectedPackageManager::Dnf5).as_deref(),
            Some("sudo dnf install starship"),
        );
    }

    #[test]
    fn starship_cargo_install_command_present() {
        let cfg = load_defaults();
        let pkg = cfg.pkg.get("starship").expect("starship default present");
        assert_eq!(
            install_cmd_for(pkg, DetectedPackageManager::Cargo).as_deref(),
            Some("cargo install starship"),
        );
    }

    #[test]
    fn sk_only_installable_via_cargo() {
        // skim isn't in mainline Fedora; only cargo install path applies.
        let cfg = load_defaults();
        let pkg = cfg.pkg.get("sk").expect("sk default present");
        assert!(install_cmd_for(pkg, DetectedPackageManager::Dnf5).is_none());
        assert_eq!(
            install_cmd_for(pkg, DetectedPackageManager::Cargo).as_deref(),
            Some("cargo install skim"),
        );
    }

    #[test]
    fn cargo_dnf_install_command_uses_rustup() {
        let cfg = load_defaults();
        let pkg = cfg.pkg.get("cargo").expect("cargo default present");
        assert_eq!(
            install_cmd_for(pkg, DetectedPackageManager::Dnf5).as_deref(),
            Some("sudo dnf install rustup"),
        );
    }

    #[test]
    fn cargo_pkg_has_no_cargo_install_path() {
        // Can't bootstrap cargo via cargo itself.
        let cfg = load_defaults();
        let pkg = cfg.pkg.get("cargo").expect("cargo default present");
        assert!(install_cmd_for(pkg, DetectedPackageManager::Cargo).is_none());
    }

    #[test]
    fn brew_bound_pkgs_have_no_dnf5_install_path() {
        // Pkgs whose shell init references ${brew_prefix}/... can't be
        // satisfied by dnf5, so their dnf5 hint must be empty by design.
        let cfg = load_defaults();
        for key in [
            "brew-macos",
            "brew-linux",
            "brew-python",
            "bash-completion",
            "llvm",
            "sk",
        ] {
            let pkg = cfg.pkg.get(key).unwrap_or_else(|| panic!("{key} missing"));
            assert!(
                pkg.install_hint.dnf5.packages.is_empty(),
                "{key}: expected empty dnf5 packages, got {:?}",
                pkg.install_hint.dnf5.packages,
            );
        }
    }

    fn ubuntu_2404() -> Os {
        Os::Linux(crate::os::Linux {
            distro: crate::os::Distro::Ubuntu(crate::os::Ubuntu {
                version: crate::os::UbuntuVersion::U2404,
            }),
        })
    }

    fn arch_host() -> Os {
        Os::Linux(crate::os::Linux {
            distro: crate::os::Distro::Arch,
        })
    }

    #[test]
    fn detect_native_on_arch_is_pacman() {
        assert_eq!(
            pkg_manager::detect_native(&arch_host()),
            Some(DetectedPackageManager::Pacman),
        );
    }

    #[test]
    fn starship_pacman_install_command_matches_research() {
        let cfg = load_defaults();
        let pkg = cfg.pkg.get("starship").expect("starship default present");
        assert_eq!(
            install_cmd_for(pkg, DetectedPackageManager::Pacman).as_deref(),
            Some("sudo pacman -S starship"),
        );
    }

    #[test]
    fn cargo_pacman_install_command_uses_rustup() {
        let cfg = load_defaults();
        let pkg = cfg.pkg.get("cargo").expect("cargo default present");
        assert_eq!(
            install_cmd_for(pkg, DetectedPackageManager::Pacman).as_deref(),
            Some("sudo pacman -S rustup"),
        );
    }

    #[test]
    fn sk_pacman_install_command_uses_skim() {
        // Arch ships `skim` in extra; native install path unlike on
        // Fedora and Ubuntu where the cargo fallback is the only option.
        let cfg = load_defaults();
        let pkg = cfg.pkg.get("sk").expect("sk default present");
        assert_eq!(
            install_cmd_for(pkg, DetectedPackageManager::Pacman).as_deref(),
            Some("sudo pacman -S skim"),
        );
    }

    #[test]
    fn detect_native_on_ubuntu_2404_is_apt() {
        assert_eq!(
            pkg_manager::detect_native(&ubuntu_2404()),
            Some(DetectedPackageManager::Apt),
        );
    }

    #[test]
    fn starship_apt_install_command_matches_research() {
        let cfg = load_defaults();
        let pkg = cfg.pkg.get("starship").expect("starship default present");
        assert_eq!(
            install_cmd_for(pkg, DetectedPackageManager::Apt).as_deref(),
            Some("sudo apt install starship"),
        );
    }

    #[test]
    fn cargo_apt_install_command_uses_rustup() {
        let cfg = load_defaults();
        let pkg = cfg.pkg.get("cargo").expect("cargo default present");
        assert_eq!(
            install_cmd_for(pkg, DetectedPackageManager::Apt).as_deref(),
            Some("sudo apt install rustup"),
        );
    }

    #[test]
    fn sk_only_installable_via_cargo_on_ubuntu_too() {
        // skim is missing from mainline Ubuntu repos as well as Fedora;
        // cargo install remains the recommended path on both.
        let cfg = load_defaults();
        let pkg = cfg.pkg.get("sk").expect("sk default present");
        assert!(install_cmd_for(pkg, DetectedPackageManager::Apt).is_none());
        assert_eq!(
            install_cmd_for(pkg, DetectedPackageManager::Cargo).as_deref(),
            Some("cargo install skim"),
        );
    }

    #[test]
    fn brew_macos_when_gate_excludes_fedora_host() {
        // The `when = "macos"` gate uses the new OsPattern matcher; on a
        // Fedora host it must evaluate false so the pkg stays inactive.
        let cfg = load_defaults();
        let conds = Conditions::compile(cfg.conditions.clone()).unwrap();
        let pkg = cfg
            .pkg
            .get("brew-macos")
            .expect("brew-macos default present");
        let tmp = tempfile::tempdir().unwrap();
        let sys = IndexMap::new();
        let ctx = HostContext {
            os: fedora42(),
            shell: None,
            hostname: "fedora42-test",
            home: tmp.path(),
            system_inputs: &sys,
        };
        assert!(
            !pkg.evaluate_when(&conds, &ctx).unwrap(),
            "brew-macos should not match a Fedora host"
        );
    }

    #[test]
    fn brew_linux_when_gate_matches_fedora_host() {
        // `when = "linux"` should still match Fedora — `OsPattern::Linux`
        // is the any-distro pattern, so Fedora is a valid Linux.
        let cfg = load_defaults();
        let conds = Conditions::compile(cfg.conditions.clone()).unwrap();
        let pkg = cfg
            .pkg
            .get("brew-linux")
            .expect("brew-linux default present");
        let tmp = tempfile::tempdir().unwrap();
        let sys = IndexMap::new();
        let ctx = HostContext {
            os: fedora42(),
            shell: None,
            hostname: "fedora42-test",
            home: tmp.path(),
            system_inputs: &sys,
        };
        assert!(
            pkg.evaluate_when(&conds, &ctx).unwrap(),
            "brew-linux should match a Fedora host (linux is any-distro)"
        );
    }
}