//! The owner and agents from the command line (spec §8.0, §8.1, §8.3), as //! the built binary in a hermetic environment: `identity create` makes the //! owner, `init` proves it, `agent add` delegates only when the owner //! confirms, ownership is shown, and the ledger verifies throughout. use spectra_core::keys::KeyPair; use std::path::Path; use std::process::{Command, Output}; fn hermetic(dir: &Path) -> Command { let mut c = Command::new(env!("CARGO_BIN_EXE_spectra")); c.env("SPECTRA_DATA", dir.join("data")) .env("SPECTRA_HOME", dir.join("owner")) .env("HOME", dir) .env("XDG_DATA_HOME", dir.join("xdg")) .env("NO_COLOR", "1"); c } fn run(dir: &Path, args: &[&str]) -> Output { hermetic(dir).args(args).output().unwrap() } fn text(o: &Output) -> String { format!("{}{}", String::from_utf8_lossy(&o.stdout), String::from_utf8_lossy(&o.stderr)) } fn verify_clean(dir: &Path) { let o = run(dir, &["admin", "verify", "--json"]); let v: serde_json::Value = serde_json::from_slice(&o.stdout).unwrap_or_else(|_| panic!("{}", text(&o))); assert!(o.status.success() && v["findings"].as_array().unwrap().is_empty(), "{v}"); } #[test] fn init_then_an_owned_agent_end_to_end() { let dir = tempfile::tempdir().unwrap(); let d = dir.path(); let o = run(d, &["identity", "create", "--name", "Andy McCall", "--contact", "andy@example.org"]); assert!(o.status.success(), "{}", text(&o)); assert!(!run(d, &["identity", "create", "--name", "again"]).status.success(), "one identity per home"); let o = run(d, &["init", "--name", "laptop"]); assert!(o.status.success(), "{}", text(&o)); assert!(!run(d, &["init"]).status.success(), "a second init is refused"); let owner = KeyPair::load_home(&d.join("owner")).unwrap(); let instance = KeyPair::instance(&d.join("data"), "laptop").unwrap(); assert!(owner.identity.delegates_to(&instance.key, 1), "init delegates to its own instance key"); verify_clean(d); // A project is any work; with no folder at all, too. let o = run(d, &["project", "add", "oxygen", "-d", "the version control system"]); assert!(o.status.success(), "{}", text(&o)); let home = d.join("agents/claude-oxygen"); let home_s = home.to_str().unwrap(); let args = ["agent", "add", "claude-oxygen", "--kind", "claude-code", "--project", "oxygen", "--home", home_s, "--expires", "8h"]; let o = run(d, &args); assert!(!o.status.success(), "not a terminal and no --yes: nothing delegated"); assert!(text(&o).contains("--yes"), "{}", text(&o)); let agent = KeyPair::load_home(&home).unwrap(); assert!(!KeyPair::load_home(&d.join("owner")).unwrap().identity.delegates_to(&agent.key, 1)); let mut yes = args.to_vec(); yes.push("--yes"); let o = run(d, &yes); assert!(o.status.success(), "{}", text(&o)); assert!(text(&o).contains("Added claude-oxygen") && text(&o).contains("Andy McCall's agent"), "{}", text(&o)); assert_eq!(KeyPair::load_home(&home).unwrap().key, agent.key, "the key made first is the key recorded"); let d8 = KeyPair::load_home(&d.join("owner")).unwrap().identity.delegate(&agent.key).cloned().unwrap(); assert!(d8.expires.is_some(), "the expiry was delegated"); let list: serde_json::Value = serde_json::from_slice(&run(d, &["agent", "list", "--json"]).stdout).unwrap(); assert_eq!(list[0]["display"], "claude-oxygen · Andy McCall's agent"); assert_eq!(list[0]["state"], "ok"); verify_clean(d); let o = run(d, &["agent", "renew", "claude-oxygen", "--expires", "never"]); assert!(o.status.success(), "{}", text(&o)); assert_eq!(KeyPair::load_home(&d.join("owner")).unwrap().identity.delegate(&agent.key).unwrap().expires, None); verify_clean(d); assert!(run(d, &["agent", "disable", "claude-oxygen"]).status.success()); assert!(KeyPair::load_home(&d.join("owner")).unwrap().identity.delegate(&agent.key).is_none(), "the delegation is removed"); let list: serde_json::Value = serde_json::from_slice(&run(d, &["agent", "list", "--json"]).stdout).unwrap(); assert!(list[0]["state"].as_str().unwrap().contains("disabled"), "{list}"); verify_clean(d); assert!(run(d, &["admin", "check"]).status.success()); // Piped, as here, output is plain: no colour codes, no glyphs. let o = run(d, &["agent", "list"]); let t = text(&o); assert!(!t.contains('\x1b') && !t.contains('✔'), "{t}"); } #[test] fn an_owner_without_a_secret_key_cannot_init() { let dir = tempfile::tempdir().unwrap(); let o = run(dir.path(), &["init"]); assert!(!o.status.success()); assert!(text(&o).contains("the owner signs with their own identity"), "{}", text(&o)); } #[tokio::test] async fn a_second_writer_is_refused() { let dir = tempfile::tempdir().unwrap(); let d = dir.path(); KeyPair::create_home(&d.join("owner"), "Andy McCall", "").unwrap(); assert!(run(d, &["init"]).status.success()); let instance = KeyPair::instance(&d.join("data"), "x").unwrap(); let held = spectra_core::ledger::Ledger::open(&d.join("data/spectra.db"), Some(instance)).await.unwrap(); let o = run(d, &["agent", "disable", "nobody"]); assert!(text(&o).contains("another process is writing"), "{}", text(&o)); assert!(run(d, &["admin", "verify"]).status.success(), "reading needs no lock"); drop(held); }