wasmrt 4.0.0

wasmrt — a fast, small WebAssembly runtime CLI (run/assemble/validate/pin).
//! **The exit status is the verdict** — `interop.md` §2.3: success → 0, host-side failure (bad
//! arguments, unreadable file, invalid module) → non-zero, AGREED in both copies of the contract.
//!
//! Measured 2026-09-19 during `coordinate`, running both runtimes: wasmrt broke the agreed row in
//! three places, and wazmrt exited 1 in all three:
//!
//! * `wasmrt <file>` summarized an **invalid** module and exited **0** — so `wasmrt m.wasm && …`
//!   passed every invalid module, and this project's own `.wat` corpus gate was fooled by it twice;
//! * `wasmrt` with **no arguments** printed the version and exited **0**;
//! * `wasmrt wast` exited **0** on a failed assertion, an unparseable script and an unreadable
//!   file — no CI job could gate on it.
//!
//! Each test below keys on the STATUS, because the status is what a script branches on.

use std::process::Command;

fn write_temp(name: &str, bytes: &[u8]) -> std::path::PathBuf {
    // A folder per process: two test runs on one host (two sessions, a gate beside a release) must
    // not truncate each other's fixtures (review round 3; the file name itself is unchanged).
    let d = std::path::Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("wasmrt_tests_{}", std::process::id()));
    std::fs::create_dir_all(&d).expect("scratch dir");
    let p = d.join(name);
    std::fs::write(&p, bytes).expect("write fixture");
    p
}

fn code(args: &[&str]) -> Option<i32> {
    Command::new(env!("CARGO_BIN_EXE_wasmrt"))
        .args(args)
        .output()
        .expect("spawn wasmrt")
        .status
        .code()
}

const VALID: &[u8] = br#"(module (func (export "f") (result i32) (i32.const 7)))"#;
const INVALID: &[u8] = br#"(module (func (export "f") (result i32) (i64.const 7)))"#;

#[test]
fn summarize_exits_zero_on_a_valid_module() {
    let p = write_temp("wasmrt_exit_valid.wat", VALID);
    assert_eq!(code(&[p.to_str().unwrap()]), Some(0));
}

/// ⚠️⚠️ The regression this file exists for.
#[test]
fn summarize_exits_nonzero_on_an_invalid_module() {
    let p = write_temp("wasmrt_exit_invalid.wat", INVALID);
    assert_eq!(code(&[p.to_str().unwrap()]), Some(1), "an invalid module must not exit 0");
    // …and for a BINARY, which cannot fail earlier at assembly: the text path above could pass
    // for the wrong reason if the assembler ever started refusing ill-typed text.
    let bin = wasmrt_core::wat::assemble(INVALID).expect("the assembler does not type-check");
    let p = write_temp("wasmrt_exit_invalid.wasm", &bin);
    assert_eq!(code(&[p.to_str().unwrap()]), Some(1));
}

/// 🔒 `interop.md` §2.5 (owner, 2026-09-19 — Z3): the summary must never describe a module as valid
/// unless it validated. The exit status and every validity claim in the output agree.
#[test]
fn the_summary_never_calls_an_invalid_module_valid() {
    let p = write_temp("wasmrt_exit_z3.wat", INVALID);
    let out = Command::new(env!("CARGO_BIN_EXE_wasmrt")).arg(&p).output().expect("spawn");
    let text = String::from_utf8_lossy(&out.stdout);
    assert_eq!(out.status.code(), Some(1));
    assert!(text.contains("validation FAILED"), "the verdict must be stated; got: {text}");
    for line in text.lines() {
        let l = line.to_ascii_lowercase();
        let claims_valid = l.contains("validation ok") || (l.contains("valid") && !l.contains("invalid") && !l.contains("validation"));
        assert!(!claims_valid, "a line claims validity for an invalid module: {line:?}");
    }
    // …and a VALID module says so, so the check above is not passing on silence.
    let ok = write_temp("wasmrt_exit_z3_ok.wat", VALID);
    let out = Command::new(env!("CARGO_BIN_EXE_wasmrt")).arg(&ok).output().expect("spawn");
    assert!(String::from_utf8_lossy(&out.stdout).contains("validation OK"));
}

#[test]
fn no_arguments_is_a_usage_error() {
    assert_eq!(code(&[]), Some(1));
    // The explicit spellings are still requests, and still succeed.
    assert_eq!(code(&["--version"]), Some(0));
    assert_eq!(code(&["--help"]), Some(0));
}

#[test]
fn wast_exits_zero_only_when_every_assertion_passed() {
    let pass = write_temp(
        "wasmrt_exit_pass.wast",
        br#"(module (func (export "f") (result i32) (i32.const 1)))
            (assert_return (invoke "f") (i32.const 1))"#,
    );
    let fail = write_temp(
        "wasmrt_exit_fail.wast",
        br#"(module (func (export "f") (result i32) (i32.const 1)))
            (assert_return (invoke "f") (i32.const 2))"#,
    );
    let unparseable = write_temp("wasmrt_exit_unparseable.wast", b"(module (func");
    let missing = std::path::Path::new(env!("CARGO_TARGET_TMPDIR")).join("wasmrt_exit_no_such_file.wast");
    let _ = std::fs::remove_file(&missing);

    assert_eq!(code(&["wast", pass.to_str().unwrap()]), Some(0));
    assert_eq!(code(&["wast", fail.to_str().unwrap()]), Some(1), "a failed assertion");
    assert_eq!(code(&["wast", unparseable.to_str().unwrap()]), Some(1), "an unparseable script");
    assert_eq!(code(&["wast", missing.to_str().unwrap()]), Some(1), "an unreadable file");
    // One bad file fails the whole run, even beside a good one.
    assert_eq!(
        code(&["wast", pass.to_str().unwrap(), fail.to_str().unwrap()]),
        Some(1)
    );
}