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

nordisk/edda

crates/nornir-cli-outcome/src/selftest.rs

//! # Functional-status emit bridge — `nornir-cli-outcome` → test matrix
//!
//! The single internal call site this crate uses to push a *functional* row
//! into the [`nornir_testmatrix`] matrix, so `nornir test --features testmatrix`
//! records whether `nornir-cli-outcome`'s real surfaces (the `CommandOutcome`
//! sannr contract) ACTUALLY behave — without eyeballing anything.
//!
//! ## Release strips everything
//! The `testmatrix` cargo feature gates the whole bridge: with it OFF (the
//! release default) [`emit`] is an `#[inline]` no-op and `nornir-testmatrix` is
//! not even linked. Lit ONLY under `cargo … --features testmatrix`.

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

/// Push ONE functional-status row for a single real check (feature ON).
///
/// * `component` — the unit under test (becomes the row's `suite`).
/// * `check` — the specific assertion's name (becomes the row's `test_name`).
/// * `ok` — the **real** verdict.
/// * `detail` — the **measured** value.
#[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 (release builds). Inlined and
/// empty so the call — and, with the wrap-the-call-site pattern, the detail
/// string-building — is stripped entirely.
#[cfg(not(feature = "testmatrix"))]
#[inline]
pub fn emit(_component: &str, _check: &str, _ok: bool, _detail: &str) {}

/// `assert!`-with-emit: assert on the real return value / object state AND record
/// the verdict as a matrix row. The `assert!` is the test's actual gate; the emit
/// is the matrix observation.
#[macro_export]
macro_rules! assert_emit {
    ($component:expr, $check:expr, $ok:expr, $($detail:tt)+) => {{
        let __ok: bool = $ok;
        let __detail = ::std::format!($($detail)+);
        $crate::selftest::emit($component, $check, __ok, &__detail);
        ::std::assert!(__ok, "{}::{} — {}", $component, $check, __detail);
    }};
}

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

    /// Exercise `CommandOutcome`'s real sannr contract, emit an honest row per
    /// surface, then drain the process-global functional buffer and assert the
    /// rows landed — proving the emit bridge is wired end to end.
    #[test]
    fn emits_honest_rows_for_command_outcome_surfaces() {
        // Real green surface: a populated ok outcome is sannr.
        let good = CommandOutcome::ok(
            "selftest ok",
            serde_json::json!({ "n": 1 }),
            "one item",
        );
        assert_emit!(
            "nornir-cli-outcome",
            "ok_outcome_is_sannr",
            good.is_sannr(),
            "command={} ok={}",
            good.command,
            good.ok
        );

        // Real RED surfaces (RAGNARÖK): a fail, and an ok-but-empty, are NOT sannr.
        let failed = CommandOutcome::fail("selftest fail", "nothing found");
        crate::selftest::emit(
            "nornir-cli-outcome",
            "fail_outcome_is_not_sannr",
            !failed.is_sannr(),
            &format!("is_sannr={}", failed.is_sannr()),
        );
        let empty = CommandOutcome::ok("selftest empty", serde_json::json!({}), "");
        crate::selftest::emit(
            "nornir-cli-outcome",
            "empty_data_outcome_is_not_sannr",
            !empty.is_sannr(),
            &format!("is_sannr={}", empty.is_sannr()),
        );

        // The process-exit contract: fail ⇒ non-zero, ok ⇒ 0, and the strict
        // sannr gate turns a falsely-green (ok-but-empty) into non-zero.
        assert_emit!(
            "nornir-cli-outcome",
            "fail_outcome_exits_nonzero",
            failed.exit_code() == 1,
            "exit_code={}",
            failed.exit_code()
        );
        assert_emit!(
            "nornir-cli-outcome",
            "empty_outcome_strict_exits_nonzero",
            empty.exit_code() == 0 && empty.sannr_exit_code() == 1,
            "exit_code={} sannr_exit_code={}",
            empty.exit_code(),
            empty.sannr_exit_code()
        );

        let rows = nornir_testmatrix::drain_functional_rows();
        let has = |t: &str, pass: bool| {
            rows.iter().any(|r| {
                r.suite == "nornir-cli-outcome"
                    && r.test_name == t
                    && (r.status == "pass") == pass
            })
        };
        assert!(has("ok_outcome_is_sannr", true), "rows={rows:?}");
        assert!(has("fail_outcome_is_not_sannr", true), "rows={rows:?}");
        assert!(has("empty_data_outcome_is_not_sannr", true), "rows={rows:?}");
        assert!(has("fail_outcome_exits_nonzero", true), "rows={rows:?}");
        assert!(has("empty_outcome_strict_exits_nonzero", true), "rows={rows:?}");
        assert!(rows.iter().all(|r| r.aspect == "functional"), "rows={rows:?}");
    }
}