Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

4.9 KB
Raw Blame
//! "Run Claude inside the VM": the switch and chips in `GET/PUT /api/settings/computer`
//! (design/ui.md's contract, Ada's D1).
mod common;

use axum::http::StatusCode;
use kwnt::sandbox::agent_vm::{broker::BrokerConfig, AgentVm, AgentsInVmConfig};
use serde_json::json;

#[tokio::test]
async fn the_switch_saves_and_the_chips_say_where_each_engine_runs() {
    let data = tempfile::tempdir().unwrap();
    let env = common::setup_with(|state| {
        // An image name that's never built: turning the switch on can't start a real download
        state.agent_vm = AgentVm::with_parts(
            data.path(),
            BrokerConfig::new(data.path().join("broker.log")),
            Some("kwnt-agent-settings-test:never-built".into()),
            AgentsInVmConfig::default(),
            None,
        );
    })
    .await;
    let (s, v) = common::call(&env.app, "GET", "/api/settings/computer", None).await;
    assert_eq!(s, StatusCode::OK);
    assert_eq!(v["isolate_claude"], false, "off by default: {v}");
    assert_eq!(v["agents"]["claude"], json!({"state": "off", "reason": null, "detail": null, "progress": null}));
    for other in ["codex", "grok"] {
        assert_eq!(v["agents"][other]["state"], "not_isolated");
        assert_eq!(v["agents"][other]["reason"], "not_supported_yet");
    }
    assert!(v["isolate_claude_toggle"]["available"].is_boolean(), "{v}");
    if cfg!(target_os = "macos") {
        assert_eq!(v["isolate_claude_toggle"]["reason"], "mac_later");
    }
    // Isolation is about Claude alone: `isolates` never names the CLI agents
    assert!(!v["isolates"].as_array().unwrap().iter().any(|i| i == "cli_agents"), "{v}");

    // Nothing to change, and a wrong type, are refused
    assert_eq!(common::call(&env.app, "PUT", "/api/settings/computer", Some(json!({}))).await.0, StatusCode::BAD_REQUEST);
    let (s, _) = common::call(&env.app, "PUT", "/api/settings/computer", Some(json!({"isolate_claude": "yes"}))).await;
    assert_eq!(s, StatusCode::BAD_REQUEST);

    // Only the person can turn it on
    assert_eq!(common::call_remote(&env.app, "PUT", "/api/settings/computer").await, StatusCode::UNAUTHORIZED);

    let (s, v) = common::call(&env.app, "PUT", "/api/settings/computer", Some(json!({"isolate_claude": true}))).await;
    // Branch on the PUT's own answer, not the GET's `available` above: each asks Docker again
    // (`docker version`, 3 s timeout), so on a busy runner the two can disagree
    if s == StatusCode::OK {
        assert_eq!(v["isolate_claude"], true);
        let claude = &v["agents"]["claude"];
        if v["mode"] == "local" {
            // D1: in local mode the switch is kept but does nothing, and the chip says so
            assert_eq!((claude["state"].as_str(), claude["reason"].as_str()), (Some("not_isolated"), Some("local_mode")), "{v}");
        } else {
            // The test image is never built: it's getting ready or it failed, never "isolated"
            assert!(["preparing", "unavailable"].contains(&claude["state"].as_str().unwrap()), "{v}");
        }
    } else {
        assert_eq!(s, StatusCode::BAD_REQUEST, "{v}");
        // The code for the client, and words for the person (ui-check-446 fix 5)
        let code = v["code"].as_str().unwrap_or_default();
        assert!(["mac_later", "docker_unavailable", "not_supported_yet"].contains(&code), "{v}");
        assert_eq!(v["error"].as_str(), Some(kwnt::sandbox::agent_vm::toggle_message(code)), "{v}");
        assert!(!v["error"].as_str().unwrap().contains('_'), "{v}");
        let (_, v) = common::call(&env.app, "GET", "/api/settings/computer", None).await;
        assert_eq!(v["isolate_claude"], false);
    }

    // Turning it off always works
    let (s, v) = common::call(&env.app, "PUT", "/api/settings/computer", Some(json!({"isolate_claude": false}))).await;
    assert_eq!(s, StatusCode::OK);
    assert_eq!(v["isolate_claude"], false);
    assert_eq!(v["agents"]["claude"]["state"], "off");
}

/// A save asks Docker once, so its reply agrees with the check it just passed: a switch that
/// turned on is never shown as unavailable in the same answer (computer-save-one-docker-check).
#[tokio::test]
async fn a_save_that_turns_the_switch_on_says_it_is_available() {
    let data = tempfile::tempdir().unwrap();
    let env = common::setup_with(|state| {
        state.agent_vm = AgentVm::with_parts(
            data.path(),
            BrokerConfig::new(data.path().join("broker.log")),
            Some("kwnt-agent-settings-test:never-built".into()),
            AgentsInVmConfig::default(),
            None,
        );
    })
    .await;
    let (s, v) = common::call(&env.app, "PUT", "/api/settings/computer", Some(json!({"isolate_claude": true}))).await;
    if s == StatusCode::OK {
        assert_eq!(v["isolate_claude_toggle"]["available"], true, "{v}");
    } else {
        assert_eq!(s, StatusCode::BAD_REQUEST, "{v}");
        assert_eq!(v["isolate_claude"], json!(null), "a refusal is an error, not the view: {v}");
    }
}