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

nordisk/holger

mannequin/tests/facet_reaction_matrix.rs

//! **holger's FACETT REACTION MATRIX** — Phase-2 contract: for every
//! `(facett code × holger mount)` pair, holger MUST react, and the test asserts the
//! REACTION and the MOUNTED CODE — never a matched string.
//!
//! TABLE-DRIVEN from ONE data table (facett's shared
//! `facett_core::errcode::reaction_matrix()`, sliced to holger's mounts), so adding a
//! facett code — or a holger mount — adds rows automatically.

use facett_core::errcode::{self, FacetError, FacetRecorder, Reaction};
use mannequin::facet_react::{self, MANNEQUIN, MOUNTS};

fn raise(code: &str) -> FacetError {
    let e = errcode::lookup(code).unwrap_or_else(|| panic!("{code} is registered"));
    FacetError::new(e.component, e.n, e.code, e.message)
}

/// Drive ONE matrix row through the full check set; returns the assertion count.
fn check_row(mounted: &str, base: &'static str, mount: &str, expected: Reaction) -> usize {
    // 1. resolvable
    assert_eq!(
        facet_react::reaction_of(mounted),
        Some(expected),
        "{mounted}: must resolve to its canonical reaction"
    );
    // 2. mount-independent — the reaction is a property of the CODE
    assert_eq!(
        errcode::reaction_for(base),
        Some(expected),
        "{base}: same reaction unmounted"
    );
    // 3. round-trip
    assert_eq!(errcode::base_code(mounted), base);
    assert_eq!(errcode::mount_of(mounted), Some(mount));
    // 4. holger reacts with exactly the canonical reaction (hermetic recorder)
    let mut rec = FacetRecorder::new();
    let m = MOUNTS
        .iter()
        .find(|m| m.prefix() == mount)
        .unwrap_or_else(|| panic!("{mount} is a declared holger mount"));
    let err = raise(base);
    assert_eq!(
        facet_react::react(&mut rec, *m, &err),
        expected,
        "{mounted}: holger must take the canonical reaction"
    );
    // 5. the MOUNTED code was recorded
    assert!(
        rec.saw(mounted),
        "{mounted}: the MOUNTED code must be recorded"
    );
    // 6. base code + human message survive
    let e = &rec.entries()[0];
    assert_eq!(
        e.code, base,
        "{mounted}: facett's frozen base code is untouched"
    );
    assert!(
        !e.message.is_empty(),
        "{mounted}: the visible message is still carried"
    );
    // 7. the pane's PUBLISHED data drives the reaction
    let mut rec2 = FacetRecorder::new();
    let state = serde_json::json!({ "error_code": base });
    assert_eq!(
        facet_react::react_to_state(&mut rec2, *m, &state),
        Some(expected),
        "{mounted}: reacting off state_json.error_code yields the same reaction"
    );
    8
}

fn run_kind(want: Reaction) -> (usize, usize) {
    let (mut rows, mut asserts) = (0, 0);
    for (mounted, base, mount, reaction) in facet_react::matrix() {
        if reaction != want {
            continue;
        }
        asserts += check_row(&mounted, base, mount, reaction);
        rows += 1;
    }
    (rows, asserts)
}

#[test]
fn empty_state_codes_show_an_honest_empty_state() {
    let (rows, asserts) = run_kind(Reaction::EmptyState);
    assert!(rows > 0, "holger mounts a component that can be empty");
    eprintln!("holger empty_state: {rows} rows, {asserts} assertions");
}

#[test]
fn diagnose_codes_are_recorded_as_faults() {
    let (rows, asserts) = run_kind(Reaction::Diagnose);
    assert!(
        rows > 0,
        "holger mounts a component that can report a fault"
    );
    eprintln!("holger diagnose: {rows} rows, {asserts} assertions");
}

