use chrono::{Local, TimeZone};
use tablero::render::{Bounds, RenderContext};
use tablero::widget::{ClockWidget, Dashboard, Msg};
fn tick(h: u32, m: u32, s: u32) -> Msg {
Msg::Tick(Local.with_ymd_and_hms(2026, 6, 27, h, m, s).unwrap())
}
#[test]
fn message_updates_state_and_drives_exact_redraw_decision() {
let mut dash = Dashboard::new(vec![Box::new(ClockWidget::new(Bounds::new(0, 0, 320, 32)))]);
assert!(
dash.update(&tick(23, 58, 10)),
"first tick must request a redraw"
);
assert!(
!dash.update(&tick(23, 58, 45)),
"an unchanged minute must not request a redraw"
);
assert!(
dash.update(&tick(23, 59, 0)),
"a new minute must request a redraw"
);
}
#[test]
fn redraw_after_message_paints_the_current_state() {
let mut dash = Dashboard::new(vec![Box::new(ClockWidget::new(Bounds::new(0, 0, 320, 32)))]);
let mut ctx = RenderContext::new(320, 32);
assert!(dash.update(&tick(12, 0, 0)));
dash.draw(&mut ctx);
let px = ctx.pixels();
assert_eq!(px.len(), 320 * 32 * 4, "frame covers the whole surface");
let corner = &px[px.len() - 4..];
assert!(
corner[0] < 0x30 && corner[1] < 0x30 && corner[2] < 0x30,
"corner is not the dark background: {corner:?}"
);
assert_eq!(corner[3], 0xFF, "corner is not opaque");
assert!(
px.chunks_exact(4).any(|p| p[0] > 0x60),
"no rendered clock glyphs found"
);
}