Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

14.6 KB
Raw Blame
use super::*;
use crate::config::schema::{CapabilityProviderConfig, CapabilityProviderTrustState, Config};
use crate::config::test_env::EnvVarGuard;
use crate::core::runtime::DispatchCtx;
use crate::core::{FieldSchema, TypeSchema};

#[test]
fn registry_entries_include_mcp_and_controller_tools() {
    let entries = registry_entries(&DispatchCtx::host_default());

    // The MCP-transport half of the inventory is sourced from
    // `mcp::server::tool_specs()`, which the `mcp` feature compiles out (the
    // stub returns an empty catalog). Only this half is gated — the
    // controller half below must keep its coverage in BOTH builds.
    #[cfg(feature = "mcp")]
    {
        let memory_recall = entries
            .iter()
            .find(|entry| entry.tool_id == "memory.recall")
            .expect("memory.recall mcp tool");
        assert_eq!(memory_recall.transport, ToolRegistryTransport::McpStdio);
        assert_eq!(memory_recall.route["method"], json!("tools/call"));
        assert_eq!(memory_recall.health, ToolRegistryHealth::Available);
    }

    // With `mcp` compiled out the registry must contain NO MCP-transport
    // entries at all — the inventory degrades to controller tools only.
    #[cfg(not(feature = "mcp"))]
    assert!(
        !entries
            .iter()
            .any(|entry| entry.transport == ToolRegistryTransport::McpStdio),
        "no MCP-transport tools may be registered when the `mcp` feature is off"
    );

    let web_search = entries
        .iter()
        .find(|entry| entry.tool_id == "tools.web_search")
        .expect("tools.web_search controller tool");
    assert_eq!(web_search.transport, ToolRegistryTransport::JsonRpc);
    assert_eq!(
        web_search.route["method"],
        json!("openhuman.tools_web_search")
    );
    assert_eq!(web_search.input_schema["type"], json!("object"));
}

#[test]
fn registry_entries_are_unique_and_sorted_by_tool_id() {
    let entries = registry_entries(&DispatchCtx::host_default());
    let ids = entries
        .iter()
        .map(|entry| entry.tool_id.as_str())
        .collect::<Vec<_>>();
    let mut sorted = ids.clone();
    sorted.sort_unstable();
    sorted.dedup();

    assert_eq!(ids, sorted);
}

#[tokio::test]
async fn diagnostics_reports_inventory_and_policy_surfaces() {
    let outcome = diagnostics_for_config(&Config::default()).await;

    assert!(outcome.value.total_tools > 0);
    assert_eq!(outcome.value.total_tools, outcome.value.enabled_tools);
    // MCP-transport tools only exist when the `mcp` feature is compiled in;
    // with it off the count is legitimately zero (see `mcp::server::stub`).
    #[cfg(feature = "mcp")]
    assert!(outcome.value.mcp_stdio_tools > 0);
    #[cfg(not(feature = "mcp"))]
    assert_eq!(outcome.value.mcp_stdio_tools, 0);
    assert!(outcome.value.json_rpc_tools > 0);
    assert!(outcome
        .value
        .policy_surfaces
        .iter()
        .any(|tool_id| tool_id == "security.policy_info"));
    assert!(outcome
        .value
        .possible_write_surfaces
        .iter()
        .any(|tool_id| tool_id == "tools.composio_execute"));

    assert!(!outcome.value.posture.autonomy_level.is_empty());
    assert!(outcome.value.mcp_write_audit.enabled);
}

#[tokio::test]
async fn diagnostics_loads_active_capability_provider_config() {
    let _lock = crate::config::TEST_ENV_LOCK.lock().await;
    let tmp = tempfile::tempdir().expect("tempdir");
    let _env = EnvVarGuard::set("OPENHUMAN_WORKSPACE", tmp.path());
    std::fs::write(
        tmp.path().join("config.toml"),
        r#"
[[capability_providers]]
id = "Runtime Provider"
display_name = "Runtime Provider"
trust_state = "trusted"
enabled = true
"#,
    )
    .expect("write config");

    let outcome = diagnostics().await.expect("diagnostics");

    assert_eq!(outcome.value.capability_providers.total_providers, 1);
    assert_eq!(outcome.value.capability_providers.enabled_providers, 1);
    assert_eq!(outcome.value.capability_providers.trusted_providers, 1);
    assert_eq!(
        outcome.value.capability_providers.trusted_enabled_providers,
        1
    );
    assert!(outcome
        .value
        .capability_providers
        .registry_errors
        .is_empty());
}

