phoebus_os/installer-rs/crates/phoebus-installer/tests/sys_cli.rs
Phoebus f9e39925b4 feat(install): AI-first sys install workflow
Implement the shared installation workflow (ADR 0013). sys install, sys ask
install and the explicit backend flags all use one path: identify -> evidence
-> AI investigation -> deterministic validation/effects -> review -> approval
-> execute -> verify -> history.

- phoebus-ai::install: bounded, multi-turn investigation. The model may request
  typed read-only evidence (discover_candidates, inspect_candidate,
  check_backend, inspect_installation), ask one bounded question about intent,
  and must return a structured InstallProposal. Turn/request/size limits and
  repeated-request detection bound it; a rejected proposal is revised.
- phoebus-core: add the four read-only investigation actions and strict
  validators; add a transaction id; backend/kind wire names now match as_str.
- phoebus-executor::install: resolve_explicit (no model, no fallback),
  resolve_deterministic and resolve_with_ai; validate_proposal re-resolves the
  candidate against real metadata, rejects fabrication/ambiguity/staleness/
  duplicates, maps it to the existing broker action, and binds approval to a
  digest; revalidate + verify_installed. chooser gains per-lane friendly-name
  resolution and structured evidence.
- transaction::begin/finish record an in-progress transaction before mutation.
- sys: --portage/--flatpak/--nix/--snap/--appimage/--ebuild (mutually
  exclusive) bypass the model; an unqualified install consults the model and,
  with none, prints the deterministic selection and installs nothing.
- The installer persists /etc/phoebus/install.toml; sys loads it and adds free
  space, Wayland, usable lanes, saved preferences and recovery capability to
  the bounded facts.
- Docs: ADR 0013, docs/SYS.md install sections and cases 23-31, CHANGELOG,
  STATUS, HANDOFF, ROADMAP. New live-VM workflow check script.

Host gates pass: fmt, clippy (no new warnings), workspace tests, make test.
VM: verified the workflow CLI and read-only investigation on the reference
LiveGUI ISO; a full target install journey is running (see HANDOFF).
2026-09-23 20:30:13 -07:00

281 lines
8.6 KiB
Rust

