tessera-command 0.6.0

The tesserax command: opens a recipe, then serves, fetches or runs tasks
Documentation
use super::mcp::{Servers, answer};
use serde_json::{Value, json};

fn ask(servers: &mut Servers, message: Value) -> Value {
    answer(servers, &message.to_string()).unwrap_or_default()
}

/// The handshake, the tool list and refusals.
#[test]
fn answers_the_protocol() {
    let mut servers = Servers::default();
    let init = json!({"jsonrpc": "2.0", "id": 1, "method": "initialize",
        "params": {"protocolVersion": "2025-06-18"}});
    let reply = ask(&mut servers, init);
    assert_eq!(reply["result"]["protocolVersion"], "2025-06-18");
    let note = json!({"jsonrpc": "2.0", "method": "notifications/initialized"});
    assert_eq!(answer(&mut servers, &note.to_string()), None);
    let bad = answer(&mut servers, "{").unwrap_or_default();
    assert_eq!(bad["error"]["code"], -32700);

    let list = ask(
        &mut servers,
        json!({"jsonrpc": "2.0", "id": 2, "method": "tools/list"}),
    );
    let tools = list["result"]["tools"]
        .as_array()
        .cloned()
        .unwrap_or_default();
    let names: Vec<&str> = tools.iter().filter_map(|t| t["name"].as_str()).collect();
    assert_eq!(
        names,
        [
            "serve",
            "check-kernels",
            "compact-memory",
            "fetch",
            "machine",
            "models",
            "recipe",
            "size",
            "teleport",
            "tune",
            "ask",
            "embed",
            "image",
            "tag",
            "stop",
        ]
    );
    let tool = |name: &str| tools.iter().find(|t| t["name"] == name).unwrap();
    assert!(tool("serve")["inputSchema"]["properties"]["recipe"].is_object());
    assert!(tool("serve")["inputSchema"]["properties"]["profile"].is_object());
    assert_eq!(
        tool("serve")["inputSchema"]["properties"]["flags"]["type"],
        "array"
    );
    assert!(tool("machine")["inputSchema"]["properties"]["recipe"].is_null());
    assert_eq!(
        tool("tune")["inputSchema"]["properties"]["guard"]["type"],
        "array"
    );
    assert_eq!(
        tool("tune")["inputSchema"]["properties"]["flags"]["type"],
        "array"
    );

    // Every CLI command except `mcp` and `task` is exposed.
    let commands = crate::surface::json()["commands"]
        .as_object()
        .cloned()
        .unwrap_or_default();
    for name in commands.keys() {
        if matches!(name.as_str(), "mcp" | "task") {
            continue;
        }
        assert!(
            names.contains(&name.as_str()),
            "CLI command `{name}` is missing from MCP tools"
        );
    }

    let models = ask(
        &mut servers,
        json!({"jsonrpc": "2.0", "id": 4, "method": "tools/call",
            "params": {"name": "models", "arguments": {}}}),
    );
    assert_ne!(models["result"]["isError"], true);
    assert!(
        models["result"]["structuredContent"]["text"]
            .as_str()
            .unwrap_or_default()
            .contains("| Architecture |")
    );

    let bad_tune = ask(
        &mut servers,
        json!({"jsonrpc": "2.0", "id": 5, "method": "tools/call",
            "params": {"name": "tune", "arguments": {"target": "bogus"}}}),
    );
    assert_eq!(bad_tune["result"]["isError"], true);

    let call = |name: &str| {
        json!({"jsonrpc": "2.0", "id": 3, "method": "tools/call",
        "params": {"name": name, "arguments": {"handle": "serve-1"}}})
    };
    assert_eq!(ask(&mut servers, call("stop"))["result"]["isError"], true);
    assert_eq!(ask(&mut servers, call("chat"))["error"]["code"], -32602);
}