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

nordisk/dwarves

dwarves-gui/tests/variant_discriminator.rs

//! **T7 — `dwarves-ui` and `dwarves-ui-gpu` were ONE ARTIFACT WITH TWO NAMES.**
//!
//! MEASURED on t14s 2026-08-27: `/usr/local/bin/dwarves-ui` and
//! `/usr/local/bin/dwarves-ui-gpu` were byte-identical — both sha256
//! `6eb9ec98717b270e…`, both equal to that box's `~/cargo-target/release/dwarves-ui-gpu`,
//! and that target tree contained **no `dwarves-ui` at all**. The GPU build was installed
//! under both names, and nothing either file printed could reveal it.
//!
//! # What this file asserts, and what it deliberately does not
//!
//! It asserts the RUNTIME DISCRIMINATOR: that the answer to "which artifact are you"
//! comes from `CARGO_BIN_NAME` and not from a cargo feature. The declaration side is
//! already guarded by
//! `crates/dwarves/tests/build_variants.rs::the_gpu_and_cpu_ui_atoms_differ_only_in_the_renderer`
//! — and that guard says itself that it can only see the declaration.
//!
//! It does NOT try to build both binaries and compare them. That is structurally
//! impossible inside one cargo invocation, and the reason is the finding:
//!
//! > MEASURED on oden 2026-08-27, Xvfb :96, a real window, both bins from ONE
//! > `cargo build -p dwarves-gui --bin dwarves-ui --bin dwarves-ui-gpu --features gpu`:
//! > the PLAIN `dwarves-ui` (sha256 `186d2caf084a93e8…`) printed
//! > `dwarves-ui-gpu: instanced cloud renderer installed (UNIVERSE star-field on the GPU)`.
//!
//! Feature resolution is per-PACKAGE, so asking cargo for both bins at once gives the CPU
//! half the GPU feature. A test that built both and compared them would therefore be
//! comparing two GPU builds. What CAN be asserted here — and is — is that the artifact's
//! own identity survives that, and that the GPU lane is armed by the NAME.

/// ★ The stamp is a function of the VARIANT, and the variants do not collide.
///
/// Every `[[bin]]` off `dwarves-gui/src/main.rs` must print a different stamp. Before
/// 2026-08-27 all of them printed `dwarves v0.1.4 (<hash>)`, which is why two
/// byte-identical files on t14s could each be asked what they were and answer the same.
#[test]
fn every_declared_variant_prints_a_distinct_stamp() {
    // The three `[[bin]]`s declared in dwarves-gui/Cargo.toml off ONE src/main.rs.
    const BINS: [&str; 3] = ["dwarves-ui", "dwarves-ui-gpu", "dwarves-solo"];
    let stamps: Vec<String> = BINS
        .iter()
        .map(|b| dwarves_gui::version_line_for(b))
        .collect();

    for (i, b) in BINS.iter().enumerate() {
        assert!(
            stamps[i].starts_with(&format!("{b} v")),
            "`{b}` must lead its stamp with its own name, got {:?}",
            stamps[i]
        );
        // …and carry the build, or the stamp cannot answer "which build is running".
        assert!(
            stamps[i].contains(env!("CARGO_PKG_VERSION")),
            "{:?} must carry the version",
            stamps[i]
        );
    }
    for i in 0..BINS.len() {
        for j in (i + 1)..BINS.len() {
            assert_ne!(
                stamps[i], stamps[j],
                "`{}` and `{}` must not print the same stamp — that identity IS the t14s \
                 duplicate",
                BINS[i], BINS[j]
            );
        }
    }

    // The build half is shared, the variant half is not: that split is what makes the
    // stamp comparable across artifacts of one build.
    let stamp_tail = dwarves_gui::build_stamp();
    for s in &stamps {
        assert!(
            s.ends_with(&stamp_tail),
            "{s:?} must end with {stamp_tail:?}"
        );
    }
}

/// ★ **The GPU lane is armed by the NAME, and the rule really discriminates.**
///
/// The red this replaces is not hypothetical: it was measured on the shipped binary (see
/// the module docs). A rule that answered `true` for everything, or that read the feature,
/// would pass a naive "the gpu build arms the gpu lane" assertion — so both directions are
/// asserted here, including for a name that merely CONTAINS `gpu` without being the
/// variant.
#[test]
fn the_gpu_lane_is_armed_by_the_bin_name_and_nothing_else() {
    use dwarves_gui::gpu_lane_armed_for as armed;

    assert!(armed("dwarves-ui-gpu"), "the -gpu artifact owns the lane");
    assert!(!armed("dwarves-ui"), "the CPU half must NOT arm it");
    assert!(!armed("dwarves-solo"), "solo is not a renderer variant");

    // The rule is a SUFFIX, not a substring: a name that merely mentions gpu is not the
    // gpu variant, and a rule written with `contains` would silently arm these.
    assert!(
        !armed("dwarves-gpu-ui"),
        "`contains` would wrongly arm this"
    );
    assert!(!armed("gpu"), "bare `gpu` is not a variant name");
    assert!(!armed(""), "and neither is nothing");
    assert!(armed("korp-server-gpu"), "the same rule reads korp's pair");

    // ── the assertion that would catch a regression to `cfg!(feature = "gpu")` ──
    // This test binary is compiled with whatever features the invocation asked for. If
    // the rule ever consulted a feature instead of its argument, the two answers below
    // could not stay different, because both calls run in ONE compilation.
    assert_ne!(
        armed("dwarves-ui"),
        armed("dwarves-ui-gpu"),
        "the answer must depend on the ARGUMENT. Two different answers inside one \
         compilation is exactly what a `cfg!(feature = ...)` cannot produce — and that \
         impossibility is the measured defect: with `--features gpu` on the package, \
         `cfg!(feature = \"gpu\")` is TRUE in dwarves-ui too."
    );
}

/// ★ The REAL binary prints its own name.
///
/// `CARGO_BIN_EXE_dwarves-ui` is the shipped artifact. Running it proves the wiring in
/// `main.rs` (`env!("CARGO_BIN_NAME")` → `version_line_for`) and not just the library
/// function — a library that formats perfectly and a `main` that hardcodes `"dwarves-ui"`
/// look identical from inside the library.
#[test]
fn the_real_binary_names_itself_in_its_version_line() {
    let out = std::process::Command::new(env!("CARGO_BIN_EXE_dwarves-ui"))
        .arg("--version")
        .env_remove("DISPLAY")
        .env_remove("WAYLAND_DISPLAY")
        .output()
        .expect("run dwarves-ui --version");
    assert!(out.status.success(), "`--version` must exit 0");
    let line = String::from_utf8_lossy(&out.stdout).trim().to_string();
    assert_eq!(
        line,
        dwarves_gui::version_line_for("dwarves-ui"),
        "the binary's stamp must be the library's stamp for ITS OWN name"
    );
    assert!(
        line.starts_with("dwarves-ui v"),
        "and it must lead with the artifact name: {line:?}"
    );
    // The old, blind shape, named so a regression is recognisable: `dwarves v…` was the
    // string every bin printed until 2026-08-27.
    assert!(
        !line.starts_with("dwarves v"),
        "regression: the stamp is back to the family name, which cannot tell two \
         artifacts apart: {line:?}"
    );
}