Cotton code
151 lines · 10.1 KB · RustRaw
use super::*;fn line(speaker: &str, start_ms: i64, end_ms: i64, text: &str) -> Line {    Line { speaker: speaker.into(), start_ms, end_ms, text: text.into(), language: None }}fn heard(index: usize, start_ms: i64, end_ms: i64, lines: Vec<Line>) -> WindowHeard {    WindowHeard { index, start_ms, end_ms, language: None, lines, speakers: Vec::new() }}/// RULE (meetings-core, 17 Nov 2026): Long audio is cut in windows of about thirty seconds sharing three with the next, every moment covered, the last ending with the audio.#[test]fn long_audio_is_cut_in_overlapping_windows_that_cover_it() {    let cut = windows(95_000, &[], WINDOW_MS, OVERLAP_MS);    assert_eq!(cut.iter().map(|w| (w.start_ms, w.end_ms)).collect::<Vec<_>>(), [(0, 30_000), (27_000, 57_000), (54_000, 84_000), (81_000, 95_000)]);    assert_eq!(cut.iter().map(|w| w.index).collect::<Vec<_>>(), [0, 1, 2, 3]);    assert!(windows(0, &[], WINDOW_MS, OVERLAP_MS).is_empty());    assert_eq!(windows(10_000, &[], WINDOW_MS, OVERLAP_MS).len(), 1);}/// RULE (meetings-core, 17 Nov 2026): With voice activity given, a cut moves into the nearest silence within five seconds, and a window with no speech in it is left out.#[test]fn a_cut_lands_in_silence_and_a_silent_window_is_left_out() {    let speech = [Span { start_ms: 0, end_ms: 28_500 }, Span { start_ms: 29_500, end_ms: 50_000 }, Span { start_ms: 150_000, end_ms: 160_000 }];    let cut = windows(170_000, &speech, WINDOW_MS, OVERLAP_MS);    assert_eq!(cut[0].end_ms, 29_000, "the first cut is the middle of the pause at 28.5–29.5 s");    for w in &cut {        assert!(speech.iter().any(|s| s.start_ms < w.end_ms && s.end_ms > w.start_ms), "{w:?} holds speech");    }    assert!(cut.iter().any(|w| w.start_ms <= 150_000 && w.end_ms >= 160_000 || (w.start_ms < 160_000 && w.end_ms > 150_000)));    assert_eq!(cut.iter().map(|w| w.index).collect::<Vec<_>>(), (0..cut.len()).collect::<Vec<_>>());}/// RULE (meetings-core, 17 Nov 2026): Where two windows share audio, a line is kept once: by the earlier window when it starts before the middle of the shared stretch, else by the later.#[test]fn a_line_in_the_overlap_is_kept_once() {    let a = heard(0, 0, 30_000, vec![line("A", 1_000, 5_000, "hello there"), line("A", 27_500, 29_900, "shared words")]);    let b = heard(1, 27_000, 57_000, vec![line("X", 27_400, 29_800, "shared words"), line("X", 29_000, 31_000, "after the middle")]);    let t = merge("test", &[a, b]);    let texts: Vec<&str> = t.lines.iter().map(|l| l.text.as_str()).collect();    assert_eq!(texts, ["hello there", "shared words", "after the middle"]);}/// RULE (meetings-core, 17 Nov 2026): Voices are one set across windows: by embedding when the engine gives one, else by speaking at the same moment in the shared stretch; labels run S1, S2… in order of first speech.#[test]fn speakers_are_one_set_across_windows() {    let mut a = heard(0, 0, 30_000, vec![line("b", 0, 2_000, "first"), line("a", 3_000, 5_000, "second")]);    a.speakers = vec![WindowSpeaker { label: "a".into(), embedding: Some(vec![1.0, 0.0]) }, WindowSpeaker { label: "b".into(), embedding: Some(vec![0.0, 1.0]) }];    let mut b = heard(1, 27_000, 57_000, vec![line("z", 40_000, 42_000, "third"), line("y", 43_000, 45_000, "fourth")]);    b.speakers = vec![WindowSpeaker { label: "z".into(), embedding: Some(vec![0.1, 0.99]) }, WindowSpeaker { label: "y".into(), embedding: Some(vec![0.0, 0.0001]) }];    let t = merge("test", &[a, b]);    let who: Vec<(&str, &str)> = t.lines.iter().map(|l| (l.speaker.as_str(), l.text.as_str())).collect();    assert_eq!(who, [("S1", "first"), ("S2", "second"), ("S1", "third"), ("S3", "fourth")]);    assert_eq!(t.speakers.len(), 3);    // No embeddings: matched by speaking together in the overlap.    let a = heard(0, 0, 30_000, vec![line("p", 1_000, 2_000, "one"), line("q", 27_200, 28_000, "two")]);    let b = heard(1, 27_000, 57_000, vec![line("m", 27_100, 28_100, "two"), line("m", 40_000, 41_000, "three")]);    let t = merge("test", &[a, b]);    let who: Vec<(&str, &str)> = t.lines.iter().map(|l| (l.speaker.as_str(), l.text.as_str())).collect();    assert_eq!(who, [("S1", "one"), ("S2", "two"), ("S2", "three")]);}/// RULE (meetings-core, 17 Nov 2026): A line carries its own language, else its window's; the transcript lists them in order of first speech, so a meeting that switches language is followed line by line.#[test]fn a_switch_of_language_is_followed_line_by_line() {    let mut a = heard(0, 0, 30_000, vec![line("a", 0, 2_000, "good morning")]);    a.language = Some("en".into());    let mut b = heard(1, 27_000, 57_000, vec![line("a", 35_000, 37_000, "नमस्ते")]);    b.language = Some("hi".into());    b.lines.push(Line { language: Some("en".into()), ..line("a", 40_000, 41_000, "okay") });    let t = merge("test", &[a, b]);    assert_eq!(t.languages, ["en", "hi"]);    assert_eq!(t.lines.iter().map(|l| l.language.as_deref().unwrap()).collect::<Vec<_>>(), ["en", "hi", "en"]);}/// RULE (meetings-core, 17 Nov 2026): A transcription again keeps every line a person corrected, over the new line it overlaps.#[test]fn a_corrected_line_survives_a_new_transcription() {    let fresh = merge("test", &[heard(0, 0, 30_000, vec![line("a", 0, 2_000, "helo"), line("a", 3_000, 4_000, "world")])]);    let held = [HeldLine { start_ms: 0, end_ms: 2_100, text: "Hello".into(), speaker: "S1".into(), language: None }];    let kept = keep_corrections(&fresh, &held);    assert_eq!(kept.lines.iter().map(|l| l.text.as_str()).collect::<Vec<_>>(), ["Hello", "world"]);}/// RULE (meetings-core, 17 Nov 2026): The open bundle's text names each turn's speaker and time once, its SRT is one cue a line, and a speaker's given name is used where one was given.#[test]fn the_bundle_says_who_spoke_when() {    let t = merge("test", &[heard(0, 0, 4_000_000, vec![line("a", 0, 1_500, "Hi."), line("a", 1_600, 2_000, "Ready?"), line("b", 3_723_004, 3_725_000, "Yes.")])]);    let names = vec![("S1".to_string(), "Asha".to_string())];    assert_eq!(to_text("Standup", &t, &names), "Standup\n\nAsha [0:00]\nHi. Ready?\n\nS2 [1:02:03]\nYes.\n");    assert_eq!(to_srt(&t, &names).lines().take(4).collect::<Vec<_>>(), ["1", "00:00:00,000 --> 00:00:01,500", "Asha: Hi.", ""]);    assert!(to_srt(&t, &names).contains("3\n01:02:03,004 --> 01:02:05,000\nS2: Yes.\n"));    let json = to_json("Standup", &t, &names);    assert_eq!(json["speakers"][0]["name"], "Asha");    assert!(json["speakers"][1]["name"].is_null());}/// RULE (meetings-core, 17 Nov 2026): Word error rate counts substitutions, deletions and insertions against the reference's words, punctuation and case aside.#[test]fn word_errors_count_edits_over_reference_words() {    assert_eq!(word_errors("The cat sat.", "the cat sat"), (0, 3));    assert_eq!(word_errors("the cat sat on the mat", "the bat sat on mat"), (2, 6));    assert_eq!(word_errors("नमस्ते दोस्त", "नमस्ते"), (1, 2));    let figures = speaker_figures(&merge("t", &[heard(0, 0, 9_000, vec![line("a", 0, 1_000, "x"), line("b", 2_000, 5_000, "y"), line("a", 6_000, 6_500, "z")])]));    assert_eq!(figures, [SpeakerFigures { label: "S1".into(), lines: 2, talk_ms: 1_500, first_ms: 0 }, SpeakerFigures { label: "S2".into(), lines: 1, talk_ms: 3_000, first_ms: 2_000 }]);}/// RULE (meetings-comms, 23 Nov 2026): A phone that cannot tell voices apart hands lines with no speaker; they land as one voice, S1, across every window, never refused and never one new speaker per window.#[test]fn lines_with_no_speaker_are_one_voice_across_windows() {    let heard: Vec<WindowHeard> = serde_json::from_value(serde_json::json!([        { "index": 0, "start_ms": 0, "end_ms": 30_000, "lines": [{ "start_ms": 1_000, "end_ms": 4_000, "text": "first" }] },        { "index": 1, "start_ms": 27_000, "end_ms": 57_000, "lines": [{ "start_ms": 40_000, "end_ms": 42_000, "text": "second" }] },        { "index": 2, "start_ms": 54_000, "end_ms": 84_000, "lines": [{ "start_ms": 70_000, "end_ms": 72_000, "text": "third" }] }    ]))    .expect("a line may come without a speaker");    let t = merge("phone:test", &heard);    let who: Vec<(&str, &str)> = t.lines.iter().map(|l| (l.speaker.as_str(), l.text.as_str())).collect();    assert_eq!(who, [("S1", "first"), ("S1", "second"), ("S1", "third")]);    assert_eq!(t.speakers.len(), 1);}/// RULE (owner, 23 Nov 2026; chat CONTRACT "Take notes"): a call's notes are one track per person, so who said what/// is the track's: each track is one voice across its windows whatever labels its engine gave, and the tracks'/// lines are laid together by time.#[test]fn a_calls_tracks_are_one_voice_each_laid_together_by_time() {    let line = |speaker: &str, start: i64, end: i64, text: &str| Line { speaker: speaker.into(), start_ms: start, end_ms: end, text: text.into(), language: Some("en".into()) };    let window = |index: usize, start: i64, end: i64, lines: Vec<Line>| WindowHeard { index, start_ms: start, end_ms: end, language: None, lines, speakers: vec![] };    let me = TrackHeard {        track: "me".into(),        windows: vec![            window(0, 0, 30_000, vec![line("S1", 1_000, 3_000, "Shall we book the boat house?"), line("S2", 20_000, 22_000, "Ten o'clock then.")]),            window(1, 25_000, 60_000, vec![line("S1", 40_000, 42_000, "I will pay the deposit.")]),        ],    };    let asha = TrackHeard { track: "m-asha".into(), windows: vec![window(0, 0, 60_000, vec![line("S1", 4_000, 6_000, "Yes, by Wednesday."), line("", 30_000, 31_000, "And the maps.")])] };    let silent = TrackHeard { track: "m-ravi".into(), windows: vec![window(0, 0, 60_000, vec![])] };    let heard = merge_tracks("phone:test", &[me, asha, silent]);    let said: Vec<(&str, &str)> = heard.lines.iter().map(|l| (l.speaker.as_str(), l.text.as_str())).collect();    assert_eq!(said, [        ("me", "Shall we book the boat house?"),        ("m-asha", "Yes, by Wednesday."),        ("me", "Ten o'clock then."),        ("m-asha", "And the maps."),        ("me", "I will pay the deposit."),    ]);    assert_eq!(heard.speakers.iter().map(|s| s.label.as_str()).collect::<Vec<_>>(), ["me", "m-asha"], "a track that said nothing is no voice");    assert_eq!(heard.languages, ["en"]);}