Luigit
repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by admin

smith-harness/src/wasm/runtime_tests.rs

Raw
//! SMH-SPEC-PLUG0001: whole-candidate reload preserves published empty state.
use super::*;

#[test]
fn failed_reload_retains_published_empty_generation() {
    let root = std::env::temp_dir().join(format!("smith-empty-reload-{}", std::process::id()));
    std::fs::create_dir_all(&root).unwrap();
    let mut dispatcher =
        runtime_tools_dispatcher(&root, AuthStore::at(root.join("auth")), false, None);
    dispatcher.packages = PackageRegistry::at(root.join("registry"));
    dispatcher.publish_candidate().unwrap();
    assert!(dispatcher.published);
    assert!(dispatcher.plugins.is_empty());
    std::fs::create_dir_all(root.join("registry")).unwrap();
    std::fs::write(root.join("registry/registry.kdl"), "broken {").unwrap();
    dispatcher.publish_candidate().unwrap();
    assert!(dispatcher.plugins.is_empty());
    std::fs::remove_dir_all(root).unwrap();
}

#[test]
fn credential_module_login_tools_are_not_provider_tools() {
    let engine = PluginEngine::new().unwrap();
    let manifest = PluginManifest::parse("id \"smith:oauth\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"oauth.wasm\"\ncapability \"secret-proxy\"").unwrap();
    let generation = PluginGeneration::load_embedded(
        &engine,
        manifest,
        include_bytes!(concat!(env!("OUT_DIR"), "/smith_plugin_oauth.wasm")),
        &[Capability::SecretProxy],
    )
    .unwrap();
    assert!(!generation.tools().is_empty());
    let mut registry = PluginRegistry::new();
    registry.publish_all(vec![generation]).unwrap();
    let snapshot = registry.snapshot().unwrap();
    assert!(!snapshot.tools.is_empty());
    assert_eq!(snapshot.policy.len(), snapshot.registered_tools.len());
    for tool in &snapshot.registered_tools {
        assert!(
            snapshot
                .policy
                .contains(&(tool.name.clone(), ToolPolicyState::Advertised))
        );
    }
    assert!(registry.snapshot_for_role(true).unwrap().tools.is_empty());
}

#[test]
fn denied_responses_replacement_retains_published_codec_and_tools() {
    let engine = PluginEngine::new().unwrap();
    let responses = PluginManifest::parse(
        r#"id "smith:protocol-openai-responses"
version "0.1.0"
world "0.1.0"
artifact "responses.wasm"
capability "net-scope"
capability "secret-proxy""#,
    )
    .unwrap();
    let oauth = PluginManifest::parse(
        r#"id "smith:oauth"
version "0.1.0"
world "0.1.0"
artifact "oauth.wasm"
capability "secret-proxy""#,
    )
    .unwrap();
    let mut registry = PluginRegistry::new();
    registry
        .publish_all(vec![
            PluginGeneration::load_embedded(
                &engine,
                responses,
                include_bytes!(concat!(
                    env!("OUT_DIR"),
                    "/smith_plugin_protocol_openai_responses.wasm"
                ))
                .as_slice(),
                &[Capability::NetScope, Capability::SecretProxy],
            )
            .unwrap(),
            PluginGeneration::load_embedded(
                &engine,
                oauth,
                include_bytes!(concat!(env!("OUT_DIR"), "/smith_plugin_oauth.wasm")).as_slice(),
                &[Capability::SecretProxy],
            )
            .unwrap(),
        ])
        .unwrap();
    let prior_tools = registry.snapshot().unwrap().tools;
    assert!(!prior_tools.is_empty());
    assert!(registry.protocol_generation(None).unwrap().is_some());

    let ungranted = PluginManifest::parse(
        r#"id "external:responses"
version "0.1.0"
world "0.1.0"
artifact "responses.wasm""#,
    )
    .unwrap();
    let candidate = PluginGeneration::load_embedded(
        &engine,
        ungranted,
        include_bytes!(concat!(
            env!("OUT_DIR"),
            "/smith_plugin_protocol_openai_responses.wasm"
        ))
        .as_slice(),
        &[],
    )
    .unwrap();
    assert!(registry.publish_all(vec![candidate]).is_err());

    assert_eq!(registry.snapshot().unwrap().tools, prior_tools);
    assert!(registry.protocol_generation(None).unwrap().is_some());
}

#[test]
fn tool_policy_requires_qualified_unique_states() {
    let policy = ToolPolicy::from_specs(&[
        "example:tools/read=hidden".to_string(),
        "example:tools/write=disabled".to_string(),
    ])
    .unwrap();
    assert_eq!(policy.state("example:tools/read"), ToolPolicyState::Hidden);
    assert_eq!(
        policy.state("example:tools/write"),
        ToolPolicyState::Disabled
    );
    assert_eq!(
        policy.state("example:tools/other"),
        ToolPolicyState::Advertised
    );
    assert!(ToolPolicy::from_specs(&["read=hidden".to_string()]).is_err());
    assert!(
        ToolPolicy::from_specs(&[
            "example:tools/read=hidden".to_string(),
            "example:tools/read=disabled".to_string(),
        ])
        .is_err()
    );
}