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

nordisk/dwarves

crates/nornir-warehouse-embed/tests/mimir_verbs.rs

//! **Every Mímir lens verb is on both protos, with one signature.**
//!
//! nornir dispatches 25 table-fed lenses (`nornir::mimir_lens::LENSES`). The forge
//! serves exactly the verbs dwarves' trimmed `proto/mimir.proto` declares (tonic's
//! generated server trait has no default bodies, so `MimirProxy` forwards every
//! declared verb and nothing else). Measured 2026-10-16 on dwarf and loki: the trim
//! carried 12 of the 18, so `DataReach`, `Trust`, `NameCollision`, `SymbolIdentity`,
//! `UndocApi` and `ReachableVuln` answered `Unimplemented` at the forge and no UI,
//! CLI or external client could reach a third of the lenses.
//!
//! This test lists the 25 (the clone signals joined 2026-10-17, ReimplementsShared 2026-10-22) by their wire names and asks BOTH protos for each: the
//! trimmed copy here and edda `nornir-wire`'s canonical `nornir.proto`. Each verb's
//! signature (request and response type) must be the same text in both.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

/// The 25 lenses nornir answers (`mimir_lens::LENSES`), by their gRPC verb.
const LENS_VERBS: [&str; 25] = [
    "GenericCost",
    "ZombieFeatures",
    "ChangeRisk",
    "SimilarCode",
    "SemanticClones",
    "WeakTests",
    "BenchGaps",
    "Villa",
    "Churn",
    "Anomaly",
    "WorldImpact",
    "BinaryDrift",
    "DataReach",
    "Trust",
    "NameCollision",
    "SymbolIdentity",
    "UndocApi",
    "ReachableVuln",
    "ExactClones",
    "NearClones",
    "StructuralClones",
    "RoleNeighbours",
    "SameIntent",
    "SemanticTwins",
    "ReimplementsShared",
];

/// `rpc name -> "(Req) returns (Resp)"` of the `service Mimir` block, comments out.
fn mimir_rpcs(src: &str) -> BTreeMap<String, String> {
    let mut out = BTreeMap::new();
    let mut inside = false;
    for raw in src.lines() {
        let line = raw.split("//").next().unwrap_or("").trim();
        if line.starts_with("service ") {
            inside = line.split_whitespace().nth(1).map(|n| n.trim_end_matches('{')) == Some("Mimir");
            continue;
        }
        if !inside {
            continue;
        }
        if line.starts_with('}') {
            inside = false;
            continue;
        }
        let Some(rest) = line.strip_prefix("rpc ") else { continue };
        let Some((name, sig)) = rest.split_once('(') else { continue };
        let sig: String = format!("({sig}").split_whitespace().collect::<Vec<_>>().join(" ");
        let sig = sig.replace("( ", "(").replace(" )", ")").trim_end_matches(';').to_string();
        out.insert(name.trim().to_string(), sig);
    }
    out
}

fn here() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}

fn trimmed() -> String {
    std::fs::read_to_string(here().join("proto/mimir.proto")).expect("read trimmed mimir.proto")
}

/// edda's canonical proto, beside the dwarves checkout (`<root>/edda`), the same
/// place `proto_drift.rs` reads it from.
fn canonical() -> Option<String> {
    let here = here();
    let root: &Path = here.ancestors().nth(3)?;
    std::fs::read_to_string(root.join("edda/crates/nornir-wire/proto/nornir.proto")).ok()
}

#[test]
fn the_trimmed_proto_declares_every_lens_verb() {
    let rpcs = mimir_rpcs(&trimmed());
    let missing: Vec<&str> = LENS_VERBS.iter().copied().filter(|v| !rpcs.contains_key(*v)).collect();
    assert!(
        missing.is_empty(),
        "dwarves' trimmed mimir.proto lacks {} of the 24 lens verbs: {missing:?} — the forge \
         answers `Unimplemented` for them",
        missing.len()
    );
}

#[test]
fn the_canonical_proto_declares_every_lens_verb_with_the_same_signature() {
    let Some(src) = canonical() else {
        panic!("edda/crates/nornir-wire/proto/nornir.proto is not beside this checkout");
    };
    let up = mimir_rpcs(&src);
    let ours = mimir_rpcs(&trimmed());
    for v in LENS_VERBS {
        let Some(u) = up.get(v) else { panic!("nornir.proto's Mimir has no `{v}`") };
        if let Some(o) = ours.get(v) {
            assert_eq!(o, u, "`{v}` is spelled differently in the trim and in nornir.proto");
        }
    }
}

#[test]
fn the_parser_reads_a_service_block() {
    let src = "service Other { rpc X (A) returns (B); }\nservice Mimir {\n  rpc Villa (LensQuery) returns (JsonResponse); // x\n  rpc Trust    ( TrustQuery )  returns (JsonResponse);\n}\n";
    let r = mimir_rpcs(src);
    assert_eq!(r.len(), 2, "{r:?}");
    assert_eq!(r["Villa"], "(LensQuery) returns (JsonResponse)");
    assert_eq!(r["Trust"], "(TrustQuery) returns (JsonResponse)");
}