use super::{ARM, ARMED, ASKED, CLOSE, CONFIRM, HEADING, NOT_ARMED, REFUSED_IF, render, said};
use crate::paint_probe::frame::Window;
use crate::test_support::window::{click, pane, seated};
use crate::ui::{Model, Unmaking};
use serde_json::json;
fn arming() -> Model {
let mut model = seated();
model.begin_unmaking();
model
}
fn armed() -> Model {
let mut model = arming();
model.unmaking.as_mut().expect("just opened").typed = "home".to_owned();
model
}
fn press(model: &mut Model, label: &str) {
let window = Window::new();
click(&window, label, |ctx| {
egui::CentralPanel::default().show(ctx, |ui| {
render(ui, model);
});
});
}
#[test]
fn it_paints_nothing_at_all_with_no_unmaking_open() {
let mut model = seated();
let painted = pane(|ui| {
assert!(!render(ui, &mut model));
});
assert_eq!(painted.trim(), "");
}
#[test]
fn it_says_what_would_be_unmade_and_what_would_refuse_it() {
let mut model = arming();
let painted = pane(|ui| {
assert!(render(ui, &mut model));
});
for word in [HEADING, "home on (this box's own engine)", REFUSED_IF, ARM] {
assert!(
painted.lines().any(|line| line == word),
"{word:?}:\n{painted}"
);
}
}
#[test]
fn the_pane_says_which_of_the_three_states_it_is_in() {
let aim = seated().aim.expect("the fixture is aimed at a wall");
let mut held = Unmaking::at(aim);
assert_eq!(said(&held), NOT_ARMED);
held.typed = "home".to_owned();
assert_eq!(said(&held), ARMED);
held.posted = true;
assert_eq!(said(&held), ASKED);
}
#[test]
fn the_act_is_painted_unarmed_and_fires_nothing() {
let mut model = arming();
let painted = pane(|ui| {
render(ui, &mut model);
});
for word in [CONFIRM, CLOSE, NOT_ARMED] {
assert!(
painted.lines().any(|line| line == word),
"{word:?}:\n{painted}"
);
}
press(&mut model, CONFIRM);
assert!(model.outbox.is_empty(), "a disabled control fires nothing");
}
#[test]
fn armed_it_composes_the_unmaking_and_keeps_the_arming() {
let mut model = armed();
press(&mut model, CONFIRM);
assert_eq!(
model.outbox,
vec![crate::ui::Posted::act(
json!({"op": "delete-workspace", "workspace": "home", "typed": "home"})
)]
);
assert_eq!(
model.unmaking.as_ref().map(|held| held.typed.clone()),
Some("home".to_owned())
);
let painted = pane(|ui| {
render(ui, &mut model);
});
assert!(painted.lines().any(|line| line == ASKED), "{painted}");
}
#[test]
fn the_way_out_is_first_and_composes_nothing() {
let mut model = armed();
let painted = pane(|ui| {
render(ui, &mut model);
});
let index = |word: &str| {
painted
.lines()
.position(|line| line == word)
.unwrap_or_else(|| panic!("{word:?}:\n{painted}"))
};
assert!(index(CLOSE) < index(CONFIRM), "{painted}");
press(&mut model, CLOSE);
assert_eq!(model.unmaking, None);
assert!(model.outbox.is_empty());
}