use crate::human::keyboard_event::key_to_code;
use crate::human::keystroke::{plan_keystrokes, TypingPlan};
use crate::human::telemetry::BehavioralEvent;
use crate::sampling::coefficient_of_variation;
use rand::{rngs::StdRng, SeedableRng};
#[must_use]
pub fn human_typing_stream(text: &str, seed: u64) -> Vec<BehavioralEvent> {
let mut rng = StdRng::seed_from_u64(seed);
let plan = TypingPlan::default();
let keys = plan_keystrokes(text, plan, &mut rng);
let mut ts = 0u64;
let mut events = Vec::with_capacity(keys.len() * 2);
for k in keys {
ts += u64::from(k.gap_ms_before);
let key = k.ch.to_string();
let code = key_to_code(k.ch);
events.push(BehavioralEvent::KeyDown {
ts,
key: key.clone(),
code: code.clone(),
});
ts += u64::from(k.hold_ms);
events.push(BehavioralEvent::KeyUp { ts, key, code });
}
events
}
#[must_use]
pub fn bot_typing_stream(text: &str, gap_ms: u64, hold_ms: u64) -> Vec<BehavioralEvent> {
let mut ts = 0u64;
let chs: Vec<char> = text.chars().collect();
let mut events = Vec::with_capacity(chs.len() * 2);
for ch in chs {
ts += gap_ms;
let key = ch.to_string();
let code = key_to_code(ch);
events.push(BehavioralEvent::KeyDown {
ts,
key: key.clone(),
code: code.clone(),
});
ts += hold_ms;
events.push(BehavioralEvent::KeyUp { ts, key, code });
}
events
}
pub fn timing_cv(events: &[BehavioralEvent]) -> Option<f64> {
let keydown_ts: Vec<f64> = events
.iter()
.filter_map(|e| match e {
BehavioralEvent::KeyDown { ts, .. } => Some(*ts as f64),
_ => None,
})
.collect();
if keydown_ts.len() < 3 {
return None;
}
let deltas: Vec<f64> = keydown_ts.windows(2).map(|w| w[1] - w[0]).collect();
coefficient_of_variation(&deltas)
}
pub fn lift_ratio(human_score: f64, bot_score: f64) -> f64 {
if bot_score == 0.0 {
return f64::INFINITY;
}
human_score / bot_score
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE_TEXT: &str = "the quick brown fox jumps over 7 lazy dogs";
#[test]
fn human_stream_clears_detector_floor() {
for seed in 0..16u64 {
let stream = human_typing_stream(SAMPLE_TEXT, seed);
let cv = timing_cv(&stream).expect("multi-event stream has a CV");
assert!(
cv >= crate::sampling::HUMAN_TIMING_CV_FLOOR,
"human stream CV {cv:.4} below detector floor {} (seed {seed})",
crate::sampling::HUMAN_TIMING_CV_FLOOR
);
}
}
#[test]
fn bot_stream_fails_detector_floor() {
let stream = bot_typing_stream(SAMPLE_TEXT, 120, 60);
let cv = timing_cv(&stream).expect("multi-event stream has a CV");
assert!(
cv < crate::sampling::HUMAN_TIMING_CV_FLOOR,
"uniform bot stream CV {cv:.4} should be below detector floor {}",
crate::sampling::HUMAN_TIMING_CV_FLOOR
);
}
#[test]
fn human_stream_has_quantifiable_lift_over_bot() {
let human_stream = human_typing_stream(SAMPLE_TEXT, 42);
let bot_stream = bot_typing_stream(SAMPLE_TEXT, 120, 60);
let human_cv = timing_cv(&human_stream).unwrap();
let bot_cv = timing_cv(&bot_stream).unwrap();
let lift = lift_ratio(human_cv, bot_cv);
assert!(
lift > 2.0,
"human CV {human_cv:.4} vs bot CV {bot_cv:.4} gives lift {lift:.2}x; expected >2x"
);
}
#[test]
fn bot_stream_cv_is_near_zero() {
let stream = bot_typing_stream("hello world", 100, 50);
let cv = timing_cv(&stream).unwrap();
assert!(cv < 0.001, "uniform cadence CV should be ~0, got {cv}");
}
#[test]
fn short_stream_has_no_cv() {
let stream = bot_typing_stream("ab", 100, 50);
assert_eq!(timing_cv(&stream), None);
}
}