use super::*;
#[test]
fn sample_trajectory_includes_terminal_click() {
let traj = sample_trajectory((0.0, 0.0), (100.0, 200.0), GrammarWeights::default(), 1);
assert!(traj
.motions
.iter()
.any(|m| matches!(m, Motion::Click { .. })));
}
#[test]
fn sample_trajectory_renders_minimum_sample_floor() {
for seed in 0u64..50 {
let traj = sample_trajectory((0.0, 0.0), (100.0, 200.0), GrammarWeights::default(), seed);
let mut rng = StdRng::seed_from_u64(seed);
let samples = traj.render(&mut rng);
assert!(
samples.len() >= 10,
"seed {seed}: got only {} samples",
samples.len()
);
}
}
#[test]
fn sample_trajectory_bezier_path_renders_at_least_40_samples() {
let w = GrammarWeights {
approach_bezier: 1.0,
approach_spline: 0.0,
approach_linear: 0.0,
..GrammarWeights::default()
};
let traj = sample_trajectory((0.0, 0.0), (200.0, 200.0), w, 7);
let mut rng = StdRng::seed_from_u64(7);
let samples = traj.render(&mut rng);
assert!(
samples.len() >= 40,
"bezier produced only {} samples",
samples.len()
);
}
#[test]
fn samples_end_near_target() {
let traj = sample_trajectory((0.0, 0.0), (500.0, 300.0), GrammarWeights::default(), 42);
let mut rng = StdRng::seed_from_u64(42);
let samples = traj.render(&mut rng);
let last = samples.last().unwrap();
assert!(
(last.x - 500.0).abs() < 50.0,
"last x {} too far from 500",
last.x
);
assert!(
(last.y - 300.0).abs() < 50.0,
"last y {} too far from 300",
last.y
);
}
#[test]
fn bezier_samples_smooth_between_endpoints() {
let mut rng = StdRng::seed_from_u64(0);
let samples = bezier_samples(
(0.0, 0.0),
(50.0, 100.0),
(100.0, 100.0),
(200.0, 0.0),
50,
0.01,
&mut rng,
);
let xs: Vec<f64> = samples.iter().map(|s| s.x).collect();
let mut increasing = 0;
for w in xs.windows(2) {
if w[1] >= w[0] {
increasing += 1;
}
}
let total = xs.len() - 1;
let ratio = (increasing as f64) / (total as f64);
assert!(
ratio > 0.80,
"bezier should be mostly monotonic in x; got {ratio:.2}"
);
}
#[test]
fn catmull_rom_passes_through_interior_points() {
let mut rng = StdRng::seed_from_u64(0);
let pts = vec![(0.0, 0.0), (100.0, 50.0), (200.0, 80.0), (300.0, 30.0)];
let samples = catmull_rom_samples(&pts, 100, &mut rng);
assert!(!samples.is_empty());
}
#[test]
fn overshoot_returns_two_samples_ending_at_target() {
let mut rng = StdRng::seed_from_u64(0);
let samples = overshoot_samples((100.0, 100.0), 20.0, 50, &mut rng);
assert_eq!(samples.len(), 2);
assert!((samples[1].x - 100.0).abs() < 0.01);
}
#[test]
fn dwell_holds_at_position() {
let mut rng = StdRng::seed_from_u64(0);
let samples = dwell_samples((50.0, 50.0), 100, 1.0, &mut rng);
for s in &samples {
assert!((s.x - 50.0).abs() < 5.0);
assert!((s.y - 50.0).abs() < 5.0);
}
}
#[test]
fn grammar_weights_default_sum_to_one_for_approach() {
let w = GrammarWeights::default();
let total = w.approach_bezier + w.approach_spline + w.approach_linear;
assert!((total - 1.0).abs() < 0.001);
}
#[test]
fn render_to_js_produces_async_iife() {
let samples = vec![
Sample {
x: 10.0,
y: 20.0,
dt_ms: 0,
},
Sample {
x: 30.0,
y: 40.0,
dt_ms: 15,
},
];
let js = render_to_js(&samples);
assert!(js.contains("async () =>"));
assert!(js.contains("MouseEvent"));
assert!(js.contains("clientX: 10"));
assert!(js.contains("clientY: 40"));
}
#[test]
fn render_to_js_does_not_use_set_timeout_zero_for_first_sample() {
let samples = vec![Sample {
x: 1.0,
y: 2.0,
dt_ms: 99,
}];
let js = render_to_js(&samples);
assert!(js.contains("setTimeout(r, 0)"));
}
#[test]
fn scale_10k_random_trajectories_render_cleanly() {
for seed in 0..10_000u64 {
let traj = sample_trajectory((0.0, 0.0), (500.0, 300.0), GrammarWeights::default(), seed);
let mut rng = StdRng::seed_from_u64(seed);
let samples = traj.render(&mut rng);
assert!(samples.len() >= 5);
}
}
#[test]
fn production_distribution_matches_grammar_weights() {
let w = GrammarWeights::default();
let n = 5_000;
let mut bezier = 0;
let mut spline = 0;
let mut linear = 0;
for seed in 0..n {
let traj = sample_trajectory((0.0, 0.0), (300.0, 200.0), w, seed);
match &traj.motions[0] {
Motion::Bezier { .. } => bezier += 1,
Motion::Spline { .. } => spline += 1,
Motion::LinearJittered { .. } => linear += 1,
_ => panic!("first motion was not an approach"),
}
}
let bf = bezier as f64 / n as f64;
let sf = spline as f64 / n as f64;
let lf = linear as f64 / n as f64;
assert!(
(bf - w.approach_bezier).abs() < 0.05,
"bezier rate {bf} != {} ± 0.05",
w.approach_bezier
);
assert!(
(sf - w.approach_spline).abs() < 0.05,
"spline rate {sf} != {} ± 0.05",
w.approach_spline
);
assert!(
(lf - w.approach_linear).abs() < 0.05,
"linear rate {lf} != {} ± 0.05",
w.approach_linear
);
}
#[test]
fn trajectory_reproducible_by_seed() {
let t1 = sample_trajectory((0.0, 0.0), (200.0, 200.0), GrammarWeights::default(), 99);
let t2 = sample_trajectory((0.0, 0.0), (200.0, 200.0), GrammarWeights::default(), 99);
assert_eq!(t1.motions, t2.motions);
}
#[test]
fn distinct_seeds_produce_distinct_trajectories() {
let mut seen = std::collections::HashSet::new();
let mut collisions = 0;
for seed in 0..1_000 {
let traj = sample_trajectory((0.0, 0.0), (200.0, 200.0), GrammarWeights::default(), seed);
let key = format!("{:?}", traj.motions);
if !seen.insert(key) {
collisions += 1;
}
}
assert!(
collisions < 5,
"{collisions} collisions out of 1000 trajectories"
);
}
proptest::proptest! {
#![proptest_config(proptest::test_runner::Config {
cases: 10_000, .. proptest::test_runner::Config::default()
})]
#[test]
fn prop_trajectory_renders_without_panic(
from_x in -1000.0..1000.0_f64,
from_y in -1000.0..1000.0_f64,
to_x in -1000.0..1000.0_f64,
to_y in -1000.0..1000.0_f64,
seed in 0u64..1_000_000,
) {
let traj = sample_trajectory((from_x, from_y), (to_x, to_y), GrammarWeights::default(), seed);
let mut rng = StdRng::seed_from_u64(seed);
let samples = traj.render(&mut rng);
assert!(!samples.is_empty());
}
#[test]
fn prop_render_to_js_contains_set_timeout_per_sample(
n_samples in 1usize..50,
) {
let samples: Vec<Sample> = (0..n_samples)
.map(|i| Sample { x: i as f64, y: i as f64, dt_ms: 10 })
.collect();
let js = render_to_js(&samples);
let count = js.matches("setTimeout").count();
assert_eq!(count, n_samples);
}
}