Luigit
repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by admin

smith-harness/tests/fixtures/lifecycle_support.rs

Raw
//! SMH-SPEC-PLUG0001: build ordinary raw-WIT lifecycle consumers.
use std::path::{Path, PathBuf};
pub fn compile_fixture(root: &Path, source: &str, name: &str) -> PathBuf {
    // Shared dependencies stay cached; each invocation owns its output artifact.
    let name = format!("{name}-{}", smith::id::SessionId::new());
    std::fs::create_dir_all(root.join("wit")).unwrap();
    std::fs::write(
        root.join("wit/smith-plugin.wit"),
        include_str!("../../../wit/smith-plugin.wit"),
    )
    .unwrap();
    std::fs::write(root.join("lib.rs"), source).unwrap();
    std::fs::write(
        root.join("Cargo.toml"),
        format!(
            r#"[package]
name = "{name}"
version = "0.0.0"
edition = "2024"
[lib]
path = "lib.rs"
crate-type = ["cdylib"]
[dependencies]
wit-bindgen = {{ version = "=0.62.0", default-features = false, features = ["macros"] }}
[workspace]
"#
        ),
    )
    .unwrap();
    let target = Path::new(env!("CARGO_MANIFEST_DIR")).join("../target");
    for args in [
        vec!["generate-lockfile", "--offline"],
        vec![
            "build",
            "--offline",
            "--locked",
            "--target",
            "wasm32-wasip2",
        ],
    ] {
        assert!(std::process::Command::new("cargo").args(args).current_dir(root).env_remove("CARGO_ENCODED_RUSTFLAGS").env("RUSTFLAGS", "-C target-cpu=generic -C link-arg=--initial-memory=2097152 -C link-arg=--max-memory=2097152").env("CARGO_TARGET_DIR", &target).env("XDG_CONFIG_HOME", root.join("config")).status().unwrap().success());
    }
    target.join(format!(
        "wasm32-wasip2/debug/{}.wasm",
        name.replace('-', "_")
    ))
}

pub fn install_consumers(root: &Path, registry_root: &Path) {
    use smith_harness::{plugins::registry::PackageRegistry, wasm::PluginEngine};
    let registry = PackageRegistry::at(registry_root.into());
    let engine = PluginEngine::new().unwrap();
    for (id, name, actions) in [
        (
            "example:context",
            "project-root",
            "vec![Action::EnsureContinuation, Action::EditContext(vec![ContextEdit { target: event.root_entry_id, replacement: Some(Replacement::Text(\"effective-root\".into())) }])]",
        ),
        (
            "example:prompt",
            "prepare-prompt",
            "vec![Action::EnsureContinuation, Action::SetPrompt(\"next-request-prompt\".into())]",
        ),
    ] {
        let source = include_str!("lifecycle.rs")
            .replace("vec![Action::EnsureContinuation]", actions)
            .replace("\"continue\"", &format!("\"{name}\""));
        let artifact = compile_fixture(&root.join(name), &source, &format!("lifecycle-{name}"));
        install(
            &registry,
            &engine,
            &root.join(id.replace(":", "-")),
            id,
            &artifact,
        );
    }
}
pub fn install(
    registry: &smith_harness::plugins::registry::PackageRegistry,
    engine: &smith_harness::wasm::PluginEngine,
    package: &Path,
    id: &str,
    artifact: &Path,
) {
    std::fs::create_dir_all(package).unwrap();
    std::fs::copy(artifact, package.join("fixture.wasm")).unwrap();
    std::fs::write(
        package.join("plugin.kdl"),
        format!("id \"{id}\"\nversion \"0.1.0\"\nworld \"0.1.0\"\nartifact \"fixture.wasm\"\n"),
    )
    .unwrap();
    registry.install(engine, package, &[]).unwrap();
}