shine-cli 1.8.0

Give personal automation a reviewable lifecycle
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
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
use anyhow::{Context, Result, bail};
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::time::Duration;

use crate::{colors, config::Config};

use super::{
    LoadedSysPreset, SysDetection, SysDetectionProbe, SysInstall, SysItem, SysItemOutcome,
    SysItemStatus, SysPackageProvider,
};

const BOOTSTRAP_TIMEOUT: Duration = Duration::from_secs(30 * 60);
const MAX_LOG_BYTES: usize = 64 * 1024;

#[derive(Debug)]
enum DetectionResult {
    Present(String),
    Missing,
}

pub(super) async fn item_is_present(config: &Config, item: &SysItem) -> Result<bool> {
    let detect = item
        .detect
        .as_ref()
        .with_context(|| format!("sys item `{}` has no standard detection", item.id))?;
    Ok(matches!(
        detect_item(config, detect).await?,
        DetectionResult::Present(_)
    ))
}

pub(super) async fn run_standard_bootstrap_item(
    config: &Config,
    os_id: &str,
    loaded: &LoadedSysPreset,
    item: &SysItem,
    sys_shell: &str,
    proxy_env: &[(&'static str, String)],
) -> Result<SysItemOutcome> {
    let Some(detect) = item.detect.as_ref() else {
        bail!("sys item `{}` has no standard detection", item.id);
    };
    let Some(install) = item.install.as_ref() else {
        bail!("sys item `{}` has no standard installer", item.id);
    };

    if let DetectionResult::Present(detail) = detect_item(config, detect).await? {
        return Ok(outcome(
            item,
            SysItemStatus::AlreadyInstalled,
            detail,
            Vec::new(),
        ));
    }

    if let SysInstall::Script { path, .. } = install {
        let script_path = resolve_preset_file(loaded, path)?;
        require_external_code_permission(config, &script_path, "install script")?;
    }
    super::execution::print_item_install_start(item, install_requires_admin(os_id, install, item)?);

    let execution = match install {
        SysInstall::Package {
            provider, package, ..
        } => run_package_provider(os_id, *provider, package, proxy_env).await?,
        SysInstall::Script { path, .. } => {
            let script_path = resolve_preset_file(loaded, path)?;
            run_item_script(
                config,
                os_id,
                &script_path,
                &loaded.root,
                sys_shell,
                item,
                proxy_env,
            )
            .await?
        }
    };

    if !execution.success {
        return Ok(outcome(
            item,
            SysItemStatus::Failed,
            execution.failure_detail,
            execution.logs,
        ));
    }

    let detected = detect_item(config, detect).await?;
    if matches!(detected, DetectionResult::Missing) {
        return Ok(outcome(
            item,
            SysItemStatus::Failed,
            "installer succeeded but the declared detection is still missing".to_string(),
            execution.logs,
        ));
    }

    let status = install.success_status();
    let mut detail = install.success_hint().to_string();
    if detail.is_empty()
        && let DetectionResult::Present(detected_detail) = detected
    {
        detail = detected_detail;
    }

    Ok(outcome(item, status, detail, execution.logs))
}

fn install_requires_admin(os_id: &str, install: &SysInstall, item: &SysItem) -> Result<bool> {
    match install {
        SysInstall::Package {
            provider, package, ..
        } => {
            let (_, _, requires_admin) = package_command(os_id, *provider, package, &[])?;
            Ok(requires_admin)
        }
        SysInstall::Script { .. } => Ok(item.requires_admin && os_id != "windows"),
    }
}

pub(super) fn standard_install_preview(
    config: &Config,
    os_id: &str,
    loaded: &LoadedSysPreset,
    item: &SysItem,
) -> Result<String> {
    let Some(install) = item.install.as_ref() else {
        bail!("sys item `{}` has no standard installer", item.id);
    };
    match install {
        SysInstall::Package {
            provider, package, ..
        } => package_preview(os_id, *provider, package),
        SysInstall::Script { path, .. } => {
            let script_path = resolve_preset_file(loaded, path)?;
            let permission = if (config.is_external_presets
                || config
                    .active_presets_overlay_dir()
                    .is_some_and(|overlay| script_path.starts_with(overlay)))
                && !config.allow_sys_code
            {
                " (requires allow_sys_code = true)"
            } else {
                ""
            };
            Ok(format!("{}{}", script_path.display(), permission))
        }
    }
}

pub(super) fn preflight_standard_bootstrap_item(
    config: &Config,
    loaded: &LoadedSysPreset,
    item: &SysItem,
) -> Result<()> {
    if let Some(SysInstall::Script { path, .. }) = &item.install {
        let script_path = resolve_preset_file(loaded, path)?;
        if !script_path.is_file() {
            bail!(
                "sys item `{}` install script is missing: {}",
                item.id,
                script_path.display()
            );
        }
        require_external_code_permission(config, &script_path, "install script")?;
    }
    Ok(())
}

pub(super) fn require_external_code_permission(
    config: &Config,
    path: &Path,
    label: &str,
) -> Result<()> {
    let overlay_code = config
        .active_presets_overlay_dir()
        .is_some_and(|overlay| path.starts_with(overlay));
    if (config.is_external_presets || overlay_code) && !config.allow_sys_code {
        return Err(external_code_permission_error(
            config,
            &format!("executable sys {label}"),
            Some(path),
        ));
    }
    Ok(())
}

pub(super) fn external_code_permission_error(
    config: &Config,
    capability: &str,
    code_path: Option<&Path>,
) -> anyhow::Error {
    let overlay = config.active_presets_overlay_dir();
    let (reason, remediation) = match (config.is_external_presets, overlay.is_some()) {
        (true, true) => (
            "an external preset source and preset overlay are active",
            "Disable both the external preset source and preset overlay",
        ),
        (true, false) => (
            "an external preset source is active",
            "Disable the external preset source",
        ),
        (false, true) => ("a preset overlay is active", "Disable the preset overlay"),
        (false, false) => {
            unreachable!("external code permission error created without an external source")
        }
    };
    let mut source_details = String::new();
    if config.is_external_presets {
        source_details.push_str(&format!(
            "Preset source:  {}\n",
            config.presets_dir().display()
        ));
    }
    if let Some(path) = overlay {
        source_details.push_str(&format!("Preset overlay: {}\n", path.display()));
    }
    if let Some(path) = code_path {
        source_details.push_str(&format!("Code path:      {}\n", path.display()));
    }
    let allow_setting = colors::bold_yellow_stderr("allow_sys_code = true");
    let global_config = colors::cyan_stderr(&config.global_config_path().display().to_string());
    let keep_blocked = colors::yellow_stderr(remediation);

    anyhow::anyhow!(
        "{capability} is blocked because {reason}.\n\n\
{source_details}\
After reviewing the active preset sources, choose one:\n\n\
  Allow external sys code:\n\
    Set {allow_setting} in {global_config}\n\n\
  Keep external sys code blocked:\n\
    {keep_blocked}."
    )
}

fn resolve_preset_file(loaded: &LoadedSysPreset, relative: &str) -> Result<PathBuf> {
    let candidate = loaded.root.join(relative);
    let canonical = std::fs::canonicalize(&candidate)
        .with_context(|| format!("resolving sys preset resource {}", candidate.display()))?;
    if !canonical.starts_with(&loaded.root) {
        bail!("sys preset path escapes its root: {relative}");
    }
    Ok(canonical)
}

async fn detect_item(config: &Config, detect: &SysDetection) -> Result<DetectionResult> {
    match detect {
        SysDetection::Command {
            command,
            version_args,
        } => detect_command(config, command, version_args).await,
        SysDetection::Path { path } => {
            let expanded = crate::home::full_expand_with_home(path, &config.home_dir)
                .with_context(|| format!("expanding sys detection path `{path}`"))?;
            Ok(if Path::new(&expanded).exists() {
                DetectionResult::Present(expanded)
            } else {
                DetectionResult::Missing
            })
        }
        SysDetection::Any { probes } => {
            for probe in probes {
                let result = match probe {
                    SysDetectionProbe::Command { command } => {
                        detect_command(config, command, &[]).await?
                    }
                    SysDetectionProbe::Path { path } => {
                        let expanded = crate::home::full_expand_with_home(path, &config.home_dir)
                            .with_context(|| {
                            format!("expanding sys detection path `{path}`")
                        })?;
                        if Path::new(&expanded).exists() {
                            DetectionResult::Present(expanded)
                        } else {
                            DetectionResult::Missing
                        }
                    }
                };
                if matches!(result, DetectionResult::Present(_)) {
                    return Ok(result);
                }
            }
            Ok(DetectionResult::Missing)
        }
    }
}

async fn detect_command(
    config: &Config,
    command: &str,
    version_args: &[String],
) -> Result<DetectionResult> {
    let Some(program) = find_command(&config.home_dir, command) else {
        return Ok(DetectionResult::Missing);
    };
    if version_args.is_empty() {
        return Ok(DetectionResult::Present(program.display().to_string()));
    }
    let output = tokio::process::Command::new(&program)
        .args(version_args)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .kill_on_drop(true)
        .output()
        .await;
    let detail = output
        .ok()
        .filter(|output| output.status.success())
        .and_then(|output| {
            String::from_utf8(output.stdout)
                .ok()
                .and_then(|stdout| stdout.lines().next().map(str::to_string))
        })
        .filter(|line| !line.trim().is_empty())
        .unwrap_or_else(|| program.display().to_string());
    Ok(DetectionResult::Present(detail))
}

fn find_command(home_dir: &Path, command: &str) -> Option<PathBuf> {
    let mut directories = std::env::var_os("PATH")
        .map(|paths| std::env::split_paths(&paths).collect::<Vec<_>>())
        .unwrap_or_default();
    directories.extend([
        home_dir.join(".local/bin"),
        home_dir.join(".cargo/bin"),
        home_dir.join(".bun/bin"),
        home_dir.join(".local/share/pnpm"),
        home_dir.join("AppData/Local/Microsoft/WinGet/Links"),
    ]);
    directories.extend([
        PathBuf::from("/opt/homebrew/bin"),
        PathBuf::from("/usr/local/bin"),
        PathBuf::from("/home/linuxbrew/.linuxbrew/bin"),
    ]);
    for directory in directories {
        let candidate = directory.join(command);
        if is_executable_file(&candidate) {
            return Some(candidate);
        }
        #[cfg(windows)]
        {
            for extension in ["exe", "cmd", "bat", "ps1"] {
                let candidate = directory.join(format!("{command}.{extension}"));
                if is_executable_file(&candidate) {
                    return Some(candidate);
                }
            }
        }
    }
    None
}

fn is_executable_file(path: &Path) -> bool {
    if !path.is_file() {
        return false;
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        path.metadata()
            .is_ok_and(|metadata| metadata.permissions().mode() & 0o111 != 0)
    }
    #[cfg(not(unix))]
    true
}

struct ExecutionResult {
    success: bool,
    failure_detail: String,
    logs: Vec<String>,
}

async fn run_package_provider(
    os_id: &str,
    provider: SysPackageProvider,
    package: &str,
    proxy_env: &[(&'static str, String)],
) -> Result<ExecutionResult> {
    let (program, args, requires_admin) = package_command(os_id, provider, package, proxy_env)?;
    if requires_admin && !crate::privilege::ensure_admin(1).await? {
        return Ok(ExecutionResult {
            success: false,
            failure_detail: "administrator authorization was not granted".to_string(),
            logs: Vec::new(),
        });
    }

    let mut command = if requires_admin {
        let mut command = crate::install_core::file_ops::sudo_command();
        if !proxy_env.is_empty() {
            command.arg("env");
            for (key, value) in proxy_env {
                command.arg(format!("{key}={value}"));
            }
        }
        command.arg(&program);
        command
    } else {
        tokio::process::Command::new(&program)
    };
    command
        .args(&args)
        .stdin(Stdio::inherit())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .kill_on_drop(true);
    for (key, value) in proxy_env {
        command.env(key, value);
    }
    let mut execution = run_command(command, format!("{provider:?} install failed")).await?;
    if !execution.success && provider == SysPackageProvider::Winget {
        execution.failure_detail.push_str(
            "; retry from an elevated PowerShell if the package requires administrator access",
        );
    }
    Ok(execution)
}

fn package_preview(os_id: &str, provider: SysPackageProvider, package: &str) -> Result<String> {
    let (program, args, requires_admin) = package_command(os_id, provider, package, &[])?;
    let prefix = if requires_admin { "sudo " } else { "" };
    Ok(format!(
        "{prefix}{} {}",
        program.to_string_lossy(),
        args.join(" ")
    ))
}

fn package_command(
    os_id: &str,
    provider: SysPackageProvider,
    package: &str,
    proxy_env: &[(&'static str, String)],
) -> Result<(OsString, Vec<String>, bool)> {
    match provider {
        SysPackageProvider::Homebrew | SysPackageProvider::HomebrewCask => {
            if os_id == "windows" {
                bail!("Homebrew sys provider is unavailable on Windows");
            }
            let program = [
                PathBuf::from("/opt/homebrew/bin/brew"),
                PathBuf::from("/usr/local/bin/brew"),
                PathBuf::from("/home/linuxbrew/.linuxbrew/bin/brew"),
            ]
            .into_iter()
            .find(|path| is_executable_file(path))
            .map(PathBuf::into_os_string)
            .unwrap_or_else(|| OsString::from("brew"));
            let mut args = vec!["install".to_string()];
            if provider == SysPackageProvider::HomebrewCask {
                args.push("--cask".to_string());
            }
            args.push(package.to_string());
            Ok((program, args, false))
        }
        SysPackageProvider::Apt => {
            if os_id != "ubuntu" {
                bail!("APT sys provider is supported only on Ubuntu presets");
            }
            Ok((
                OsString::from("apt-get"),
                vec!["install".to_string(), "-y".to_string(), package.to_string()],
                true,
            ))
        }
        SysPackageProvider::Winget => {
            if os_id != "windows" {
                bail!("Winget sys provider is supported only on Windows presets");
            }
            let mut args = vec![
                "install".to_string(),
                "--exact".to_string(),
                "--id".to_string(),
                package.to_string(),
                "--accept-package-agreements".to_string(),
                "--accept-source-agreements".to_string(),
            ];
            if let Some((_, proxy)) = proxy_env.iter().find(|(key, _)| *key == "SHINE_SYS_PROXY") {
                args.extend(["--proxy".to_string(), proxy.clone()]);
            }
            Ok((OsString::from("winget"), args, false))
        }
    }
}

async fn run_item_script(
    config: &Config,
    os_id: &str,
    script_path: &Path,
    preset_root: &Path,
    sys_shell: &str,
    item: &SysItem,
    proxy_env: &[(&'static str, String)],
) -> Result<ExecutionResult> {
    if !script_path.is_file() {
        bail!(
            "sys item `{}` install script is missing: {}",
            item.id,
            script_path.display()
        );
    }
    let (program, fixed_args): (&str, &[&str]) = if os_id == "windows" {
        (
            "powershell.exe",
            &["-NoProfile", "-ExecutionPolicy", "Bypass", "-File"],
        )
    } else if os_id == "macos" {
        ("zsh", &[])
    } else {
        ("bash", &[])
    };
    let requires_unix_admin = item.requires_admin && os_id != "windows";
    if requires_unix_admin && !crate::privilege::ensure_admin(1).await? {
        return Ok(ExecutionResult {
            success: false,
            failure_detail: "administrator authorization was not granted".to_string(),
            logs: Vec::new(),
        });
    }

    let mut env_values = vec![
        (
            "SHINE_SYS_PRESET_ROOT".to_string(),
            preset_root.as_os_str().to_string_lossy().into_owned(),
        ),
        ("SHINE_SYS_SHELL".to_string(), sys_shell.to_string()),
        (
            "SHINE_TARGET_HOME".to_string(),
            config.home_dir.as_os_str().to_string_lossy().into_owned(),
        ),
    ];
    for key in &item.required_env {
        if let Some(value) = config.env.get(key) {
            env_values.push((key.clone(), value.clone()));
        }
    }
    env_values.extend(
        proxy_env
            .iter()
            .map(|(key, value)| ((*key).to_string(), value.clone())),
    );

    let mut command = if requires_unix_admin {
        let mut command = crate::install_core::file_ops::sudo_command();
        command.arg(format!(
            "--preserve-env={}",
            env_values
                .iter()
                .map(|(key, _)| key.as_str())
                .collect::<Vec<_>>()
                .join(",")
        ));
        command.arg(program);
        command
    } else {
        tokio::process::Command::new(program)
    };
    command
        .args(fixed_args)
        .arg(script_path)
        .current_dir(script_path.parent().unwrap_or_else(|| Path::new(".")))
        .stdin(Stdio::inherit())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .kill_on_drop(true);
    for (key, value) in &env_values {
        command.env(key, value);
    }
    let mut execution = run_command(
        command,
        format!("sys item `{}` install script failed", item.id),
    )
    .await?;
    if !execution.success && item.requires_admin && os_id == "windows" {
        execution
            .failure_detail
            .push_str("; retry from an elevated PowerShell");
    }
    Ok(execution)
}

async fn run_command(
    mut command: tokio::process::Command,
    failure_prefix: String,
) -> Result<ExecutionResult> {
    let output = match tokio::time::timeout(BOOTSTRAP_TIMEOUT, command.output()).await {
        Ok(output) => output.context("running sys bootstrap installer")?,
        Err(_) => {
            return Ok(ExecutionResult {
                success: false,
                failure_detail: format!("{failure_prefix}: timed out after 30 minutes"),
                logs: Vec::new(),
            });
        }
    };
    let logs = bounded_logs(&output.stdout, &output.stderr);
    let failure_detail = if output.status.success() {
        String::new()
    } else {
        format!(
            "{failure_prefix} (exit {})",
            output
                .status
                .code()
                .map(|code| code.to_string())
                .unwrap_or_else(|| "signal".to_string())
        )
    };
    Ok(ExecutionResult {
        success: output.status.success(),
        failure_detail,
        logs,
    })
}

fn bounded_logs(stdout: &[u8], stderr: &[u8]) -> Vec<String> {
    let mut combined = Vec::new();
    combined.extend_from_slice(stdout);
    if !stdout.is_empty() && !stderr.is_empty() {
        combined.push(b'\n');
    }
    combined.extend_from_slice(stderr);
    let truncated = combined.len() > MAX_LOG_BYTES;
    if truncated {
        combined = combined[combined.len() - MAX_LOG_BYTES..].to_vec();
    }
    let mut logs = String::from_utf8_lossy(&combined)
        .lines()
        .filter(|line| !line.trim().is_empty())
        .map(str::to_string)
        .collect::<Vec<_>>();
    if truncated {
        logs.insert(
            0,
            "installer output truncated at 64 KiB; showing final output".to_string(),
        );
    }
    logs
}

fn outcome(
    item: &SysItem,
    status: SysItemStatus,
    detail: String,
    logs: Vec<String>,
) -> SysItemOutcome {
    SysItemOutcome {
        item_id: item.id.clone(),
        label: format!("sys/{}", item.id),
        status,
        detail,
        logs,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sys::manifest::parse_and_validate_manifest;
    #[cfg(unix)]
    use tokio::fs;

    #[test]
    fn package_commands_are_fixed_argv() {
        let (program, args, admin) =
            package_command("ubuntu", SysPackageProvider::Apt, "fzf", &[]).unwrap();
        assert_eq!(program, OsString::from("apt-get"));
        assert_eq!(args, ["install", "-y", "fzf"]);
        assert!(admin);

        let (_, args, admin) = package_command(
            "windows",
            SysPackageProvider::Winget,
            "jdx.mise",
            &[("SHINE_SYS_PROXY", "http://127.0.0.1:7890".to_string())],
        )
        .unwrap();
        assert_eq!(
            args,
            [
                "install",
                "--exact",
                "--id",
                "jdx.mise",
                "--accept-package-agreements",
                "--accept-source-agreements",
                "--proxy",
                "http://127.0.0.1:7890",
            ]
        );
        assert!(!admin);
    }

    #[test]
    fn install_admin_requirement_includes_packages_and_script_metadata() {
        let manifest = parse_and_validate_manifest(
            r#"
version = 2

[[items]]
id = "apt-tool"
label = "APT tool"
detect = { kind = "command", command = "apt-tool" }
install = { kind = "package", provider = "apt", package = "apt-tool" }

[[items]]
id = "script-tool"
label = "Script tool"
requires_admin = true
detect = { kind = "command", command = "script-tool" }
install = { kind = "script", path = "install/script-tool.sh" }
"#,
        )
        .unwrap();

        assert!(
            install_requires_admin(
                "ubuntu",
                manifest.items[0].install.as_ref().unwrap(),
                &manifest.items[0]
            )
            .unwrap()
        );
        assert!(
            install_requires_admin(
                "ubuntu",
                manifest.items[1].install.as_ref().unwrap(),
                &manifest.items[1]
            )
            .unwrap()
        );
        assert!(
            !install_requires_admin(
                "windows",
                manifest.items[1].install.as_ref().unwrap(),
                &manifest.items[1]
            )
            .unwrap()
        );
    }

    #[test]
    fn bounded_logs_truncates_combined_output() {
        let stdout = vec![b'x'; MAX_LOG_BYTES + 1];
        let logs = bounded_logs(&stdout, b"error");
        assert_eq!(
            logs.first().unwrap(),
            "installer output truncated at 64 KiB; showing final output"
        );
    }

    #[test]
    fn permission_error_identifies_all_external_layers_and_config() {
        let dir =
            std::env::temp_dir().join(format!("shine-sys-permission-{}", uuid::Uuid::new_v4()));
        let overlay = dir.join("overlay");
        let mut config = Config::new_for_test(&dir);
        config.is_external_presets = true;
        let config = config.with_presets_overlay_dir_override(Some(overlay.clone()));
        let code_path = config.presets_dir().join("sys/ubuntu/init.sh");

        let error =
            require_external_code_permission(&config, &code_path, "legacy bootstrap script")
                .unwrap_err();
        let message = error.to_string();

        assert!(message.contains(
            "executable sys legacy bootstrap script is blocked because an external preset source and preset overlay are active"
        ));
        assert!(message.contains(&format!(
            "Preset source:  {}",
            config.presets_dir().display()
        )));
        assert!(message.contains(&format!("Preset overlay: {}", overlay.display())));
        assert!(message.contains(&format!("Code path:      {}", code_path.display())));
        assert!(message.contains("After reviewing the active preset sources, choose one:"));
        assert!(message.contains(&format!(
            "Set allow_sys_code = true in {}",
            config.global_config_path().display()
        )));
        assert!(message.contains("Keep external sys code blocked:"));
        assert!(message.contains("Disable both the external preset source and preset overlay."));
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn per_item_script_uses_exit_code_and_post_detection() {
        let dir = crate::test_support::make_temp_dir("shine-sys-standard-script").await;
        let preset_root = dir.join("presets/sys/ubuntu");
        fs::create_dir_all(preset_root.join("install"))
            .await
            .unwrap();
        fs::write(
            preset_root.join("install/tool.sh"),
            r#"#!/bin/bash
set -euo pipefail
mkdir -p "$SHINE_TARGET_HOME/.local/bin"
printf '#!/bin/sh\n' > "$SHINE_TARGET_HOME/.local/bin/test-sys-tool"
chmod +x "$SHINE_TARGET_HOME/.local/bin/test-sys-tool"
"#,
        )
        .await
        .unwrap();
        let manifest = parse_and_validate_manifest(
            r#"
version = 2

[[items]]
id = "tool"
label = "Tool"

[items.detect]
kind = "command"
command = "test-sys-tool"

[items.install]
kind = "script"
path = "install/tool.sh"
"#,
        )
        .unwrap();
        let loaded = LoadedSysPreset {
            manifest,
            root: std::fs::canonicalize(&preset_root).unwrap(),
        };
        let mut config = Config::new_for_test(&dir);
        config.is_external_presets = true;
        config.allow_sys_code = true;

        let outcome = run_standard_bootstrap_item(
            &config,
            "ubuntu",
            &loaded,
            &loaded.manifest.items[0],
            "bash",
            &[],
        )
        .await
        .unwrap();
        assert_eq!(outcome.status, SysItemStatus::Installed);
        assert!(dir.join(".local/bin/test-sys-tool").is_file());

        fs::remove_dir_all(&dir).await.unwrap();
    }
}