use super::{HINT, render};
use crate::paint_probe::frame::{Window, press};
use crate::test_support::window::{pane, seated};
use crate::ui::{Aim, Model};
use serde_json::json;
fn founding() -> Model {
let mut model = Model {
conversation: None,
..seated()
};
model.begin_founding(crate::seat::OWN);
model
}
fn entered(model: &mut Model) {
let window = Window::new();
let mut body = |ctx: &egui::Context| {
egui::CentralPanel::default().show(ctx, |ui| render(ui, model));
};
window.frame(Vec::new(), &mut body);
window.focus(egui::Id::new(crate::ui::keys::BOX_ID));
window.frame(vec![press(egui::Key::Enter)], &mut body);
}
#[test]
fn the_box_asks_for_a_name_then_a_goal() {
let mut model = founding();
let painted = pane(|ui| crate::ui::composer::render(ui, &mut model));
assert!(painted.contains(HINT), "{painted}");
assert_eq!(model.fill, None, "the caret request was taken");
}
#[test]
fn enter_stages_a_start_on_the_named_workspace() {
let mut model = founding();
let before = model.aim.clone();
model.draft = "lab fix the build".to_owned();
entered(&mut model);
assert_eq!(
model.outbox,
vec![crate::ui::Posted::act(
json!({"op": "prepare", "workspace": "lab", "payload": {"rung": "bare"}})
)]
);
assert_eq!(
model.aim, before,
"nothing is asked of a wall not founded yet"
);
assert!(model.founding.is_some());
assert_eq!(
model.start.as_ref().map(|start| start.goal.clone()),
Some("fix the build".to_owned())
);
let own = model.roster[0].channel.clone();
model.receipt(&own, "message", crate::reply::Read::Refusal("x".to_owned()));
assert!(model.founding.is_some(), "only the prepare's answer aims");
model.receipt(
&own,
"prepare",
crate::reply::Read::Refusal("no key".to_owned()),
);
assert_eq!(
model.aim,
Some(Aim {
channel: crate::seat::OWN.to_owned(),
address: "lab".to_owned(),
})
);
assert_eq!(model.founding, None, "the aim put the founding down");
}
#[test]
fn an_unsent_founding_aims_and_gives_the_goal_back() {
let mut model = founding();
model.draft = "lab fix the build".to_owned();
entered(&mut model);
let gone = crate::channel::Reach::Unsent("no route".to_owned());
model.acted("prepare", &gone, None);
assert_eq!(model.founding, None);
assert_eq!(model.aim.map(|aim| aim.address), Some("lab".to_owned()));
assert_eq!(model.draft, "fix the build");
}
#[test]
fn a_name_alone_composes_nothing() {
let mut model = founding();
model.draft = "lab ".to_owned();
entered(&mut model);
assert!(model.outbox.is_empty(), "{:?}", model.outbox);
assert!(model.founding.is_some());
assert_eq!(model.draft, "lab ");
}
#[test]
fn a_slash_line_founds_nothing() {
let mut model = founding();
model.draft = "/help me".to_owned();
entered(&mut model);
assert!(model.outbox.is_empty(), "{:?}", model.outbox);
}
#[test]
fn esc_and_a_selection_put_it_down() {
let mut model = founding();
model.escape();
assert_eq!(model.founding, None);
let mut model = founding();
model.select("home/x");
assert_eq!(model.founding, None);
}
#[test]
fn the_founding_box_carries_the_start_s_acts() {
let harness = crate::snapshot::seat(founding(), 1400.0, 900.0);
let inventory = crate::snapshot::parity::inventory(&harness);
for op in [crate::verbs::PREPARE, crate::verbs::PROMPT] {
assert!(inventory.contains(op), "{op:?} is tagged: {inventory:?}");
}
}