surjection 1.1.0

Surjection CLI: manage MCP servers, skills and secret references for AI coding clients
//! `surject migrate loadout` from the command line: `--dry-run` writes
//! nothing (not even the file renames every start makes), and
//! `secrets service-account clear` also clears the token Loadout 1.0.x
//! kept under its keyring service.

use std::path::PathBuf;
use std::process::{Output, Stdio};

use serde_json::Value;
use surjection_testkit::TempHome;

const BIN: &str = env!("CARGO_BIN_EXE_surject");

struct Cli {
    th: TempHome,
    home: PathBuf,
}

impl Cli {
    fn new() -> Self {
        let th = TempHome::new();
        let home = th.root().join("lh");
        Self { th, home }
    }

    fn run(&self, args: &[&str]) -> Output {
        self.th
            .command(BIN)
            .arg("--home")
            .arg(&self.home)
            .args(args)
            .stdin(Stdio::null())
            .output()
            .expect("run surject")
    }

    fn json(&self, args: &[&str]) -> Value {
        let output = self.run(&[&["--json"], args].concat());
        assert!(
            output.status.success(),
            "{args:?}: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        serde_json::from_slice(&output.stdout).unwrap()
    }
}

#[test]
fn a_dry_run_renames_nothing() {
    let cli = Cli::new();
    let logs = cli.home.join("logs");
    std::fs::create_dir_all(&logs).unwrap();
    std::fs::write(logs.join("loadout.log"), "old line\n").unwrap();

    let doc = cli.json(&["migrate", "loadout", "--dry-run"]);
    let steps = doc["migration"]["steps"].as_array().unwrap();
    let names = steps
        .iter()
        .find(|step| step["step"] == "file_names")
        .unwrap();
    assert_eq!(names["status"], "planned", "{doc:#}");
    assert_eq!(
        std::fs::read_to_string(logs.join("loadout.log")).unwrap(),
        "old line\n"
    );

    let doc = cli.json(&["migrate", "loadout"]);
    assert!(!logs.join("loadout.log").exists(), "{doc:#}");
    assert_eq!(
        std::fs::read_to_string(logs.join("surjection.log"))
            .unwrap()
            .lines()
            .next(),
        Some("old line")
    );
}

#[test]
fn clearing_the_service_account_token_also_clears_the_loadout_one() {
    let cli = Cli::new();
    std::fs::write(
        cli.th.keyring_file(),
        r#"{"loadout/OP_SERVICE_ACCOUNT_TOKEN": "fake-service-account-token-0001"}"#,
    )
    .unwrap();
    let cleared = cli.json(&["secrets", "service-account", "clear"]);
    assert_eq!(cleared["changed"], true, "{cleared:#}");
    let keyring = std::fs::read_to_string(cli.th.keyring_file()).unwrap();
    assert!(!keyring.contains("OP_SERVICE_ACCOUNT_TOKEN"), "{keyring}");
}