hex/ios/HEXMobile/DictationController+LaunchFixtures.swift
Ryan Vogel 9e41970efe
iOS: both speech models download on Wi-Fi, with Use mobile data (#2846)
* iOS: both speech models download on Wi-Fi, with Use mobile data

What: Settings › Dictation › Model's two downloads now follow one rule
(SpeechModelDownloadNetwork). On Wi-Fi (KeyboardParakeetDownload.isUnmetered: not
cellular, a hotspot or Low Data Mode) a picked model downloads as before. Off Wi-Fi it
waits: English's line already said "Downloads on Wi-Fi."; Multilingual's now says the
same (ParakeetUltraDownload.State.waitingForWiFi) instead of starting at once. Both lines
get a Use mobile data action that starts that download now. Wi-Fi coming back starts a
waiting download by itself; a multilingual download running on Wi-Fi now stops where it
is when Wi-Fi goes (its files resume), as English's always did. Use mobile data is one
decision for that model's download, kept in the App Group until the model is on the
iPhone (a stop, Try again or relaunch carries on); picking the other model forgets it.
Multilingual's row text now reads "A 632.2 MB download on Wi-Fi." (zh-Hans updated);
the one new string is "Use mobile data" (zh-Hans 使用蜂窝数据). English's own network
monitor moved into SpeechModelDownloadNetwork, which both downloads observe.

Unchanged: a download already running on Wi-Fi; unpicked (the old model stays until the
new one is downloaded and has loaded once); picking English still stops and removes a
partial multilingual download; the keyboard extension and its notice; the Speech model
in keyboard switch turned on by hand still downloads English on any connection.

Why: TestFlight build 151 (no Wi-Fi at home): English waited for Wi-Fi but Multilingual
started a 632 MB download over mobile data the moment it was picked. Ryan's decision:
wait for Wi-Fi, with a Use mobile data button, as on Android.

Verified: tests in DictationControllerTests (both wait off Wi-Fi, Use mobile data starts
them, Wi-Fi returning starts a waiting one, leaving Wi-Fi stops one, both start at once on
Wi-Fi, picking the other model asks again); suite run separately.

* iOS: test that English picked mid-wait leaves a waiting multilingual download stopped

What: the multilingual Wi-Fi test now also stops a download waiting for Wi-Fi (what picking
English does) and checks Wi-Fi coming back doesn't start it, and that picking Multilingual
again waits again.
Why: the task asks for switching models mid-wait to be covered.
Verified: suite run separately.

* iOS: snapshot scenes for Settings › Dictation › Model's download states

What: --snap-state settings-dictation-model-<english|multilingual>-<waiting|downloading|ready>
opens Settings › Dictation with that model picked and its download in that state
(DictationController.applySpeechModelFixture, ParakeetUltraDownload.applyFixture(share:)).
Waiting goes through the real rule: with --simulate-cellular the connection reads as off
Wi-Fi and the keyboard limit is recorded, so only the connection holds the download.
SettingsSnapshotFixture.applyDefaults keeps Multilingual picked for -multilingual- scenes.
Why: screenshots of the Use mobile data row in each state; the simulator has no cellular.
Verified: scenes rendered in a simbox (screenshots in the PR).

* iOS: Model snapshot scenes record the keyboard limit only for waiting

What: settings-dictation-model-*-downloading and -ready no longer record a keyboard limit.
Why: with a limit and the simulator's Wi-Fi, English's real download started under the
downloading scene and finished during the screenshot run, so a later downloading shot
read Downloaded. Without a limit nothing downloads, as with Home's model-card scenes.
Verified: the 12 Model row screenshots retaken in the simbox.

* iOS: check the multilingual Wi-Fi test's resume request, not the last request

What: testAMultilingualDownloadFollowsWiFiComingAndGoing now checks the first two requests
are the fresh one and the bytes=800- resume, instead of the last request.
Why: the stub model has two files, so the last request is the second file's, with no range;
the suite failed on that assertion alone.
Verified: full suite rerun in the simbox.
2026-11-25 09:10:45 -05:00

273 lines
13 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 Foundation
import SwiftUI
extension DictationController {
// MARK: - Launch Fixtures for Simulator Verification (`--snap-state <name>`)
static func applyLaunchFixtureIfPresent(to controller: DictationController) -> Bool {
let args = ProcessInfo.processInfo.arguments
guard let flagIndex = args.firstIndex(of: "--snap-state"),
flagIndex + 1 < args.count else {
return false
}
let scene = args[flagIndex + 1]
controller.engine = .appleSpeech
controller.transcriptionLanguage = "English"
controller.transcriptionLanguageID = "en-US"
controller.keyboardBridge.setModelInstalled(true)
controller.isModelReady = true
switch scene {
case "setup", "setup-sheet", "setup-sheet-microphone", "setup-sheet-noaccess", "paste-setting", _ where scene.hasPrefix("onboarding-") || scene.hasPrefix("settings-") || scene.hasPrefix("model-card-"):
controller.applyFixturePhase(.ready)
controller.warmDuration = .fifteenMinutes
controller.setWarmSession(.cold)
if scene.hasPrefix("model-card-") { KeyboardParakeetDownload.shared.applyFixture(scene) }
if scene.hasPrefix("settings-dictation-model-") { controller.applySpeechModelFixture(scene) }
case "language-unavailable":
// Russian picked by hand on an iPhone whose Apple Speech can't install it.
let unavailable = SpeechModelUnavailable(
language: "ru-RU",
reason: .notOnThisIPhone,
fallback: "en-US",
automatic: false
)
controller.isModelReady = false
controller.engine = .appleSpeech
controller.applyFixturePhase(.failed(UserFacingError.message(for: unavailable, context: .model)))
controller.languageOffer = "en-US"
case "language-switched":
// Automatic moved on from Russian to English.
let unavailable = SpeechModelUnavailable(language: "ru-RU", reason: .notOnThisIPhone, fallback: "en-US")
controller.applyFixturePhase(.ready)
controller.languageNotice = UserFacingError.message(for: unavailable, context: .model)
case "ready":
controller.applyFixturePhase(.ready)
controller.warmDuration = .off
controller.setWarmSession(.cold)
controller.transcript = "Meet at ten on Thursday and bring the updated design notes."
case "warm":
controller.applyFixturePhase(.ready)
controller.warmDuration = .fifteenMinutes
controller.applyFixtureWarmSession(remaining: 14 * 60 + 42)
controller.transcript = "Meet at ten on Thursday and bring the updated design notes."
case "recording":
controller.applyFixturePhase(.recording)
controller.warmDuration = .fifteenMinutes
controller.audioLevel = 0.74
controller.loudnessDB = -31.0
controller.recordingDuration = 6.4
controller.applyFixtureWarmSession(remaining: 14 * 60 + 54)
case "transcribing":
controller.applyFixturePhase(.transcribing)
controller.warmDuration = .fifteenMinutes
controller.applyFixtureWarmSession(remaining: 14 * 60 + 52)
case "handoff-listening", "handoff-no-home-bar":
controller.applyFixturePhase(.recording)
controller.audioLevel = 0.68
controller.loudnessDB = -32.5
controller.recordingDuration = 3.2
controller.applyFixtureWarmSession(remaining: 14 * 60 + 57)
Task { @MainActor in
let start = Date().timeIntervalSince1970 - 3.0
var simTime: Float = 0.0
while !Task.isCancelled {
controller.loudnessDB = DotScopeSimulation.demoLoudness(at: simTime) + 4.5
controller.recordingDuration = Date().timeIntervalSince1970 - start
simTime += 0.033
try? await Task.sleep(for: .milliseconds(33))
}
}
case "handoff-tour":
controller.applyFixturePhase(.recording)
controller.audioLevel = 0.68
controller.loudnessDB = -32.0
controller.recordingDuration = 0.0
controller.applyFixtureWarmSession(remaining: 14 * 60 + 58)
Task { @MainActor in
let boot = Date().timeIntervalSince1970
var simTime: Float = 0.0
while !Task.isCancelled {
let elapsed = Date().timeIntervalSince1970 - boot
if elapsed < 3.4 {
if controller.phase != .recording {
withAnimation(HexMotion.stage) {
controller.applyFixturePhase(.recording)
}
}
controller.loudnessDB = DotScopeSimulation.demoLoudness(at: simTime) + 4.5
controller.recordingDuration = elapsed
simTime += 0.033
} else if elapsed < 4.7 {
if controller.phase != .transcribing {
withAnimation(HexMotion.stage) {
controller.applyFixturePhase(.transcribing)
}
}
controller.loudnessDB = -120.0
} else {
if controller.phase != .ready {
withAnimation(HexMotion.stage) {
controller.applyFixturePhase(.ready)
}
}
}
try? await Task.sleep(for: .milliseconds(33))
}
}
case "hosted-keyboard-warm":
controller.applyFixturePhase(.ready)
controller.warmDuration = .fifteenMinutes
controller.applyFixtureWarmSession(remaining: 14 * 60 + 42)
Task { @MainActor in
while !Task.isCancelled {
let t = Date().timeIntervalSince1970
controller.keyboardBridge.publish(
KeyboardSnapshot(
state: .ready,
heartbeat: t,
expiresAt: t + 14 * 60 + 42
)
)
try? await Task.sleep(for: .milliseconds(250))
}
}
case "hosted-keyboard-recording", "hosted-keyboard-light":
controller.applyFixturePhase(.ready)
controller.warmDuration = .fifteenMinutes
controller.applyFixtureWarmSession(remaining: 14 * 60 + 55)
let startWall = Date().timeIntervalSince1970 - 4.0
Task { @MainActor in
var simTime: Float = 0.4
while !Task.isCancelled {
let t = Date().timeIntervalSince1970
let db = -28.5 + 5.2 * sin(simTime * 19.0) + 2.4 * cos(simTime * 7.3)
controller.keyboardBridge.publish(
KeyboardSnapshot(
state: .recording,
heartbeat: t,
expiresAt: t + 15 * 60,
recordingStartedAt: startWall,
audioLevel: 0.78,
loudnessDB: db,
jobID: "hosted-demo-job"
)
)
simTime += 0.033
try? await Task.sleep(for: .milliseconds(33))
}
}
case "hosted-keyboard-tour":
controller.applyFixturePhase(.ready)
controller.warmDuration = .fifteenMinutes
controller.applyFixtureWarmSession(remaining: 14 * 60 + 58)
Task { @MainActor in
let boot = Date().timeIntervalSince1970
let freshResultID = "hosted-tour-\(UUID().uuidString)"
var simTime: Float = 0.0
while !Task.isCancelled {
let t = Date().timeIntervalSince1970
let elapsed = t - boot
if elapsed < 2.5 {
controller.keyboardBridge.publish(
KeyboardSnapshot(
state: .ready,
heartbeat: t,
expiresAt: t + 15 * 60
)
)
} else if elapsed < 5.2 {
let db = DotScopeSimulation.demoLoudness(at: simTime) + 4.5
controller.keyboardBridge.publish(
KeyboardSnapshot(
state: .recording,
heartbeat: t,
expiresAt: t + 15 * 60,
recordingStartedAt: boot + 2.5,
audioLevel: 0.78,
loudnessDB: db,
jobID: "hosted-tour-job"
)
)
simTime += 0.033
} else if elapsed < 6.4 {
controller.keyboardBridge.publish(
KeyboardSnapshot(
state: .transcribing,
heartbeat: t,
expiresAt: t + 15 * 60,
jobID: "hosted-tour-job"
)
)
} else {
controller.keyboardBridge.publish(
KeyboardSnapshot(
state: .ready,
heartbeat: t,
expiresAt: t + 14 * 60 + 54,
resultID: freshResultID,
resultExpiresAt: boot + 30,
transcript: "See you at ten."
)
)
}
try? await Task.sleep(for: .milliseconds(33))
}
}
default:
return false
}
return true
}
private func applyFixtureWarmSession(remaining: TimeInterval) {
setWarmSession(.warm(expiresAt: Date().timeIntervalSince1970 + remaining), remaining: remaining)
}
/// `settings-dictation-model-<english|multilingual>-<waiting|downloading|ready>`: Settings ›
/// Dictation › Model with that model picked and its download in that state. Waiting is the
/// real rule: pass `--simulate-cellular` and the keyboard has measured room for the model,
/// so only the connection holds it. The other states leave the keyboard unmeasured, so
/// nothing really downloads on the simulator's Wi-Fi.
private func applySpeechModelFixture(_ scene: String) {
let multilingual = scene.contains("-multilingual-")
let step = scene.split(separator: "-").last.map(String.init) ?? "waiting"
KeyboardParakeetDownload.shared.applyFixture(multilingual ? "model-card-waiting" : "model-card-\(step)")
if step == "waiting" {
KeyboardParakeetSettings().recordKeyboardLimit(KeyboardParakeetBudget.requiredLimit)
}
MultilingualParakeetSettings().isEnabled = multilingual
downloadNetwork.forgetMobileData()
guard multilingual else { return }
ParakeetUltraModel.remove()
switch step {
case "downloading":
multilingualDownload.applyFixture(share: 0.42)
case "ready":
// Files of the right sizes (sparse), so it reads as on the iPhone; nothing loads them.
for file in ParakeetUltraModel.files {
let url = ParakeetUltraModel.directory.appendingPathComponent(file.path)
try? FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
FileManager.default.createFile(atPath: url.path, contents: nil)
let handle = try? FileHandle(forWritingTo: url)
try? handle?.truncate(atOffset: UInt64(file.size))
try? handle?.close()
}
KeyboardParakeetSettings().setKeyboardState(.ready, multilingual: true)
default:
startMultilingualDownloadIfWanted()
}
}
}