// (270 lines), case for case and name for name.//// The TypeScript drives a rendered field; these drive `DockModel`, which is// where every one of those decisions actually lives — `fieldEdited` IS// `changeText`, `caretMoved(to:)` IS the platform's selection event, and// `displayText` IS what the field renders. `FakeRecogniser`// (`DictationModelTests.swift`) is the platform here: there is no microphone in// any of this.//// TWO of the TypeScript's assertions become something else in Swift, and are// worth naming rather than leaving as a silent shape change://// • `props.editable === false` becomes ``DockModel/isDictationLive``. The// view reads the same flag to make the field inert, but the RULE is "the// field is dictation's while a session is live", and that is the model's.// • `queryByTestId("composer-mic")` being null becomes ``DockModel/canSend``// and `isDictationLive` together — which of the card's two states the one// control row is in, decided here rather than by which node happens to be// mounted. `canSend` no longer decides whether the MIC is drawn (it always// is); it decides whether the arrow beside it is live or dimmed.importCottonKitimport XCTest@testable importCottonUI@MainActorfinalclassComposerTests: XCTestCase {privatevar captures = RecordingCapture()privatevar recogniser = FakeRecogniser()privatevar haptics = RecordingHaptics()privatevar toasts = ToastCenter(scheduler: ManualToastScheduler())overridefunc setUp() asyncthrows { captures = RecordingCapture() recogniser = FakeRecogniser() haptics = RecordingHaptics() toasts = ToastCenter(scheduler: ManualToastScheduler()) }/// Short guards and a short tick, so a test that proves a timer fires does/// not wait two and a half real seconds for it.privatefunc composer(place: DockPlace = .page) -> DockModel {let dictation = DictationModel( recogniser: recogniser, levels: recogniser, timings: DictationModel.Timings( stopGuard: .milliseconds(200), cancelGuard: .milliseconds(150), tick: .milliseconds(5) ) )let model = DockModel( place: place, captures: captures, media: RecordingMedia(), dictation: dictation, haptics: haptics, toasts: toasts )return model }/// Tap the mic and bring the session up.privatefunc startDictating(_ model: DockModel) async {await model.startDictation() recogniser.emit(.started) }privatefunc settled(_ check: @escaping @MainActor () -> Bool) async {for _ in0..<200 {if check() { return }try? awaitTask.sleep(for: .milliseconds(5)) } }// MARK: - The fieldfunc testCapturesTrimmedTextOnSendAndClearsTheField() async {let model = composer() model.fieldEdited(" Pay GST by 20th ")await model.send()let sent = await captures.last() XCTAssertEqual(sent?.request.text, "Pay GST by 20th") XCTAssertEqual(model.text, "") }// MARK: - Which haptic each control fires//// `press` and `select` map to two DIFFERENT generators, and the composer's// four controls are all `press`. Send is the most repeated control in the// app, so a tick where the phone should thump is felt every day — which is// why these are asserted rather than left to the eye.func testSendThumpsRatherThanTicking() async {let model = composer() model.fieldEdited("Pay GST by 20th")await model.send() XCTAssertEqual(haptics.events, ["press"]) }func testAnEmptyDraftFeelsNothingAtAllBecauseNothingHappened() async {let model = composer() model.fieldEdited(" ")await model.send() XCTAssertEqual(haptics.events, []) }func testTheMicFiresItsPressEvenWhenThePlatformRefuses() async {// The haptic fires BEFORE `dictation.start`: a tap that cannot start a// dictation is still a tap that landed. recogniser.permission = falselet model = composer()await model.startDictation() XCTAssertEqual(haptics.events, ["press"]) }func testTheDiscardPressesAndFiresNothingElse() async {let model = composer()await startDictating(model) XCTAssertEqual(haptics.events, ["press"], "the mic") model.cancelDictation()// Exactly two, and both presses: the model must fire none of its own,// or `/take` — which mounts the same `DictationModel` and fires nothing// on these buttons — would feel a haptic the previous screen never had. XCTAssertEqual(haptics.events, ["press", "press"], "and the ✕") }func testALandedDictationSucceedsRatherThanPressing() async {// The one haptic in this file that is NOT a press: a finished// dictation is a `success`.let model = composer()await startDictating(model) recogniser.speak("meet anil at noon")await model.finishDictation() recogniser.emit(.ended)await settled { !model.isDictationLive } XCTAssertEqual(haptics.events.first, "press", "the mic") XCTAssertTrue( haptics.events.contains("success"),"the words landing is the success; got \(haptics.events)" ) }func testOffersTheMicNotSendWhileTheFieldIsEmpty() async {// The send arrow REPLACES the mic once there are words — an empty field// has nothing to send, so the slot belongs to the other way of filling// it. Whitespace is nothing.let model = composer() XCTAssertFalse(model.canSend) model.fieldEdited(" ") XCTAssertFalse(model.canSend)await model.send()let asked = await captures.sent() XCTAssertTrue(asked.isEmpty, "send no-ops on an empty draft") }func testSwapsTheMicForSendTheMomentThereAreWordsAndBackOnClear() {let model = composer() model.fieldEdited("Pay GST") XCTAssertTrue(model.canSend) model.fieldEdited("") XCTAssertFalse(model.canSend) }func testReportsTheDraftTextAndCaretForTheDocksTokenizer() {var reported: [String] = []let model = composer() model.onTextChange = { reported.append($0) } model.fieldEdited("pay #gs") XCTAssertEqual(model.text, "pay #gs")// A text change puts the caret where typing puts it: the end. XCTAssertEqual(model.caret, 7) XCTAssertEqual(reported, ["pay #gs"])// A caret move alone reports the draft WITHOUT re-firing the classifier. model.caretMoved(to: 3) XCTAssertEqual(model.caret, 3) XCTAssertEqual(model.text, "pay #gs") XCTAssertEqual(reported, ["pay #gs"], "a caret move is not a keystroke") }func testKeepsMultiLineCapturesIntact() async {let model = composer() model.fieldEdited("Call Ramesh\nabout the cold chain")await model.send()let sent = await captures.last() XCTAssertEqual(sent?.request.text, "Call Ramesh\nabout the cold chain") }// MARK: - Dictation — a tap, never a holdfunc testStreamsPartialsIntoTheVisibleFieldWhileDictating() async {let model = composer()await startDictating(model)// The field never unmounts; it streams, read-only, with the listening// line above it. XCTAssertTrue(model.isDictationLive) recogniser.speak("call subash") XCTAssertEqual(model.displayText, "call subash")// A session reports a CUMULATIVE transcript, so the partial REPLACES. recogniser.speak("call subash about trip five") XCTAssertEqual(model.displayText, "call subash about trip five") }func testYouCanSpeakIntoAHalfTypedThoughtAndTheTranscriptFoldsOntoIt() async {// The mic and the arrow are BOTH drawn now, so a thought typed halfway// and then spoken is reachable — and the words already in the field are// carried as the prefix rather than thrown away.let model = composer() model.fieldEdited("Tomorrow:") XCTAssertTrue(model.canSend, "the arrow is live — and the mic is still there")await startDictating(model) recogniser.speak("pay the rent") XCTAssertEqual(model.displayText, "Tomorrow: pay the rent") recogniser.emit(.ended)await model.finishDictation() XCTAssertEqual(model.text, "Tomorrow: pay the rent") }func testDictationOverAnEmptyFieldStartsClean() async {let model = composer()await startDictating(model) recogniser.speak("pay the rent") XCTAssertEqual(model.displayText, "pay the rent") }func testFinishLandsTheWordsNamesTheEndingAndReEnablesTheField() async {let model = composer()await startDictating(model) recogniser.speak("buy cotton seeds") recogniser.whenStopped = { [recogniser = self.recogniser] in recogniser.emit(.segment("buy cotton seeds")) recogniser.emit(.ended) }await model.finishDictation() XCTAssertEqual(model.text, "buy cotton seeds") XCTAssertFalse(model.isDictationLive) XCTAssertEqual(model.ending, DockModel.DictationEnding(outcome: .dictated, take: 1))// `dictated` owes no sentence — the words speak for themselves. XCTAssertNil(toasts.current) }func testCancelDiscardsTheDictationAndNamesTheEnding() async {let model = composer()await startDictating(model) recogniser.speak("drop me") recogniser.whenCancelled = { [recogniser = self.recogniser] in recogniser.emit(.failed(.aborted)) recogniser.emit(.ended) } model.cancelDictation() XCTAssertFalse(model.isDictationLive) XCTAssertEqual(model.text, "") XCTAssertEqual(model.ending, DockModel.DictationEnding(outcome: .dictationCancelled, take: 1))// A deliberate discard owes no sentence either. XCTAssertNil(toasts.current) }/// VERBATIM: the composer's field takes the recogniser's own words —/// fillers, no capital, no punctuation, a spoken number — and nothing/// arrives afterwards to change them.func testTheFieldTakesTheRecognisersWordsAndNothingChangesThemLater() async {let spoken = "um buy twenty five cotton seeds full stop"let model = composer()await startDictating(model) recogniser.speak(spoken) recogniser.whenStopped = { [recogniser = self.recogniser] in recogniser.emit(.segment(spoken)) recogniser.emit(.ended) }await model.finishDictation() XCTAssertEqual(model.text, spoken)try? awaitTask.sleep(for: .milliseconds(50)) XCTAssertEqual(model.text, spoken, "nothing is in flight to rewrite it") }func testAnIncomingCallKeepsEveryWordInTheFieldAndNamesTheEnding() async {// The bug of 2026-09-04: three minutes of dictation, a phone call, and// the whole field went blank. iOS's own dictation keeps what it heard// and stops; so does this. The platform reports the named failure, then// the forced end.let model = composer()await startDictating(model) recogniser.speak("call subash about the cold chain") recogniser.emit(.failed(.interrupted)) recogniser.emit(.ended) XCTAssertEqual(model.text, "call subash about the cold chain") XCTAssertFalse(model.isDictationLive) XCTAssertTrue(model.canSend, "the arrow is live again over the words that landed") XCTAssertEqual(model.ending, DockModel.DictationEnding(outcome: .dictationInterrupted, take: 1))// A named ending is spoken, and it is spoken as an ERROR. XCTAssertEqual(toasts.current?.tone, .error) XCTAssertEqual( toasts.current?.message,VoiceTake.dictationMessage(.dictationInterrupted) ) }func testDeniedPermissionNamesItsEnding() async { recogniser.permission = falselet model = composer()await model.startDictation() XCTAssertEqual(model.ending, DockModel.DictationEnding(outcome: .dictationDenied, take: 1)) XCTAssertEqual(toasts.current?.tone, .error) XCTAssertFalse(model.isDictationLive) }}