// The dictated region against its two writers: the core's settled text and // corrections, and the person's typing mid-dictation. @testable import CottonVoice import CottonKit import XCTest final class VoiceRegionTests: XCTestCase { /// RULE: The field shows the core's settled words, then the tentative ones, and the tentative range is where they sit. func testTheFieldShowsSettledThenTentative() { var region = VoiceRegion() region.take(settled: "Hello there.", tentative: " how are", edits: []) XCTAssertEqual(region.display, "Hello there. how are") XCTAssertEqual(region.tentativeRange, NSRange(location: 12, length: 8)) } /// RULE: A second pass's correction moves the settled text by the core's edits and is marked as corrected. func testASecondPassCorrectionIsAppliedAndMarked() { var region = VoiceRegion() region.take(settled: "I met Sita at the station.", tentative: "", edits: []) region.take(settled: "I met Seetha at the station.", tentative: "", edits: [VoiceEdit(from: 6, to: 10, text: "Seetha")]) XCTAssertEqual(region.settled, "I met Seetha at the station.") XCTAssertEqual(region.corrected, [NSRange(location: 6, length: 6)]) } /// RULE: Edits that do not reproduce the core's settled text are not trusted: the difference of the two texts is applied. func testEditsThatDisagreeWithTheSettledTextFallBackToTheDifference() { var region = VoiceRegion() region.take(settled: "one two three", tentative: "", edits: []) region.take(settled: "one too three", tentative: "", edits: [VoiceEdit(from: 0, to: 3, text: "nonsense")]) XCTAssertEqual(region.settled, "one too three") } /// RULE: The person's edit mid-dictation is reported in the core's coordinates and is left alone by every later answer. func testAnEditMidDictationIsLeftAlone() { var region = VoiceRegion() region.take(settled: "Ravi walks in.", tentative: " He sits", edits: []) // The person replaces "Ravi" with "RAVI" while the microphone is open. let touch = region.typed("RAVI walks in. He sits") // The smallest range that changed: the R stood. XCTAssertEqual(touch, VoiceRegion.Touch(from: 1, to: 4, text: "AVI")) // The core goes on, unaware of the person's text: it still says Ravi, // and adds a sentence and a correction later in the text. region.take(settled: "Ravi walks in. He sits down.", tentative: "", edits: []) XCTAssertEqual(region.settled, "RAVI walks in. He sits down.") region.take(settled: "Ravi walks in. He sat down.", tentative: "", edits: []) XCTAssertEqual(region.settled, "RAVI walks in. He sat down.") } /// RULE: Typing at the end of the session's text joins it as the person's own, offsets counting the tentative words too. func testTypingAtTheEndIsThePersonsOwn() { var region = VoiceRegion() region.take(settled: "Buy milk.", tentative: " and", edits: []) XCTAssertEqual(region.typed("Buy milk. and eggs"), VoiceRegion.Touch(from: 13, to: 13, text: " eggs")) } /// RULE: The core's answer to a touch has the person's change applied, and its edits start from there. func testATouchedAnswerLetsThePendingChangeGo() { var region = VoiceRegion() region.take(settled: "Ravi walks in.", tentative: "", edits: []) let touch = region.typed("Mira walks in.")! XCTAssertEqual(region.pending.count, 1) region.acknowledge(touch) region.take(settled: "Mira walks in.", tentative: " She", edits: [VoiceEdit(from: 14, to: 14, text: " She")]) XCTAssertTrue(region.pending.isEmpty) XCTAssertEqual(region.display, "Mira walks in. She") XCTAssertEqual(region.tentative, " She") } /// RULE: Letters typed one by one while the core answers in between are each the person's, once, in order (the walk's "Quuietly"). func testKeystrokesWhileTheCoreAnswersArePutDownOnceEach() { // A core that applies touches in order, as the real one does. var coreText = "Ravi, the tra" var region = VoiceRegion() region.take(settled: "Ravi,", tentative: " the tra", edits: []) var sent: [VoiceRegion.Touch] = [] // "Q" typed at the end, sent; then "u", sent before Q's answer came back. sent.append(region.typed(region.display + "Q")!) sent.append(region.typed(region.display + "u")!) XCTAssertEqual(region.display, "Ravi, the traQu") // A hearing the core answered before it read either touch. coreText = "Ravi, the train" region.take(settled: "Ravi,", tentative: " the train", edits: []) XCTAssertEqual(region.display.filter { $0 == "Q" }.count, 1, region.display) // The core reads Q, then u (in its own text, which has moved on). for touch in sent { coreText = VoiceRegion.apply([VoiceRegion.Touch(from: touch.from, to: touch.to, text: touch.text)], to: coreText) region.acknowledge(touch) region.take(settled: "Ravi,", tentative: String(coreText.dropFirst(5)), edits: []) XCTAssertEqual(region.display.filter { $0 == "Q" }.count, 1, region.display) } // Once every touch is answered the field is the core's text exactly. XCTAssertEqual(region.display, coreText) XCTAssertTrue(region.pending.isEmpty) XCTAssertEqual(region.display.filter { $0 == "Q" }.count, 1, region.display) XCTAssertTrue(region.display.contains("Qu"), region.display) } /// RULE: Offsets are UTF-16 units, so Telugu and Japanese map exactly. func testOffsetsAreUTF16ForTeluguAndJapanese() { var region = VoiceRegion() region.take(settled: "నమస్కారం 東京", tentative: "", edits: []) let touch = region.typed("నమస్కారం 京都") XCTAssertEqual(touch?.from, ("నమస్కారం " as NSString).length) region.take(settled: "నమస్కారం 東京へ", tentative: "", edits: []) XCTAssertEqual(region.settled, "నమస్కారం 京都へ") } /// RULE: The difference of two texts is the smallest runs that change. func testTheDifferenceIsTheChangedRuns() { let edits = VoiceRegion.difference("the cat sat", "the bat sat down") XCTAssertEqual(try VoiceRegion.apply(edits, to: "the cat sat"), "the bat sat down") XCTAssertEqual(edits.count, 2) } } final class VoiceWireTests: XCTestCase { /// RULE: The plan decodes the plan's shape, its lines flattened to strings, a null second pass absent. func testThePlanDecodes() throws { let json = #""" {"session":"s1","mode":"dictate","languages":["te","en"], "live":{"engine":"sherpa:nemotron-3.5-streaming-160ms-int8"}, "second":null,"vocabulary":["Seetha","Ravi"], "downloads":[{"model":"indicconformer-te-int8","bytes":123,"url":"https://x/y","sha256":"ab","why":"Better Telugu, 120 MB"}], "lines":{"listening":"Listening","nested":{"x":"y"}}} """# let plan = try JSONDecoder().decode(VoicePlan.self, from: Data(json.utf8)) XCTAssertEqual(plan.live.sherpaModel, "nemotron-3.5-streaming-160ms-int8") XCTAssertNil(plan.second) XCTAssertEqual(plan.downloads.first?.why, "Better Telugu, 120 MB") XCTAssertEqual(plan.lines, ["listening": "Listening"]) } /// RULE: A hearing encodes timed words with snake-case times. func testAHearingEncodesTimedWords() throws { let heard = VoiceHeard(session: "s1", pass: .second, final: true, text: "hi", words: [VoiceWord(w: "hi", startMs: 10, endMs: 200, conf: 0.9)]) let text = String(decoding: try JSONEncoder().encode(heard), as: UTF8.self) XCTAssertTrue(text.contains(#""start_ms":10"#), text) XCTAssertTrue(text.contains(#""pass":"second""#), text) } /// RULE: The catalogue's entries decode with nulls for what is not yet measured or uploaded. func testTheCatalogueDecodesWithNulls() throws { let json = #""" [{"model":"silero-vad-v5","family":"silero-vad","kind":"vad-silero","role":"vad","languages":["*"], "files":[{"name":"silero_vad.onnx","bytes":null,"sha256":null,"url":null}]}] """# let answer = try JSONDecoder().decode(VoiceModelsAnswer.self, from: Data(json.utf8)) XCTAssertEqual(answer.models.first?.kind, "vad-silero") XCTAssertEqual(answer.models.first?.files.first?.name, "silero_vad.onnx") XCTAssertEqual(answer.models.first?.state, .absent) } /// RULE: Engine tokens become words: a sentencepiece mark starts a word, each kanji is its own word. func testTokensBecomeWords() { let words = VoiceTokens.words(tokens: ["▁HE", "LLO", "▁WORLD"], times: [0.1, 0.2, 0.5], offsetMs: 1000, endMs: 1900) XCTAssertEqual(words.map(\.w), ["HELLO", "WORLD"]) XCTAssertEqual(words.first?.startMs, 1100) let japanese = VoiceTokens.words(tokens: ["東", "京"], times: [0, 0.3], offsetMs: 0, endMs: 600) XCTAssertEqual(japanese.map(\.w), ["東", "京"]) } }