Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/holger

server/lib/tests/the_open_meter_is_built_in.rs

//! ★ **The open rows' meter is built in, answers Allow, and needs no wasm host**
//! (Holger v2.0 H2 #2, owner 2026-10-25).
//!
//! The open MVP rows used to link wasmtime (about 41 crates) to run one meter guest
//! that answers Allow unconditionally (`holger-plugin-allocation`). With `allow-meter`
//! that guest is compiled in as a LINKED meter: the same code, the same census row,
//! the same grants (`usage.read`, `store.list`), and no wasm runtime. wasmtime stays
//! behind `wasm-plugins`, which only the sold (`metered`) rows name.

use std::sync::Arc;

use holger_server_lib::{read_ron_config, Holger};

fn open_box(tmp: &std::path::Path) -> Holger {
    let ron = format!(
        r#"(
    repositories: [],
    exposed_endpoints: [ (ron_name: "grpc", ron_url: "127.0.0.1:1") ],
    storage_endpoints: [],
    audit: (dir: "{audit}", enabled: false),
)"#,
        audit = tmp.join("audit").display(),
    );
    let path = tmp.join("holger.ron");
    std::fs::write(&path, ron).unwrap();
    read_ron_config(&path).expect("the RON parses")
}

#[test]
#[allow(clippy::assertions_on_constants)] // the build's own feature set IS the precondition, refused loudly
fn an_open_box_has_the_built_in_allow_meter_with_no_config_and_no_wasm_host() {
    assert!(
        cfg!(not(feature = "wasm-plugins")),
        "run this test without `wasm-plugins`: the point is the build with no wasm host"
    );
    let tmp = tempfile::tempdir().unwrap();
    let h = open_box(tmp.path());
    let census = h.module_census().expect("the census builds");
    let meters: Vec<_> = census
        .modules
        .iter()
        .filter(|m| m.kind == "meter")
        .collect();
    assert_eq!(
        meters.len(),
        1,
        "one meter, the built-in one: {:?}",
        census.modules
    );
    let m = meters[0];
    assert_eq!(
        (m.name.as_str(), m.origin.as_str()),
        ("allocation", "linked"),
        "{m:?}"
    );
    assert_eq!(
        m.granted,
        vec!["store.list".to_string(), "usage.read".to_string()],
        "its own two capabilities, nothing more"
    );
    assert!(
        m.digest.is_empty(),
        "a linked meter has no module bytes: {m:?}"
    );
}

#[test]
fn the_built_in_meter_answers_allow() {
    use holger_plugin_host::abi::v2::{WireCallContext, WireVerb, WireVerdict};
    let tmp = tempfile::tempdir().unwrap();
    let h = open_box(tmp.path());
    let store: holger_plugin_host::SharedStore =
        Arc::new(holger_plugin_host::DirStore::new(tmp.path().join("meters")));
    let catalog = h
        .meter_catalog(store, Arc::new(holger_plugin_host::NoServices))
        .expect("the meter plane builds");
    assert_eq!(catalog.meters().len(), 1);
    for verb in [
        WireVerb::Fetch,
        WireVerb::Put,
        WireVerb::Promote,
        WireVerb::Delete,
    ] {
        let ctx = WireCallContext {
            abi: 2,
            repository: "crates".into(),
            verb,
            principal: None,
            artifact: None,
            now: None,
        };
        assert_eq!(
            catalog.meters()[0].evaluate(&ctx),
            Ok(WireVerdict::Allow),
            "{verb:?}"
        );
    }
}