use bone_render::{
    Camera2, PixelsPerMm, PreviewArc, SketchPreview, SketchRenderer, SketchScene, Style,
    ViewportExtent,
};
use bone_types::{Angle, Length, Point2};
use uom::si::angle::radian;
use uom::si::length::millimeter;

mod common;

fn extent() -> ViewportExtent {
    common::extent_square(256)
}

#[test]
fn preview_arc_paints_pixels_outside_background() {
    let ctx = common::make_context(extent());
    let mut renderer = SketchRenderer::new(ctx.gpu(), ctx.color_format());
    let scene = SketchScene::empty();
    let preview = SketchPreview {
        arcs: vec![PreviewArc {
            center: Point2::from_mm(0.0, 0.0),
            radius: Length::new::<millimeter>(1.0),
            start_angle: Angle::new::<radian>(0.0),
            sweep_angle: Angle::new::<radian>(std::f64::consts::FRAC_PI_2),
        }],
        ..SketchPreview::empty()
    };
    let camera = Camera2::new(extent()).with_zoom(PixelsPerMm::new(64.0));
    let style = Style::default();
    let Ok(frame) = renderer.render_with_preview(&ctx, &scene, &preview, camera, &style) else {
        panic!("render_with_preview failed");
    };
    let bg = style.background().to_rgba8();
    let pixels = frame.rgba();
    let painted = pixels
        .chunks_exact(4)
        .filter(|p| p[0] != bg[0] || p[1] != bg[1] || p[2] != bg[2])
        .count();
    assert!(
        painted > 0,
        "preview arc should paint pixels distinct from the {bg:?} background",
    );
}
