phoebus_os/installer-rs/crates/phoebus-installer/tests/ai_guidance.rs
Phoebus 0d65d56eda feat(installer): wire OpenCode into the GUI, CLI and sys ask
Select the OpenCode adapter from canonical config in ai_check/ai_test;
add --ai-backend/--opencode-* flags to the installer and sys. Require a
validated interaction (not a provider name) for the mandatory-AI gate in
both the bridge and the CLI, invalidated by config, model or credential
changes. Tests cover the gate, invalidation and the OpenCode backend.
2026-09-22 02:36:29 -07:00

176 lines
5.8 KiB
Rust

//! End-to-end tests for the AI-assisted path.
//!
//! These run the real binary with `--ai-proposal-file`, which sends a proposal
//! through exactly the same parse -> review (evidence + conflicts) -> apply ->
//! plan path the live provider path uses. Provider fixtures are labelled as
//! such; no test here claims a live provider call.
use std::process::Command;
fn bin() -> &'static str {
env!("CARGO_BIN_EXE_phoebus-installer")
}
fn run_with_proposal(body: &str, extra: &[&str]) -> std::process::Output {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("proposal.json");
std::fs::write(&path, body).unwrap();
let mut cmd = Command::new(bin());
cmd.arg("--ai-proposal-file")
.arg(&path)
.arg("--plan")
.args(extra);
cmd.output().expect("run phoebus-installer")
}
const VALID: &str = r#"{
"version": 1,
"changes": [
{"action":"set_kernel_profile","value":"safe"},
{"action":"set_build_jobs","value":4}
],
"rationale": [
{"action":"set_kernel_profile","reason":"stability over novelty","evidence":["the proven boot uses gentoo-kernel-bin"]},
{"action":"set_build_jobs","reason":"16 GiB RAM","evidence":["memory_mib=16384"]}
]
}"#;
#[test]
fn valid_proposal_is_accepted_and_reaches_the_plan() {
let out = run_with_proposal(VALID, &[]);
assert!(out.status.success(), "{:?}", out);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(stdout.contains("Accepted AI changes"), "{stdout}");
assert!(stdout.contains("kernel profile = safe"), "{stdout}");
assert!(
stdout.contains("reason: stability over novelty"),
"{stdout}"
);
// The canonical plan reflects the change.
assert!(stdout.contains("gentoo-kernel-bin"), "{stdout}");
}
#[test]
fn missing_evidence_is_rejected() {
let body = r#"{
"version": 1,
"changes": [{"action":"set_hostname","value":"box"}],
"rationale": []
}"#;
let out = run_with_proposal(body, &[]);
assert!(!out.status.success());
assert!(String::from_utf8_lossy(&out.stderr).contains("reason"));
}
#[test]
fn conflicting_changes_are_rejected() {
let body = r#"{
"version": 1,
"changes": [
{"action":"set_hostname","value":"one"},
{"action":"set_hostname","value":"two"}
],
"rationale": [
{"action":"set_hostname","reason":"a"},
{"action":"set_hostname","reason":"b"}
]
}"#;
let out = run_with_proposal(body, &[]);
assert!(!out.status.success());
assert!(String::from_utf8_lossy(&out.stderr).contains("conflicting"));
}
#[test]
fn malicious_proposal_is_refused() {
let body = r#"{"version":1,"changes":[{"action":"run_shell","command":"rm -rf /"}]}"#;
let out = run_with_proposal(body, &[]);
assert!(!out.status.success());
let err = String::from_utf8_lossy(&out.stderr);
assert!(err.contains("invalid") || err.contains("unknown"), "{err}");
}
#[test]
fn invalid_value_is_refused() {
let body = r#"{
"version": 1,
"changes": [{"action":"set_hostname","value":"bad host"}],
"rationale": [{"action":"set_hostname","reason":"x"}]
}"#;
let out = run_with_proposal(body, &[]);
assert!(!out.status.success());
}
#[test]
fn install_without_ai_is_refused() {
// Installing Phoebus requires a connected model.
let out = Command::new(bin())
.args(["--install", "--yes"])
.output()
.expect("run phoebus-installer");
assert!(!out.status.success());
let err = String::from_utf8_lossy(&out.stderr);
assert!(err.contains("validated AI interaction"), "{err}");
}
#[test]
fn no_ai_is_the_explicit_break_glass() {
// `--no-ai` passes the gate; the (incomplete) default config then stops
// before any disk is touched, with no AI requirement.
for flag in ["--no-ai", "--legacy"] {
let out = Command::new(bin())
.args(["--install", "--yes", flag])
.output()
.expect("run phoebus-installer");
let err = String::from_utf8_lossy(&out.stderr);
assert!(!err.contains("validated AI interaction"), "{flag}: {err}");
assert!(err.contains("configuration is incomplete"), "{flag}: {err}");
}
}
#[test]
fn an_unreachable_provider_does_not_satisfy_the_ai_gate() {
// A provider *name* is not enough: without a real, validated model
// interaction the install is refused (a deliberately closed port makes the
// result deterministic and offline).
let out = Command::new(bin())
.args([
"--install",
"--yes",
"--ai",
"--ai-provider",
"local",
"--ai-endpoint",
"http://127.0.0.1:1/v1/chat/completions",
])
.output()
.expect("run phoebus-installer");
assert!(!out.status.success());
let err = String::from_utf8_lossy(&out.stderr);
assert!(err.contains("validated AI interaction"), "{err}");
}
#[test]
fn no_ai_fallback_still_prints_a_plan() {
let out = Command::new(bin())
.arg("--plan")
.output()
.expect("run phoebus-installer --plan");
assert!(out.status.success());
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(stdout.contains("Installation plan"));
assert!(!stdout.contains("Accepted AI changes"));
}
#[test]
fn ai_facts_are_printed_without_a_provider() {
let out = Command::new(bin())
.arg("--ai-facts")
.output()
.expect("run phoebus-installer --ai-facts");
assert!(out.status.success(), "{:?}", out);
let stdout = String::from_utf8_lossy(&out.stdout);
// The approved fact set is JSON and contains no personal path.
assert!(stdout.trim_start().starts_with('{'), "{stdout}");
assert!(stdout.contains("gentoo_profile"));
assert!(!stdout.contains("/home/"), "{stdout}");
}