Files
Marcos Paulo 00e96fe8c6 feat(whiteboard): wave 2 whiteboard
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-13 19:11:18 -03:00

179 lines
7.2 KiB
Rust

//! Determinism and geometry robustness of the hand-drawn shape generator.
use notescraft_whiteboard::rough::{Polyline, Style};
use notescraft_whiteboard::{Element, ElementKind, IdGen, Whiteboard, rough};
fn bits(lines: &[Polyline]) -> Vec<u32> {
lines.iter().flat_map(|l| l.iter().flat_map(|p| [p[0].to_bits(), p[1].to_bits()])).collect()
}
fn assert_finite(label: &str, lines: &[Polyline]) {
for line in lines {
for p in line {
assert!(p[0].is_finite() && p[1].is_finite(), "{label}: non-finite point {p:?}");
}
}
}
#[test]
fn same_seed_gives_identical_output() {
let mut ids = IdGen::new();
let a = Element::rectangle(&mut ids, 10.0, 20.0, 100.0, 60.0);
let mut ids = IdGen::new();
let b = Element::rectangle(&mut ids, 10.0, 20.0, 100.0, 60.0);
assert_eq!(a, b, "builders must be deterministic for the same generator position");
for style in [Style::HandDrawn, Style::Clean] {
let first = rough::outline(&a, style);
let second = rough::outline(&b, style);
assert_eq!(bits(&first), bits(&second), "{style:?} output must be bit-identical");
let again = rough::outline(&a, style);
assert_eq!(bits(&first), bits(&again), "{style:?} output must be repeatable");
}
}
#[test]
fn different_seed_changes_hand_drawn_but_not_clean() {
let mut ids = IdGen::new();
let a = Element::rectangle(&mut ids, 0.0, 0.0, 100.0, 60.0);
let mut b = a.clone();
b.seed = a.seed.wrapping_add(1);
assert_ne!(bits(&rough::outline(&a, Style::HandDrawn)), bits(&rough::outline(&b, Style::HandDrawn)));
assert_eq!(bits(&rough::outline(&a, Style::Clean)), bits(&rough::outline(&b, Style::Clean)));
}
#[test]
fn hand_drawn_is_a_double_stroke_and_perturbs() {
let mut ids = IdGen::new();
let rect = Element::rectangle(&mut ids, 0.0, 0.0, 100.0, 60.0);
let hand = rough::outline(&rect, Style::HandDrawn);
assert_eq!(hand.len(), 2, "hand drawn shapes are stroked twice");
assert_ne!(bits(std::slice::from_ref(&hand[0])), bits(std::slice::from_ref(&hand[1])), "the two strokes must differ");
let clean = rough::outline(&rect, Style::Clean);
assert_eq!(clean.len(), 1, "clean shapes are a single polyline");
let corners = clean.first().expect("one polyline");
assert_eq!(corners.len(), 5, "closed rectangle: 4 corners plus the closing point");
assert_eq!(corners.first(), corners.last());
assert_eq!(corners[0], [0.0, 0.0]);
assert_eq!(corners[2], [100.0, 60.0]);
let nudged: Polyline = hand[0].clone();
assert!(nudged.iter().any(|p| *p != [0.0, 0.0]), "hand drawn geometry must be perturbed");
}
#[test]
fn clean_geometry_is_exact_for_every_supported_shape() {
let mut ids = IdGen::new();
let diamond = Element::diamond(&mut ids, 0.0, 0.0, 100.0, 60.0);
let lines = rough::outline(&diamond, Style::Clean);
assert_eq!(lines[0][0], [50.0, 0.0]);
assert_eq!(lines[0][1], [100.0, 30.0]);
assert_eq!(lines[0][2], [50.0, 60.0]);
assert_eq!(lines[0][3], [0.0, 30.0]);
let ellipse = Element::ellipse(&mut ids, 0.0, 0.0, 100.0, 60.0);
let lines = rough::outline(&ellipse, Style::Clean);
let ring = lines.first().expect("one polyline");
assert!(ring.len() >= 32, "ellipse needs enough segments");
assert_eq!(ring.first(), ring.last(), "ellipse ring is closed");
let line = Element::line(&mut ids, 5.0, 7.0, 30.0, 40.0);
let lines = rough::outline(&line, Style::Clean);
assert_eq!(lines[0][0], [5.0, 7.0]);
assert_eq!(lines[0][1], [35.0, 47.0]);
let arrow = Element::arrow(&mut ids, 0.0, 0.0, 100.0, 0.0);
let lines = rough::outline(&arrow, Style::Clean);
assert_eq!(lines[0].len(), 2, "arrow shaft");
assert_eq!(lines.len(), 3, "shaft plus two arrowhead legs");
assert_finite("arrow", &lines);
}
#[test]
fn hand_drawn_lines_overshoot_both_ends() {
let mut ids = IdGen::new();
let line = Element::line(&mut ids, 0.0, 0.0, 100.0, 0.0);
let clean = rough::outline(&line, Style::Clean);
let hand = rough::outline(&line, Style::HandDrawn);
assert_eq!(clean[0].len(), 2, "clean strokes keep the exact path");
for stroke in &hand {
assert_eq!(stroke.len(), 4, "two original points plus two overshoots");
for (drawn, exact) in stroke[1..3].iter().zip(&clean[0]) {
// roughness 1 jitters by at most 1.2 px per axis
assert!((drawn[0] - exact[0]).abs() <= 1.25 && (drawn[1] - exact[1]).abs() <= 1.25, "{drawn:?} drifted from {exact:?}");
}
}
}
#[test]
fn unsupported_kinds_have_no_outline() {
let mut ids = IdGen::new();
let image: Element = serde_json::from_str(r#"{"id":"i","type":"image","fileId":"f"}"#).expect("parses");
for element in [Element::text(&mut ids, 0.0, 0.0, "hi"), image] {
assert_eq!(rough::outline(&element, Style::Clean), Vec::<Polyline>::new());
assert_eq!(rough::outline(&element, Style::HandDrawn), Vec::<Polyline>::new());
}
}
#[test]
fn degenerate_geometry_never_produces_nan_or_infinity() {
let mut ids = IdGen::new();
let mut elements = vec![
Element::rectangle(&mut ids, 0.0, 0.0, 0.0, 0.0),
Element::ellipse(&mut ids, 0.0, 0.0, 0.0, 1e30),
Element::diamond(&mut ids, f64::NAN, f64::INFINITY, -50.0, 1e9),
Element::line(&mut ids, 1e300, 1e300, 0.0, 0.0),
Element::arrow(&mut ids, 0.0, 0.0, 0.0, 0.0),
];
elements[4].seed = u64::MAX;
if let ElementKind::Arrow { points, .. } = &mut elements[4].kind {
points.clear();
}
for element in &elements {
for style in [Style::Clean, Style::HandDrawn] {
let lines = rough::outline(element, style);
assert_finite("degenerate", &lines);
for line in &lines {
for p in line {
assert!(p[0].abs() <= 1.0e6 && p[1].abs() <= 1.0e6, "coordinates must stay clamped: {p:?}");
}
}
}
}
}
#[test]
fn roughness_zero_falls_back_to_clean_geometry() {
let mut ids = IdGen::new();
let mut rect = Element::rectangle(&mut ids, 0.0, 0.0, 100.0, 60.0);
rect.roughness = 0;
assert_eq!(bits(&rough::outline(&rect, Style::HandDrawn)), bits(&rough::outline(&rect, Style::Clean)));
}
#[test]
fn board_survives_a_geometry_free_editing_session() {
let mut ids = IdGen::new();
let mut board = Whiteboard::default();
let rect = Element::rectangle(&mut ids, 0.0, 0.0, 100.0, 50.0);
board.add(rect.clone());
board.add(Element::arrow(&mut ids, 100.0, 25.0, 60.0, 0.0));
board.add(Element::text(&mut ids, 10.0, 80.0, "hello"));
assert_eq!(board.elements.len(), 3);
assert!(board.delete(&rect.id).is_some());
assert!(board.delete("missing").is_none());
assert_eq!(board.elements.len(), 3, "delete keeps the element");
assert!(board.elements.iter().all(|e| e.id != rect.id || e.is_deleted));
// the arrow sits at (100, 25)-(160, 25), the text below at (10, 80)-(70, 105)
let box_ = board.bounding_box().expect("visible elements");
assert_eq!(box_.x, 10.0);
assert_eq!(box_.y, 25.0);
assert_eq!(box_.width, 150.0);
assert_eq!(box_.height, 80.0);
let json = board.to_string_pretty().expect("serializes");
assert_eq!(Whiteboard::from_str(&json).expect("re-parses"), board);
}