/// THE COVERAGE REPORT — every holger row, checked, counted, printed.
#[test]
fn matrix_coverage_report() {
    let matrix = facet_react::matrix();
    assert!(
        !matrix.is_empty(),
        "holger must implement a non-empty reaction matrix"
    );
    let mut asserts = 0;
    let mut seen = std::collections::HashSet::new();
    for (mounted, base, mount, reaction) in &matrix {
        assert!(
            seen.insert(mounted.clone()),
            "duplicate matrix row {mounted}"
        );
        asserts += check_row(mounted, base, mount, *reaction);
    }
    eprintln!(
        "\n══ holger FACETT REACTION MATRIX ══\n  {} (code × mount) rows · {asserts} assertions\n",
        matrix.len()
    );
}

/// **THE ANTI-FICTION GUARD** (the dwarves lesson). Every mount holger DECLARES must
/// be real: its `site` file must exist and actually reference the facett component
/// whose codes it claims.
#[test]
fn every_declared_mount_is_actually_wired_in_its_declared_file() {
    // CARGO_MANIFEST_DIR is <repo>/mannequin; the sites are repo-relative.
    let repo = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .expect("mannequin sits in the holger repo");
    for m in MOUNTS {
        let path = repo.join(m.site);
        assert!(
            path.exists(),
            "{}: declared site {} does not exist",
            m.prefix(),
            m.site
        );
        let src = std::fs::read_to_string(&path).expect("readable mount site");
        let claimed: std::collections::HashSet<&str> = facet_react::matrix()
            .iter()
            .filter(|(_, _, mount, _)| *mount == m.prefix())
            .map(|(_, base, _, _)| errcode::lookup(base).expect("registered").component)
            .collect();
        assert!(
            !claimed.is_empty(),
            "{}: declares no components",
            m.prefix()
        );
        for comp in claimed {
            let krate = format!("facett_{comp}");
            assert!(
                src.contains(&krate),
                "{}: claims facet-{comp}-* codes but {} never references {krate} — \
                 a DECLARED mount must be a REAL mount, not fiction",
                m.prefix(),
                m.site
            );
        }
    }
}

/// No silent gaps: the registry's consumer column and holger's matrix must agree.
#[test]
fn the_registry_and_holgers_matrix_agree() {
    let mounted: std::collections::HashSet<&str> = facet_react::matrix()
        .into_iter()
        .map(|(_, base, _, _)| base)
        .collect();
    for e in errcode::registry() {
        assert_eq!(
            e.consumer.contains("holger/"),
            mounted.contains(e.code),
            "{}: the registry consumer column and holger's implemented matrix disagree",
            e.code
        );
    }
    eprintln!(
        "  holger implements {} of {} facett codes",
        mounted.len(),
        errcode::registry().len()
    );
}

/// holger publishes the Phase-2 surface as data.
#[test]
fn holger_publishes_the_reaction_matrix_as_state() {
    let mut rec = FacetRecorder::new();
    facet_react::react(&mut rec, MANNEQUIN, &raise("facet-syschart-1"));
    let s = facet_react::state_json(&rec);
    assert_eq!(s["scheme"], "holger/<surface>:facet-<component>-<n>");
    assert_eq!(
        s["pairs"].as_u64().unwrap() as usize,
        facet_react::matrix().len()
    );
    assert_eq!(
        s["recorded"]["entries"][0]["mounted_code"],
        "holger/mannequin:facet-syschart-1"
    );
    for r in s["matrix"].as_array().expect("rows") {
        let mounted = r["mounted_code"].as_str().unwrap();
        assert_eq!(
            facet_react::reaction_of(mounted).map(|x| x.token()),
            r["reaction"].as_str()
        );
    }
}

/// An unregistered facett code is DIAGNOSED, never ignored.
#[test]
fn an_unregistered_code_is_diagnosed_not_ignored() {
    let mut rec = FacetRecorder::new();
    let bogus = FacetError::new("nope", 99, "facet-nope-99", "invented");
    assert_eq!(
        facet_react::react(&mut rec, MANNEQUIN, &bogus),
        Reaction::Diagnose
    );
    assert!(rec.saw("holger/mannequin:facet-nope-99"));
}