//! The strokes as a hand writes them, on their own clock (owner, 3 Nov//! 2026: an H and an N written in separate tall strokes on the People//! Rolodex read "iii"): which strokes are one letter's is decided by when//! and where each was written, and a group is read as the fewest letters it//! makes. Every stroke here is synthetic, drawn from `ink-synthetic.json`'s//! shapes at a finger's pace; the page is 450 pt tall. The model's answers//! are Core ML's, recorded (`testing::Recorded`).usesuper::*;usecrate::ink::letters::{Edit, Letters};usecrate::ink::testing::{line, shape, timed, Recorded, SYNTHETIC_SHAPES};usecrate::ink::{shipped, Admits, Point, Reading, CHECK};const PAGE: f64 = 450.0;fn v(points: &[[f64; 2]]) -> Vec<Vec<f64>> { points.iter().map(|p| p.to_vec()).collect()}fn stem() -> Vec<Vec<f64>> { v(&[[0., 0.], [0., 100.]])}fn stem_right() -> Vec<Vec<f64>> { v(&[[100., 0.], [100., 100.]])}fn bar() -> Vec<Vec<f64>> { v(&[[0., 50.], [100., 50.]])}fn slash() -> Vec<Vec<f64>> { v(&[[0., 0.], [100., 100.]])}fn slash_up() -> Vec<Vec<f64>> { v(&[[0., 0.], [100., 100.], [100., 0.]])}fn at(x: f64, y: f64) -> Point {Point::new(x, y)}fn replay(strokes: &[TimedStroke], model: &mutRecorded) -> Vec<GroupRead> {Stream::replay(strokes, Reader::shipped(), Some(PAGE), Admits::LETTERS, model)}/// The letters the readout would show after `strokes`, read on their own/// clock through the grouping and the letters.fn letters(strokes: &[TimedStroke], model: &mutRecorded) -> String {let knobs = &shipped().knobs;letmut letters = Letters::default();for group in replay(strokes, model) {for run in group.runs {match run.reading {Reading::Letter { letter, .. } | Reading::Doubtful { letter, .. } => letters = letters.adding(&letter, false),Reading::Clear => letters = letters.applying(Edit::Clear),Reading::Line { from, to } => {ifletSome(edit) = letters.edit_by_line(from, to, knobs) { letters = letters.applying(edit); } }Reading::Mark(_) | Reading::Nothing => {} } } } letters.letters}/// The letters of `word` as a hand writes them (`natural`), 110 pt tall,/// side by side a seventh of that apart, each stroke `gap` after the last.fn side_by_side(word: &str, gap: f64) -> Vec<TimedStroke> {let size = 110.0;letmut strokes: Vec<TimedStroke> = Vec::new();letmut left = 40.0;for letter in word.chars() {let shape = SYNTHETIC_SHAPES.natural.get(&letter.to_string()).unwrap_or_else(|| panic!("no natural {letter}"));let xs: Vec<f64> = shape.iter().flatten().map(|p| p[0]).collect();let (min_x, max_x) = (xs.iter().cloned().fold(f64::INFINITY, f64::min), xs.iter().cloned().fold(f64::NEG_INFINITY, f64::max));let start = strokes.last().map(|stroke| stroke.up + gap).unwrap_or(1_000.0);let seed = strokes.len() as u64 + 1; strokes.extend(timed(shape, size, None, at(left + (50.0 - min_x) * size / 100.0, 400.0), &vec![gap; shape.len()], start, seed)); left += (max_x - min_x) * size / 100.0 + size / 7.0; } strokes}fn c_shape() -> Vec<Vec<f64>> { shape("c")}/// RULE: the owner's H and N — three tall strokes, or two — lifted 0.5 to 0.9 s apart, are each one group, never three; the H reads "h", never "iii".#[test]fn tall_h_and_n_written_slowly_are_one_letter() {letmut model = Recorded::default();for gap in [0.5, 0.7, 0.9] {for (name, strokes) in [ ("H |,|,-", vec![stem(), stem_right(), bar()]), ("H |,-,|", vec![stem(), bar(), stem_right()]), ("N |,\\,|", vec![stem(), slash(), stem_right()]), ("N |,\\|", vec![stem(), slash_up()]), ] {let written = timed(&strokes, 300.0, Some(190.0), at(200.0, 400.0), &[gap, gap], 1_000.0, 1);let groups = replay(&written, &mut model); assert_eq!(groups.iter().map(|group| group.strokes.len()).collect::<Vec<_>>(), vec![strokes.len()], "{name} at {gap} s gaps"); }for strokes in [vec![stem(), stem_right(), bar()], vec![stem(), bar(), stem_right()]] {let written = timed(&strokes, 300.0, Some(190.0), at(200.0, 400.0), &[gap, gap], 1_000.0, 1); assert_eq!(letters(&written, &mut model), "h", "an H at {gap} s gaps"); } } model.all_read();}/// RULE: a small H in three strokes 0.7 s apart is an H too: the wait is the stem's, not the size's.#[test]fn a_small_slow_h_is_an_h() {letmut model = Recorded::default();let written = timed(&[stem(), stem_right(), bar()], 110.0, Some(80.0), at(200.0, 400.0), &[0.7, 0.7], 1_000.0, 1); assert_eq!(letters(&written, &mut model), "h"); model.all_read();}/// RULE: a C then its stem 0.3 to 0.9 s later, touching it as a lower-case a's does, is one group.#[test]fn a_c_then_its_stem_is_one_group() {letmut model = Recorded::default();let bowl: Vec<Vec<f64>> = c_shape().iter().map(|p| vec![p[0] * 0.9, p[1]]).collect();for gap in [0.3, 0.6, 0.9] {let written = timed(&[bowl.clone(), v(&[[80., 0.], [80., 100.]])], 110.0, None, at(200.0, 400.0), &[gap], 1_000.0, 1); assert_eq!(replay(&written, &mut model).iter().map(|group| group.strokes.len()).collect::<Vec<_>>(), vec![2], "c then its stem {gap} s later"); } model.all_read();}/// RULE: "co" written beside each other, then a short stroke to the left, leaves "c": the stroke is a backspace, never part of the o.#[test]fn co_then_a_left_stroke_leaves_c() {letmut model = Recorded::default();for pause in [0.8, 1.3] {letmut written = timed(&[c_shape()], 110.0, None, at(120.0, 400.0), &[], 1_000.0, 1);let start = written.last().unwrap().up + 0.6; written.extend(timed(&[shape("o")], 110.0, None, at(250.0, 400.0), &[], start, 1));let start = written.last().unwrap().up + pause; written.extend(timed(&[v(&[[100., 50.], [0., 52.]])], 100.0, None, at(200.0, 300.0), &[], start, 1)); assert_eq!(letters(&written, &mut model), "c", "the left stroke {pause} s after the o"); } model.all_read();}/// RULE: a tall upright line alone is no letter, so a tall I then a C reads "c" however soon the C comes; a short one is a letter's stroke the model reads (an i or an l), so a short I then a C after the wait reads it and then "c".#[test]fn a_tall_lone_line_is_no_letter_and_a_short_one_is_an_i() {letmut model = Recorded::default();let knobs = &shipped().knobs;for gap in [0.3, 0.7, 1.2] {letmut tall = timed(&[stem()], 320.0, None, at(100.0, 400.0), &[], 1_000.0, 1);let start = tall.last().unwrap().up + gap; tall.extend(timed(&[c_shape()], 120.0, None, at(220.0, 400.0), &[], start, 1)); assert_eq!(letters(&tall, &mut model), "c", "a tall I then a C {gap} s later"); }letmut short = timed(&[stem()], 120.0, None, at(100.0, 400.0), &[], 1_000.0, 1);let start = short.last().unwrap().up + knobs.group_near_seconds + 0.2; short.extend(timed(&[c_shape()], 120.0, None, at(200.0, 400.0), &[], start, 1));let read = letters(&short, &mut model); assert!(read == "ic" || read == "lc", "a short I then a C after the wait reads {read}"); model.all_read();}/// RULE: an i's dot starts no letter — it is read with the stem before it — and a stroke written after the wait begins the next letter.#[test]fn a_dot_is_nothing_and_a_late_stroke_is_the_next_letter() {letmut model = Recorded::default();let knobs = &shipped().knobs;let dotted = timed(&[v(&[[50., 30.], [50., 100.]]), v(&[[50., 0.], [53., 5.]])], 100.0, None, at(200.0, 400.0), &[0.4], 1_000.0, 1); assert_eq!(letters(&dotted, &mut model), "i");let late_gap = knobs.group_near_seconds + 0.2;let late = timed(&[stem(), stem_right(), bar()], 300.0, Some(190.0), at(200.0, 400.0), &[late_gap, late_gap], 1_000.0, 1); assert_eq!(replay(&late, &mut model).len(), 3, "each stroke after the wait is a group of its own"); model.all_read();}/// RULE: a quick stroke far from the letter in progress is a letter of its own, however soon it comes.#[test]fn a_quick_stroke_far_away_is_a_new_letter() {letmut model = Recorded::default();letmut written = timed(&[stem()], 100.0, None, at(60.0, 400.0), &[], 1_000.0, 1);let start = written.last().unwrap().up + 0.2; written.extend(timed(&[c_shape()], 100.0, None, at(330.0, 400.0), &[], start, 1));let read = letters(&written, &mut model); assert!(read == "ic" || read == "lc", "an I then a C far away reads {read}"); model.all_read();}/// RULE: a finished letter waits `group_seconds` for another stroke, one that may be a longer letter begun (a stem, a bar, a bowl) `group_near_seconds`; a long level line is read at once.#[test]fn how_long_the_letter_in_progress_waits() {letmut model = Recorded::default();let knobs = &shipped().knobs;letmut stream = Stream::new(Reader::shipped(), Some(PAGE), Admits::LETTERS);let s = timed(&[shape("s")], 120.0, None, at(200.0, 400.0), &[], 1_000.0, 1).remove(0); assert!(stream.lift(s, &mut model).is_empty()); assert_eq!(stream.wait(&mut model), Some(knobs.group_seconds));letmut stream = Stream::new(Reader::shipped(), Some(PAGE), Admits::LETTERS);let _ = stream.lift(timed(&[stem()], 200.0, None, at(200.0, 400.0), &[], 1_000.0, 1).remove(0), &mut model); assert_eq!(stream.wait(&mut model), Some(knobs.group_near_seconds));letmut stream = Stream::new(Reader::shipped(), Some(PAGE), Admits::LETTERS);let swipe = timed(&[v(&[[100., 50.], [0., 51.]])], knobs.swipe_at_once + 40.0, None, at(200.0, 400.0), &[], 1_000.0, 1).remove(0); assert_eq!(stream.lift(swipe, &mut model).len(), 1, "the page swipe is read at once"); assert_eq!(stream.wait(&mut model), None); model.all_read();}/// RULE: an S, an H in three strokes and an O, written side by side a letter's space apart without a pause, read "sho", each letter read as the next one begins beside it.#[test]fn more_strokes_than_one_letter_read_the_first_letter() {letmut model = Recorded::default();let first = timed(&[shape("s")], 150.0, None, at(20.0, 550.0), &[], 1_000.0, 1);let h = timed(&[stem(), stem_right(), bar()], 300.0, Some(190.0), at(280.0, 400.0), &[0.3, 0.3], first[0].up + 0.3, 1);let o = timed(&[shape("o")], 100.0, None, at(480.0, 500.0), &[], h[2].up + 0.3, 1);let all: Vec<TimedStroke> = first.iter().chain(&h).chain(&o).cloned().collect();letmut stream = Stream::new(Reader::shipped(), Some(PAGE), Admits::LETTERS);letmut read = Vec::new();for stroke in &all { read.extend(stream.lift(stroke.clone(), &mut model)); }let said: Vec<String> = read.iter().flat_map(|group| group.runs.iter()).filter_map(|run| run.reading.letter().map(str::to_string)).collect(); assert_eq!(said, vec!["s", "h"], "the S and the H are read by the O's lift"); assert_eq!(letters(&all, &mut model), "sho"); model.all_read();}/// RULE: letters written side by side are never one letter (owner, 3 Nov 2026): "ca", "cl", "lo", "ab", "in" and "cat", each stroke 0.2–0.9 s after the last, read every letter written (a lone stem is the model's i or l).#[test]fn letters_side_by_side_read_each_letter() {letmut model = Recorded::default();for word in ["ca", "cl", "lo", "ab", "in", "cat"] {for gap in [0.2, 0.3, 0.4, 0.6, 0.9] {let read = letters(&side_by_side(word, gap), &mut model); assert_eq!(read.replace('i', "l"), word.replace('i', "l"), "{word} at {gap} s"); } } model.all_read();}/// RULE (owner, 3 Nov 2026: "Every time there's some time gap … it starts from the beginning. We don't need that"): "ca", a pause of 5 s, then "t" reads "cat"; so does a pause of a minute.#[test]fn a_pause_never_starts_the_letters_again() {letmut model = Recorded::default();let t = SYNTHETIC_SHAPES.natural["t"].len();for pause in [5.0, 60.0] {let cat = side_by_side("cat", 0.3);let count = cat.len();let later: Vec<TimedStroke> = cat .into_iter() .enumerate() .map(|(place, stroke)| {if place < count - t {return stroke; }TimedStroke { times: stroke.times.iter().map(|time| time + pause).collect(), down: stroke.down + pause, up: stroke.up + pause, points: stroke.points } }) .collect(); assert_eq!(letters(&later, &mut model), "cat", "t {pause} s after ca"); } model.all_read();}/// RULE: a letter's own strokes overlap or touch across, so they stay one letter however written: H, N, A, t, k and x, each stroke 0.5–0.9 s after the last, read one letter.#[test]fn a_letters_strokes_stay_one_letter() {letmut model = Recorded::default();for letter in ["H", "N", "A", "t", "k", "x"] {for gap in [0.5, 0.7, 0.9] { assert_eq!(letters(&side_by_side(letter, gap), &mut model), letter.to_lowercase(), "{letter} at {gap} s"); } } model.all_read();}/// RULE (owner, 3 Nov 2026: "a bit of a lag between writing a letter and that getting filled"): a letter written beside the letter in progress closes it at once: in "ca", "cat", "ab" and "co" every letter but the last is read by the lift of the next letter's first stroke.#[test]fn a_new_letter_closes_the_old_one_at_once() {letmut model = Recorded::default();for word in ["ca", "cat", "ab", "co"] {for gap in [0.2, 0.4, 0.9] {let strokes = side_by_side(word, gap);let reads = replay(&strokes, &mut model);for group in &reads[..reads.len() - 1] {let last_up = group.strokes.last().unwrap().up;let next = strokes.iter().find(|stroke| stroke.down > last_up).unwrap_or_else(|| panic!("{word} at {gap} s")); assert!(group.read <= next.up + 1e-9, "{word} at {gap} s: read by the next letter's first lift"); } assert_eq!(letters(&strokes, &mut model), word, "{word} at {gap} s"); } } model.all_read();}/// RULE: at every lift the letter in progress reads as it stands, shown provisionally: a bowl reads c, and with its stem it becomes the a in place; what shows at the last lift is what is read.#[test]fn the_letter_in_progress_reads_at_every_lift() {letmut model = Recorded::default();let bowl: Vec<Vec<f64>> = c_shape().iter().map(|p| vec![p[0] * 0.9, p[1]]).collect();let written = timed(&[bowl, v(&[[80., 0.], [80., 100.]])], 110.0, None, at(200.0, 400.0), &[0.3], 1_000.0, 1);letmut stream = Stream::new(Reader::shipped(), Some(PAGE), Admits::LETTERS); assert!(stream.lift(written[0].clone(), &mut model).is_empty());let said = |runs: Vec<crate::ink::Run>| runs.iter().filter_map(|run| run.reading.letter().map(str::to_string)).collect::<Vec<_>>(); assert_eq!(said(stream.reading(&mut model)), vec!["c"]); assert!(stream.lift(written[1].clone(), &mut model).is_empty(), "the stem joins the c");let shown = said(stream.reading(&mut model));let read: Vec<crate::ink::Run> = stream.flush(written[1].up + 1.0, &mut model).into_iter().flat_map(|group| group.runs).collect(); assert_eq!(said(read), shown); model.all_read();}/// RULE (owner, 3 Nov 2026): a scratch-out takes the letter in progress with it, at once — one clear over both, never the letter read first — and clears the letters before it.#[test]fn a_scratch_out_clears_the_letter_in_progress() {letmut model = Recorded::default();let ab = side_by_side("ab", 0.3);let bowl = timed(&SYNTHETIC_SHAPES.natural["c"], 110.0, None, at(400.0, 400.0), &[], ab.last().unwrap().up + 0.3, 1);let scratch = timed(&[v(&[[0., 45.], [100., 50.], [0., 55.], [100., 60.], [0., 65.]])], 140.0, None, at(400.0, 400.0), &[], bowl.last().unwrap().up + 0.3, 1);letmut stream = Stream::new(Reader::shipped(), Some(PAGE), Admits::LETTERS);for stroke in ab.iter().chain(&bowl) {let _ = stream.lift(stroke.clone(), &mut model); } assert!(!stream.group.is_empty(), "the c is in progress");let cleared = stream.lift(scratch[0].clone(), &mut model); assert!(stream.group.is_empty()); assert_eq!(cleared.last().unwrap().runs.iter().map(|run| run.reading.clone()).collect::<Vec<_>>(), vec![Reading::Clear], "one clear, at the scratch's lift"); assert_eq!(cleared.last().unwrap().read, scratch[0].up);let all: Vec<TimedStroke> = ab.iter().chain(&bowl).chain(&scratch).cloned().collect(); assert_eq!(letters(&all, &mut model), ""); model.all_read();}/// RULE: a check is a group of its own, read at its lift: a letter in progress before it is read first as it stands, and the check never joins it, however soon or near.#[test]fn a_check_is_a_group_of_its_own_read_at_once() {letmut model = Recorded::default();let tick = v(&[[0., 60.], [25., 85.], [100., 0.]]);let written = timed(&[tick.clone()], 90.0, None, at(200.0, 300.0), &[], 1_000.0, 1);letmut stream = Stream::new(Reader::shipped(), Some(PAGE), Admits::LETTERS);let read = stream.lift(written[0].clone(), &mut model); assert_eq!(read.iter().map(|group| group.runs.iter().map(|run| run.reading.clone()).collect::<Vec<_>>()).collect::<Vec<_>>(), vec![vec![Reading::Mark(CHECK.into())]]); assert!(stream.group.is_empty(), "nothing waits after a check");let after = timed(&[stem(), tick], 90.0, None, at(200.0, 300.0), &[0.1], 1_000.0, 1);let groups = replay(&after, &mut model); assert_eq!(groups.iter().map(|group| group.strokes.len()).collect::<Vec<_>>(), vec![1, 1], "the stem is read alone, then the check"); assert_eq!(groups.last().unwrap().runs[0].reading, Reading::Mark(CHECK.into())); model.all_read();}/// RULE: a tall upright line read alone against the page is a line, and the stroke written after the wait reads apart (no model needed).#[test]fn a_tall_line_against_the_page() {let reader = Reader::shipped();letmut model = Recorded::default();let tall = line(at(100.0, 100.0), at(102.0, 100.0 + PAGE * 0.7)); assert_eq!(reader.read(&tall, Some(PAGE), Admits::EVERY, &mut model), Reading::Line { from: at(100.0, 100.0), to: at(102.0, 100.0 + PAGE * 0.7) }); model.all_read();}/// RULE (the hands-on passes, 21 Nov 2026): a quick flick that hooks at its end is read at once as the line it is, never as a letter: on no letters it is the page swipe, and after letters a short one leftward takes the last back as a level line does; the model is never asked.#[test]fn a_quick_hooked_flick_is_never_a_letter() {letmut model = Recorded::default();letmut stream = Stream::new(Reader::shipped(), Some(PAGE), Admits::LETTERS);let points = vec![at(300.0, 400.0), at(260.0, 401.0), at(210.0, 400.0), at(160.0, 395.0), at(150.0, 370.0), at(155.0, 355.0)];let times: Vec<f64> = (0..points.len()).map(|i| 1_000.0 + i as f64 * 0.02).collect();let flick = TimedStroke { points: points.clone(), times, down: 1_000.0, up: 1_000.1 };let read = stream.lift(flick, &mut model); assert_eq!(read.len(), 1, "read at once"); assert_eq!(read[0].runs.len(), 1); assert_eq!(read[0].runs[0].reading, Reading::Line { from: points[0], to: points[5] }); assert_eq!(stream.wait(&mut model), None, "nothing waits after it");let (from, to) = (points[0], points[5]); assert_eq!(Letters::default().edit_by_line(from, to, &shipped().knobs), None, "on no letters: the page swipe");let written = Letters::default().adding("b", false); assert!(written.edit_by_line(from, to, &shipped().knobs).is_some(), "after a letter: it takes it back"); model.all_read();}