use super::*;
use rand::SeedableRng;
#[test]
fn hot_bigrams_table_is_lowercase_and_two_chars() {
for (k, _, _) in HOT_BIGRAMS {
assert_eq!(k.len(), 2, "bigram key {} not 2 chars", k);
assert_eq!(*k, k.to_lowercase(), "bigram key {} not lowercase", k);
}
}
#[test]
fn bigram_gap_uses_hot_table_for_th() {
let (lo, hi) = bigram_gap('t', 'h');
assert_eq!(lo, 60);
assert_eq!(hi, 100);
}
#[test]
fn bigram_gap_is_case_insensitive() {
assert_eq!(bigram_gap('T', 'h'), bigram_gap('t', 'h'));
assert_eq!(bigram_gap('t', 'H'), bigram_gap('t', 'h'));
assert_eq!(bigram_gap('T', 'H'), bigram_gap('t', 'h'));
}
#[test]
fn bigram_gap_falls_back_to_cold_envelope() {
assert_eq!(
bigram_gap('q', 'z'),
(COLD_BIGRAM_GAP_MIN_MS, COLD_BIGRAM_GAP_MAX_MS)
);
assert_eq!(
bigram_gap('x', 'q'),
(COLD_BIGRAM_GAP_MIN_MS, COLD_BIGRAM_GAP_MAX_MS)
);
}
#[test]
fn space_transition_uses_space_envelope() {
let (lo, hi) = bigram_gap('e', ' ');
assert_eq!(lo, SPACE_GAP_MIN_MS);
assert_eq!(hi, SPACE_GAP_MAX_MS);
}
#[test]
fn digit_pair_uses_digit_envelope() {
let (lo, hi) = bigram_gap('5', '7');
assert_eq!(lo, DIGIT_GAP_MIN_MS);
assert_eq!(hi, DIGIT_GAP_MAX_MS);
}
#[test]
fn digit_to_letter_uses_cold_or_hot_table() {
let (lo, hi) = bigram_gap('5', 'a');
assert_eq!(lo, COLD_BIGRAM_GAP_MIN_MS);
assert_eq!(hi, COLD_BIGRAM_GAP_MAX_MS);
}
#[test]
fn hold_envelope_matches_class_buckets() {
let (lo, _) = hold_envelope('a');
assert_eq!(lo, 30);
let (lo_u, _) = hold_envelope('A');
assert_eq!(lo_u, 50);
let (lo_d, _) = hold_envelope('7');
assert_eq!(lo_d, 45);
let (lo_s, _) = hold_envelope(' ');
assert_eq!(lo_s, 35);
}
#[test]
fn qwerty_neighbour_for_letters_returns_a_neighbour() {
for ch in 'a'..='z' {
let n = qwerty_neighbour(ch, 0);
assert!(n.is_some(), "no neighbour table for {}", ch);
let nch = n.unwrap();
assert_ne!(nch, ch, "neighbour of {} is itself", ch);
}
}
#[test]
fn qwerty_neighbour_for_non_letters_is_none() {
assert!(qwerty_neighbour('5', 0).is_none());
assert!(qwerty_neighbour('!', 0).is_none());
assert!(qwerty_neighbour(' ', 0).is_none());
}
#[test]
fn plan_keystrokes_no_typos_no_pauses_preserves_length() {
let mut rng = StdRng::seed_from_u64(1);
let plan = TypingPlan {
typo_probability: 0.0,
thinking_pause_probability: 0.0,
..Default::default()
};
let keys = plan_keystrokes("the quick", plan, &mut rng);
assert_eq!(keys.len(), 9);
assert!(keys.iter().all(|k| !k.is_correction));
}
#[test]
fn plan_keystrokes_with_typos_inserts_correction_pairs() {
let mut rng = StdRng::seed_from_u64(42);
let plan = TypingPlan {
typo_probability: 1.0, thinking_pause_probability: 0.0,
..Default::default()
};
let keys = plan_keystrokes("ab", plan, &mut rng);
assert_eq!(keys.len(), 6);
assert!(keys[0].is_correction); assert_eq!(keys[1].ch, '\u{0008}'); assert!(keys[1].is_correction);
assert!(!keys[2].is_correction); assert_eq!(keys[2].ch, 'a');
assert!(keys[3].is_correction); assert_eq!(keys[4].ch, '\u{0008}');
assert_eq!(keys[5].ch, 'b');
}
#[test]
fn plan_keystrokes_typos_skip_non_alpha() {
let mut rng = StdRng::seed_from_u64(42);
let plan = TypingPlan {
typo_probability: 1.0,
thinking_pause_probability: 0.0,
..Default::default()
};
let keys = plan_keystrokes("a 5 b", plan, &mut rng);
assert_eq!(keys.len(), 9);
}
#[test]
fn plan_keystrokes_thinking_pauses_extend_gaps() {
let mut rng = StdRng::seed_from_u64(7);
let plan = TypingPlan {
typo_probability: 0.0,
thinking_pause_probability: 1.0,
..Default::default()
};
let keys = plan_keystrokes("the", plan, &mut rng);
assert_eq!(keys.len(), 3);
assert_eq!(keys[0].gap_ms_before, 0);
for k in &keys[1..] {
assert!(
k.gap_ms_before >= 200,
"expected thinking pause to extend gap to ≥200ms, got {}",
k.gap_ms_before,
);
}
}
#[test]
fn plan_keystrokes_hot_bigrams_produce_fast_gaps() {
let mut rng = StdRng::seed_from_u64(1234);
let plan = TypingPlan {
typo_probability: 0.0,
thinking_pause_probability: 0.0,
..Default::default()
};
let keys = plan_keystrokes("the", plan, &mut rng);
assert_eq!(keys[0].gap_ms_before, 0);
assert!(
keys[1].gap_ms_before >= 60 && keys[1].gap_ms_before <= 100,
"t→h bigram should give 60–100ms, got {}",
keys[1].gap_ms_before,
);
assert!(
keys[2].gap_ms_before >= 65 && keys[2].gap_ms_before <= 105,
"h→e bigram should give 65–105ms, got {}",
keys[2].gap_ms_before,
);
}
#[test]
fn center_weighted_peaks_at_envelope_center_not_flat() {
let mut rng = StdRng::seed_from_u64(7);
let (lo, hi) = (180u16, 320u16); let third = (hi - lo) / 3;
let (mid_lo, mid_hi) = (lo + third, hi - third);
let n = 40_000usize;
let mut middle = 0usize;
for _ in 0..n {
let v = center_weighted(&mut rng, lo, hi);
assert!(v >= lo && v <= hi, "sample {v} escaped [{lo},{hi}]");
if v >= mid_lo && v <= mid_hi {
middle += 1;
}
}
let middle_frac = middle as f64 / n as f64;
assert!(
middle_frac > 0.45,
"central-third mass {middle_frac:.3} ≈ uniform (0.33), the sampler is flat, \
not center-weighted; a real typist's per-bigram latency is unimodal"
);
assert_eq!(center_weighted(&mut rng, 90, 90), 90);
}
#[test]
fn plan_keystrokes_first_keystroke_has_zero_gap() {
let mut rng = StdRng::seed_from_u64(0);
let plan = TypingPlan::default();
let keys = plan_keystrokes("a", plan, &mut rng);
assert_eq!(keys.len(), 1);
assert_eq!(keys[0].gap_ms_before, 0);
}
#[test]
fn plan_keystrokes_post_backspace_recovery_within_envelope() {
let mut rng = StdRng::seed_from_u64(99);
let plan = TypingPlan {
typo_probability: 1.0,
thinking_pause_probability: 0.0,
..Default::default()
};
let keys = plan_keystrokes("a", plan, &mut rng);
assert_eq!(keys.len(), 3);
assert!(
keys[2].gap_ms_before >= 60 && keys[2].gap_ms_before <= 180,
"post-backspace recovery should be 60–180ms, got {}",
keys[2].gap_ms_before,
);
}
#[test]
fn generated_typing_rhythm_clears_human_cv_floor() {
use crate::sampling::{coefficient_of_variation, HUMAN_TIMING_CV_FLOOR};
let text = "the quick brown fox jumps over 7 lazy dogs while typing fairly naturally";
let plan = TypingPlan::default();
let mut worst = f64::INFINITY;
let mut worst_seed = 0u64;
for seed in 0..64u64 {
let mut rng = StdRng::seed_from_u64(seed);
let keys = plan_keystrokes(text, plan, &mut rng);
let gaps: Vec<f64> = keys
.iter()
.skip(1)
.map(|k| k.gap_ms_before as f64)
.collect();
let cv = coefficient_of_variation(&gaps).expect("multi-key sample has a CV");
if cv < worst {
worst = cv;
worst_seed = seed;
}
}
assert!(
worst >= HUMAN_TIMING_CV_FLOOR,
"generated typing CV {worst:.4} (seed {worst_seed}) < human floor \
{HUMAN_TIMING_CV_FLOOR}, the emitted rhythm is too uniform; the same \
metric our probe gates a browser on would flag this disguise"
);
}
#[test]
fn typo_and_pause_injection_widen_dispersion() {
use crate::sampling::coefficient_of_variation;
let text = "the quick brown fox jumps over the lazy dog";
let bare = TypingPlan {
typo_probability: 0.0,
thinking_pause_probability: 0.0,
..Default::default()
};
let rich = TypingPlan::default();
let cv_of = |plan: TypingPlan, seed: u64| {
let mut rng = StdRng::seed_from_u64(seed);
let keys = plan_keystrokes(text, plan, &mut rng);
let gaps: Vec<f64> = keys
.iter()
.skip(1)
.map(|k| k.gap_ms_before as f64)
.collect();
coefficient_of_variation(&gaps).expect("multi-key sample has a CV")
};
let bare_cv = cv_of(bare, 1);
assert!(
bare_cv >= crate::sampling::HUMAN_TIMING_CV_FLOOR,
"bare bigram rhythm CV {bare_cv:.4} should already be human-shaped"
);
let mean_bare: f64 = (0..32).map(|s| cv_of(bare, s)).sum::<f64>() / 32.0;
let mean_rich: f64 = (0..32).map(|s| cv_of(rich, s)).sum::<f64>() / 32.0;
assert!(
mean_rich >= mean_bare - 0.02,
"rich plan dispersion {mean_rich:.4} collapsed below bare {mean_bare:.4}"
);
}