#[tokio::test]
async fn diagnostics_for_config_reports_capability_provider_summary() {
    let config = Config {
        capability_providers: vec![
            capability_provider(
                "trusted-enabled",
                CapabilityProviderTrustState::Trusted,
                true,
            ),
            capability_provider(
                "trusted-disabled",
                CapabilityProviderTrustState::Trusted,
                false,
            ),
            capability_provider(
                "untrusted-enabled",
                CapabilityProviderTrustState::Untrusted,
                true,
            ),
        ],
        ..Config::default()
    };

    let outcome = diagnostics_for_config(&config).await;

    assert_eq!(outcome.value.capability_providers.total_providers, 3);
    assert_eq!(outcome.value.capability_providers.enabled_providers, 2);
    assert_eq!(outcome.value.capability_providers.trusted_providers, 2);
    assert_eq!(
        outcome.value.capability_providers.trusted_enabled_providers,
        1
    );
    assert!(outcome
        .value
        .capability_providers
        .registry_errors
        .is_empty());
}

#[tokio::test]
async fn diagnostics_for_config_reports_capability_provider_errors() {
    let config = Config {
        capability_providers: vec![
            capability_provider("Acme Tools", CapabilityProviderTrustState::Trusted, true),
            capability_provider("acme-tools", CapabilityProviderTrustState::Trusted, true),
        ],
        ..Config::default()
    };

    let outcome = diagnostics_for_config(&config).await;

    assert_eq!(outcome.value.capability_providers.total_providers, 2);
    assert_eq!(outcome.value.capability_providers.enabled_providers, 0);
    assert!(outcome.value.capability_providers.registry_errors[0].contains("duplicate"));
    assert!(outcome.value.capability_providers.registry_errors[0].contains("acme-tools"));
}

#[test]
fn looks_write_capable_detects_action_prefixes_and_suffixes() {
    assert!(looks_write_capable("user.create"));
    assert!(looks_write_capable("create.user"));
    assert!(looks_write_capable("tools.composio_execute"));
    assert!(!looks_write_capable("tools.search"));
}

#[test]
fn is_policy_surface_includes_policy_namespaces() {
    assert!(is_policy_surface("security.audit_status"));
    assert!(is_policy_surface("approval.request"));
    assert!(is_policy_surface("tool_registry.diagnostics"));
    assert!(!is_policy_surface("tools.web_search"));
}

#[test]
fn insert_registry_entry_skips_duplicate_tool_id() {
    let mut entries = BTreeMap::new();
    let first_entry = ToolRegistryEntry {
        tool_id: "duplicate.tool".to_string(),
        name: "duplicate.tool".to_string(),
        title: "First Entry".to_string(),
        description: "First description.".to_string(),
        version: REGISTRY_ENTRY_VERSION.to_string(),
        transport: ToolRegistryTransport::JsonRpc,
        route: json!({}),
        input_schema: json!({}),
        output_schema: json!({}),
        allowed_agents: vec!["*".to_string()],
        tags: vec!["test".to_string()],
        enabled: true,
        health: ToolRegistryHealth::Available,
    };
    let second_entry = ToolRegistryEntry {
        title: "Second Entry".to_string(),
        description: "Second description.".to_string(),
        ..first_entry.clone()
    };

    insert_registry_entry(&mut entries, first_entry, "first");
    // Should not panic; first entry is kept, second is silently dropped.
    insert_registry_entry(&mut entries, second_entry, "second");

    assert_eq!(entries.len(), 1);
    assert_eq!(entries["duplicate.tool"].title, "First Entry");
}

#[test]
fn get_tool_trims_and_returns_exact_entry() {
    // `memory.recall` is an MCP-transport entry, so it is absent when the `mcp`
    // feature is compiled out. The behaviour under test here is id *trimming*,
    // not MCP — so fall back to a controller-transport entry rather than gating
    // the whole test away and losing that coverage in slim builds.
    #[cfg(feature = "mcp")]
    let tool_id = "memory.recall";
    #[cfg(not(feature = "mcp"))]
    let tool_id = "tools.web_search";

    let outcome =
        get_tool(&DispatchCtx::host_default(), &format!("  {tool_id}  ")).expect("registry lookup");
    assert_eq!(outcome.value.tool_id, tool_id);
}

#[test]
fn get_tool_rejects_blank_id() {
    let err = get_tool(&DispatchCtx::host_default(), "  ").expect_err("blank id should fail");
    assert!(err.contains("non-empty"));
}

#[test]
fn get_tool_reports_unknown_id() {
    let err =
        get_tool(&DispatchCtx::host_default(), "missing.tool").expect_err("unknown id should fail");
    assert!(err.contains("missing.tool"));
}

#[test]
fn all_registry_entries_have_non_empty_name_and_description() {
    let entries = registry_entries(&DispatchCtx::host_default());
    assert!(
        !entries.is_empty(),
        "registry must contain at least one tool"
    );
    let mut violations: Vec<String> = Vec::new();
    for entry in &entries {
        if entry.name.trim().is_empty() {
            violations.push(format!("tool_id='{}' has empty name", entry.tool_id));
        }
        if entry.description.trim().is_empty() {
            violations.push(format!("tool_id='{}' has empty description", entry.tool_id));
        }
    }
    assert!(
        violations.is_empty(),
        "registry integrity violations:\n{}",
        violations.join("\n")
    );
}