//! CLI-level regression tests for the `sys` entry paths.
//!
//! These exercise the real binary so the deterministic (manual) and JSON paths
//! are proven to fail closed on invalid input and to exit nonzero on failure.
//! They never mutate the system: the invalid cases are refused before any
//! command is built, and the read-only case targets a temporary root.
use std::process::Command;
fn sys() -> Command {
Command::new(env!("CARGO_BIN_EXE_sys"))
}
#[test]
fn natural_option_like_operand_is_rejected() {
// `--` stops clap option parsing, so the operand reaches the parser intact.
let out = sys()
.args(["--", "install", "--color=n"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("could not parse") || stderr.contains("invalid"),
"stderr: {stderr}"
);
}
#[test]
fn json_option_like_operand_is_rejected() {
let out = sys()
.args(["--action", r#"{"action":"install_package","atom":"-C"}"#])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(stderr.contains("invalid"), "stderr: {stderr}");
}
#[test]
fn read_only_history_succeeds_against_a_temporary_root() {
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.arg("--root")
.arg(root.path())
.arg("history")
.output()
.expect("run sys");
assert!(
out.status.success(),
"stderr: {}",
String::from_utf8_lossy(&out.stderr)
);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
stdout.contains("no snapshot history") || stdout.contains("snapshots:"),
"stdout: {stdout}"
);
}
#[test]
fn version_is_reported() {
let out = sys().arg("--version").output().expect("run sys");
assert!(out.status.success());
assert!(String::from_utf8_lossy(&out.stdout).contains("sys"));
}
#[test]
fn appimage_and_custom_ebuild_inventories_are_safe_and_empty_on_a_fresh_root() {
let root = tempfile::tempdir().expect("temp root");
for args in [vec!["appimage", "list"], vec!["ebuilds"]] {
let out = sys()
.arg("--root")
.arg(root.path())
.args(&args)
.output()
.expect("run sys");
assert!(
out.status.success(),
"{args:?} stderr: {}",
String::from_utf8_lossy(&out.stderr)
);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
stdout.contains("no managed AppImages")
|| stdout.contains("no Phoebus-managed custom ebuilds"),
"{args:?} stdout: {stdout}"
);
}
}
#[test]
fn invalid_snap_name_via_json_fails_closed() {
let out = sys()
.args(["--action", r#"{"action":"snap_install","name":"Bad Name"}"#])
.output()
.expect("run sys");
assert!(!out.status.success());
assert!(String::from_utf8_lossy(&out.stderr).contains("invalid"));
}
#[test]
fn invalid_appimage_hash_via_json_fails_closed() {
let out = sys()
.args([
"--action",
r#"{"action":"appimage_acquire","name":"x","url":"https://e/x","sha256":"00"}"#,
])
.output()
.expect("run sys");
assert!(!out.status.success());
assert!(String::from_utf8_lossy(&out.stderr).contains("SHA-256"));
}
#[test]
fn a_mutating_action_without_root_is_refused_before_any_command() {
// This runs as the test user (not root); the mutation must be refused.
if effective_uid_is_root() {
return;
}
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.arg("--root")
.arg(root.path())
.args(["--action", r#"{"action":"snap_list"}"#])
.output()
.expect("run sys");
// snap_list is read-only, so it will instead fail because snapd is absent;
// the point is only that it never mutates. A mutating action is checked
// separately below.
let _ = out;
let mutating = sys()
.arg("--root")
.arg(root.path())
.args([
"--action",
r#"{"action":"set_package_preference","name":"firefox","backend":"flatpak"}"#,
])
.output()
.expect("run sys");
assert!(!mutating.status.success());
assert!(String::from_utf8_lossy(&mutating.stderr).contains("root"));
}
fn effective_uid_is_root() -> bool {
std::process::Command::new("id")
.arg("-u")
.output()
.ok()
.map(|o| String::from_utf8_lossy(&o.stdout).trim() == "0")
.unwrap_or(false)
}
#[test]
fn conflicting_backend_flags_are_rejected_by_the_cli() {
let out = sys()
.args(["--flatpak", "--nix", "install", "some-app"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("cannot be used with")
|| stderr.contains("conflict")
|| stderr.contains("not allowed"),
"stderr: {stderr}"
);
}
#[test]
fn a_backend_flag_without_an_install_is_refused() {
let out = sys()
.args(["--flatpak", "history"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(stderr.contains("only apply to"), "stderr: {stderr}");
}
#[test]
fn an_explicit_backend_never_falls_back_to_another_lane() {
// No Portage on this host, so an explicit `--portage` install must fail
// with an explanation and must not try Flatpak/snap/anything else.
if effective_uid_is_root() {
return;
}
let root = tempfile::tempdir().expect("temp root");
let empty = tempfile::tempdir().expect("empty PATH");
let out = sys()
.env("PATH", empty.path())
.arg("--root")
.arg(root.path())
.args(["install", "some-app", "--portage"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("cannot satisfy") || stderr.contains("no real candidate"),
"stderr: {stderr}"
);
}
#[test]
fn an_unqualified_install_without_a_model_offers_options_and_installs_nothing() {
if effective_uid_is_root() {
return;
}
let root = tempfile::tempdir().expect("temp root");
let empty = tempfile::tempdir().expect("empty PATH");
let out = sys()
.env("PATH", empty.path())
.env("PHOEBUS_AI_PROVIDER", "none")
.env("PHOEBUS_AI_BACKEND", "direct")
.arg("--root")
.arg(root.path())
.args(["install", "firefox"])
.output()
.expect("run sys");
assert!(
!out.status.success(),
"an unqualified install must not proceed without a model"
);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("No connected model") || stderr.contains("no AI provider"),
"stderr: {stderr}"
);
assert!(
stderr.contains("--flatpak") && stderr.contains("--portage"),
"the offer must name the explicit backends: {stderr}"
);
// Nothing may have been installed or executed.
assert!(!root
.path()
.join("var/lib/phoebus/transactions.toml")
.exists());
}
#[test]
fn the_help_lists_every_backend_flag() {
let out = sys().arg("--help").output().expect("run sys");
assert!(out.status.success());
let stdout = String::from_utf8_lossy(&out.stdout);
for flag in [
"--portage",
"--flatpak",
"--nix",
"--snap",
"--appimage",
"--ebuild",
] {
assert!(stdout.contains(flag), "help is missing {flag}");
}
}
#[test]
fn choose_never_invents_a_candidate() {
// Depending on the host, a lane may or may not be resolvable. Either way the
// output must be a real selection with rejected alternatives, or a clear
// fail-closed error — never a fabricated package.
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.arg("--root")
.arg(root.path())
.args(["choose", "some-package"])
.output()
.expect("run sys");
let stdout = String::from_utf8_lossy(&out.stdout);
let stderr = String::from_utf8_lossy(&out.stderr);
if out.status.success() {
assert!(stdout.contains("Selected package:"), "stdout: {stdout}");
assert!(stdout.contains("source:"), "stdout: {stdout}");
} else {
assert!(
stderr.contains("no compatible") || stderr.contains("sys:"),
"stderr: {stderr}"
);
}
}