use crate::ui::Model;
use egui_kittest::Harness;
use egui_kittest::kittest::{Queryable, by};
pub(crate) fn fault(
bounds: Option<(f64, f64, f64, f64)>,
width: f32,
height: f32,
) -> Option<String> {
let Some((x0, y0, x1, y1)) = bounds else {
return Some("the tree gives it no rectangle, so nothing can aim at it".to_owned());
};
if x1 <= x0 || y1 <= y0 {
return Some(format!("its rectangle is empty: {x0},{y0} to {x1},{y1}"));
}
if x1 <= 0.0 || y1 <= 0.0 || x0 >= f64::from(width) || y0 >= f64::from(height) {
return Some(format!(
"it lies wholly outside the {width}x{height} window: {x0},{y0} to {x1},{y1}"
));
}
None
}
pub(crate) fn complaints(
at: &str,
width: f32,
height: f32,
harness: &Harness<'_, Model>,
) -> Vec<String> {
harness
.query_all(by())
.filter(|node| !node.is_hidden() && (node.is_clickable() || node.is_focusable()))
.filter_map(|node| {
let bounds = node
.bounding_box()
.map(|rect| (rect.x0, rect.y0, rect.x1, rect.y1));
fault(bounds, width, height).map(|why| {
let label = node.label().unwrap_or_else(|| "(no label)".to_owned());
let role = node.role();
format!("{at}: the {role:?} reading {label:?} — {why}")
})
})
.collect()
}