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

nordisk/edda

crates/nornir-pages/src/selftest.rs

//! # Functional-status emit bridge — `nornir-pages` → test matrix
//!
//! Pushes a *functional* row per render surface into the [`nornir_testmatrix`]
//! matrix, so `nornir test --features testmatrix` records whether the generated
//! `index.html` ACTUALLY carries the cache-bust + every interpolated knob — the
//! served-artifact-is-faithful verdict — without eyeballing.
//!
//! ## Release strips everything
//! The `testmatrix` feature gates the whole bridge: OFF (the default) [`emit`] is
//! an `#[inline]` no-op and `nornir-testmatrix` is not even linked.

/// The aspect tag every self-emitted row carries.
pub const ASPECT_FUNCTIONAL: &str = "functional";

/// Push ONE functional-status row for a single real check (feature ON).
#[cfg(feature = "testmatrix")]
#[inline]
pub fn emit(component: &str, check: &str, ok: bool, detail: &str) {
    nornir_testmatrix::functional_status(component, check, ok, detail);
}

/// No-op stub when the `testmatrix` feature is OFF (default / release builds).
#[cfg(not(feature = "testmatrix"))]
#[inline]
pub fn emit(_component: &str, _check: &str, _ok: bool, _detail: &str) {}

// ─── inject-and-assert wiring test (feature ON only) ─────────────────────────
#[cfg(all(test, feature = "testmatrix"))]
mod wiring {
    use crate::{render_index_html, PagesConfig};
    use std::path::PathBuf;

    fn cfg() -> PagesConfig {
        PagesConfig {
            demo_crate: "nornir-demo",
            canvas_id: "demo_canvas",
            state_export: "__nornir_state",
            title: "nornir — live web demo",
            description: "d",
            pages_url: "https://pages.example/nornir",
            pages_host: "pages.example",
            pages_branch: "pages",
            remote_env: "NORNIR_PAGES_REMOTE",
            commit_label: "nornir live web demo",
            extra_build_args: &[],
            workspace_root: PathBuf::from("/ws"),
        }
    }

    /// Render the real `index.html`, emit an honest row per checked property, then
    /// drain the process-global functional buffer and assert the rows landed.
    #[test]
    fn emits_honest_rows_for_index_render() {
        let html = render_index_html(&cfg(), "abc1234");

        let js_bust = html.contains("./nornir-demo.js?v=abc1234");
        crate::selftest::emit("nornir-pages", "js_cache_bust", js_bust, "?v=abc1234 on js");

        let wasm_bust = html.contains("./nornir-demo_bg.wasm?v=abc1234");
        crate::selftest::emit("nornir-pages", "wasm_cache_bust", wasm_bust, "?v=abc1234 on wasm");

        let canvas = html.contains(r#"<canvas id="demo_canvas">"#);
        crate::selftest::emit("nornir-pages", "canvas_knob", canvas, "canvas=demo_canvas");

        let state = html.contains("window.__nornir_state = __nornir_state;");
        crate::selftest::emit("nornir-pages", "state_knob", state, "state=__nornir_state");

        // RED surface: a stale-serving template (no ?v=) must be rejected.
        crate::selftest::emit(
            "nornir-pages",
            "no_bare_wasm_import",
            !html.contains("'./nornir-demo_bg.wasm'"),
            "no un-busted wasm URL",
        );

        let rows = nornir_testmatrix::drain_functional_rows();
        let has = |t: &str| {
            rows.iter()
                .any(|r| r.suite == "nornir-pages" && r.test_name == t && r.status == "pass")
        };
        assert!(has("js_cache_bust"), "rows={rows:?}");
        assert!(has("wasm_cache_bust"), "rows={rows:?}");
        assert!(has("canvas_knob"), "rows={rows:?}");
        assert!(has("state_knob"), "rows={rows:?}");
        assert!(has("no_bare_wasm_import"), "rows={rows:?}");
        assert!(rows.iter().all(|r| r.aspect == "functional"), "rows={rows:?}");
    }
}