Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

3.8 KB
Raw Blame
use assert_cmd::Command;
use predicates::str::contains;
use serde_json::Value;
use tempfile::tempdir;

fn cli() -> Command {
    Command::new(assert_cmd::cargo::cargo_bin!("boxlite"))
}

fn project_link_error(dir: &std::path::Path) -> Value {
    let result = cli()
        .env_clear()
        .env("HOME", dir)
        .current_dir(dir)
        .args(["app", "status", "--json"])
        .assert()
        .code(1)
        .stdout("");
    serde_json::from_slice::<Value>(&result.get_output().stderr).unwrap()["error"].clone()
}

#[test]
fn app_link_error_identifies_an_unselected_app() {
    let dir = tempdir().unwrap();
    let error = project_link_error(dir.path());
    assert_eq!(error["code"], "not_linked");
    assert_eq!(
        error["next"],
        "pass --app <name> or link the project with `boxlite app init --app <name>`"
    );
}

#[test]
fn app_link_error_identifies_a_damaged_project_link() {
    let dir = tempdir().unwrap();
    let config = dir.path().join(".boxlite");
    std::fs::create_dir(&config).unwrap();
    for content in ["{", r#"{"app":"shop"}"#] {
        std::fs::write(config.join("link.json"), content).unwrap();
        let error = project_link_error(dir.path());
        assert_eq!(error["code"], "invalid_link");
        assert_eq!(
            error["next"],
            "delete .boxlite/link.json and run `boxlite app init --app <name>`"
        );
    }
}

#[test]
fn cloud_app_reports_the_installed_boxlite_version() {
    let version = env!("CARGO_PKG_VERSION");
    cli()
        .arg("--version")
        .assert()
        .success()
        .stdout(format!("boxlite {version}\n"));
    cli()
        .args(["app", "--version"])
        .assert()
        .success()
        .stdout(format!("boxlite app {version}\n"));
}

#[test]
fn cloud_app_help_and_reference_work_without_a_context() {
    let dir = tempdir().unwrap();
    for (args, expected) in [
        (vec!["--help"], "Usage: boxlite app [OPTIONS]"),
        (vec!["psql", "--help"], "--proxy"),
        (vec!["deploy", "--help"], "boxlite app deploy"),
    ] {
        cli()
            .current_dir(dir.path())
            .env("BOXLITE_CONTEXT", "absent")
            .arg("--home")
            .arg(dir.path())
            .arg("app")
            .args(args)
            .assert()
            .success()
            .stdout(contains(expected));
    }
    cli()
        .args(["help", "app", "vars", "set"])
        .assert()
        .success()
        .stdout(contains("Usage: boxlite app vars set"));
}

#[test]
fn cloud_app_preserves_a_leading_option_terminator() {
    cli()
        .args(["app", "--", "--version"])
        .assert()
        .code(1)
        .stderr(contains("unrecognized subcommand '--version'"));
}

#[test]
fn app_parse_errors_use_the_json_error_contract() {
    let result = cli()
        .args(["app", "--json", "does-not-exist"])
        .assert()
        .code(1)
        .stdout("");
    let body: Value = serde_json::from_slice(&result.get_output().stderr).unwrap();
    assert_eq!(body["error"]["code"], "usage");
    assert!(
        body["error"]["message"]
            .as_str()
            .unwrap()
            .contains("does-not-exist")
    );
}

#[test]
fn cloud_app_check_preserves_json_stderr_and_exit_code() {
    let dir = tempdir().unwrap();
    std::fs::write(dir.path().join("boxlite.toml"), "[unexpected]\nkey = 2\n").unwrap();
    let result = cli()
        .current_dir(dir.path())
        .args(["app", "--json", "deploy", "--check"])
        .assert()
        .code(7)
        .stdout("");
    let err: Value = serde_json::from_slice(&result.get_output().stderr).unwrap();
    assert_eq!(err["error"]["code"], "invalid_manifest");
    assert!(
        err["error"]["next"]
            .as_str()
            .unwrap()
            .contains("boxlite app deploy --check")
    );
}