Cotton code
93 lines · 5.3 KB · RustRaw
//! The line the phone keeps of every group read.use super::*;use crate::ink::reader::Reader;use crate::ink::stream::Stream;use crate::ink::testing::{checks, timed, written, Hand, Recorded};use crate::ink::{Admits, Point, Run, CHECK};fn h() -> GroupRead {    let shapes = vec![vec![vec![0.0, 0.0], vec![0.0, 100.0]], vec![vec![100.0, 0.0], vec![100.0, 100.0]], vec![vec![0.0, 50.0], vec![100.0, 50.0]]];    let strokes = timed(&shapes, 300.0, Some(190.0), Point::new(200.0, 400.0), &[0.7, 0.7], 1_793_000_000.0, 1);    let mut model = Recorded::default();    let read = Stream::replay(&strokes, Reader::shipped(), Some(450.0), Admits::EVERY, &mut model);    model.all_read();    read[0].clone()}fn context(on_card: bool, tier: &str, letters: &str) -> Context {    Context { screen: [402.0, 640.0], on_card, tool: Some("person".into()), view: Some("people/all".into()), tier: tier.into(), letters: letters.into() }}/// RULE: a kept line is the shape every lane shares — strokes ([x, y, t]: pt, ms from the first touch), label (null), read, score, runners ([letter, score]), gaps (seconds), box, screen, where (card or ground) and at (ISO 8601) — with what else the phone knows beside it.#[test]fn a_line_is_the_shared_shape() {    let line = Captured::of(&h(), &context(true, "deck", "h"));    let json: serde_json::Value = serde_json::from_str(&line.line()).unwrap();    for key in ["strokes", "label", "read", "score", "runners", "gaps", "box", "screen", "where", "at"] {        assert!(json.get(key).is_some(), "{key}");    }    assert!(json["label"].is_null(), "the phone writes no label");    assert_eq!(json["read"], "h");    assert_eq!(json["where"], "card");    assert!(json["runners"][0][0].is_string() && json["runners"][0][1].is_f64(), "a runner is [letter, score]");    assert_eq!(json["strokes"].as_array().unwrap().len(), 3);    assert_eq!(json["strokes"][0][0][2].as_f64(), Some(0.0), "t counts from the group's first touch");    assert!((json["gaps"][0].as_f64().unwrap() - 0.7).abs() < 0.002, "gaps in seconds");    assert_eq!(json["screen"], serde_json::json!([402.0, 640.0]));    assert!(seconds(json["at"].as_str().unwrap()).is_some(), "at is ISO 8601");    assert_eq!(line.runs.as_ref().unwrap()[0].strokes, vec![0, 1, 2]);    assert_eq!(line.letters.as_deref(), Some("h"));    let keys: Vec<&String> = json.as_object().unwrap().keys().collect();    let mut sorted = keys.clone();    sorted.sort();    assert_eq!(keys, sorted, "keys in a steady order");}/// RULE: a line written by another lane with the shared keys alone reads: its strokes replay on their own times.#[test]fn the_shared_keys_alone_read() {    let text = r#"{"strokes":[[[10,10,0],[10,200,300]],[[120,10,900],[120,200,1200]],[[10,105,1900],[120,105,2200]]],"label":"h","read":"h","score":0.91,"runners":[["n",0.84]],"gaps":[0.6,0.7],"box":[10,10,110,190],"screen":[402,640],"where":"card","at":"2026-11-03T06:30:00.250Z"}"#;    let line: Captured = serde_json::from_str(text).unwrap();    assert_eq!(line.label.as_deref(), Some("h"));    assert_eq!(line.runners, vec![("n".to_string(), 0.84)]);    let timed = line.timed(0.0);    assert!((timed[1].down - timed[0].up - 0.6).abs() < 0.001);}/// RULE: a kept line read back replays as it was read: the same strokes on the same clock, the same letter.#[test]fn a_line_reads_back_as_written() {    let line = Captured::of(&h(), &Context { tier: "pick".into(), ..Default::default() });    let back: Captured = serde_json::from_str(&line.line()).unwrap();    assert_eq!(back, line);    let mut model = Recorded::default();    let replayed = Stream::replay(&back.timed(0.0), Reader::shipped(), back.height, Admits::EVERY, &mut model);    model.all_read();    assert_eq!(replayed.len(), 1);    assert_eq!(replayed[0].runs.iter().filter_map(|run| run.reading.letter()).collect::<Vec<_>>(), vec!["h"]);    assert!((back.timed(0.0)[1].down - back.timed(0.0)[0].up - 0.7).abs() < 0.002);}/// RULE: a line another lane wrote from a public dataset — strokes and a label, no reading, no time — reads, its label the letter meant.#[test]fn a_datasets_line_reads() {    let text = r#"{"strokes":[[[0,0,0],[0,120,96]]],"label":"l","case":"lower","writer":"w1","source":"ujipenchars2","input":"stylus"}"#;    let line: Captured = serde_json::from_str(text).unwrap();    assert_eq!(line.label.as_deref(), Some("l"));    assert_eq!(line.timed(10.0)[0].down, 10.0);}/// RULE: the capture keeps a check as it keeps the other marks, read `check`, and a scratch-out's run says how often it turned back.#[test]fn marks_are_kept_by_name() {    let stroke = written(&checks()[0].1, Hand::at(90.0, 200.0, 400.0));    let timed = TimedStroke { times: stroke.iter().map(|_| 0.0).collect(), points: stroke, down: 0.0, up: 1.0 };    let group = GroupRead { strokes: vec![timed.clone()], runs: vec![Run { strokes: vec![0], reading: Reading::Mark(CHECK.into()), runners: vec![] }], read: 1.0, height: None };    let line = Captured::of(&group, &context(true, "deck", ""));    assert_eq!(line.read, "check");    assert_eq!(line.runs.as_ref().unwrap()[0].read, "check");    let cleared = GroupRead { runs: vec![Run { strokes: vec![0], reading: Reading::Clear, runners: vec![] }], ..group };    let line = Captured::of(&cleared, &context(false, "deck", ""));    assert_eq!(line.runs.as_ref().unwrap()[0].reversals, Some(marks::turns(&timed.points).reversals));}