phoebus_os/installer-rs/crates/phoebus-installer/tests/sys_cli.rs
Phoebus 7ebb616c99
Some checks are pending
ci / test (push) Waiting to run
feat(ai,desktop): unify administration layer, provision QML, and add conversation continuity
Unifies AI-first administration across the CLI (sys) and desktop GUI
(phoebus-assistant) through a shared service layer, fixes installed
desktop QML asset provisioning, adds Polkit elevation, and introduces
contextual conversation continuity.

- Extract phoebus_installer::service shared by CLI and desktop assistant bridge:
  - Reviewable plan staging (PreparedPlan) with SHA-256 cryptographic digests,
    15-minute TTL expiration, and replay prevention (PlanStore).
  - Pre-mutation system state revalidation binding execution strictly to the reviewed plan.
  - Honest recommendations displaying genuine provenance and unmeasured compile times.
  - Two-phase transaction logging (begin before mutation, finish with verification).
- Provision installed desktop assets and Polkit policy in phoebus-executor::desktop:
  - Added gui-apps/quickshell to canonical Mango desktop packages.
  - Embed 13 QML and policy files into target root under /usr/share/phoebus/assistant/.
  - Installed Polkit policy (club.phoebus.sys.policy) allowing unprivileged users to
    authenticate mutating plans via pkexec without blanket passwordless sudo.
- Eliminate startup freeze and polling in phoebus-assistant:
  - Multi-source update queries moved to on-demand tab switching.
  - Handshake timer reduced to one-shot 100ms with fast deterministic check.
- Add phoebus_installer::conversation:
  - Bounded 10-turn history with credential redaction.
  - Contextual follow-up resolution for comparisons, backend switching, and confirmation.
- Implement unified cross-source removal (plan_removal) preserving user data by default.
- Verified: all 486 workspace tests pass, release and crt-static builds succeed,
  and 23/23 VM acceptance cases pass (PASS=23 FAIL=0).
2026-09-24 17:21:16 -07:00

470 lines
14 KiB
Rust

//! CLI-level regression tests for the `sys` entry paths and contracts.
//!
//! Natural language is the default interaction (calling the AI assistant).
//! Deterministic operation is explicitly selected with `--legacy`.
//! JSON action entry points require `--legacy` or `--broker`.
use std::process::Command;
fn sys() -> Command {
Command::new(env!("CARGO_BIN_EXE_sys"))
}
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 natural_option_like_operand_is_rejected_in_legacy_mode() {
let out = sys()
.args(["--legacy", "--", "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.contains("legacy"),
"stderr: {stderr}"
);
}
#[test]
fn json_without_legacy_or_broker_is_rejected() {
let out = sys()
.args(["--action", r#"{"action":"history"}"#])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("require --legacy") || stderr.contains("--broker"),
"stderr: {stderr}"
);
}
#[test]
fn json_option_like_operand_is_rejected_in_legacy_mode() {
let out = sys()
.args([
"--legacy",
"--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 broker_allows_action_execution() {
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.arg("--root")
.arg(root.path())
.args(["--broker", "--action", r#"{"action":"history"}"#])
.output()
.expect("run sys");
assert!(
out.status.success(),
"stderr: {}",
String::from_utf8_lossy(&out.stderr)
);
}
#[test]
fn read_only_history_succeeds_deterministically_with_legacy() {
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.arg("--root")
.arg(root.path())
.args(["history", "--legacy"])
.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 history_without_legacy_and_no_model_gives_actionable_legacy_guidance() {
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.env("PHOEBUS_AI_PROVIDER", "none")
.arg("--root")
.arg(root.path())
.arg("history")
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("sys history --legacy"),
"expected legacy suggestion in stderr: {stderr}"
);
}
#[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())
.arg("--legacy")
.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([
"--legacy",
"--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([
"--legacy",
"--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() {
if effective_uid_is_root() {
return;
}
let root = tempfile::tempdir().expect("temp root");
let mutating = sys()
.arg("--root")
.arg(root.path())
.args([
"--legacy",
"--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"));
}
#[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", "--legacy"])
.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 legacy_install_without_source_flag_is_refused_with_choices() {
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.arg("--root")
.arg(root.path())
.args(["install", "firefox", "--legacy"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("requires an explicit package source flag"),
"stderr: {stderr}"
);
assert!(
stderr.contains("--flatpak") && stderr.contains("--portage"),
"stderr must show explicit options: {stderr}"
);
}
#[test]
fn source_flag_without_legacy_enters_ai_workflow_and_suggests_legacy() {
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.env("PHOEBUS_AI_PROVIDER", "none")
.arg("--root")
.arg(root.path())
.args(["install", "firefox", "--flatpak"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("sys install firefox --legacy --flatpak"),
"stderr must give legacy alternative: {stderr}"
);
}
#[test]
fn an_explicit_backend_never_falls_back_to_another_lane() {
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", "--legacy", "--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 please_install_enters_same_ai_workflow() {
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(["please", "install", "firefox"])
.output()
.expect("run sys");
assert!(!out.status.success());
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}"
);
}
#[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",
"--legacy",
"--broker",
] {
assert!(stdout.contains(flag), "help is missing {flag}");
}
}
#[test]
fn choose_never_invents_a_candidate() {
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(["choose", "some-package", "--legacy"])
.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}"
);
}
}
#[test]
fn remove_without_installed_copy_fails_closed() {
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.arg("--root")
.arg(root.path())
.args(["remove", "nonexistent-app", "--legacy"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("is not installed") || stderr.contains("found"),
"stderr: {stderr}"
);
}
#[test]
fn remove_ambiguous_copies_requires_explicit_lane() {
let root = tempfile::tempdir().expect("temp root");
// Create an AppImage record
let appimage_dir = root.path().join("var/lib/phoebus/appimages");
std::fs::create_dir_all(&appimage_dir).unwrap();
std::fs::write(
appimage_dir.join("shared-app.toml"),
"name = \"shared-app\"\nversion = \"1.0\"\nsha256 = \"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\"\nprovenance = \"https://example.com/app\"\narch = \"x86_64\"\nsize_bytes = 100\ninstalled_unix = 0\n",
).unwrap();
// Create a Portage VDB entry
let portage_pkg_dir = root.path().join("var/db/pkg/app-misc/shared-app-1.0");
std::fs::create_dir_all(&portage_pkg_dir).unwrap();
std::fs::write(portage_pkg_dir.join("CONTENTS"), "").unwrap();
// Removal without source flag must fail indicating ambiguity
let out = sys()
.arg("--root")
.arg(root.path())
.args(["remove", "shared-app", "--legacy"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("multiple lanes") && stderr.contains("explicitly"),
"stderr must report ambiguity: {stderr}"
);
// Disambiguating with --appimage allows targeting specifically that copy
let disambiguated = sys()
.arg("--root")
.arg(root.path())
.args(["remove", "shared-app", "--legacy", "--appimage"])
.output()
.expect("run sys");
let out_str = String::from_utf8_lossy(&disambiguated.stdout);
let err_str = String::from_utf8_lossy(&disambiguated.stderr);
assert!(
out_str.contains("Removal plan:") || err_str.contains("root"),
"stdout: {out_str}, stderr: {err_str}"
);
}
#[test]
fn broker_execute_plan_rejects_missing_file() {
let out = sys()
.args([
"--broker",
"--execute-plan",
"/tmp/nonexistent-plan-id.json",
])
.output()
.expect("run sys");
assert!(!out.status.success());
}