//! Ad-hoc perf check. Renders a 100-node canvas into an off-screen
//! ratatui buffer and asserts it stays under the SPEC §8 budget.
//!
//! The threshold is intentionally generous in debug builds — the
//! SPEC budget (100 ms) targets `--release`. This test only fails
//! when something has *clearly* regressed. Run with `--release` for
//! the strict number: `cargo test --release perf`.

use std::time::Instant;

use jcv::canvas::{Canvas, Node, TextNode};
use jcv::layout::Viewport;
use jcv::render::render_canvas;
use jcv::render::theme::Theme;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;

#[test]
fn renders_100_nodes_under_budget() {
    let nodes: Vec<Node> = (0..100)
        .map(|i| {
            let row = i / 10;
            let col = i % 10;
            Node::Text(TextNode {
                id: format!("n{i}"),
                x: col * 240,
                y: row * 120,
                width: 200,
                height: 100,
                text: format!("node-{i}"),
                color: Some(((i % 6) + 1).to_string()),
            })
        })
        .collect();
    let canvas = Canvas {
        nodes,
        edges: vec![],
    };
    let vp = Viewport::new(200, 60);
    let theme = Theme::with_colors(true);
    let area = Rect::new(0, 0, 200, 60);
    let mut buf = Buffer::empty(area);

    let start = Instant::now();
    for _ in 0..10 {
        render_canvas(&canvas, &vp, &theme, &mut buf);
    }
    let avg = start.elapsed() / 10;

    let budget = if cfg!(debug_assertions) {
        std::time::Duration::from_millis(500)
    } else {
        std::time::Duration::from_millis(100)
    };
    assert!(
        avg < budget,
        "render of 100 nodes too slow: {:?} (budget {:?})",
        avg,
        budget
    );
}