#[test]
fn controller_json_schema_marks_required_and_optional_fields() {
    let schema = schema_fields_to_json_schema(&[
        FieldSchema {
            name: "query",
            ty: TypeSchema::String,
            comment: "Query text.",
            required: true,
        },
        FieldSchema {
            name: "max_results",
            ty: TypeSchema::Option(Box::new(TypeSchema::U64)),
            comment: "Optional cap.",
            required: false,
        },
    ]);

    assert_eq!(schema["required"], json!(["query"]));
    assert_eq!(schema["properties"]["query"]["type"], json!("string"));
    assert_eq!(
        schema["properties"]["max_results"]["anyOf"][0]["type"],
        json!("integer")
    );
    assert_eq!(
        schema["properties"]["max_results"]["description"],
        json!("Optional cap.")
    );
}

#[test]
fn controller_json_schema_carries_bounded_integer_range() {
    // A model reads the declared range as a promise, so it must see the bound
    // the dispatch gate enforces (#6137).
    let schema = schema_fields_to_json_schema(&[FieldSchema {
        name: "order",
        ty: TypeSchema::BoundedU64 {
            min: 1,
            max: u32::MAX as u64,
        },
        comment: "Sort position.",
        required: true,
    }]);

    assert_eq!(
        schema["properties"]["order"],
        json!({
            "type": "integer",
            "minimum": 1,
            "maximum": 4_294_967_295u64,
            "description": "Sort position.",
        })
    );
}

fn capability_provider(
    id: &str,
    trust_state: CapabilityProviderTrustState,
    enabled: bool,
) -> CapabilityProviderConfig {
    CapabilityProviderConfig {
        id: id.to_string(),
        display_name: id.to_string(),
        source_uri: None,
        source_digest: None,
        trust_state,
        enabled,
    }
}

/// The health probe must read the store the writer writes to.
///
/// This is the invariant that broke: the audit log moved to its own database
/// and the probe kept querying the old table inside the memory engine's chunk
/// store, which nothing writes any more. Nothing failed — it reported zero
/// writes forever while the log underneath was healthy. Asserting a count
/// alone would not have caught that (zero is what a quiet install reports
/// too), so this writes a row first and requires the probe to see it.
#[cfg(feature = "mcp")]
#[tokio::test]
async fn the_write_audit_probe_reads_the_store_the_writer_wrote_to() {
    let workspace = tempfile::tempdir().expect("tempdir");
    let config = Config {
        workspace_dir: workspace.path().to_path_buf(),
        ..Config::default()
    };

    crate::mcp::install_test_module_port(&config);
    let before = diagnostics_for_config(&config)
        .await
        .value
        .mcp_write_audit
        .recent_rows;
    assert_eq!(before, Some(0), "a fresh workspace has recorded nothing");

    crate::mcp::audit::record_write(
        &config,
        crate::mcp::audit::NewMcpWriteRecord {
            timestamp_ms: chrono::Utc::now().timestamp_millis(),
            client_info: "probe-test".to_string(),
            tool_name: "memory_write".to_string(),
            args_summary: serde_json::json!({}),
            resulting_chunk_id: None,
            success: true,
            error_message: None,
        },
    )
    .await
    .expect("record a write");

    let after = diagnostics_for_config(&config).await.value.mcp_write_audit;
    assert_eq!(
        after.recent_rows,
        Some(1),
        "the probe must see a row the writer just recorded; last_error={:?}",
        after.last_error
    );
    assert!(after.enabled, "the log is enabled when it is readable");
}

#[tokio::test]
async fn diagnostics_report_denials_recorded_through_tinytools() {
    tinytools::registry::denials::record(
        "tools.registry_snapshot_probe",
        "policy",
        "blocked",
        "probe denial reason",
    );
    let workspace = tempfile::tempdir().expect("tempdir");
    let config = Config {
        workspace_dir: workspace.path().to_path_buf(),
        ..Config::default()
    };
    let diagnostics = diagnostics_for_config(&config).await;
    let denial = diagnostics
        .value
        .recent_denials
        .iter()
        .find(|d| d.tool_name == "tools.registry_snapshot_probe")
        .expect("recorded denial surfaces in diagnostics");
    assert_eq!(denial.reason, "probe denial reason");
}

#[test]
fn registry_entries_read_the_handed_context_and_agree_with_the_config_form() {
    // The context form and the config form differ only in where connected MCP
    // client servers are read from; with none connected the snapshots match, and
    // neither consults the calling task's ambient scope.
    let from_ctx = registry_entries(&DispatchCtx::unbound());
    let from_host = registry_entries(&DispatchCtx::host_default());
    let ids = |entries: &[ToolRegistryEntry]| {
        entries
            .iter()
            .map(|entry| entry.tool_id.clone())
            .collect::<Vec<_>>()
    };
    assert!(!from_ctx.is_empty());
    assert_eq!(ids(&from_ctx), ids(&from_host));
    let listed = list_tools(&DispatchCtx::host_default());
    assert_eq!(ids(&listed.value.tools), ids(&from_host));
}