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

nordisk/facett

facett-timeline/tests/render.rs

//! **Render-proof for facett-timeline.** Run a real [`TimelineView`] through
//! `egui_kittest` so the painter actually executes, then prove (a) each laid-out
//! bar rode into the AccessKit tree as a queryable, labelled node (FC-4), and
//! (b) the bars are laid out at the offsets the model's window dictates — the
//! "renders N bars at correct offsets" proof, asserted as DATA via `state_json`.

use egui::accesskit::Role;
use egui_kittest::Harness;
use egui_kittest::kittest::{NodeT, Queryable};

use facett_core::Facet;
use facett_timeline::{Bar, BarStatus, Lane, TimelineModel, TimelineView, layout};

#[test]
fn timeline_paints_bars_into_the_accesskit_tree() {
    let view = TimelineView::local();
    let bar_count = view.model().bar_count();
    assert!(bar_count >= 6, "the demo seeds executor/job/task bars");

    let mut view_for_ui = TimelineView::local();
    let mut harness = Harness::new_ui(move |ui| {
        view_for_ui.ui(ui);
    });
    harness.run();

    // A solid bar (a job) and the segmented straggler task both entered the tree as
    // labelled Button nodes — painter-only rects made queryable (FC-4/FC-5).
    let job = harness.get_by_label("job 0 · done").accesskit_node();
    assert_eq!(job.role(), Role::Button, "a painted bar is a queryable Button node");
    let straggler = harness.get_by_label("task 2 · failed").accesskit_node();
    assert_eq!(straggler.role(), Role::Button, "the segmented straggler bar is queryable too");

    // The render is DATA-checked: state_json carries one entry per bar, each with a
    // finite start/width fraction in [0,1] over the active window.
    let st = view.state_json();
    assert_eq!(st["bar_count"].as_u64().unwrap(), bar_count as u64);
    let painted: usize = st["model"]["lanes"].as_array().unwrap().iter().map(|l| l["bars"].as_array().unwrap().len()).sum();
    assert_eq!(painted, bar_count, "every bar is laid out");
}

#[test]
fn n_bars_render_at_correct_offsets() {
    // Three bars across a 0..1000 window → predictable left/width at 1px == 1 unit.
    let model = TimelineModel::new(0.0, 1000.0).with_lane(Lane::new("L").with_bars(vec![
        Bar::new("a", 0.0, 100.0, BarStatus::Done),
        Bar::new("b", 250.0, 500.0, BarStatus::Running),
        Bar::new("c", 900.0, 1000.0, BarStatus::Done),
    ]));
    let content = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1000.0, 40.0));
    let laid = layout(&model, 0.0, 1000.0, content);
    assert_eq!(laid.len(), 3, "N bars laid out");
    let lefts: Vec<f32> = laid.iter().map(|b| b.rect.left()).collect();
    let widths: Vec<f32> = laid.iter().map(|b| b.rect.width()).collect();
    assert!((lefts[0] - 0.0).abs() < 0.5 && (widths[0] - 100.0).abs() < 0.5);
    assert!((lefts[1] - 250.0).abs() < 0.5 && (widths[1] - 250.0).abs() < 0.5);
    assert!((lefts[2] - 900.0).abs() < 0.5 && (widths[2] - 100.0).abs() < 0.5);
}