surjection 1.1.0

Surjection CLI: manage MCP servers, skills and secret references for AI coding clients
//! The `loadout` legacy shim: arguments, stdin, stdout and the exit code
//! pass through to `surject` (the one next to it first), with one notice
//! line on stderr, except for `exec` and `secret get`, which stay silent.
//! The shim is the testkit's `loadout-shim` (same source).

use std::ffi::OsStr;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::{Command, Output, Stdio};

use surjection_testkit::TempHome;
use surjection_testkit::op_stub::built_binary;

const NOTICE: &str = "loadout is now surject";

fn exe(dir: &Path, stem: &str) -> PathBuf {
    dir.join(format!("{stem}{}", std::env::consts::EXE_SUFFIX))
}

/// A folder with the shim as `loadout` and `target` as `surject` next to it.
fn install(th: &TempHome, target: &Path) -> PathBuf {
    let dir = th.root().join("programs");
    std::fs::create_dir_all(&dir).unwrap();
    let shim = exe(&dir, "loadout");
    std::fs::copy(built_binary("loadout-shim"), &shim).unwrap();
    std::fs::copy(target, exe(&dir, "surject")).unwrap();
    shim
}

fn run(th: &TempHome, shim: &Path, args: &[&OsStr], stdin: &[u8]) -> Output {
    let mut child = th
        .command(shim)
        .args(args)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn the shim");
    let mut input = child.stdin.take().unwrap();
    input.write_all(stdin).unwrap();
    drop(input);
    child.wait_with_output().unwrap()
}

fn os<'a>(words: &[&'a str]) -> Vec<&'a OsStr> {
    words.iter().map(|word| OsStr::new(*word)).collect()
}

fn stderr(output: &Output) -> String {
    String::from_utf8_lossy(&output.stderr).into_owned()
}

#[test]
fn arguments_stdio_and_the_exit_code_pass_through() {
    let th = TempHome::new();
    // The fake server as `surject`: `echo` copies stdin to stdout, `exit N`
    // exits with N.
    let shim = install(&th, &built_binary("fake-mcp-server"));
    let bytes = b"{\"jsonrpc\":\"2.0\"}\r\n\x00binary\xff\n";
    let output = run(&th, &shim, &os(&["echo"]), bytes);
    assert_eq!(output.status.code(), Some(0), "{}", stderr(&output));
    assert_eq!(output.stdout, bytes);
    assert_eq!(stderr(&output).trim_end(), NOTICE);

    let output = run(&th, &shim, &os(&["exit", "7"]), b"");
    assert_eq!(output.status.code(), Some(7));
    assert!(output.stdout.is_empty());
}

#[test]
fn exec_and_secret_get_stay_silent() {
    let th = TempHome::new();
    let shim = install(&th, Path::new(env!("CARGO_BIN_EXE_surject")));
    let home = th.root().join("lh");
    let server = built_binary("fake-mcp-server");

    // `exec` serves an MCP transport on stdout: nothing else may appear.
    let bytes = b"{\"jsonrpc\":\"2.0\",\"id\":1}\n";
    let mut args = os(&["exec", "--"]);
    args.push(server.as_os_str());
    args.push(OsStr::new("echo"));
    let output = run(&th, &shim, &args, bytes);
    assert_eq!(output.status.code(), Some(0), "{}", stderr(&output));
    assert_eq!(output.stdout, bytes);
    assert!(!stderr(&output).contains(NOTICE), "{}", stderr(&output));

    // `secret get` (after a global flag with a value): no notice either.
    let mut args = vec![OsStr::new("--home"), home.as_os_str()];
    args.extend(os(&["secret", "get", "--ref", "env:SURJECTION_SHIM_UNSET"]));
    let output = run(&th, &shim, &args, b"");
    assert!(!stderr(&output).contains(NOTICE), "{}", stderr(&output));

    // Anything else says it once, and `surject` answers.
    let output = run(&th, &shim, &os(&["--version"]), b"");
    assert_eq!(output.status.code(), Some(0));
    assert!(
        String::from_utf8_lossy(&output.stdout).contains(env!("CARGO_PKG_VERSION")),
        "{output:?}"
    );
    assert_eq!(stderr(&output).matches(NOTICE).count(), 1);
}

#[test]
fn without_a_sibling_the_shim_finds_surject_on_path() {
    let th = TempHome::new();
    let dir = th.root().join("shim-only");
    std::fs::create_dir_all(&dir).unwrap();
    let shim = exe(&dir, "loadout");
    std::fs::copy(built_binary("loadout-shim"), &shim).unwrap();
    let path_dir = th.root().join("on-path");
    std::fs::create_dir_all(&path_dir).unwrap();
    std::fs::copy(built_binary("fake-mcp-server"), exe(&path_dir, "surject")).unwrap();
    let output = Command::new(&shim)
        .args(["exit", "5"])
        .env("PATH", &path_dir)
        .stdin(Stdio::null())
        .output()
        .unwrap();
    assert_eq!(output.status.code(), Some(5), "{output:?}");
}