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

nordisk/dwarves

dwarves-gui/tests/robot_lens_door.rs

//! **Every lens the Lenses tab asks still reaches the same answer** now that the pane
//! asks through the one lens door (`dwarves::lens`) instead of its own 24-arm switch.
//!
//! A mock `Mimir` serves a distinct sentinel body per lens; for every lens in
//! [`THIN_LENSES`] the pane's `ask_once` and `ask_until_answered` must come back with
//! exactly that lens's body (so each name still reaches its own RPC), and an unknown
//! name is still refused by the pane.

use dwarves_gui::mimir_lens::{RemoteLensSource, THIN_LENSES};
use nornir_warehouse_embed::testkit::{spawn_mock_mimir, MockMimir};
use serde_json::json;

fn sentinel(lens: &str) -> String {
    json!({ "kind": lens, "lens_meta": { "state": "complete", "lens": lens }, "worst": [], "__door__": lens }).to_string()
}

fn mock() -> MockMimir {
    MockMimir {
        generic_cost: sentinel("generic_cost"),
        zombie_features: sentinel("zombie_features"),
        change_risk: sentinel("change_risk"),
        weak_tests: sentinel("weak_tests"),
        bench_gaps: sentinel("bench_gaps"),
        similar_code: sentinel("similar_code"),
        semantic_clones: sentinel("semantic_clones"),
        villa: sentinel("villa"),
        churn: sentinel("churn"),
        anomaly: sentinel("anomaly"),
        world_impact: sentinel("world_impact"),
        binary_drift: sentinel("binary_drift"),
        data_reach: sentinel("data_reach"),
        trust: sentinel("trust"),
        name_collision: sentinel("name_collision"),
        symbol_identity: sentinel("symbol_identity"),
        undoc_api: sentinel("undoc_api"),
        reachable_vuln: sentinel("reachable_vuln"),
        subclone: sentinel("subclone"),
        semantic_twins: sentinel("semantic_twins"),
        group_twins: sentinel("group_twins"),
        reimplements_shared: sentinel("reimplements_shared"),
        ..Default::default()
    }
}

#[test]
fn every_lens_name_reaches_its_own_answer_through_the_door() {
    let srv = spawn_mock_mimir(mock());
    let src = RemoteLensSource::new(srv.endpoint.clone(), "", "nordisk");
    assert_eq!(THIN_LENSES.len(), 22, "the roster this test covers");
    for (lens, _) in THIN_LENSES {
        let want: serde_json::Value = serde_json::from_str(&sentinel(lens)).unwrap();
        assert_eq!(src.ask_once(lens), want, "{lens}: ask_once");
        let mut got = Vec::new();
        src.ask_until_answered(lens, |u| {
            got.push(u);
            true
        });
        assert_eq!(got.len(), 1, "{lens}: one answer, it was not queued");
        assert!(got[0].last, "{lens}");
        assert_eq!(got[0].answer, want, "{lens}: ask_until_answered");
    }
}

#[test]
fn an_unknown_lens_is_still_refused_by_the_pane() {
    let srv = spawn_mock_mimir(mock());
    let src = RemoteLensSource::new(srv.endpoint.clone(), "", "nordisk");
    let v = src.ask_once("no_such_lens");
    assert_eq!(v["lens_meta"]["state"], "error", "{v}");
    assert!(v["evidence_note"].as_str().unwrap().contains("not a lens this pane asks"), "{v}");
}