asr-core 0.0.3

Unified streaming, offline, and cloud speech-to-text engine for Rust
Documentation
use super::*;

#[test]
fn growing_snapshots_become_appending_deltas() {
    let mut deltas = TextDeltaBuilder::new();
    assert_eq!(deltas.update("hello").as_deref(), Some("hello"));
    assert_eq!(deltas.update("hello world").as_deref(), Some(" world"));
    assert_eq!(deltas.update("hello world!").as_deref(), Some("!"));
    assert_eq!(deltas.sent(), "hello world!");
}

#[test]
fn a_rollback_snapshot_sends_nothing_and_resyncs_on_reextension() {
    // The client accumulates by appending, so a revision that rewrites
    // sent text must not travel as a delta — appending it would produce
    // "hello worlhello world" on the client. The builder waits on the
    // last sent text; when a snapshot extends it again ("hello world"
    // extends "hello worl"), exactly the missing suffix travels and the
    // client converges.
    let mut deltas = TextDeltaBuilder::new();
    assert_eq!(deltas.update("hello worl").as_deref(), Some("hello worl"));
    assert_eq!(deltas.update("hello").as_deref(), None);
    assert_eq!(deltas.update("help").as_deref(), None);
    assert_eq!(deltas.update("hello world").as_deref(), Some("d"));
    assert_eq!(deltas.sent(), "hello world");
}

#[test]
fn a_corrected_utterance_skips_until_done_carries_the_truth() {
    // ASR case: the recognizer revises a character ("号" → "好") after the
    // first reading already traveled. The revised snapshot shares a prefix
    // but rewrites sent text, so no expressible delta exists — the builder
    // skips it and the client's partial lags until the done frame replaces
    // the whole text. Appending instead would corrupt the client's text
    // into "今天天气很号很好".
    let mut deltas = TextDeltaBuilder::new();
    assert_eq!(
        deltas.update("今天天气很号").as_deref(),
        Some("今天天气很号")
    );
    assert_eq!(deltas.update("今天天气很好").as_deref(), None);
    assert_eq!(deltas.sent(), "今天天气很号");
}

#[test]
fn unchanged_snapshots_yield_no_delta() {
    let mut deltas = TextDeltaBuilder::new();
    assert_eq!(deltas.update("你好").as_deref(), Some("你好"));
    assert_eq!(deltas.update("你好").as_deref(), None);
}

#[test]
fn a_snapshot_that_only_shrinks_still_blocks_until_reextension() {
    // Committed text never shrinks, but a Reset self-heal could deliver a
    // shorter view; until something extends what was sent, nothing is
    // expressible on the wire.
    let mut deltas = TextDeltaBuilder::new();
    assert_eq!(
        deltas.update("你好世界再见").as_deref(),
        Some("你好世界再见")
    );
    assert_eq!(deltas.update("你好").as_deref(), None);
    assert_eq!(deltas.update("你好世界再见!").as_deref(), Some("!"));
}