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

nordisk/facett

facet-wrapped/tests/gpu_lane_is_observable.rs

//! **Row 3, second half — compiling the GPU lane in does NOT put anything on it.**
//!
//! Turning `facett-map3d/wgpu` on (row 3's first half) changes what the binary
//! contains. It changes nothing about what runs. `Map3D` starts with
//! `use_gpu: false` and a `CpuPainterBackend`; the GPU lane is selected only by
//! `with_gpu(true)`, which is `#[cfg(feature = "wgpu")]` — so before this row the call
//! did not even exist, and after the feature flip the adapter still did not make it.
//!
//! MEASURED on the bundle with all three features already ON, before this half:
//!
//! ```text
//! "gpu_lane_selected": false,
//! "backend": "cpu-painter",
//! "lane_flag": "pending",
//! "used_gpu": null
//! ```
//!
//! A build that compiles a GPU renderer it never selects is the most expensive kind of
//! green: every feature flag says yes, the page reports `webgpu`, and every triangle is
//! still projected, culled and lit in software.
//!
//! ## Two corrections to how this row was specified
//!
//! 1. The brief said to assert `state_json.renderer == "wgpu"`. **There is no
//!    `renderer` field.** map3d publishes `lane_flag` (`gpu`/`cpu`/`pending`, from
//!    `facett_core::render::lane::LaneFlag::token()`), plus `used_gpu`,
//!    `gpu_lane_selected` and `backend`. Asserting on `renderer` would have compared
//!    `null` to `"wgpu"` and failed forever, on a correct build — so the row would
//!    have been "done" only by deleting its own check.
//! 2. `facet_backend()` and `facet_canvas_api()` cannot stand in for it. Both answered
//!    `"webgpu"` before this row on a build with no GPU renderer compiled in at all,
//!    and both answer `"webgpu"` after. They describe the SURFACE, not the renderer
//!    painting onto it — the identity value in LAW 2's sense.
//!
//! The observability itself is `facett-map3d`'s and is already complete; this file
//! asserts that the wasm bundle's adapter actually SELECTS the lane it now ships.

use serde_json::json;

fn osm3d_payload() -> serde_json::Value {
    json!({
        "title": "lane probe",
        "ways": [
            {"osm_id": 1,
             "coords": [17.70, 59.60, 17.72, 59.60, 17.72, 59.62, 17.70, 59.62, 17.70, 59.60],
             "tags": {"building": "yes"}},
            {"osm_id": 2, "coords": [17.70, 59.60, 17.73, 59.63], "tags": {"highway": "primary"}}
        ]
    })
}

#[test]
fn the_wasm_bundles_osm3d_selects_the_gpu_lane() {
    // THE assertion of this half. `gpu_lane_selected` reads `backend.caps().gpu` —
    // which lane is WIRED UP — and it is answerable before the first frame, unlike
    // `lane_flag`.
    let f = facet_wrapped::map::osm3d(&osm3d_payload()).expect("osm3d builds");
    let st = f.state_json();
    assert_eq!(
        st["gpu_lane_selected"], true,
        "the bundle ships facett-map3d/wgpu and then mounts the map on the CPU painter. \
         Compiling a renderer in is not selecting it. state: {st}"
    );
}

#[test]
fn the_backend_it_names_is_the_gpu_one() {
    // `gpu_lane_selected` is a bool and could in principle be true with the wrong
    // backend swapped in. The NAME says which engine, and it comes from the same
    // `caps()` the bool does.
    let f = facet_wrapped::map::osm3d(&osm3d_payload()).unwrap();
    let st = f.state_json();
    let backend = st["backend"].as_str().unwrap_or_default();
    assert_ne!(backend, "cpu-painter", "still on the CPU painter: {st}");
    assert!(
        backend.contains("wgpu"),
        "`{backend}` is not a wgpu backend — the map3d GPU lane names itself `wgpu-vello`"
    );
}

#[test]
fn the_lane_is_pending_and_not_a_verdict_before_any_frame() {
    // Selecting the lane must NOT make the observed flag claim a GPU frame happened.
    // These are two different questions and map3d's own doc records what conflating
    // them cost: `used_gpu` collapsed "not measured" into `false`, korp's rebuild log
    // ran before the first frame, and three people read `gpu=false` as the defect on a
    // build where everything was correct.
    let f = facet_wrapped::map::osm3d(&osm3d_payload()).unwrap();
    let st = f.state_json();
    assert_eq!(st["lane_flag"], "pending", "no frame has painted; that is not `cpu`");
    assert!(st["used_gpu"].is_null(), "`used_gpu` must be null, not false, before a frame");
}

