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

nordisk/gunnar-ui

xtask/tests/trunk_pin.rs

//! **The console's trunk pin is the lock's wasm-bindgen** (GUN-FIELD-TEST, 2026-10-31).
//!
//! `crates/gunnar-ui-web/Trunk.toml` pins `[tools] wasm_bindgen`, and trunk
//! runs exactly that wasm-bindgen CLI over the wasm the `wasm-bindgen` crate
//! in `Cargo.lock` produced. Its own comment says the two must match exactly.
//! MEASURED 2026-10-31 on gunnar-ui main 567e230: the pin said 0.2.128 and
//! the lock 0.2.129. The build still ran (the two share a schema), but the
//! 0.2.128 CLI wrote a different `_bg.wasm` (another sha384) than the lock's
//! 0.2.129 does, so a lane that set `TRUNK_TOOLS_WASM_BINDGEN` to the lock's
//! version shipped other bytes than one that did not, and the next schema
//! change would stop the build. A lock follow that moves wasm-bindgen now
//! turns this test red by name.

use std::path::{Path, PathBuf};

fn root() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .expect("xtask sits in the repository root")
        .to_path_buf()
}

/// The quoted value after `key = ` on the first line that starts with it.
fn value_after(text: &str, key: &str) -> Option<String> {
    text.lines()
        .map(str::trim)
        .find_map(|l| l.strip_prefix(key))
        .map(|rest| rest.trim_start().trim_start_matches('=').trim().trim_matches('"').to_owned())
}

/// The version of the package `name` in a Cargo.lock (the line after its `name =`).
fn locked(lock: &str, name: &str) -> Vec<String> {
    let want = format!("name = \"{name}\"");
    let lines: Vec<&str> = lock.lines().collect();
    lines
        .iter()
        .enumerate()
        .filter(|(_, l)| l.trim() == want)
        .filter_map(|(i, _)| lines.get(i + 1))
        .filter_map(|l| value_after(l, "version"))
        .collect()
}

#[test]
fn the_trunk_pin_is_the_wasm_bindgen_in_the_lock() {
    let trunk = std::fs::read_to_string(root().join("crates/gunnar-ui-web/Trunk.toml")).expect("Trunk.toml");
    let lock = std::fs::read_to_string(root().join("Cargo.lock")).expect("Cargo.lock");
    let pin = value_after(&trunk, "wasm_bindgen").expect("Trunk.toml pins [tools] wasm_bindgen");
    let versions = locked(&lock, "wasm-bindgen");
    assert_eq!(versions.len(), 1, "one wasm-bindgen in the lock: {versions:?}");
    assert_eq!(
        pin, versions[0],
        "Trunk.toml pins wasm_bindgen {pin} but Cargo.lock has wasm-bindgen {}: \
         trunk runs another wasm-bindgen CLI than the crate; set the pin to the lock's version",
        versions[0]
    );
}

/// The scanner can go red: a lock with another version is told apart.
#[test]
fn the_scanner_reads_the_version_it_is_shown() {
    let lock = "[[package]]\nname = \"wasm-bindgen\"\nversion = \"9.9.9\"\n";
    assert_eq!(locked(lock, "wasm-bindgen"), vec!["9.9.9".to_owned()]);
    assert_eq!(value_after("[tools]\nwasm_bindgen = \"1.2.3\"\n", "wasm_bindgen").as_deref(), Some("1.2.3"));
}