use super::super::fixture::World;
use super::super::screen::{Screen, press};
use super::drive::{SECOND, at, column, driven_world, elbows, seat, visible};
use super::{CHILD, GRANDCHILD};
fn selected(world: &World) -> String {
world
.model
.focused_agent()
.map_or_else(|| "-".to_owned(), |a| a.agent_id.clone())
}
fn key(screen: &Screen, world: &mut World, key: egui::Key) {
screen.frame(world, vec![press(key, egui::Modifiers::COMMAND)]);
}
fn selected_root() -> (World, Screen) {
let (mut world, screen) = driven_world();
let title = seat(&column(&screen, &mut world), "hello").center();
super::super::screen::click(&screen, &mut world, title);
assert_eq!(selected(&world), "c-1", "the click selects the root row");
assert!(
world.state.expanded.is_empty(),
"and unfolds nothing by doing it: {:?}",
world.state.expanded
);
(world, screen)
}
#[test]
fn the_right_arrow_unfolds_the_selected_row_and_then_the_generation_below_it() {
let (mut world, screen) = selected_root();
let (child, grandchild) = (
super::name_of(&world, CHILD),
super::name_of(&world, GRANDCHILD),
);
key(&screen, &mut world, egui::Key::ArrowRight);
let open = column(&screen, &mut world);
assert_eq!(selected(&world), "c-1", "→ opens the row, it does not walk");
assert_eq!(
world.state.expanded.len(),
1,
"and opens exactly the one row it names: {:?}",
world.state.expanded
);
assert!(
at(&open, &grandchild).is_none(),
"one press, one generation — the grandchild is still folded under its own parent"
);
assert!(
at(&open, "▼ 1/2").is_some() && at(&open, "▶ 1/1").is_some(),
"the root's arrow points down and the revealed child wears a shut field of its own:\n{:?}",
open.iter().map(|(t, _)| t).collect::<Vec<_>>()
);
key(&screen, &mut world, egui::Key::ArrowDown);
assert_eq!(selected(&world), CHILD, "↓ enters the revealed child");
key(&screen, &mut world, egui::Key::ArrowRight);
let deep = column(&screen, &mut world);
assert_eq!(selected(&world), CHILD, "and → still does not walk");
let edges: Vec<f32> = [
seat(&deep, "hello"),
seat(&deep, &child),
seat(&deep, &grandchild),
]
.iter()
.map(egui::Rect::left)
.collect();
for pair in edges.windows(2) {
assert!(
pair[1] > pair[0] + 1.0,
"each generation indents past the one above it: {edges:?}"
);
}
assert_eq!(elbows(&deep), 2, "one reply elbow per revealed child");
assert!(
at(&deep, "▼ 1/1").is_some(),
"and the child's own field states its own subtree, open"
);
}
#[test]
fn the_walk_steps_over_a_folded_subtree_and_opens_nothing() {
let (mut world, screen) = selected_root();
let child = super::name_of(&world, CHILD);
assert_eq!(
visible(&world).len(),
2,
"shut, the three-generation conversation and its neighbour are two rows"
);
key(&screen, &mut world, egui::Key::ArrowDown);
let after = column(&screen, &mut world);
assert_eq!(
selected(&world),
SECOND,
"↓ lands on the next row at the same level, not inside the fold"
);
assert!(
world.state.expanded.is_empty(),
"and the walk expanded nothing on its way: {:?}",
world.state.expanded
);
assert!(
at(&after, &child).is_none() && elbows(&after) == 0,
"so the column is still the shut list:\n{:?}",
after.iter().map(|(t, _)| t).collect::<Vec<_>>()
);
key(&screen, &mut world, egui::Key::ArrowUp);
assert_eq!(selected(&world), "c-1", "↑ comes back to the root");
key(&screen, &mut world, egui::Key::ArrowRight);
key(&screen, &mut world, egui::Key::ArrowDown);
assert_eq!(
selected(&world),
CHILD,
"and once it is open, ↓ steps into the child rather than over it"
);
}
#[test]
fn the_left_arrow_pages_up_to_the_parent_before_it_folds_the_parent_shut() {
let (mut world, screen) = selected_root();
let child = super::name_of(&world, CHILD);
key(&screen, &mut world, egui::Key::ArrowRight);
key(&screen, &mut world, egui::Key::ArrowDown);
assert_eq!(selected(&world), CHILD, "standing on the revealed child");
key(&screen, &mut world, egui::Key::ArrowLeft);
let paged = column(&screen, &mut world);
assert_eq!(
selected(&world),
"c-1",
"← on a child pages the selection up to its parent"
);
assert!(
world.state.expanded.contains("c-1"),
"without folding what it paged out of: {:?}",
world.state.expanded
);
assert!(
at(&paged, &child).is_some() && elbows(&paged) == 1,
"so the child is still on screen, where the operator left it"
);
key(&screen, &mut world, egui::Key::ArrowLeft);
let shut = column(&screen, &mut world);
assert!(
world.state.expanded.is_empty(),
"the second ← shuts the row the first one landed on: {:?}",
world.state.expanded
);
assert_eq!(selected(&world), "c-1", "and stays where it is");
assert!(
at(&shut, &child).is_none() && elbows(&shut) == 0,
"the descent leaves the column with the fold:\n{:?}",
shut.iter().map(|(t, _)| t).collect::<Vec<_>>()
);
assert!(
at(&shut, "▶ 1/2").is_some(),
"and the arrow points right again"
);
key(&screen, &mut world, egui::Key::ArrowLeft);
assert_eq!(
(selected(&world), world.state.expanded.len()),
("c-1".to_owned(), 0),
"a shut root has no parent to page to and nothing to fold: ← rests"
);
}