#[test]
fn selecting_the_lane_does_not_disturb_the_components_own_state() {
    // `with_gpu` swaps a backend. If it also reset the model, every existing assertion
    // on osm3d (triangles, vertices, the geo box) would go quiet at once.
    let f = facet_wrapped::map::osm3d(&osm3d_payload()).unwrap();
    let st = f.state_json();
    assert_eq!(st["title"], "lane probe");
    assert!(st["triangles"].as_u64().is_some_and(|n| n > 0), "geometry survived: {st}");
    assert!(st["vertices"].as_u64().is_some_and(|n| n > 0));
    assert_eq!(st["geo"]["centre_lat"].as_f64().map(|v| (v * 100.0).round()), Some(5962.0));
}

#[test]
fn the_lane_token_vocabulary_is_facett_cores_one() {
    // The token must be `LaneFlag::token()`'s — the same one korp's `map3d_lane_line`
    // and the pane's chip print — not a fourth spelling of three states.
    use facett_core::render::lane::LaneFlag;
    let f = facet_wrapped::map::osm3d(&osm3d_payload()).unwrap();
    let reported = f.state_json()["lane_flag"].as_str().unwrap().to_string();
    assert!(
        [LaneFlag::Gpu, LaneFlag::Cpu, LaneFlag::Pending].iter().any(|l| l.token() == reported),
        "`{reported}` is not one of LaneFlag's tokens"
    );
}

#[test]
fn there_is_no_renderer_field_and_the_brief_asked_for_one() {
    // Kept deliberately, as the record of a correction. If a `renderer` field is ever
    // added upstream, this fails and whoever adds it will find the note above
    // explaining which field the robots were pointed at instead — rather than two
    // fields answering the same question.
    let f = facet_wrapped::map::osm3d(&osm3d_payload()).unwrap();
    assert!(
        f.state_json().get("renderer").is_none(),
        "map3d grew a `renderer` field — point the robot at it and retire `lane_flag`, \
         or retire it; two names for one lane is how they drift"
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// graph3d — what CAN and CANNOT be asserted, said plainly.
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn graph3d_builds_with_its_gpu_lane_selected_but_that_is_a_compile_time_claim_only() {
    // `Graph3D::with_gpu` is `#[cfg(feature = "wgpu")]`. `graph.rs::graph3d` calls it,
    // so this test EXISTING and the crate COMPILING is the proof that
    // `facett-graph3d/wgpu` reached the bundle. Drop the feature from the manifest and
    // the build fails by name rather than falling back in silence.
    //
    // That is genuinely all it proves, and this test is named so nobody reads more into
    // it. `facett-graph3d` publishes no lane in `state_json` and no public getter for
    // `use_gpu` (the field is private at lib.rs:494) — so unlike osm3d above, there is
    // NO runtime observable to assert. A test that pretended otherwise would be exactly
    // the hollow guard this row is full of examples of.
    let payload = json!({
        "title": "cloud",
        "nodes": [{"id": 0, "label": "A"}, {"id": 1, "label": "B"}, {"id": 2, "label": "C"}],
        "edges": [{"src": 0, "dst": 1, "rel": "X"}, {"src": 1, "dst": 2, "rel": "X"}]
    });
    let f = facet_wrapped::graph::graph3d(&payload).expect("graph3d builds");
    let st = f.state_json();
    // graph3d reports COUNTS, not arrays (`"nodes": self.nodes.len()`) — checked
    // against the crate rather than assumed, after this assertion first read it as an
    // array and got `None`.
    assert_eq!(st["nodes"].as_u64(), Some(3), "the model survived the swap");
    assert_eq!(st["edges"].as_u64(), Some(2));
}

#[test]
fn graph3d_still_has_no_lane_readout_and_this_test_is_the_reminder() {
    // Fails the day graph3d grows one — which is the point. Whoever adds it should
    // point the robot at it and delete this test, rather than leaving the 3D graph the
    // one surface in the bundle whose renderer nobody can name.
    let payload = json!({
        "title": "cloud",
        "nodes": [{"id": 0, "label": "A"}, {"id": 1, "label": "B"}],
        "edges": [{"src": 0, "dst": 1, "rel": "X"}]
    });
    let st = facet_wrapped::graph::graph3d(&payload).unwrap().state_json();
    for absent in ["lane_flag", "gpu_lane_selected", "backend", "used_gpu"] {
        assert!(
            st.get(absent).is_none(),
            "graph3d now publishes `{absent}` — assert on it here and retire this test"
        );
    }
}