Luigit
repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by admin

smith-harness/tests/wasm_host.rs

Raw
//! Wasm host lifecycle: validate, instantiate, declare, invoke, fuel
//! (`SMH-SPEC-WASM0001`).

use smith_core::tools::{ToolEffectContext, ToolSession};
use smith_harness::plugins::{Capability, PluginManifest};
use smith_harness::wasm::bindings::exports::smith::plugin::provider::{
    CatalogModel, CatalogSnapshot, CatalogSource, MessageBlock, MessageRole, ModelCapabilities,
    ProviderMessage, ProviderRequest, ProviderSettings, ProviderTool, SettingsProvenance, ToolUse,
};
use smith_harness::wasm::{
    PluginEngine, PluginGeneration, PluginInstance, PluginRegistry, coding_tools_dispatcher,
};

/// Build a protocol artifact, returning its path.
fn protocol_artifact(package: &str, artifact: &str) -> std::path::PathBuf {
    let status = std::process::Command::new("cargo")
        .args([
            "build",
            "-p",
            package,
            "--target",
            "wasm32-wasip2",
            "--locked",
        ])
        .status()
        .unwrap();
    assert!(status.success(), "{package} plugin build failed");
    std::path::Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())
        .join("../target/wasm32-wasip2/debug")
        .join(artifact)
}

fn tool(tools: &mut ToolSession, name: &str, input: &str) -> String {
    let input = serde_json::from_str(input).unwrap();
    tools.invoke(name, &input).unwrap().output
}

fn instance_tool(instance: &mut PluginInstance, name: &str, input: &str) -> smith::Result<String> {
    let mut session = ToolSession::new(std::env::temp_dir());
    let mut effects = ToolEffectContext::for_operation(&mut session, name);
    instance.invoke_with_effects(name, input, &mut effects)
}

fn protocol_manifest(id: &str, artifact: &str) -> PluginManifest {
    PluginManifest::parse(&format!(
        "id \"{id}\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"{artifact}\""
    ))
    .unwrap()
}

/// Build the protocol artifact, returning its path.
fn openai_compatible_protocol_artifact() -> std::path::PathBuf {
    let status = std::process::Command::new("cargo")
        .args([
            "build",
            "-p",
            "smith-plugin-protocol-openai-compatible",
            "--target",
            "wasm32-wasip2",
            "--locked",
        ])
        .status()
        .unwrap();
    assert!(
        status.success(),
        "OpenAI-compatible protocol plugin build failed"
    );
    let manifest = std::env::var("CARGO_MANIFEST_DIR").unwrap();
    std::path::Path::new(&manifest)
        .join("../target/wasm32-wasip2/debug/smith_plugin_protocol_openai_compatible.wasm")
}

fn anthropic_protocol_artifact() -> std::path::PathBuf {
    protocol_artifact(
        "smith-plugin-protocol-anthropic",
        "smith_plugin_protocol_anthropic.wasm",
    )
}

fn google_protocol_artifact() -> std::path::PathBuf {
    protocol_artifact(
        "smith-plugin-protocol-google",
        "smith_plugin_protocol_google.wasm",
    )
}

fn echo_artifact() -> std::path::PathBuf {
    let status = std::process::Command::new("cargo")
        .args([
            "build",
            "-p",
            "smith-plugin-echo",
            "--target",
            "wasm32-wasip2",
        ])
        .status()
        .unwrap();
    assert!(status.success(), "echo plugin build failed");
    let manifest = std::env::var("CARGO_MANIFEST_DIR").unwrap();
    std::path::Path::new(&manifest).join("../target/wasm32-wasip2/debug/smith_plugin_echo.wasm")
}

