//! 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 out = sys() .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}" ); } }