Luigit
repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by admin

smith-harness/tests/oauth_module.rs

Raw
//! Renewable resource binding and atomic publication (`SMH-SPEC-PLUG0001`).
use smith_harness::plugins::{Capability, PluginManifest};
use smith_harness::wasm::{PluginEngine, PluginGeneration, PluginRegistry};

fn generation(engine: &PluginEngine, id: &str, oauth: bool) -> PluginGeneration {
    let (package, artifact) = if oauth {
        ("oauth", "oauth")
    } else {
        ("protocol-openai-responses", "protocol_openai_responses")
    };
    let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("..");
    let artifact = root.join(format!(
        "target/wasm32-wasip2/release/smith_plugin_{artifact}.wasm"
    ));
    let bytes = std::fs::read(&artifact).unwrap();
    let pin =
        std::fs::read_to_string(root.join(format!("plugins/{package}/component.sha256"))).unwrap();
    assert_eq!(smith::sha256::digest_hex(&bytes), pin.trim());
    let manifest = PluginManifest::parse(&format!("id \"{id}\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"component.wasm\"\ncapability \"net-scope\"\ncapability \"secret-proxy\"")).unwrap();
    PluginGeneration::load_embedded(
        engine,
        manifest,
        &bytes,
        &[Capability::NetScope, Capability::SecretProxy],
    )
    .unwrap()
}

#[test]
fn credential_binding_fails_only_when_consumed_with_sorted_candidates_and_consumer() {
    for (consumer, source) in [
        (
            "fixture:responses/openai-chatgpt-plan",
            "openai-chatgpt-plan@1",
        ),
        (
            "fixture:anthropic/anthropic-claude-plan",
            "anthropic-claude-plan@1",
        ),
    ] {
        binding_case(consumer, source);
    }
}

fn binding_case(consumer: &str, source: &str) {
    let engine = PluginEngine::new().unwrap();
    let mut registry = PluginRegistry::new();
    registry
        .publish_all(vec![generation(&engine, "fixture:responses", false)])
        .unwrap();
    let original = registry.snapshot().unwrap();
    let error = registry
        .bind_credential_source(consumer, source)
        .err()
        .unwrap()
        .to_string();
    assert!(error.contains(consumer));
    assert!(error.contains(source));
    assert!(error.contains("candidates=[]"));
    assert_eq!(registry.snapshot().unwrap(), original);
    registry
        .publish_all(vec![
            generation(&engine, "fixture:z", true),
            generation(&engine, "fixture:responses", false),
            generation(&engine, "fixture:a", true),
        ])
        .unwrap();
    let ambiguous = registry.snapshot().unwrap();
    let error = registry
        .bind_credential_source(consumer, source)
        .err()
        .unwrap()
        .to_string();
    assert!(error.contains(consumer));
    assert!(error.contains("candidates=[\"fixture:a\", \"fixture:z\"]"));
    assert_eq!(registry.snapshot().unwrap(), ambiguous);
    assert_eq!(
        registry
            .bind_credential_source_with_override(consumer, source, Some("fixture:z"),)
            .unwrap()
            .manifest()
            .id(),
        "fixture:z"
    );
    // Invalid candidate publication must leave the last complete generation intact.
    assert!(
        registry
            .publish_all(vec![
                generation(&engine, "fixture:duplicate", true),
                generation(&engine, "fixture:duplicate", true)
            ])
            .is_err()
    );
    assert_eq!(registry.snapshot().unwrap(), ambiguous);
    registry
        .publish_all(vec![generation(&engine, "fixture:only", true)])
        .unwrap();
    assert_eq!(
        registry
            .bind_credential_source("fixture:consumer", source)
            .unwrap()
            .manifest()
            .id(),
        "fixture:only"
    );
}