diff --git a/crates/ledger-gui/tests/sidenav.rs b/crates/ledger-gui/tests/sidenav.rs index 5a04f319..2df51108 100644 --- a/crates/ledger-gui/tests/sidenav.rs +++ b/crates/ledger-gui/tests/sidenav.rs @@ -104,6 +104,7 @@ const FOOTERS: [Footer; 9] = [ /// menu), about 560 events and 110 stored receipts. Removed when dropped. struct Fixture { _dir: tempfile::TempDir, + _plain_dir: tempfile::TempDir, root: PathBuf, plain: PathBuf, } @@ -153,15 +154,19 @@ impl Fixture { std::fs::create_dir_all(&root).unwrap(); fill(&root); // The same books outside the root: no Books menu. - let plain = tempfile::Builder::new() + // Outside a data root a company's badge hue is chosen from its + // folder's name (company.rs `hue_key`), so that folder has a fixed + // name here, never the temporary folder's random one: the same + // picture on every machine and every run. + let plain_dir = tempfile::Builder::new() .prefix("wb-gui-sidenav-plain-") .tempdir() - .unwrap() - .keep(); - let plain_book = plain.join("books-data"); + .unwrap(); + let plain_book = plain_dir.path().join("wanderland-copy").join("books-data"); copy_dir(&root, &plain_book); Fixture { _dir: dir, + _plain_dir: plain_dir, root, plain: plain_book, } diff --git a/crates/ledger-gui/tests/snapshots/tabs_empty_dark.shapes b/crates/ledger-gui/tests/snapshots/tabs_empty_dark.shapes new file mode 100644 index 00000000..10571da8 --- /dev/null +++ b/crates/ledger-gui/tests/snapshots/tabs_empty_dark.shapes @@ -0,0 +1 @@ +264f46e9e8e6513b diff --git a/crates/ledger-gui/tests/snapshots/tabs_empty_light.shapes b/crates/ledger-gui/tests/snapshots/tabs_empty_light.shapes new file mode 100644 index 00000000..154f7f87 --- /dev/null +++ b/crates/ledger-gui/tests/snapshots/tabs_empty_light.shapes @@ -0,0 +1 @@ +3a01e3d1c17e7a42 diff --git a/crates/ledger-gui/tests/snapshots/tabs_many_dark.shapes b/crates/ledger-gui/tests/snapshots/tabs_many_dark.shapes new file mode 100644 index 00000000..31cc95f9 --- /dev/null +++ b/crates/ledger-gui/tests/snapshots/tabs_many_dark.shapes @@ -0,0 +1 @@ +d3c3cc6f9e97b02b diff --git a/crates/ledger-gui/tests/snapshots/tabs_many_light.shapes b/crates/ledger-gui/tests/snapshots/tabs_many_light.shapes new file mode 100644 index 00000000..933f8119 --- /dev/null +++ b/crates/ledger-gui/tests/snapshots/tabs_many_light.shapes @@ -0,0 +1 @@ +19867322b240d3af diff --git a/crates/ledger-gui/tests/snapshots/tabs_one_dark.shapes b/crates/ledger-gui/tests/snapshots/tabs_one_dark.shapes new file mode 100644 index 00000000..552b57fb --- /dev/null +++ b/crates/ledger-gui/tests/snapshots/tabs_one_dark.shapes @@ -0,0 +1 @@ +36b4501433dfff25 diff --git a/crates/ledger-gui/tests/snapshots/tabs_one_light.shapes b/crates/ledger-gui/tests/snapshots/tabs_one_light.shapes new file mode 100644 index 00000000..d53a098b --- /dev/null +++ b/crates/ledger-gui/tests/snapshots/tabs_one_light.shapes @@ -0,0 +1 @@ +216c4495455951ef diff --git a/crates/ledger-gui/tests/snapshots/tabs_plus_dark.shapes b/crates/ledger-gui/tests/snapshots/tabs_plus_dark.shapes new file mode 100644 index 00000000..3932e033 --- /dev/null +++ b/crates/ledger-gui/tests/snapshots/tabs_plus_dark.shapes @@ -0,0 +1 @@ +e81f5a809e024c55 diff --git a/crates/ledger-gui/tests/snapshots/tabs_plus_light.shapes b/crates/ledger-gui/tests/snapshots/tabs_plus_light.shapes new file mode 100644 index 00000000..ede5663b --- /dev/null +++ b/crates/ledger-gui/tests/snapshots/tabs_plus_light.shapes @@ -0,0 +1 @@ +aeace623f1babf2b diff --git a/crates/ledger-gui/tests/snapshots/tabs_two_dark.shapes b/crates/ledger-gui/tests/snapshots/tabs_two_dark.shapes new file mode 100644 index 00000000..ce94b163 --- /dev/null +++ b/crates/ledger-gui/tests/snapshots/tabs_two_dark.shapes @@ -0,0 +1 @@ +057c55da25ed99a6 diff --git a/crates/ledger-gui/tests/snapshots/tabs_two_light.shapes b/crates/ledger-gui/tests/snapshots/tabs_two_light.shapes new file mode 100644 index 00000000..775b5556 --- /dev/null +++ b/crates/ledger-gui/tests/snapshots/tabs_two_light.shapes @@ -0,0 +1 @@ +43e2533dd7ebc46b diff --git a/crates/ledger-gui/tests/tabs.rs b/crates/ledger-gui/tests/tabs.rs index 11b346da..af260588 100644 --- a/crates/ledger-gui/tests/tabs.rs +++ b/crates/ledger-gui/tests/tabs.rs @@ -89,10 +89,12 @@ struct Co { impl World { fn new() -> World { - let dir = tempfile::Builder::new() - .prefix("wb-gui-tabs-") - .tempdir() - .unwrap(); + World::named("wb-gui-tabs-") + } + + /// A world whose temporary folder's name starts with `prefix`. + fn named(prefix: &str) -> World { + let dir = tempfile::Builder::new().prefix(prefix).tempdir().unwrap(); let home = dir.path().join("home"); std::fs::create_dir_all(&home).unwrap(); let root = dir.path().join("root"); @@ -1298,11 +1300,101 @@ fn every_tab_never_writes_into_its_books_in_front_or_not() { // ---- 8: how the bar looks ---------------------------------------------------- -/// Pixels compared where there is a renderer; tolerant of antialiasing -/// (as the sidebar's are: sidenav.rs). +/// A shape in words, without where its glyphs happen to lie in egui's font +/// atlas: that depends on which text this process laid out first (other +/// tests, in other threads), and no picture shows it. Positions, colours, +/// the text and each glyph's place are all here. +fn describe(shape: &egui::epaint::Shape) -> String { + use egui::epaint::Shape; + match shape { + Shape::Text(t) => { + let mut out = format!( + "Text pos={:?} under={:?} fallback={:?} over={:?} opacity={:?} angle={:?} text={:?}", + t.pos, + t.underline, + t.fallback_color, + t.override_text_color, + t.opacity_factor, + t.angle, + t.galley.job.text, + ); + for placed in &t.galley.rows { + out.push_str(&format!(" row@{:?} size={:?}", placed.pos, placed.row.size)); + for g in &placed.row.glyphs { + out.push_str(&format!(" {}@{:?}+{:?}", g.chr, g.pos, g.advance_width)); + } + for v in &placed.row.visuals.mesh.vertices { + out.push_str(&format!(" v{:?}{:?}", v.pos, v.color)); + } + } + out + } + Shape::Vec(v) => v.iter().map(describe).collect::>().join(" | "), + Shape::Mesh(m) => { + let mut out = String::from("Mesh"); + for v in &m.vertices { + out.push_str(&format!(" v{:?}{:?}", v.pos, v.color)); + } + out + } + other => format!("{other:?}"), + } +} + +/// The shapes the window paints where it can be seen: those whose clip +/// reaches into the window. (The page under a short bar is laid out far +/// below it and holds the time now, "Nov 21 2026 04:26 UTC", which no +/// picture shows: it is clipped away.) +fn shown_shapes(h: &Harness<'_, App>) -> Vec { + let screen = h.ctx.content_rect(); + h.output() + .shapes + .iter() + .filter(|s| s.clip_rect.intersects(screen)) + .map(|s| format!("{:?} {}", s.clip_rect, describe(&s.shape))) + .collect() +} + +/// A hash of every shape the window paints where it can be seen (FNV-1a +/// over their debug form): the same shapes paint the same pixels, on any +/// renderer. What the picture shows (colours, text and where each lies) is +/// in them; how a renderer rasterizes their edges is not. +fn shapes_of(h: &Harness<'_, App>) -> u64 { + let mut hash: u64 = 0xcbf2_9ce4_8422_2325; + for shape in shown_shapes(h) { + for b in shape.bytes() { + hash ^= b as u64; + hash = hash.wrapping_mul(0x100_0000_01b3); + } + } + hash +} + +/// The look of the window, compared as sidenav.rs compares its sidebar's: +/// the shapes painted are the check everywhere +/// (`tests/snapshots/.shapes`; `TABS_BLESS=1` writes them), for +/// another renderer rasterizes the same shapes differently (CI's software +/// one differs from a GPU in antialiasing, by thousands of faint pixels +/// and some dozens of strong ones along glyph edges). The pixels +/// (`tests/snapshots/.png`, tolerant of antialiasing) are compared +/// as well on the machine that made them and any with its renderer: +/// `TABS_PIXELS=1`. fn picture(h: &mut Harness<'_, App>, name: &str) { + let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/snapshots"); + let file = dir.join(format!("{name}.shapes")); + let shapes = format!("{:016x}", shapes_of(h)); + if std::env::var("TABS_BLESS").is_ok() { + std::fs::write(&file, format!("{shapes}\n")).unwrap(); + } else { + let want = std::fs::read_to_string(&file) + .unwrap_or_else(|e| panic!("{}: {e} (TABS_BLESS=1 writes it)", file.display())); + assert_eq!(shapes, want.trim(), "the shapes of {name} changed"); + } + if std::env::var("TABS_PIXELS").is_err() { + return; + } if h.render().is_err() { - eprintln!("no renderer: {name} not compared"); + eprintln!("no renderer: {name} pixels not compared"); return; } let options = egui_kittest::SnapshotOptions::new() @@ -1390,6 +1482,41 @@ fn the_tab_bar_with_one_two_and_many_tabs() { } } +/// What the window shows depends on the books and the theme alone: not on +/// the name of the temporary folder they sit in, the clock, or the +/// machine. The same companies made in two worlds whose folders are named +/// unlike paint the same shapes and the same pixels (where there is a +/// renderer), with the badge hue chosen from the id of the books. +#[test] +fn the_bar_is_the_same_from_differently_named_folders() { + let names = ["Wanderland Labs LLC", "Fernhill Studio"]; + let mut seen: Vec<(u64, Option>, Vec)> = Vec::new(); + for (prefix, dark) in [ + ("a-", false), + ("zzzz-a-much-longer-name-", false), + ("a-", true), + ("zzzz-a-much-longer-name-", true), + ] { + let w = World::named(prefix); + let mut h = shown(&w, &names, dark, BAR_SIZE); + let keys: Vec = (0..names.len()) + .map(|i| h.state().views()[i].hue_key()) + .collect(); + let pixels = h.render().ok().map(|img| img.into_raw()); + seen.push((shapes_of(&h), pixels, keys)); + } + for pair in [(0, 1), (2, 3)] { + let (a, b) = (&seen[pair.0], &seen[pair.1]); + assert_eq!(a.2, ["co-0", "co-1"], "a company's hue key is its book id"); + assert_eq!( + a.2, b.2, + "the hue key is the book id, not the folder's name" + ); + assert_eq!(a.0, b.0, "the shapes differ with the folder's name"); + assert!(a.1 == b.1, "the pixels differ with the folder's name"); + } +} + #[test] fn the_empty_window_and_the_plus_list() { for dark in [false, true] {