#[test]
fn coding_tools_defer_workspace_validation_until_invocation() {
    let root = std::env::temp_dir().join(format!("smith-coding-tools-lazy-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&root);

    let mut effects = ToolSession::new(&root);
    effects.set_dispatcher(Box::new(coding_tools_dispatcher(&root)));
    effects.begin_provider_round().unwrap();
    let input = serde_json::from_str(r#"{"path":"must-not-exist","content":"x"}"#).unwrap();
    let error = effects.invoke("write", &input).unwrap_err();
    assert!(error.to_string().contains("workspace root is invalid"));
}

#[test]
fn bundled_coding_tools_use_granted_workspace_effects() {
    let root = std::env::temp_dir().join(format!("smith_coding_tools_{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&root);
    std::fs::create_dir_all(&root).unwrap();
    std::fs::write(root.join("source.txt"), "alpha\nbeta\n").unwrap();
    std::fs::write(root.join(".gitignore"), "ignored.txt\n").unwrap();
    std::fs::write(root.join("ignored.txt"), "beta\n").unwrap();

    let mut effects = ToolSession::new(root.clone());
    effects.set_dispatcher(Box::new(coding_tools_dispatcher(&root)));
    effects.begin_provider_round().unwrap();

    assert_eq!(
        tool(
            &mut effects,
            "read",
            r#"{"path":"source.txt","offset":1,"limit":1,"max_bytes":6}"#
        ),
        "beta\n"
    );
    assert_eq!(tool(&mut effects, "ls", r#"{}"#), "source.txt");
    assert_eq!(tool(&mut effects, "find", r#"{"pattern":"ignored"}"#), "");
    assert_eq!(
        tool(&mut effects, "bash", r#"{"command":"printf coding-tools"}"#),
        "coding-tools"
    );
    tool(
        &mut effects,
        "write",
        r#"{"path":"nested/output.txt","content":"written"}"#,
    );
    tool(
        &mut effects,
        "edit",
        r#"{"path":"nested/output.txt","old_string":"written","new_string":"edited"}"#,
    );
    assert_eq!(
        std::fs::read_to_string(root.join("nested/output.txt")).unwrap(),
        "edited"
    );
    assert_eq!(
        tool(&mut effects, "grep", r#"{"path":"","query":"beta"}"#),
        "source.txt:2:beta"
    );
    let escaped: smith::structured::ToolArguments =
        serde_json::from_str(r#"{"path":"../outside","offset":0,"limit":1}"#).unwrap();
    assert!(
        effects
            .invoke("read", &escaped)
            .unwrap_err()
            .to_string()
            .contains("workspace path escapes")
    );
    #[cfg(unix)]
    {
        std::os::unix::fs::symlink(std::env::temp_dir(), root.join("escape")).unwrap();
        let escaped: smith::structured::ToolArguments =
            serde_json::from_str(r#"{"path":"escape/outside","offset":0,"limit":1}"#).unwrap();
        assert!(
            effects
                .invoke("read", &escaped)
                .unwrap_err()
                .to_string()
                .contains("symbolic link")
        );
    }

    let _ = std::fs::remove_dir_all(root);
}

#[cfg(target_os = "linux")]
#[test]
fn coding_tools_hold_the_workspace_root_across_directory_swap_attempts() {
    let root = std::env::temp_dir().join(format!("smith-workspace-swap-{}", std::process::id()));
    let outside =
        std::env::temp_dir().join(format!("smith-workspace-outside-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&root);
    let _ = std::fs::remove_dir_all(&outside);
    std::fs::create_dir_all(root.join("swap")).unwrap();
    std::fs::create_dir_all(&outside).unwrap();

    let mut effects = ToolSession::new(root.clone());
    effects.set_dispatcher(Box::new(coding_tools_dispatcher(&root)));
    effects.begin_provider_round().unwrap();
    std::fs::rename(root.join("swap"), root.join("moved")).unwrap();
    std::os::unix::fs::symlink(&outside, root.join("swap")).unwrap();

    let error = effects
        .invoke(
            "write",
            &serde_json::from_str(r#"{"path":"swap/escape.txt","content":"x"}"#).unwrap(),
        )
        .unwrap_err();
    assert!(error.to_string().contains("WRITE_ERROR"));
    assert!(!outside.join("escape.txt").exists());
    assert!(!root.join("moved/escape.txt").exists());

    let _ = std::fs::remove_dir_all(&root);
    let _ = std::fs::remove_dir_all(&outside);
}

#[test]
fn coding_tools_reject_a_session_from_another_workspace_before_effects() {
    let root_a = std::env::temp_dir().join(format!("smith-workspace-a-{}", std::process::id()));
    let root_b = std::env::temp_dir().join(format!("smith-workspace-b-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&root_a);
    let _ = std::fs::remove_dir_all(&root_b);
    std::fs::create_dir_all(&root_a).unwrap();
    std::fs::create_dir_all(&root_b).unwrap();

    let mut effects = ToolSession::new(&root_b);
    effects.set_dispatcher(Box::new(coding_tools_dispatcher(&root_a)));
    effects.begin_provider_round().unwrap();
    let input = serde_json::from_str(r#"{"path":"must-not-exist","content":"x"}"#).unwrap();
    let error = effects.invoke("write", &input).unwrap_err();
    assert!(
        error
            .to_string()
            .contains("workspace authority does not match")
    );
    assert!(!root_a.join("must-not-exist").exists());
    assert!(!root_b.join("must-not-exist").exists());

    let _ = std::fs::remove_dir_all(root_a);
    let _ = std::fs::remove_dir_all(root_b);
}

#[test]
fn process_effect_without_grant_fails_before_component_instantiation() {
    let engine = PluginEngine::new().unwrap();
    let manifest = PluginManifest::parse(
        "id \"smith:coding-tools\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"coding.wasm\"\ncapability \"process\"",
    )
    .unwrap();
    let error = match PluginGeneration::load_embedded(
        &engine,
        manifest,
        include_bytes!(concat!(env!("OUT_DIR"), "/smith_plugin_coding_tools.wasm")),
        &[Capability::Workspace],
    ) {
        Err(error) => error,
        Ok(_) => panic!("filesystem-only grant must reject process capability"),
    };
    assert!(error.to_string().contains("missing grant process"));
}

#[test]
fn workspace_effect_without_grant_fails_before_file_read() {
    let engine = PluginEngine::new().unwrap();
    let manifest = PluginManifest::parse(
        "id \"smith:coding-tools\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"coding.wasm\"\ncapability \"workspace\"",
    )
    .unwrap();
    let error = match PluginGeneration::load_embedded(
        &engine,
        manifest,
        include_bytes!(concat!(env!("OUT_DIR"), "/smith_plugin_coding_tools.wasm")),
        &[],
    ) {
        Err(error) => error,
        Ok(_) => panic!("missing workspace grant must reject before instantiation"),
    };
    assert!(error.to_string().contains("missing grant workspace"));
}

#[test]
fn echo_plugin_loads_declares_and_invokes() {
    let artifact = echo_artifact();
    let engine = PluginEngine::new().unwrap();
    let mut instance = PluginInstance::load(&engine, &artifact).unwrap();

    let tools = instance.declare_tools().unwrap();
    assert_eq!(tools.len(), 1);
    assert_eq!(tools[0].name, "echo");

    let echoed = instance_tool(&mut instance, "echo", "{\"text\":\"hi\"}").unwrap();
    assert_eq!(echoed, "{\"text\":\"hi\"}");

    let rejected = match instance_tool(&mut instance, "nope", "{}") {
        Err(error) => error,
        Ok(value) => panic!("expected plugin error, got {value}"),
    };
    assert!(rejected.to_string().contains("unknown tool: nope"));
}

#[test]
fn openai_compatible_protocol_keeps_connection_policy_outside_the_guest() {
    let engine = PluginEngine::new().unwrap();
    let manifest = PluginManifest::parse(
        "id \"smith:protocol-openai-compatible\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"smith_plugin_protocol_openai_compatible.wasm\"",
    )
    .unwrap();
    let mut generation = PluginGeneration::load(
        &engine,
        manifest,
        &openai_compatible_protocol_artifact(),
        &[],
    )
    .unwrap();
    assert_eq!(generation.registrations().credential_sources.len(), 1);
    assert_eq!(generation.registrations().providers.len(), 1);
    generation
        .require_protocol_binding(
            smith_harness::wasm::bindings::exports::smith::plugin::registration::ProtocolBinding::OpenaiCompatible,
        )
        .unwrap();
    assert!(generation
        .require_protocol_binding(
            smith_harness::wasm::bindings::exports::smith::plugin::registration::ProtocolBinding::GoogleGemini,
        )
        .is_err());
    let settings = generation
        .validate_provider_settings(ProviderSettings {
            include_usage: true,
            subscription: false,
            provenance: SettingsProvenance::BuiltIn,
        })
        .unwrap();
    assert!(settings.include_usage);
    let request = ProviderRequest {
        code_assist: None,
        messages: vec![
            ProviderMessage {
                role: MessageRole::User,
                blocks: vec![MessageBlock::Text("hello".to_string())],
            },
            ProviderMessage {
                role: MessageRole::Assistant,
                blocks: vec![
                    MessageBlock::Thinking("plan".to_string()),
                    MessageBlock::ToolUse(ToolUse {
                        name: "read".to_string(),
                        input_json: r#"{"path":"lib.rs"}"#.to_string(),
                        call_id: "call-1".to_string(),
                    }),
                ],
            },
        ],
        tools: vec![ProviderTool {
            name: "read".to_string(),
            description: "Read a file".to_string(),
            input_schema_json: r#"{"type":"object"}"#.to_string(),
        }],
        output_tokens_max: None,
    };
    let catalog = CatalogSnapshot {
        generation: 42,
        source: CatalogSource::Cache,
        models: vec![CatalogModel {
            id: "glm-4".to_string(),
            capabilities: ModelCapabilities {
                output_tokens_max: 8192,
                tool_use: true,
                thinking: false,
            },
        }],
    };
    let template = generation
        .build_provider_request("glm-4", &request, settings, &catalog)
        .unwrap();
    assert_eq!(template.url, "https://protocol.invalid/chat/completions");
    assert_eq!(
        template.authorization,
        "Bearer {{secret:connection-api-key}}"
    );
    assert!(!template.body.contains("connection-api-key"));
    let body: serde_json::Value = serde_json::from_str(&template.body).unwrap();
    assert_eq!(body["tools"][0]["function"]["name"], "read");
    assert_eq!(
        body["messages"][1]["tool_calls"][0]["function"]["arguments"],
        r#"{"path":"lib.rs"}"#
    );

    let events = generation
        .decode_provider_chunk(
            &[],
            b"data: {\"choices\":[{\"delta\":{\"content\":\"accepted\",\"reasoning_content\":\"thought\"}}]}\n\ndata: {\"choices\":[{\"delta\":{},\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":1,\"completion_tokens\":2}}\n\ndata: [DONE]\n\n",
            true,
        )
        .unwrap();
    assert!(events.events.iter().any(|event| matches!(
        event,
        smith_harness::wasm::bindings::exports::smith::plugin::provider::StreamEvent::Thinking(text)
            if text == "thought"
    )));
    assert!(events.events.iter().any(|event| matches!(
        event,
        smith_harness::wasm::bindings::exports::smith::plugin::provider::StreamEvent::Stop((_, Some(usage)))
            if usage.input_tokens == 1 && usage.output_tokens == 2
    )));
}

#[test]
fn publication_qualifies_connection_sources_and_providers_by_package() {
    let manifest = |id| {
        PluginManifest::parse(&format!(
            "id \"{id}\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"smith_plugin_protocol_openai_compatible.wasm\"\ncapability \"net-scope\"\ncapability \"secret-proxy\""
        ))
        .unwrap()
    };
    let engine = PluginEngine::new().unwrap();
    let artifact = openai_compatible_protocol_artifact();
    let lower = PluginGeneration::load(
        &engine,
        manifest("example:a"),
        &artifact,
        &[Capability::NetScope, Capability::SecretProxy],
    )
    .unwrap();
    let higher = PluginGeneration::load(
        &engine,
        manifest("example:b"),
        &artifact,
        &[Capability::NetScope, Capability::SecretProxy],
    )
    .unwrap();
    let mut registry = PluginRegistry::new();
    registry.publish_all(vec![higher, lower]).unwrap();
    let error = match registry.bind_credential_source("test", "connection-api-key") {
        Ok(_) => panic!("short source alias must be ambiguous"),
        Err(error) => error,
    };
    assert!(
        error
            .to_string()
            .contains("candidates=[\"example:a\", \"example:b\"]")
    );
    for owner in ["example:a", "example:b"] {
        let source = format!("{owner}/connection-api-key");
        assert_eq!(
            registry
                .bind_credential_source("test", &source)
                .unwrap()
                .manifest()
                .id(),
            owner
        );
    }
    let duplicate = || {
        PluginGeneration::load(
            &engine,
            manifest("example:a"),
            &artifact,
            &[Capability::NetScope, Capability::SecretProxy],
        )
        .unwrap()
    };
    let error = registry
        .publish_all(vec![duplicate(), duplicate()])
        .unwrap_err();
    assert!(
        error
            .to_string()
            .contains("duplicate credential source example:a/connection-api-key")
    );
    assert_eq!(
        registry
            .bind_credential_source("test", "example:b/connection-api-key")
            .unwrap()
            .manifest()
            .id(),
        "example:b"
    );
}

#[test]
fn protocol_guests_decode_recorded_vendor_conformance_fixtures() {
    use smith_harness::wasm::bindings::exports::smith::plugin::provider::{
        StopReason, StreamEvent,
    };

    let cases = [
        (
            "smith:protocol-anthropic",
            "smith_plugin_protocol_anthropic.wasm",
            anthropic_protocol_artifact(),
            b"data: {\"type\":\"message_start\",\"message\":{\"usage\":{\"input_tokens\":12}}}\n\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"Look\"}}\n\ndata: {\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"thinking_delta\",\"thinking\":\"hmm\"}}\n\ndata: {\"type\":\"content_block_start\",\"index\":2,\"content_block\":{\"type\":\"tool_use\",\"id\":\"call-anthropic\",\"name\":\"write\"}}\n\ndata: {\"type\":\"content_block_delta\",\"index\":2,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"path\\\"\"}}\n\ndata: {\"type\":\"content_block_delta\",\"index\":2,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\":\\\"f\\\"}\"}}\n\ndata: {\"type\":\"content_block_stop\",\"index\":2}\n\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\"},\"usage\":{\"output_tokens\":7,\"cache_read_input_tokens\":3}}\n\ndata: {\"type\":\"message_stop\"}\n\n".as_slice(),
        ),
        (
            "smith:protocol-google",
            "smith_plugin_protocol_google.wasm",
            google_protocol_artifact(),
            b"data: {\"candidates\":[{\"content\":{\"parts\":[{\"text\":\"Writing\"},{\"functionCall\":{\"id\":\"call-google\",\"name\":\"write\",\"args\":{\"path\":\"f\"}}}]},\"finishReason\":\"STOP\"}],\"usageMetadata\":{\"promptTokenCount\":5,\"candidatesTokenCount\":9,\"cachedContentTokenCount\":2}}\n\n".as_slice(),
        ),
    ];
    let engine = PluginEngine::new().unwrap();
    for (id, artifact_name, artifact, bytes) in cases {
        let mut generation = PluginGeneration::load(
            &engine,
            protocol_manifest(id, artifact_name),
            &artifact,
            &[],
        )
        .unwrap();
        let step = generation
            .decode_provider_chunk(&[], bytes, true)
            .unwrap_or_else(|error| panic!("{id}: {error}"));
        assert!(
            step.events
                .iter()
                .any(|event| matches!(event, StreamEvent::Text(text) if !text.is_empty())),
            "{id}"
        );
        let expected_call_id = if id.ends_with("google") {
            "call-google"
        } else {
            "call-anthropic"
        };
        assert!(step.events.iter().any(|event| matches!(event, StreamEvent::ToolUse(tool) if tool.name == "write" && tool.input_json == r#"{"path":"f"}"# && tool.call_id == expected_call_id)), "{id}");
        if id.ends_with("google") {
            assert!(step.events.iter().any(|event| matches!(event, StreamEvent::Stop((StopReason::ToolUse, Some(usage))) if usage.input_tokens > 0 && usage.output_tokens > 0)));
        } else {
            assert!(step.events.iter().any(|event| matches!(event, StreamEvent::Stop((StopReason::ToolUse, Some(usage))) if usage.input_tokens > 0 && usage.output_tokens > 0)));
            assert!(
                step.events
                    .iter()
                    .any(|event| matches!(event, StreamEvent::Thinking(text) if text == "hmm"))
            );
        }
    }
}

#[test]
fn protocol_guests_preserve_utf8_at_every_chunk_boundary() {
    let cases = [
        ("smith:protocol-anthropic", "smith_plugin_protocol_anthropic.wasm", anthropic_protocol_artifact(), b"data: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"\xe2\x98\x83\"}}\n\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"}}\n\ndata: {\"type\":\"message_stop\"}\n\n".as_slice()),
        ("smith:protocol-google", "smith_plugin_protocol_google.wasm", google_protocol_artifact(), b"data: {\"candidates\":[{\"content\":{\"parts\":[{\"text\":\"\xe2\x98\x83\"}]},\"finishReason\":\"STOP\"}]}\n\n".as_slice()),
    ];
    let engine = PluginEngine::new().unwrap();
    for (id, artifact_name, artifact, bytes) in cases {
        let mut generation = PluginGeneration::load(
            &engine,
            protocol_manifest(id, artifact_name),
            &artifact,
            &[],
        )
        .unwrap();
        for split in 0..=bytes.len() {
            let first = generation
                .decode_provider_chunk(&[], &bytes[..split], false)
                .unwrap();
            let second = generation
                .decode_provider_chunk(&first.state, &bytes[split..], true)
                .unwrap();
            assert!(first.events.into_iter().chain(second.events).any(|event| matches!(event, smith_harness::wasm::bindings::exports::smith::plugin::provider::StreamEvent::Text(text) if text == "☃")), "{id} split {split}");
        }
    }
}

#[test]
fn anthropic_guest_preserves_refusal_stop_reason() {
    let engine = PluginEngine::new().unwrap();
    let mut generation = PluginGeneration::load(
        &engine,
        protocol_manifest(
            "smith:protocol-anthropic",
            "smith_plugin_protocol_anthropic.wasm",
        ),
        &anthropic_protocol_artifact(),
        &[],
    )
    .unwrap();
    let response = b"data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"refusal\"}}\n\ndata: {\"type\":\"message_stop\"}\n\n";

    let step = generation
        .decode_provider_chunk(&[], response, true)
        .unwrap();
    assert!(step.events.iter().any(|event| matches!(
        event,
        smith_harness::wasm::bindings::exports::smith::plugin::provider::StreamEvent::Stop((
            smith_harness::wasm::bindings::exports::smith::plugin::provider::StopReason::Refusal,
            _
        ))
    )));
}

#[test]
fn protocol_guests_reject_malformed_and_provider_error_responses() {
    let cases = [
        (
            "smith:protocol-anthropic",
            "smith_plugin_protocol_anthropic.wasm",
            anthropic_protocol_artifact(),
            b"data: {not-json}\n\n".as_slice(),
            b"data: {\"type\":\"error\",\"error\":{\"message\":\"busy\"}}\n\n".as_slice(),
        ),
        (
            "smith:protocol-google",
            "smith_plugin_protocol_google.wasm",
            google_protocol_artifact(),
            b"data: {not-json}\n\n".as_slice(),
            b"data: {\"error\":{\"code\":429,\"message\":\"quota\"}}\n\n".as_slice(),
        ),
    ];
    let engine = PluginEngine::new().unwrap();
    for (id, artifact_name, artifact, malformed, provider_error) in cases {
        let mut generation = PluginGeneration::load(
            &engine,
            protocol_manifest(id, artifact_name),
            &artifact,
            &[],
        )
        .unwrap();
        assert!(
            generation
                .decode_provider_chunk(&[], malformed, true)
                .is_err(),
            "{id} malformed"
        );
        assert!(
            generation
                .decode_provider_chunk(&[], provider_error, true)
                .is_err(),
            "{id} provider error"
        );
    }
}

#[test]
fn openai_compatible_protocol_decodes_utf8_at_every_real_component_byte_split() {
    let bytes = b"data: {\"choices\":[{\"delta\":{\"content\":\"\xe2\x98\x83\"}}]}\n\ndata: {\"choices\":[{\"delta\":{},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n";
    let engine = PluginEngine::new().unwrap();
    let artifact = openai_compatible_protocol_artifact();
    let manifest = PluginManifest::parse(
        "id \"smith:protocol-openai-compatible\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"smith_plugin_protocol_openai_compatible.wasm\"",
    )
    .unwrap();

    let mut generation = PluginGeneration::load(&engine, manifest, &artifact, &[]).unwrap();
    for split in 0..=bytes.len() {
        let first = generation
            .decode_provider_chunk(&[], &bytes[..split], false)
            .unwrap();
        let second = generation
            .decode_provider_chunk(&first.state, &bytes[split..], true)
            .unwrap();
        let events = first
            .events
            .into_iter()
            .chain(second.events)
            .collect::<Vec<_>>();
        assert!(events.iter().any(|event| matches!(
            event,
            smith_harness::wasm::bindings::exports::smith::plugin::provider::StreamEvent::Text(text)
                if text == "☃"
        )), "split {split}");
        assert!(
            events.iter().any(|event| matches!(
                event,
                smith_harness::wasm::bindings::exports::smith::plugin::provider::StreamEvent::Stop(
                    _
                )
            )),
            "split {split}"
        );
    }
}

#[test]
fn openai_compatible_protocol_orders_double_digit_tool_indexes_numerically() {
    let engine = PluginEngine::new().unwrap();
    let manifest = PluginManifest::parse(
        "id \"smith:protocol-openai-compatible\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"smith_plugin_protocol_openai_compatible.wasm\"",
    )
    .unwrap();
    let mut generation = PluginGeneration::load(
        &engine,
        manifest,
        &openai_compatible_protocol_artifact(),
        &[],
    )
    .unwrap();
    let calls = (0..12)
        .map(|index| {
            format!(
                r#"{{"index":{index},"id":"call-{index}","function":{{"name":"tool-{index}","arguments":"{{}}"}}}}"#
            )
        })
        .collect::<Vec<_>>()
        .join(",");
    let response = format!(
        "data: {{\"choices\":[{{\"delta\":{{\"tool_calls\":[{calls}]}},\"finish_reason\":\"tool_calls\"}}]}}\n\ndata: [DONE]\n\n"
    );
    let step = generation
        .decode_provider_chunk(&[], response.as_bytes(), true)
        .unwrap();
    let names = step.events.into_iter().filter_map(|event| match event {
        smith_harness::wasm::bindings::exports::smith::plugin::provider::StreamEvent::ToolUse(tool) => Some(tool.name),
        _ => None,
    }).collect::<Vec<_>>();
    assert_eq!(
        names,
        (0..12)
            .map(|index| format!("tool-{index}"))
            .collect::<Vec<_>>()
    );
}

#[test]
fn guest_trap_is_reported() {
    let artifact = echo_artifact();
    let engine = PluginEngine::new().unwrap();
    let mut instance = PluginInstance::load(&engine, &artifact).unwrap();
    let oversized = "x".repeat(2 * 1024 * 1024);
    let mut session = ToolSession::new(std::env::temp_dir());
    let mut effects = ToolEffectContext::for_operation(&mut session, "echo");
    let error = instance
        .invoke_with_effects("echo", &oversized, &mut effects)
        .unwrap_err();
    assert!(error.to_string().contains("!abort"), "{error}");
    assert!(effects.cancel_handle().is_cancelled());
    assert!(matches!(
        instance.invoke_with_effects("echo", "{}", &mut effects),
        Err(smith::error::SmithError::Cancelled)
    ));
}

#[test]
fn plugin_refills_its_fuel_budget_for_each_call() {
    let artifact = echo_artifact();
    let engine = PluginEngine::new().unwrap();
    let mut instance = PluginInstance::load_with_ceilings(
        &engine,
        &artifact,
        smith_harness::wasm::PluginCeilings {
            fuel: 100_000,
            ..Default::default()
        },
    )
    .unwrap();

    assert_eq!(
        instance_tool(&mut instance, "echo", "{}"),
        Ok("{}".to_string())
    );
    assert_eq!(
        instance_tool(&mut instance, "echo", "{}"),
        Ok("{}".to_string())
    );
}

#[test]
fn failed_generation_load_preserves_the_published_generation() {
    let artifact = echo_artifact();
    let engine = PluginEngine::new().unwrap();
    let manifest = PluginManifest::parse(
        "id \"smith:echo\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"smith_plugin_echo.wasm\"",
    )
    .unwrap();
    let generation = PluginGeneration::load(&engine, manifest, &artifact, &[]).unwrap();
    let mut registry = PluginRegistry::new();
    assert!(registry.publish(generation).is_none());

    let ungranted = PluginManifest::parse(
        "id \"smith:echo\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"smith_plugin_echo.wasm\"\ncapability \"log\"",
    )
    .unwrap();
    let error = match PluginGeneration::load(&engine, ungranted, &artifact, &[]) {
        Err(error) => error,
        Ok(_) => panic!("ungranted capability must reject the generation"),
    };
    assert!(error.to_string().contains("missing grant log"));

    let mut session = ToolSession::new(std::env::temp_dir());
    let mut effects = ToolEffectContext::for_operation(&mut session, "echo");
    assert_eq!(
        registry.invoke_with_effects("echo", "{\"kept\":true}", &mut effects),
        Ok("{\"kept\":true}".to_string())
    );
    let mut effects = ToolEffectContext::for_operation(&mut session, "nope");
    let error = registry
        .invoke_with_effects("nope", "{}", &mut effects)
        .unwrap_err();
    assert!(error.to_string().contains("tool nope is not registered"));
}

#[test]
fn generation_load_rejects_unsupported_world_before_execution() {
    let artifact = echo_artifact();
    let engine = PluginEngine::new().unwrap();
    let manifest = PluginManifest::parse(
        "id \"smith:echo\"\nversion \"0.1.0\"\nworld \"9.0.0\"\nartifact \"smith_plugin_echo.wasm\"",
    )
    .unwrap();

    let error = match PluginGeneration::load(&engine, manifest, &artifact, &[]) {
        Err(error) => error,
        Ok(_) => panic!("unsupported world must reject the generation"),
    };
    assert!(
        error
            .to_string()
            .contains("unsupported world version 9.0.0")
    );
}

#[test]
fn generation_load_allows_declared_grants() {
    let artifact = echo_artifact();
    let engine = PluginEngine::new().unwrap();
    let manifest = PluginManifest::parse(
        "id \"smith:echo\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"smith_plugin_echo.wasm\"\ncapability \"log\"",
    )
    .unwrap();

    let generation =
        PluginGeneration::load(&engine, manifest, &artifact, &[Capability::Log]).unwrap();
    assert_eq!(generation.tools()[0].name, "echo");
}

#[test]
fn unmodified_wasip2_std_echo_is_linked_with_fixed_memory() {
    let artifact = echo_artifact();
    let engine = PluginEngine::new().unwrap();
    assert!(engine.validate(&artifact).is_ok());
}

#[test]
fn defined_memory_without_maximum_is_rejected_before_component_validation() {
    let artifact = fixture(
        "defined_memory_without_maximum",
        b"\0asm\x01\0\0\0\x05\x03\x01\0\x01",
    );
    let engine = PluginEngine::new().unwrap();
    let error = engine.validate(&artifact).unwrap_err();
    assert!(error.to_string().contains("memory maximum is missing"));
    let _ = std::fs::remove_file(artifact);
}

#[test]
fn imported_memory_without_maximum_is_rejected_before_component_validation() {
    let artifact = fixture(
        "imported_memory_without_maximum",
        b"\0asm\x01\0\0\0\x02\r\x01\x01m\x06memory\x02\0\x01",
    );
    let engine = PluginEngine::new().unwrap();
    let error = engine.validate(&artifact).unwrap_err();
    assert!(error.to_string().contains("memory maximum is missing"));
    let _ = std::fs::remove_file(artifact);
}

#[test]
fn defined_table_without_maximum_is_rejected_before_component_validation() {
    let artifact = fixture(
        "defined_table_without_maximum",
        b"\0asm\x01\0\0\0\x04\x04\x01\x70\0\x01",
    );
    let engine = PluginEngine::new().unwrap();
    let error = engine.validate(&artifact).unwrap_err();
    assert!(error.to_string().contains("table maximum is missing"));
    let _ = std::fs::remove_file(artifact);
}

#[test]
fn imported_table_without_maximum_is_rejected_before_component_validation() {
    let artifact = fixture(
        "imported_table_without_maximum",
        b"\0asm\x01\0\0\0\x02\r\x01\x01m\x05table\x01\x70\0\x01",
    );
    let engine = PluginEngine::new().unwrap();
    let error = engine.validate(&artifact).unwrap_err();
    assert!(error.to_string().contains("table maximum is missing"));
    let _ = std::fs::remove_file(artifact);
}

#[test]
fn mismatched_memory_maximum_is_rejected_before_component_validation() {
    let artifact = std::env::temp_dir().join(format!(
        "smith_mismatched_memory_{}.wasm",
        std::process::id()
    ));
    std::fs::write(&artifact, b"\0asm\x01\0\0\0\x05\x04\x01\x01\x01\x02").unwrap();
    let engine = PluginEngine::new().unwrap();
    let error = engine.validate(&artifact).unwrap_err();
    assert!(
        error
            .to_string()
            .contains("memory maximum differs from minimum")
    );
    let _ = std::fs::remove_file(&artifact);
}

fn fixture(name: &str, bytes: &[u8]) -> std::path::PathBuf {
    let artifact = std::env::temp_dir().join(format!("smith_{name}_{}.wasm", std::process::id()));
    std::fs::write(&artifact, bytes).unwrap();
    artifact
}

#[test]
fn invalid_artifact_fails_validation() {
    let junk = std::env::temp_dir().join(format!("smith_junk_{}.wasm", std::process::id()));
    std::fs::write(&junk, b"not a component").unwrap();
    let engine = PluginEngine::new().unwrap();
    let Err(error) = PluginInstance::load(&engine, &junk) else {
        panic!("expected validation failure")
    };
    assert!(error.to_string().contains("validation failed"));
    let _ = std::fs::remove_file(&junk);
}

#[test]
fn google_guest_accepts_fragmented_eof_event_followed_by_sse_keepalive() {
    use smith_harness::wasm::bindings::exports::smith::plugin::provider::{
        StopReason, StreamEvent,
    };
    let engine = PluginEngine::new().unwrap();
    let mut generation = PluginGeneration::load(
        &engine,
        protocol_manifest("smith:protocol-google", "smith_plugin_protocol_google.wasm"),
        &google_protocol_artifact(),
        &[],
    )
    .unwrap();
    for tail in [
        "",
        "\n\n: keepalive",
        "\n\n: keepalive\n\n",
        "\n: comment\n",
        "\n\nevent: message\nid: last\n: keepalive\n\n",
    ] {
        let bytes = format!(
            "data: {{\"candidates\":[{{\"content\":{{\"parts\":[{{\"text\":\"EOF\"}}]}},\"finishReason\":\"STOP\"}}]}}{tail}"
        );
        for split in 0..=bytes.len() {
            let first = generation
                .decode_provider_chunk(&[], &bytes.as_bytes()[..split], false)
                .unwrap();
            let second = generation
                .decode_provider_chunk(&first.state, &bytes.as_bytes()[split..], true)
                .unwrap();
            let events: Vec<_> = first.events.into_iter().chain(second.events).collect();
            assert_eq!(
                events
                    .iter()
                    .filter(|event| matches!(event, StreamEvent::Text(text) if text == "EOF"))
                    .count(),
                1,
                "{tail:?} split {split}"
            );
            assert_eq!(
                events
                    .iter()
                    .filter(|event| matches!(event, StreamEvent::Stop((StopReason::EndTurn, _))))
                    .count(),
                1,
                "{tail:?} split {split}"
            );
        }
    }
}