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

nordisk/facett

facett-brokkrview/tests/camera_centred.rs

//! **The forge DAG opens centred, a click never moves it (📌 Stay), and 🎯 Focus flies
//! the clicked step to the centre** — rendered beside a 320 px rail, real clicks.

use egui::vec2;
use egui_kittest::{kittest::Queryable, Harness};
use facett_brokkrview::{BrokkrView, BrokkrViewMsg, ViewMode};
use facett_core::fly::NavMode;
use facett_core::{Elm, Facet};

fn harness(mode: NavMode) -> Harness<'static, BrokkrView> {
    let mut v = BrokkrView::demo("Brokkr");
    v.update(BrokkrViewMsg::SetView(ViewMode::Dag));
    let h = Harness::builder().with_size(vec2(1400.0, 900.0)).with_step_dt(1.0 / 60.0).build_ui_state(
        |ui, v: &mut BrokkrView| {
            egui::Panel::left("rail").exact_size(320.0).resizable(false).show_inside(ui, |ui| {
                ui.label("rail");
            });
            egui::CentralPanel::default().show_inside(ui, |ui| Facet::ui(v, ui));
        },
        v,
    );
    mode.remember(&h.ctx);
    h
}

fn label(v: &BrokkrView, step: &str) -> String {
    let n = v.model().dag.nodes.iter().find(|n| n.step == step).unwrap();
    match &n.disposition {
        Some(d) => format!("{} · {} · {} · {}", n.step, n.stage, n.status, d),
        None => format!("{} · {} · {}", n.step, n.stage, n.status),
    }
}

fn steps(h: &mut Harness<'static, BrokkrView>, n: usize) {
    for _ in 0..n {
        h.step();
    }
}

/// First load: fitted, CENTRED (±2 px) in the visible canvas, wholly inside it.
#[test]
fn the_forge_dag_opens_centred() {
    let mut h = harness(NavMode::Stay);
    steps(&mut h, 4);
    let (seen, b) = (h.state().seen_rect().expect("painted"), h.state().screen_box().expect("nodes"));
    assert!((b.center() - seen.center()).length() <= 2.0, "centred: box {b:?} in {seen:?}");
    assert!(seen.expand(0.5).contains_rect(b), "fully visible: box {b:?} in {seen:?}");
}

/// 📌 Stay: the click selects; the step does not move.
#[test]
fn stay_a_click_never_moves_the_forge_dag() {
    let mut h = harness(NavMode::Stay);
    steps(&mut h, 4);
    let before = h.state().step_screen_pos("compile").unwrap();
    let l = label(h.state(), "compile");
    h.get_by_label(&l).click();
    steps(&mut h, 30);
    assert_eq!(h.state().state_json()["selected_step"], "compile");
    let after = h.state().step_screen_pos("compile").unwrap();
    assert!((after - before).length() <= 0.5, "the step stayed put: {before:?} → {after:?}");
}

/// 🎯 Focus: the clicked step flies to the centre of the visible canvas (±2 px).
#[test]
fn focus_a_click_flies_the_step_to_the_centre() {
    let mut h = harness(NavMode::Focus);
    steps(&mut h, 4);
    let l = label(h.state(), "compile");
    h.get_by_label(&l).click();
    steps(&mut h, 60);
    let seen = h.state().seen_rect().unwrap();
    let at = h.state().step_screen_pos("compile").unwrap();
    assert!((at - seen.center()).length() <= 2.0, "compile at the centre: {at:?} vs {:?}", seen.center());
}