// The body dictation's two cases, case for case, with the rules they imply but // never held: // that the body is written on FINISH and never on a partial, that a discard // leaves the writer's words alone, that a cursor which has gone stale splices // at the end rather than trapping, and that the span lands VERBATIM. // // `FakeRecogniser` (`DictationModelTests.swift`) is the platform here too — // there is no microphone in any of this. import CottonKit import XCTest @testable import CottonUI @MainActor final class BodyDictationTests: XCTestCase { private var recogniser = FakeRecogniser() private var haptics = RecordingHaptics() private var body = "" private var endings: [DictationOutcome] = [] override func setUp() async throws { recogniser = FakeRecogniser() haptics = RecordingHaptics() body = "" endings = [] } private func editor(_ initialBody: String) -> (BodyDictation, DictationModel) { body = initialBody let model = DictationModel( recogniser: recogniser, levels: recogniser, timings: DictationModel.Timings( stopGuard: .milliseconds(200), cancelGuard: .milliseconds(150), tick: .milliseconds(5)) ) let editor = BodyDictation( dictation: model, haptics: haptics, body: { [weak self] in self?.body ?? "" }, setBody: { [weak self] next in self?.body = next }, onEnded: { [weak self] outcome in self?.endings.append(outcome) } ) return (editor, model) } // Each microphone session reads current context. A screen kept alive // while a name changes must not feed the previous session's bias list. func testEachMicSessionFeedsFreshVoiceContextToTheRecogniser() async { let core = ScriptedCore(["read.voiceContext": .ok(#"{"biasGroups":[{"tag":"screen","terms":[" Meri ","Quillby"]}]}"#)]) let (editor, _) = self.editor("") await editor.start(core: core) XCTAssertEqual(recogniser.sessions.last?.terms, [" Meri ", "Quillby"]) recogniser.emit(.started) recogniser.whenCancelled = { [recogniser = self.recogniser] in recogniser.emit(.ended) } editor.cancel() await core.answer("read.voiceContext", .ok(#"{"biasGroups":[{"tag":"screen","terms":["New name"," ","Quillby"]}]}"#)) await editor.start(core: core) XCTAssertEqual(recogniser.sessions.last?.terms, ["New name", "Quillby"]) XCTAssertEqual(recogniser.sessions.count, 2) let calls = await core.calls XCTAssertEqual(calls.map(\.method), ["read.voiceContext", "read.voiceContext"]) editor.cancel() } // MARK: - The two the TypeScript holds func testAnInterruptionSplicesTheWordsIntoTheBodyAtTheAnchorAndNamesTheEnding() async { let (editor, _) = self.editor("Before. After.") editor.selectionChanged(start: 7, end: 7) await editor.start(hints: DictationHints()) recogniser.emit(.started) recogniser.speak("the middle") XCTAssertEqual(editor.displayBody, "Before. the middle After.") XCTAssertEqual(body, "Before. After.", "a partial never writes the body") // A phone call takes the microphone. recogniser.emit(.failed(.interrupted)) recogniser.emit(.ended) XCTAssertEqual(body, "Before. the middle After.") XCTAssertEqual(endings, [.dictationInterrupted]) XCTAssertFalse(editor.isActive) XCTAssertNil(editor.displayBody) } func testAFinishTheUserAskedForLandsTheSameWay() async { let (editor, _) = self.editor("") await editor.start(hints: DictationHints()) recogniser.emit(.started) recogniser.speak("buy seeds") recogniser.whenStopped = { [recogniser = self.recogniser] in recogniser.emit(.segment("buy seeds")) recogniser.emit(.ended) } await editor.finish() XCTAssertEqual(body, "buy seeds") XCTAssertEqual(endings, [.dictated]) } // MARK: - The cursor func testABodyThatWasNeverFocusedTakesTheWordsOnTheEnd() async { let (editor, _) = self.editor("Existing note") await editor.start(hints: DictationHints()) recogniser.emit(.started) recogniser.speak("and one more thing") XCTAssertEqual(editor.displayBody, "Existing note and one more thing") recogniser.emit(.failed(.interrupted)) recogniser.emit(.ended) XCTAssertEqual(body, "Existing note and one more thing") } func testACursorPastTheEndOfTheBodySplicesAtTheEndRatherThanTrapping() async { let (editor, _) = self.editor("Short") // A selection reported before a programmatic edit shortened the text. editor.selectionChanged(start: 400, end: 900) await editor.start(hints: DictationHints()) recogniser.emit(.started) recogniser.speak("more") XCTAssertEqual(editor.displayBody, "Short more") } func testASelectionSpanIsREPLACEDByWhatWasDictated() async { let (editor, _) = self.editor("keep this drop that") editor.selectionChanged(start: 10, end: 19) await editor.start(hints: DictationHints()) recogniser.emit(.started) recogniser.speak("say this") XCTAssertEqual(editor.displayBody, "keep this say this") } // MARK: - The endings that keep no words func testCancelLeavesTheWritersOwnWordsExactlyWhereTheyWere() async { let (editor, _) = self.editor("Untouched.") await editor.start(hints: DictationHints()) recogniser.emit(.started) recogniser.speak("never mind") recogniser.whenCancelled = { [recogniser = self.recogniser] in recogniser.emit(.failed(.aborted)) recogniser.emit(.ended) } editor.cancel() XCTAssertEqual(body, "Untouched.") XCTAssertEqual(endings, [.dictationCancelled]) XCTAssertNil(editor.displayBody) } func testARefusedStartNamesItsEndingAndKeepsNoAnchor() async { recogniser.permission = false let (editor, _) = self.editor("Untouched.") let refusal = await editor.start(hints: DictationHints()) XCTAssertEqual(refusal, .dictationDenied) XCTAssertEqual(endings, [.dictationDenied]) XCTAssertEqual(body, "Untouched.") XCTAssertNil(editor.displayBody) } func testAFinishWithNothingHeardWritesNothingAndStillNamesItself() async { let (editor, _) = self.editor("Untouched.") await editor.start(hints: DictationHints()) recogniser.emit(.started) recogniser.whenStopped = { [recogniser = self.recogniser] in recogniser.emit(.ended) } await editor.finish() XCTAssertEqual(body, "Untouched.") XCTAssertEqual(endings, [.dictationEmpty]) } // MARK: - The landing is felt /// The landing is the note editor's own `haptic("success")`, and it is the /// ONLY feel this path fires: `DictationModel` fires none at all — a feel /// belongs to the control that was tapped, and the mic, ✓ and ✕ are 4e's /// screen's, not this component's. func testWordsLandingIsFeltOnceAndOnlyWhenThereAreWords() async { let (editor, _) = self.editor("") await editor.start(hints: DictationHints()) recogniser.emit(.started) recogniser.speak("something") recogniser.whenStopped = { [recogniser = self.recogniser] in recogniser.emit(.segment("something")) recogniser.emit(.ended) } await editor.finish() XCTAssertEqual(haptics.events, ["success"]) } // MARK: - Verbatim /// The span lands at the anchor exactly as the recogniser said it — fillers, /// no capital, no punctuation, a spoken number — and stays that way. func testTheSplicedSpanIsWhatTheRecogniserSaidAndStaysThatWay() async { let spoken = "um the twenty five thousand one full stop" let (editor, _) = self.editor("Before. After.") editor.selectionChanged(start: 7, end: 7) await editor.start(hints: DictationHints()) recogniser.emit(.started) recogniser.speak(spoken) recogniser.whenStopped = { [recogniser = self.recogniser] in recogniser.emit(.segment(spoken)) recogniser.emit(.ended) } await editor.finish() XCTAssertEqual(body, "Before. \(spoken) After.") // Nothing arrives later to change it: there is no polish in flight. try? await Task.sleep(for: .milliseconds(200)) XCTAssertEqual(body, "Before. \(spoken) After.") } }