hex/ios/HEXMobileTests/DictationSSOTTests.swift
Ryan Vogel d9dcfd1dca iOS Settings › Model keeps only the picked model, once the new one is ready
The coordinator decided against keeping both models (about 1.3 to 1.6 GB
for one the person switched away from). Only the picked model stays.

- SpeechModelChoice.unpicked decides what to remove: the other model goes
  only once the picked one is downloaded and has loaded once, in Hex or
  the keyboard, and never while Hex dictates with it, so nobody is left
  with neither and a failed or cancelled download leaves the old one in
  place. DictationController.keepOnlyThePickedSpeechModel runs it whenever
  Hex comes to the front (launch included) and once Hex has loaded the
  multilingual model.
- Until then the old model keeps dictating English in the keyboard
  (KeyboardParakeetPlan): with Multilingual picked and its model not here
  yet, English's model (readyV2); with English picked and its model not
  here yet, the multilingual one (new awaitingV2DownloadUsingUltra, which
  still lets English's model download by itself). Before, the keyboard
  fell back to Apple's speech recognition in both.
- The keyboard records that a model loaded (KeyboardParakeetSettings
  .hasLoadedOnce, App Group). Removing a model clears that, its compile
  record (ParakeetUltraModel.remove now forgets Ultra's) and the keyboard's
  state about it, and sets purgesCompiled: before its next load the
  keyboard removes its Core ML compiled copies
  (Library/Caches/<bundle>/com.apple.e5rt.e5bundlecache, where a probe
  keyboard's syslog on an iPhone 17 Pro showed them; 234 MB for English's
  model), so the picked one compiles again once (12 to 17 s, Apple's
  speech recognition meanwhile). The Neural Engine's own cache
  (com.apple.aned) and Hex's own compiled copy are left to iOS.
- The card says so: "Only the picked model stays on this iPhone: switching
  downloads the new one, and the old one goes once the new one is ready."
  The choices no longer say "once".

Storage: English about 700 MB (464 MB download, 234 MB compiled in the
keyboard); Multilingual about 0.9 to 1.2 GB (632 MB download, compiled
copies not measured); both for a while during a switch.

Verified: build for the simulator; DictationSSOTTests (plan fallbacks and
the removal table), DictationControllerTests and KeyboardBridgeTests on
basebook's iOS 27.2 simulator; the card in English and Chinese. Untested:
the removal and the keyboard's purge on a phone (no Neural Engine or real
compile in the simulator).
2026-11-24 10:01:18 -05:00

611 lines
29 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import XCTest
@testable import HEX
final class DictationSSOTTests: XCTestCase {
// MARK: - DictationRoute
func testDictationRoutePrecedenceTable() {
// Full Access off always routes to setUp.
for modelInstalled in [false, true] {
for warm in [false, true] {
for local in [false, true] {
XCTAssertEqual(
DictationRoute.resolve(
hasFullAccess: false,
isModelInstalled: modelInstalled,
isWarmAvailable: warm,
localAvailable: local,
opensHexReason: .memory
),
.setUp
)
}
}
}
// Full Access on, Hex's model not installed: the keyboard still dictates by itself where
// it can (it uses its own speech model), else needsModel.
for warm in [false, true] {
for local in [false, true] {
XCTAssertEqual(
DictationRoute.resolve(
hasFullAccess: true,
isModelInstalled: false,
isWarmAvailable: warm,
localAvailable: local,
opensHexReason: .speechModel
),
local ? .inKeyboard : .needsModel
)
}
}
// Warm beats local and opensHex.
for local in [false, true] {
let route = DictationRoute.resolve(
hasFullAccess: true,
isModelInstalled: true,
isWarmAvailable: true,
localAvailable: local,
opensHexReason: .memory
)
XCTAssertEqual(route, .warm)
XCTAssertTrue(route.dictatesInPlace)
}
// Local beats opensHex when warm is off.
let localRoute = DictationRoute.resolve(
hasFullAccess: true,
isModelInstalled: true,
isWarmAvailable: false,
localAvailable: true,
opensHexReason: nil
)
XCTAssertEqual(localRoute, .inKeyboard)
XCTAssertTrue(localRoute.dictatesInPlace)
// Otherwise opensHex carries the reason (or nil when unsupported).
for reason: KeyboardSelfDictation.Reason? in [nil, .memory, .speechModel, .languageUnavailable, .openedHex] {
let route = DictationRoute.resolve(
hasFullAccess: true,
isModelInstalled: true,
isWarmAvailable: false,
localAvailable: false,
opensHexReason: reason
)
XCTAssertEqual(route, .opensHex(reason))
XCTAssertFalse(route.dictatesInPlace)
}
}
/// From iOS 26 nothing a dictation tap does opens Hex: where the keyboard can't dictate it
/// says why, and only Open Hex in that line opens Hex. Before iOS 26 it opens Hex, as always.
func testADictationTapThatCantDictateHereSaysWhyFromiOS26AndOpensHexOnlyBefore() {
let routes: [(DictationRoute, KeyboardDictationBlock?)] = [
(.needsModel, .setUp),
(.opensHex(.speechModel), .speechModel(language: "de-DE")),
(.opensHex(.memory), .memory),
(.opensHex(.languageUnavailable), .languageUnavailable(language: "de-DE")),
(.setUp, nil),
(.warm, nil),
(.inKeyboard, nil),
]
for (route, block) in routes {
XCTAssertEqual(KeyboardDictationBlock.resolve(route: route, language: "de-DE", osMajorVersion: 26), block, "\(route)")
XCTAssertNil(KeyboardDictationBlock.resolve(route: route, language: "de-DE", osMajorVersion: 18), "iOS 18 unchanged: \(route)")
}
XCTAssertNil(KeyboardDictationBlock.resolve(route: .opensHex(nil), language: nil, osMajorVersion: 18))
XCTAssertEqual(KeyboardDictationBlock.resolve(route: .opensHex(.speechModel), language: nil, osMajorVersion: 27), .speechModel(language: "en-US"))
XCTAssertEqual(KeyboardDictationBlock.speechModel(language: "en-US").line, "Download English in Hex to dictate")
XCTAssertEqual(KeyboardDictationBlock.memory.line, "Not enough memory to dictate here")
XCTAssertEqual(KeyboardDictationBlock.setUp.line, "Finish setting up Hex to dictate")
// Apple has no model for it here: downloading in Hex can't help, so no Open Hex.
XCTAssertEqual(KeyboardDictationBlock.languageUnavailable(language: "mt-MT").line, "Maltese isn't available on this iPhone yet")
XCTAssertFalse(KeyboardDictationBlock.languageUnavailable(language: "mt-MT").opensHex)
XCTAssertTrue(KeyboardDictationBlock.speechModel(language: "en-US").opensHex)
}
@MainActor
func testTheDictationBlockOpensHexOnlyFromItsOpenHexAndPutsItselfAway() throws {
try XCTSkipIf(ProcessInfo.processInfo.operatingSystemVersion.majorVersion < KeyboardSelfDictation.firstVersion)
let model = KeyboardViewModel()
var opened: [URL] = []
model.onOpenURL = { opened.append($0) }
model.configureFixture(snapshot: .offline, now: 50_000)
model.automaticLanguage = "en-US"
model.opensHexReason = .memory
XCTAssertEqual(model.route, .opensHex(.memory))
XCTAssertFalse(model.dictationTapOpensHex)
model.dictateWhereUnavailable()
XCTAssertEqual(model.dictationBlock, .memory)
XCTAssertEqual(model.statusSlot, .dictationBlock(.memory), "the bar's line")
XCTAssertTrue(opened.isEmpty, "a dictation tap opens nothing")
model.dismissDictationBlock()
XCTAssertNil(model.dictationBlock, "its ✕")
XCTAssertTrue(opened.isEmpty)
model.dictateWhereUnavailable()
model.openHexForDictationBlock()
XCTAssertEqual(opened, [model.coldStartURL], "Open Hex records in Hex")
XCTAssertNil(model.dictationBlock)
// The second failure to get the speech model says so as a tap would; dictating here again puts it away.
model.opensHexReason = .speechModel
model.localDictationChanged(available: false, was: true, reason: .speechModel)
XCTAssertEqual(model.dictationBlock, .speechModel(language: "en-US"))
model.localDictationChanged(available: true, was: false, reason: nil)
XCTAssertNil(model.dictationBlock)
model.localDictationChanged(available: false, was: nil, reason: .speechModel)
XCTAssertNil(model.dictationBlock, "a keyboard that opens without the model says nothing until a tap")
XCTAssertEqual(opened.count, 1)
}
@MainActor
func testFailedWarmSessionRetriesWarmWithBarAndFullKeyboardParity() {
let t0 = 40_000.0
let errorText = "Hex couldn't start the microphone. Try again."
let model = KeyboardViewModel()
var warmTaps = 0
var localTaps = 0
var opened: [URL] = []
model.onDictateTap = { warmTaps += 1 }
model.onLocalDictateTap = { localTaps += 1 }
model.onOpenURL = { opened.append($0) }
let warmFailed = KeyboardSnapshot(
state: .failed,
heartbeat: t0,
expiresAt: t0 + 900,
message: errorText,
messageAt: t0
)
model.applyBridgeState(
hasFullAccess: true,
snapshot: warmFailed,
isModelInstalled: true,
style: .polished,
now: t0 + 1,
animated: false
)
XCTAssertEqual(model.stagePhase, .failed)
XCTAssertTrue(model.isWarmAvailable)
XCTAssertEqual(model.route, .warm)
XCTAssertTrue(model.dictatesThroughHex)
XCTAssertTrue(model.canPushToTalk)
XCTAssertEqual(model.statusSlot, .message(errorText, isFailure: true), "failure wins over warm countdown")
XCTAssertEqual(model.failureMessage, errorText)
let full = FullKeyboardView(model: model, target: FixtureTextTarget())
full.frame = CGRect(x: 0, y: 0, width: 402, height: 262)
full.layoutIfNeeded()
XCTAssertTrue(full.hexKey.isWarm)
full.hexKeyHeld()
XCTAssertEqual(warmTaps, 1, "full keyboard Hex key retries warm session")
XCTAssertEqual(localTaps, 0)
XCTAssertTrue(opened.isEmpty)
}
@MainActor
func testLocalFailureStaysInKeyboardAndOneShotFailureRemainsVisibleInStatusSlot() throws {
let t0 = 41_000.0
let errorText = "Hex couldn't start the microphone. Try again."
// Local keyboard failure: heartbeat/expiresAt are 0, so route stays inKeyboard and both surfaces show it.
let localModel = KeyboardViewModel()
var warmTaps = 0
var localTaps = 0
localModel.onDictateTap = { warmTaps += 1 }
localModel.onLocalDictateTap = { localTaps += 1 }
let localSnap = try XCTUnwrap(
KeyboardLocalCapturePhase.failed(message: errorText, until: t0 + 6)
.snapshot(now: t0, loudnessDB: -62)
)
XCTAssertEqual(localSnap.heartbeat, 0)
XCTAssertEqual(localSnap.expiresAt, 0)
localModel.applyBridgeState(
hasFullAccess: true,
snapshot: localSnap,
isModelInstalled: true,
style: .polished,
now: t0,
animated: false
)
XCTAssertFalse(localModel.isWarmAvailable)
XCTAssertEqual(localModel.route, .inKeyboard)
XCTAssertTrue(localModel.canPushToTalk)
XCTAssertEqual(localModel.statusSlot, .message(errorText, isFailure: true))
XCTAssertEqual(localModel.failureMessage, errorText)
let full = FullKeyboardView(model: localModel, target: FixtureTextTarget())
full.frame = CGRect(x: 0, y: 0, width: 402, height: 262)
full.layoutIfNeeded()
XCTAssertFalse(full.hexKey.isWarm)
full.hexKeyHeld()
XCTAssertEqual(localTaps, 1, "local failure retries in the keyboard")
XCTAssertEqual(warmTaps, 0)
// One-shot failure (warm off, local unavailable): bar StatusSlot still shows the failure.
let oneShot = KeyboardViewModel()
oneShot.opensHexReason = .memory
oneShot.applyBridgeState(
hasFullAccess: true,
snapshot: KeyboardSnapshot(state: .failed, heartbeat: 0, expiresAt: 0, message: errorText, messageAt: t0),
isModelInstalled: true,
style: .polished,
now: t0 + 1,
animated: false
)
XCTAssertEqual(oneShot.route, .opensHex(.memory))
XCTAssertFalse(oneShot.canPushToTalk)
XCTAssertEqual(oneShot.statusSlot, .message(errorText, isFailure: true))
XCTAssertEqual(oneShot.failureMessage, errorText)
}
@MainActor
func testStatusSlotIsMutuallyExclusiveAcrossStages() {
let t0 = 42_000.0
let model = KeyboardViewModel()
let note = TranslationFailure(.notDownloaded, source: Locale.Language(identifier: "es"), target: Locale.Language(identifier: "en"), detail: nil)
// No Full Access always shows noAccess.
model.applyBridgeState(
hasFullAccess: false,
snapshot: KeyboardSnapshot(state: .failed, heartbeat: 0, expiresAt: 0, message: "Oops", messageAt: t0),
isModelInstalled: true,
style: .polished,
now: t0,
animated: false
)
XCTAssertEqual(model.statusSlot, .noAccess)
XCTAssertNil(model.failureMessage)
// Starting (PR #2478) -> .starting.
model.applyBridgeState(
hasFullAccess: true,
snapshot: KeyboardSnapshot(state: .starting, heartbeat: t0, expiresAt: t0 + 60, jobID: "j1"),
isModelInstalled: true,
style: .polished,
now: t0,
animated: false
)
XCTAssertEqual(model.statusSlot, .starting)
// Recording -> .recording.
model.applyBridgeState(
hasFullAccess: true,
snapshot: KeyboardSnapshot(state: .recording, heartbeat: t0, expiresAt: t0 + 60, recordingStartedAt: t0, jobID: "j1"),
isModelInstalled: true,
style: .polished,
now: t0,
animated: false
)
XCTAssertEqual(model.statusSlot, .recording)
// Transcribing -> .none.
model.applyBridgeState(
hasFullAccess: true,
snapshot: KeyboardSnapshot(state: .transcribing, heartbeat: t0, expiresAt: t0 + 60, jobID: "j1"),
isModelInstalled: true,
style: .polished,
now: t0,
animated: false
)
XCTAssertEqual(model.statusSlot, .none)
// Idle + warm + translationNote -> translationNote wins over warm.
model.translationNote = note
model.applyBridgeState(
hasFullAccess: true,
snapshot: KeyboardSnapshot(state: .ready, heartbeat: t0, expiresAt: t0 + 900),
isModelInstalled: true,
style: .polished,
now: t0,
animated: false
)
XCTAssertEqual(model.statusSlot, .translationNote(note))
// Idle + warm + non-failure message -> .message(isFailure: false), failureMessage nil.
model.translationNote = nil
model.applyBridgeState(
hasFullAccess: true,
snapshot: KeyboardSnapshot(state: .ready, heartbeat: t0, expiresAt: t0 + 900, message: "Interrupted", messageAt: t0),
isModelInstalled: true,
style: .polished,
now: t0,
animated: false
)
XCTAssertEqual(model.statusSlot, .message("Interrupted", isFailure: false))
XCTAssertNil(model.failureMessage)
// Idle + warm + no message -> .warm.
model.applyBridgeState(
hasFullAccess: true,
snapshot: KeyboardSnapshot(state: .ready, heartbeat: t0, expiresAt: t0 + 900),
isModelInstalled: true,
style: .polished,
now: t0,
animated: false
)
XCTAssertEqual(model.statusSlot, .warm)
}
// MARK: - DictationEngine
func testDictationEngineDisplayNamesAndCapabilities() {
XCTAssertEqual(DictationEngine.undecided.displayName, "Local speech")
XCTAssertEqual(DictationEngine.appleSpeech.displayName, "Apple Speech")
XCTAssertEqual(DictationEngine.onDeviceRecognizer.displayName, "Apple Speech")
XCTAssertEqual(DictationEngine.parakeetV2.displayName, "Parakeet V2")
XCTAssertEqual(DictationEngine.parakeetUltra.displayName, "Parakeet Ultra")
XCTAssertFalse(DictationEngine.undecided.isAppleSpeech)
XCTAssertTrue(DictationEngine.appleSpeech.isAppleSpeech)
XCTAssertTrue(DictationEngine.onDeviceRecognizer.isAppleSpeech)
XCTAssertFalse(DictationEngine.parakeetV2.isAppleSpeech)
XCTAssertFalse(DictationEngine.parakeetUltra.isAppleSpeech)
XCTAssertTrue(DictationEngine.undecided.downloads)
XCTAssertTrue(DictationEngine.appleSpeech.downloads)
XCTAssertFalse(DictationEngine.onDeviceRecognizer.downloads)
XCTAssertTrue(DictationEngine.parakeetV2.downloads)
XCTAssertTrue(DictationEngine.parakeetUltra.downloads)
XCTAssertFalse(DictationEngine.undecided.keyboardSpeechFollowsApp(keyboardNeedsHex: false))
XCTAssertFalse(DictationEngine.undecided.keyboardSpeechFollowsApp(keyboardNeedsHex: true))
XCTAssertFalse(DictationEngine.parakeetV2.keyboardSpeechFollowsApp(keyboardNeedsHex: false))
XCTAssertFalse(DictationEngine.parakeetV2.keyboardSpeechFollowsApp(keyboardNeedsHex: true))
// The keyboard dictates the multilingual model's languages itself, with Apple's model
// until it has the multilingual one loaded: Hex gets that model ready for it.
XCTAssertFalse(DictationEngine.parakeetUltra.keyboardSpeechFollowsApp(keyboardNeedsHex: false))
XCTAssertFalse(DictationEngine.parakeetUltra.keyboardSpeechFollowsApp(keyboardNeedsHex: true))
XCTAssertTrue(DictationEngine.appleSpeech.keyboardSpeechFollowsApp(keyboardNeedsHex: false))
XCTAssertFalse(DictationEngine.appleSpeech.keyboardSpeechFollowsApp(keyboardNeedsHex: true))
XCTAssertTrue(DictationEngine.onDeviceRecognizer.keyboardSpeechFollowsApp(keyboardNeedsHex: false))
XCTAssertFalse(DictationEngine.onDeviceRecognizer.keyboardSpeechFollowsApp(keyboardNeedsHex: true))
}
// MARK: - KeyboardParakeetPlan & Language Normalization
func testKeyboardParakeetPlanTable() {
let mb = KeyboardParakeetBudget.megabyte
let largeLimit = 177 * mb
let smallLimit = 100 * mb
func plan(
enabled: Bool = true,
fits: Bool = true,
twoLanguage: Bool = false,
multilingual: String? = nil,
wanted: String? = "en-US",
limit: Int? = largeLimit,
v2Downloaded: Bool = false,
ultraDownloaded: Bool = false
) -> KeyboardParakeetPlan {
KeyboardParakeetPlan.resolve(
isEnabled: enabled,
fitsDevice: fits,
twoLanguageEnabled: twoLanguage,
multilingualLanguage: multilingual,
wantedLanguage: wanted,
keyboardLimit: limit,
v2Downloaded: v2Downloaded,
ultraDownloaded: ultraDownloaded
)
}
XCTAssertEqual(plan(enabled: false), .off)
XCTAssertEqual(plan(fits: false), .off)
XCTAssertNil(plan(enabled: false).modelChoice)
XCTAssertEqual(plan(twoLanguage: true), .twoLanguage)
XCTAssertEqual(KeyboardParakeetDownload.hold(plan: .twoLanguage, unmetered: true, freeBytes: 8_000_000_000), .otherEngine)
// Multilingual downloaded + fits -> parakeetUltra choice; too small -> nil choice (no drift!).
let ultraReady = plan(multilingual: "nl-NL", limit: largeLimit, ultraDownloaded: true)
XCTAssertEqual(ultraReady, .multilingual(language: "nl-NL", downloaded: true, fitsKeyboardLimit: true))
XCTAssertEqual(ultraReady.modelChoice?.model, .parakeetUltra)
XCTAssertEqual(ultraReady.modelChoice?.language, "nl-NL")
XCTAssertFalse(ultraReady.awaitsV2Download)
XCTAssertEqual(KeyboardParakeetDownload.hold(plan: ultraReady, unmetered: true, freeBytes: 8_000_000_000), .otherEngine)
let ultraTooSmall = plan(multilingual: "nl-NL", limit: smallLimit, ultraDownloaded: true)
XCTAssertEqual(ultraTooSmall, .multilingual(language: "nl-NL", downloaded: true, fitsKeyboardLimit: false))
XCTAssertNil(ultraTooSmall.modelChoice, "measured keyboard limit below requiredLimit prevents loading")
let ultraMissing = plan(multilingual: "nl-NL", limit: largeLimit, ultraDownloaded: false)
XCTAssertNil(ultraMissing.modelChoice)
// Settings › Dictation › Model switched: the model picked before keeps dictating English
// until the new one is here (it goes only once the new one has loaded once).
let multilingualPickedEnglishStill = plan(multilingual: "en-US", v2Downloaded: true, ultraDownloaded: false)
XCTAssertEqual(multilingualPickedEnglishStill, .readyV2(language: "en-US"))
XCTAssertEqual(multilingualPickedEnglishStill.modelChoice?.model, .parakeetV2)
XCTAssertEqual(plan(multilingual: "en-US", v2Downloaded: true, ultraDownloaded: true).modelChoice?.model, .parakeetUltra)
XCTAssertNil(plan(multilingual: "nl-NL", v2Downloaded: true, ultraDownloaded: false).modelChoice, "English's model only takes English")
let englishPickedUltraStill = plan(v2Downloaded: false, ultraDownloaded: true)
XCTAssertEqual(englishPickedUltraStill, .awaitingV2DownloadUsingUltra(language: "en-US"))
XCTAssertEqual(englishPickedUltraStill.modelChoice?.model, .parakeetUltra)
XCTAssertNil(
KeyboardParakeetDownload.hold(plan: englishPickedUltraStill, unmetered: true, freeBytes: 8_000_000_000),
"English's model still downloads by itself"
)
XCTAssertEqual(plan(v2Downloaded: true, ultraDownloaded: true).modelChoice?.model, .parakeetV2)
// Non-English and nil wanted language.
XCTAssertEqual(plan(wanted: "es-ES"), .notEnglish)
XCTAssertEqual(plan(wanted: nil), .notEnglish)
XCTAssertEqual(KeyboardParakeetDownload.hold(plan: .notEnglish, unmetered: true, freeBytes: 8_000_000_000), .notEnglish)
// Unmeasured limit holds V2 auto-download and hides notice, but permits V2 choice if downloaded (e.g. simulator).
let unmeasuredEmpty = plan(limit: nil, v2Downloaded: false)
XCTAssertEqual(unmeasuredEmpty, .unmeasured(language: "en-US", v2Downloaded: false))
XCTAssertNil(unmeasuredEmpty.modelChoice)
XCTAssertFalse(unmeasuredEmpty.awaitsV2Download)
XCTAssertEqual(KeyboardParakeetDownload.hold(plan: unmeasuredEmpty, unmetered: true, freeBytes: 8_000_000_000), .keyboardNotMeasured)
let unmeasuredDownloaded = plan(limit: nil, v2Downloaded: true)
XCTAssertEqual(unmeasuredDownloaded.modelChoice?.model, .parakeetV2)
XCTAssertFalse(unmeasuredDownloaded.awaitsV2Download)
// Too small keyboard blocks V2 load, notice, and auto-download consistently.
let tooSmall = plan(limit: smallLimit, v2Downloaded: true)
XCTAssertEqual(tooSmall, .tooSmall)
XCTAssertNil(tooSmall.modelChoice, "measured small keyboard never attempts V2 load")
XCTAssertFalse(tooSmall.awaitsV2Download)
XCTAssertEqual(KeyboardParakeetDownload.hold(plan: tooSmall, unmetered: true, freeBytes: 8_000_000_000), .keyboardTooSmall)
// Awaiting V2 download shows notice and allows auto-download on Wi-Fi with space.
let awaiting = plan(limit: largeLimit, v2Downloaded: false)
XCTAssertEqual(awaiting, .awaitingV2Download(language: "en-US"))
XCTAssertNil(awaiting.modelChoice)
XCTAssertTrue(awaiting.awaitsV2Download)
XCTAssertNil(KeyboardParakeetDownload.hold(plan: awaiting, unmetered: true, freeBytes: 8_000_000_000))
XCTAssertEqual(KeyboardParakeetDownload.hold(plan: awaiting, unmetered: false, freeBytes: 8_000_000_000), .notOnWiFi)
// Ready V2 selects Parakeet V2 and hides notice.
let ready = plan(limit: largeLimit, v2Downloaded: true)
XCTAssertEqual(ready, .readyV2(language: "en-US"))
XCTAssertEqual(ready.modelChoice?.model, .parakeetV2)
XCTAssertEqual(ready.modelChoice?.language, "en-US")
XCTAssertFalse(ready.awaitsV2Download)
// Settings' row: the keyboard's "ready" is about English. Dictating Hinglish after the
// download, Apple Speech takes it, and the row says so rather than "Ready".
let now = Date()
func line(_ hold: KeyboardParakeetDownload.Hold?) -> String? {
KeyboardParakeetStatus.line(state: .idle, isDownloaded: true, hold: hold, keyboardState: (.ready, now), now: now)
}
XCTAssertEqual(line(nil), "Ready in the keyboard")
XCTAssertEqual(line(.notEnglish), "Only for English dictation.")
XCTAssertEqual(
KeyboardParakeetStatus.line(state: .idle, isDownloaded: false, hold: .notEnglish, keyboardState: nil, now: now),
"Only for English dictation."
)
// Model: Multilingual. Its download speaks for itself; once on the iPhone, what the
// keyboard does with it, or why it doesn't.
func multilingual(
downloaded: Bool = true,
takes: Bool = true,
fits: Bool = true,
_ state: KeyboardParakeetSettings.KeyboardState?
) -> String? {
KeyboardParakeetStatus.multilingualLine(
isDownloaded: downloaded,
takesLanguage: takes,
fitsKeyboard: fits,
keyboardState: state.map { ($0, now) },
now: now
)
}
XCTAssertNil(multilingual(downloaded: false, .ready))
XCTAssertEqual(multilingual(.ready), "Ready in the keyboard")
XCTAssertEqual(multilingual(.preparing), "Getting ready in the keyboard")
XCTAssertEqual(multilingual(nil), "Downloaded. The keyboard gets it ready the next time it opens.")
XCTAssertEqual(multilingual(fits: false, nil), "The keyboard can't use it right now, so it uses Apple's speech recognition.")
XCTAssertEqual(
multilingual(takes: false, .ready),
"Not for this dictation language, so the keyboard uses Apple's speech recognition."
)
// Only the picked model stays, and the other goes only once the picked one is downloaded
// and has loaded once, and never while Hex dictates with it.
func unpicked(
_ picked: SpeechModelChoice,
english: (Bool, Bool) = (true, true),
multilingual: (Bool, Bool) = (true, true),
hex: KeyboardParakeetPlan.Model? = nil
) -> KeyboardParakeetPlan.Model? {
SpeechModelChoice.unpicked(
picked: picked,
englishDownloaded: english.0,
englishLoaded: english.1,
multilingualDownloaded: multilingual.0,
multilingualLoaded: multilingual.1,
hexDictatesWith: hex
)
}
XCTAssertEqual(unpicked(.multilingual), .parakeetV2)
XCTAssertNil(unpicked(.multilingual, multilingual: (false, false)), "still downloading: English's stays and dictates")
XCTAssertNil(unpicked(.multilingual, multilingual: (true, false)), "downloaded, not loaded yet")
XCTAssertNil(unpicked(.multilingual, english: (false, false)), "nothing to remove")
XCTAssertNil(unpicked(.multilingual, hex: .parakeetV2), "Hex dictates with English's")
XCTAssertEqual(unpicked(.english), .parakeetUltra)
XCTAssertNil(unpicked(.english, english: (true, false)))
XCTAssertNil(unpicked(.english, english: (false, false)), "a download that failed leaves the multilingual one")
XCTAssertNil(unpicked(.english, hex: .parakeetUltra))
let names = SpeechModelChoice.multilingualLanguages
XCTAssertTrue(names.contains("Dutch") && names.contains("Ukrainian") && names.contains("English"), names)
}
func testWantedDictationLanguagePrecedenceAndSelfDictationHandoffReason() throws {
let suite = "hex.tests.ssot.\(UUID().uuidString)"
let defaults = try XCTUnwrap(UserDefaults(suiteName: suite))
defer { defaults.removePersistentDomain(forName: suite) }
let languages = LanguageSettings(defaults: defaults)
XCTAssertEqual(languages.wantedDictationLanguage(preferred: ["fr-FR", "en-US"]), "fr-FR")
XCTAssertNil(languages.wantedDictationLanguage(preferred: []))
languages.dictationLanguage = "es-MX"
XCTAssertEqual(languages.wantedDictationLanguage(preferred: ["fr-FR"]), "es-MX")
let selfDict = KeyboardSelfDictation(defaults: defaults)
XCTAssertEqual(selfDict.handoffReason(osMajorVersion: 18), .openedHex)
XCTAssertNil(selfDict.handoffReason(osMajorVersion: 26, hasMemoryToStart: true, includeRecentHandoff: false))
XCTAssertEqual(selfDict.handoffReason(osMajorVersion: 26, hasMemoryToStart: false, includeRecentHandoff: false), .memory)
languages.markKeyboardNeedsHex()
XCTAssertEqual(selfDict.handoffReason(osMajorVersion: 26, hasMemoryToStart: true, includeRecentHandoff: false), .speechModel)
XCTAssertFalse(selfDict.dictatesItself(osMajorVersion: 26))
languages.markKeyboardNeedsHex(unavailable: true)
XCTAssertEqual(
selfDict.handoffReason(osMajorVersion: 26, hasMemoryToStart: true, includeRecentHandoff: false),
.languageUnavailable,
"Hex found Apple has no model for the language here"
)
languages.clearKeyboardNeedsHex()
XCTAssertFalse(languages.keyboardLanguageUnavailable)
selfDict.recordHandedToHex(.memory)
XCTAssertEqual(selfDict.handoffReason(osMajorVersion: 26, includeRecentHandoff: true), .memory)
XCTAssertNil(selfDict.handoffReason(osMajorVersion: 26, hasMemoryToStart: true, includeRecentHandoff: false))
}
// MARK: - DictationPipeline.Preferences
@MainActor
func testDictationPipelinePreferencesSnapshotIsIsolatedFromLaterSettingChanges() async throws {
let suite = "hex.tests.pipeline-prefs.\(UUID().uuidString)"
let defaults = try XCTUnwrap(UserDefaults(suiteName: suite))
defer { defaults.removePersistentDomain(forName: suite) }
let lowercase = DictationLowercaseSettings(defaults: defaults)
let period = DictationPeriodSettings(defaults: defaults)
lowercase.isEnabled = true
period.isEnabled = false
let snapshot = DictationPipeline.Preferences.current(
style: .polished,
vocabulary: CustomVocabularySettings(defaults: defaults),
corrections: [],
lowercase: lowercase,
lists: DictationListSettings(defaults: defaults),
punctuation: DictationPunctuationSettings(defaults: defaults),
numbers: DictationNumberSettings(defaults: defaults),
hinglish: HinglishSettings(defaults: defaults),
greeklish: GreeklishSettings(defaults: defaults),
period: period
)
// Flip settings after capture started; the snapshot keeps what was active at start.
lowercase.isEnabled = false
period.isEnabled = true
let pipeline = DictationPipeline(preferences: snapshot, language: "en-US")
let stages = await pipeline.run("Hello world")
XCTAssertEqual(stages.formatted, "hello world")
}
}