Cotton code
92 lines · 5.5 KB · SwiftRaw
import CottonKitimport XCTest@testable import CottonUI/// The tap opens the microphone and nothing else. Build 280 took about four/// seconds from the record button's tap to the first audio buffer, because/// the tap loaded the speech model (locale, assets, format, context,/// `prepareToAnalyze`) and read the voice hints from the core before a single/// sample was taken. Both now happen when the screen comes up and on every/// return to the foreground; the tap takes what is warm.@MainActorfinal class DictationWarmTests: XCTestCase {    /// RULE: a warm slot hands its prepared model to the tap of the same language and own model exactly once, and drops a model prepared for anything else — an analyzer runs one session, and a model prepared for another language would listen in the wrong one.    func testTheWarmSlotIsTakenOnceAndOnlyForItsOwnKey() {        let english = WarmKey(DictationHints(language: "en"))        let hindi = WarmKey(DictationHints(language: "hi"))        var slot = WarmSlot<String>()        slot.fill(english, "prepared")        XCTAssertTrue(slot.holds(english))        XCTAssertEqual(slot.take(for: english), "prepared")        XCTAssertNil(slot.take(for: english), "an analyzer runs one session: the slot is empty after a tap")        slot.fill(english, "prepared")        XCTAssertNil(slot.take(for: hindi), "a model prepared for another language is never taken")        XCTAssertFalse(slot.holds(english), "and it is dropped, not kept beside the wrong one")    }    /// RULE: the dock's tap starts the recogniser with the hints read when the screen came up, with no read of the core in between and the recogniser already prepared — the tap is the microphone opening and nothing else (build 280: four seconds from tap to listening).    func testTheTapTakesTheWarmPath() async {        let recogniser = FakeRecogniser()        var reads = 0        var readsAtStart: Int?        let warm = DictationHints(groups: [.init(tag: "people", terms: ["Rhyn"])])        let dock = DockModel(            place: .page, captures: RecordingCapture(), media: RecordingMedia(),            dictation: DictationModel(recogniser: recogniser),            haptics: RecordingHaptics(), toasts: ToastCenter(scheduler: ManualToastScheduler()),            hints: {                reads += 1                return warm            })        await dock.prepareDictation()        XCTAssertEqual(recogniser.preparations, [warm], "the screen coming up prepares the recogniser")        XCTAssertEqual(reads, 1)        recogniser.whenStarted = { readsAtStart = reads }        await dock.startDictation()        XCTAssertEqual(recogniser.sessions, [warm], "the tap starts with the warm hints")        XCTAssertEqual(readsAtStart, 1, "the tap read nothing from the core before the microphone opened")        XCTAssertEqual(recogniser.preparations, [warm, warm],                       "the next tap is prepared only after this one started, for the hints read then")    }    /// RULE: after a tap the next tap's model is prepared for the hints read after that tap, never for the tap's own — the person's model is compiled again from their lines, and a slot one step behind made the tap after every compile a cold one, which on the phone opened the microphone seconds late (voiceline, 16 Oct 2026).    func testTheNextTapIsPreparedForTheHintsOfNow() async {        let recogniser = FakeRecogniser()        let before = DictationHints(groups: [.init(tag: "people", terms: ["Rhyn"])])        let after = DictationHints(groups: [.init(tag: "people", terms: ["Rhyn", "Asha"])])        var answer = before        let dock = DockModel(            place: .page, captures: RecordingCapture(), media: RecordingMedia(),            dictation: DictationModel(recogniser: recogniser),            haptics: RecordingHaptics(), toasts: ToastCenter(scheduler: ManualToastScheduler()),            hints: { answer })        await dock.prepareDictation()        answer = after        await dock.startDictation()        XCTAssertEqual(recogniser.sessions, [before], "the tap starts warm, on the hints it was prepared for")        XCTAssertEqual(recogniser.preparations.last, after, "the next tap is prepared for the hints of now")    }    /// RULE: a tap before the screen's warm read came back never waits on the hints read — the recogniser starts (and the system asks for the microphone) at once, with no hints, and the next tap is prepared for the hints read after it (walker294 row 118: nothing for ~3 s, then the prompt).    func testAColdTapOpensTheMicrophoneBeforeAnyRead() async {        let recogniser = FakeRecogniser()        var reads = 0        var readsAtStart: Int?        let read = DictationHints(groups: [.init(tag: "people", terms: ["Rhyn"])])        let dock = DockModel(            place: .page, captures: RecordingCapture(), media: RecordingMedia(),            dictation: DictationModel(recogniser: recogniser),            haptics: RecordingHaptics(), toasts: ToastCenter(scheduler: ManualToastScheduler()),            hints: {                reads += 1                return read            })        recogniser.whenStarted = { readsAtStart = reads }        await dock.startDictation()        XCTAssertEqual(readsAtStart, 0, "the microphone opened before anything was read from the core")        XCTAssertEqual(recogniser.sessions, [DictationHints()], "a cold tap listens with no hints")        XCTAssertEqual(recogniser.preparations, [read], "and the next tap is prepared for the hints read after it")    }}