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

nordisk/facett

facett-core/tests/registry_md_fresh.rs

//! **The registry-markdown DRIFT GUARD.**
//!
//! `.nornir/facett-error-codes-registry.md` (THE LIST the owner reviews) is a
//! GENERATED export of the compiled `errcode::REGISTRY`. A hand-edited copy drifts —
//! it already did once (a hand-written "25 codes" against a compiled 27). This test
//! regenerates the markdown and FAILS if the committed file differs, so the export
//! can never silently drift from the source of truth again.
//!
//! Fix a failure by regenerating, never by hand-editing the markdown:
//! ```sh
//! cargo run -p facett-core --bin errcode_registry_md > .nornir/facett-error-codes-registry.md
//! ```

use std::path::PathBuf;

/// The committed LIST inside the facett repo (`facett/.nornir/...`).
fn nornir_copy() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .expect("facett-core sits in the facett workspace")
        .join(".nornir/facett-error-codes-registry.md")
}

#[test]
fn committed_registry_markdown_matches_the_compiled_registry() {
    let want = facett_core::errcode::registry_markdown();
    let path = nornir_copy();
    let got = std::fs::read_to_string(&path)
        .unwrap_or_else(|e| panic!("cannot read the committed LIST at {}: {e}", path.display()));
    assert_eq!(
        got,
        want,
        "\n\n{} is STALE — regenerate it, do not hand-edit:\n  \
         cargo run -p facett-core --bin errcode_registry_md > .nornir/facett-error-codes-registry.md\n",
        path.display()
    );
}

/// The generated catalog JSON the **Python/Streamlit demo** reads
/// (`py/facett_demo/errcodes.json`).
fn python_copy() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .expect("facett-core sits in the facett workspace")
        .join("py/facett_demo/errcodes.json")
}

/// **LAW #2 — the two demos stay in LOCKSTEP.** The native egui demo reads the
/// compiled `REGISTRY`; the Python/Streamlit demo reads the generated
/// `errcodes.json`. This test FAILS if that json drifts from the registry, so the
/// Streamlit demo can never show a stale or different catalog than the native one.
///
/// Fix by regenerating, never by hand-editing the json:
/// ```sh
/// cargo run -p facett-core --bin errcode_registry_json > py/facett_demo/errcodes.json
/// ```
#[test]
fn python_demo_catalog_json_matches_the_compiled_registry() {
    let want = facett_core::errcode::registry_json();
    let path = python_copy();
    let raw = std::fs::read_to_string(&path).unwrap_or_else(|e| {
        panic!("cannot read the Python demo catalog at {}: {e}", path.display())
    });
    let got: serde_json::Value = serde_json::from_str(&raw)
        .unwrap_or_else(|e| panic!("{} is not valid JSON: {e}", path.display()));
    assert_eq!(
        got,
        want,
        "\n\n{} is STALE — the Python demo would show a different catalog than the \
         native demo (LAW #2). Regenerate, do not hand-edit:\n  \
         cargo run -p facett-core --bin errcode_registry_json > py/facett_demo/errcodes.json\n",
        path.display()
    );
}

/// The generated LIST must actually carry the Phase-2 columns (a regression here
/// means the review artifact lost the reaction table).
#[test]
fn generated_markdown_carries_the_phase2_reaction_matrix() {
    let md = facett_core::errcode::registry_markdown();
    assert!(md.contains("reaction (Phase 2)"), "the reaction column is present");
    assert!(md.contains("The reaction MATRIX (Phase 2 test table)"), "the matrix table is present");
    assert!(md.contains("consumer mounts"), "the consumer-mount column is present");
    // Every mounted pair in the matrix appears in the markdown.
    for (mounted, _, _, _) in facett_core::errcode::reaction_matrix() {
        assert!(md.contains(&mounted), "matrix row {mounted} missing from the LIST");
    }
    // Every registered code appears.
    for e in facett_core::errcode::registry() {
        assert!(md.contains(e.code), "code {} missing from the LIST", e.code);
    }
}