Cotton code
92 lines · 4.5 KB · RustRaw
use super::*;fn words(spec: &[(&str, u64, u64)]) -> Vec<TimedWord> {    spec.iter().map(|(w, s, e)| TimedWord { w: w.to_string(), start_ms: *s, end_ms: *e, speaker: None }).collect()}fn spoken(text: &str, from: u64, each: u64) -> Vec<TimedWord> {    text.split_whitespace()        .enumerate()        .map(|(i, w)| TimedWord { w: w.into(), start_ms: from + i as u64 * each, end_ms: from + (i as u64 + 1) * each - 20, speaker: None })        .collect()}#[test]fn meeting_lines_split_at_speakers_sentences_and_silences() {    let mut said = spoken("so we ship friday full stop any questions", 0, 300);    said.extend(spoken("yes comma what about android question mark", 3000, 300));    for word in &mut said[..8] {        word.speaker = Some("A".into());    }    for word in &mut said[8..] {        word.speaker = Some("B".into());    }    let rules = Rules::of(&["en"], vec!["Android".into()]);    let lines = lines(&said, &rules, false);    let texts: Vec<(&str, &str)> = lines.iter().map(|l| (l.speaker.as_deref().unwrap(), l.text.as_str())).collect();    assert_eq!(texts, [("A", "So we ship friday."), ("A", "Any questions"), ("B", "Yes, what about Android?")]);    // The line ends where "full stop" was said.    assert_eq!((lines[0].start_ms, lines[0].end_ms), (0, 1780));    assert_eq!(lines[2].start_ms, 3000);    // A long silence mid-sentence ends the line too.    let gap = words(&[("and", 0, 200), ("then", 300, 500), ("later", 4000, 4300)]);    assert_eq!(cues_texts(&super::lines(&gap, &rules, false)), ["And then", "Later"]);}fn cues_texts(lines: &[Line]) -> Vec<&str> {    lines.iter().map(|l| l.text.as_str()).collect()}#[test]fn subtitle_cues_fit_two_lines_and_seven_seconds() {    let rules = Rules::of(&["en"], Vec::new());    let said = spoken(        "the river was high that spring comma higher than anyone in the village could remember full stop \         they moved the boats up to the old mill and waited",        0,        250,    );    let cues = cues(&said, &rules, false, CueRules::default());    for cue in &cues {        assert!(cue.lines.len() <= 2, "{cue:?}");        assert!(cue.lines.iter().all(|line| columns(line) <= 42), "{cue:?}");        assert!(cue.end_ms - cue.start_ms <= 7000, "{cue:?}");    }    let all: Vec<String> = cues.iter().map(|c| c.lines.join(" ")).collect();    assert_eq!(        all.join(" "),        "The river was high that spring, higher than anyone in the village could remember. They moved the boats up to the old mill and waited"    );    // The comma's break would leave a second line of 50 columns: the cue    // breaks where both lines fit, its sentence whole.    assert_eq!(cues[0].lines, ["The river was high that spring, higher", "than anyone in the village could remember."]);    // Cues never overlap.    for pair in cues.windows(2) {        assert!(pair[0].end_ms <= pair[1].start_ms);    }}#[test]fn japanese_cues_count_full_width_columns() {    let rules = Rules::of(&["ja"], Vec::new());    let said = words(&[("今日は朝から雨が降っていて", 0, 1500), ("句点", 1500, 1700), ("午後には晴れるでしょうと天気予報が言っていました", 1800, 4000)]);    let cues = cues(&said, &rules, false, CueRules { max_columns: 26, ..CueRules::default() });    for cue in &cues {        assert!(cue.lines.iter().all(|line| columns(line) <= 26), "{cue:?}");    }    let all: String = cues.iter().flat_map(|c| c.lines.clone()).collect();    assert_eq!(all, "今日は朝から雨が降っていて。午後には晴れるでしょうと天気予報が言っていました");}#[test]fn the_boundary_answers_the_rules() {    let formatted = crate::voice::call("voice.format", serde_json::json!({ "text": "hello comma ravi", "languages": ["en"], "vocabulary": ["Ravi"] })).unwrap();    assert_eq!(formatted["text"], "Hello, Ravi");    let after = crate::voice::call("voice.format", serde_json::json!({ "text": "then", "before": "Done.", "languages": ["en"] })).unwrap();    assert_eq!(after["text"], " Then");    let cues = crate::voice::call("voice.cues", serde_json::json!({ "words": [{ "w": "hi", "start_ms": 0, "end_ms": 300 }], "languages": ["en"] })).unwrap();    assert_eq!(cues, serde_json::json!([{ "start_ms": 0, "end_ms": 833, "lines": ["Hi"] }]));    let languages = crate::voice::call("voice.languages", serde_json::json!({})).unwrap();    assert_eq!(languages.as_array().unwrap().len(), 4);    assert!(crate::boundary::call("voice.format", r#"{"text":"ok"}"#).is_ok());}