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

nordisk/knut

crates/knut-popsim/tests/testmatrix_wiring.rs

//! Wiring proof for knut-popsim's `functional_status` test-matrix surface
//! (the newest feature: `simulate` reports the generator's health into the
//! nornir matrix). INJECT-AND-ASSERT (project LAW): drive the REAL `simulate`
//! path and assert on the REAL emitted row — offline, in-process, no network.
//!
//! Two builds, one test each (single test per binary → no race on the
//! process-global functional buffer that `--features testmatrix` records into):
//!   * default build → the emit is a compiled-out no-op; assert the public
//!     wrapper links and `simulate` still produces a graph.
//!   * `--features testmatrix` → assert `simulate` records exactly one green
//!     `population_generated` row that survives `drain_functional_rows`.

use knut_popsim::{SimParams, simulate};

/// Default build: the `testmatrix` feature is OFF, so `functional_status` is an
/// inlined no-op. Prove it links (call it) and that the real generator path it
/// is wired into still yields a graph.
#[cfg(not(feature = "testmatrix"))]
#[test]
fn functional_status_is_a_linked_noop_and_simulate_runs() {
    // The public wrapper must exist and be a harmless no-op in the release build.
    knut_popsim::functional_status("knut-popsim/simulate", "population_generated", true, "noop");
    // The surface it is wired into still works.
    let g = simulate(SimParams {
        n_persons: 300,
        n_kommun: 8,
        seed: 3,
    });
    assert!(
        !g.nodes.is_empty() && !g.edges.is_empty(),
        "generator produced a graph"
    );
}

/// `--features testmatrix`: `simulate` must record one green
/// `population_generated` functional row that the matrix runner drains.
#[cfg(feature = "testmatrix")]
#[test]
fn simulate_records_a_green_population_generated_row() {
    use nornir_testmatrix::{drain_functional_rows, status};

    // Clear any rows a prior emit may have left, so we assert on OUR run only.
    let _ = drain_functional_rows();

    let g = simulate(SimParams {
        n_persons: 400,
        n_kommun: 10,
        seed: 5,
    });
    assert!(!g.nodes.is_empty(), "sanity: simulate produced nodes");

    let rows = drain_functional_rows();
    let row = rows
        .iter()
        .find(|r| r.suite == "knut-popsim/simulate" && r.test_name == "population_generated")
        .unwrap_or_else(|| panic!("simulate must emit a population_generated row; got {rows:?}"));

    assert!(
        status::is_green(&row.status),
        "a non-empty graph is a GREEN row: {row:?}"
    );
    assert_eq!(
        row.aspect, "functional",
        "recorded under the functional aspect"
    );
    assert_eq!(row.metric, 0.0, "green rows carry metric 0.0");
    assert!(
        row.message.starts_with("nodes="),
        "detail carries the node/edge counts: {:?}",
        row.message
    );
    // The row must reflect THIS graph's real size.
    assert!(
        row.message.contains(&format!("nodes={}", g.nodes.len())),
        "detail node count matches the produced graph: {:?} vs {}",
        row.message,
        g.nodes.len()
    );
}