fix: improve model setup and native workflows

This commit is contained in:
public-repo-gate
2026-09-13 01:03:00 +03:00
parent 1cefe5b2ee
commit 30dae8b9a3
92 changed files with 13819 additions and 7022 deletions
@@ -0,0 +1,118 @@
import Foundation
import IrizDictate
import Testing
@testable import IrizSettings
@MainActor
@Suite("File transcription UI: safe queue and output")
struct SettingsFileTranscriptionTests {
@Test func preservesExistingTextAndWritesOnlyCompletePrivateResults() throws {
let root = try temporaryDirectory()
defer { try? FileManager.default.removeItem(at: root) }
let existing = root.appendingPathComponent("existing.txt")
try Data("owner text".utf8).write(to: existing)
#expect(throws: AudioBatchPlanError.destinationExists(existing.path)) {
try SettingsFileTranscription.saveTranscript("new text", to: existing)
}
#expect(try String(contentsOf: existing, encoding: .utf8) == "owner text")
let output = root.appendingPathComponent("result.txt")
#expect(throws: (any Error).self) {
try SettingsFileTranscription.saveTranscript(" \n ", to: output)
}
#expect(!FileManager.default.fileExists(atPath: output.path))
try SettingsFileTranscription.saveTranscript(" Complete text.\n", to: output)
#expect(try String(contentsOf: output, encoding: .utf8) == "Complete text.\n")
let attributes = try FileManager.default.attributesOfItem(atPath: output.path)
#expect((attributes[.posixPermissions] as? NSNumber)?.intValue == 0o600)
#expect(try !FileManager.default.contentsOfDirectory(atPath: root.path)
.contains { $0.hasPrefix(".iriz-transcript-") })
}
@Test func preventsDoubleSubmitAndRetriesOnlyFailedFiles() async throws {
let root = try temporaryDirectory()
defer { try? FileManager.default.removeItem(at: root) }
let first = try source("first.wav", in: root)
let second = try source("second.wav", in: root)
let attempts = Attempts()
let model = SettingsFileTranscription { job, _, language, progress in
let fail = await attempts.record(job.source, language: language)
await progress("test", 0.5)
if fail { throw CocoaError(.fileReadCorruptFile) }
try SettingsFileTranscription.saveTranscript("test transcript", to: job.destination)
}
model.queue = [first, second]
let task = model.start(engine: .whisperLargeV3, language: .english)
#expect(model.busy)
#expect(model.start(engine: .multilingualV3, language: .russian) == nil)
await task?.value
#expect(!model.busy)
#expect(model.queue == [second])
#expect(model.results.map(\.source) == [first.url])
#expect(model.errors[second.id] != nil)
#expect(model.progress == nil)
#expect(model.fraction == nil)
await model.start(engine: .whisperLargeV3, language: .english)?.value
#expect(model.queue.isEmpty)
#expect(model.errors.isEmpty)
#expect(model.results.map(\.source) == [first.url, second.url])
#expect(await attempts.sources == [first.url, second.url, second.url])
#expect(await attempts.languages == [.english, .english, .english])
}
@Test func stopFinishesCurrentFileAndKeepsRemainingQueue() async throws {
let root = try temporaryDirectory()
defer { try? FileManager.default.removeItem(at: root) }
let first = try source("first.wav", in: root)
let second = try source("second.wav", in: root)
let started = AsyncStream<Void>.makeStream()
let resume = AsyncStream<Void>.makeStream()
let model = SettingsFileTranscription { job, _, _, _ in
started.continuation.yield(())
for await _ in resume.stream { break }
try SettingsFileTranscription.saveTranscript("finished", to: job.destination)
}
model.queue = [first, second]
let task = model.start(engine: .multilingualV3, language: .auto)
for await _ in started.stream { break }
model.stopAfterCurrent()
resume.continuation.yield(())
await task?.value
started.continuation.finish()
resume.continuation.finish()
#expect(model.queue == [second])
#expect(model.results.map(\.source) == [first.url])
#expect(model.stopping)
#expect(!model.busy)
}
@Test func rejectsRemoteURLsBeforeAVFoundation() {
#expect(!irizDropAccepts(URL(string: "https://example.com/voice.wav")!, extensions: ["wav"]))
#expect(irizDropAccepts(URL(fileURLWithPath: "/tmp/voice.WAV"), extensions: ["wav"]))
}
private func temporaryDirectory() throws -> URL {
let root = FileManager.default.temporaryDirectory
.appendingPathComponent("iriz-file-ui-tests-\(UUID().uuidString)", isDirectory: true)
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
return root
}
private func source(_ name: String, in root: URL) throws -> IrizDropItem {
let url = root.appendingPathComponent(name)
// Только имена и очередь: ASR и декодер тесты не вызывают.
try Data("fixture".utf8).write(to: url)
return IrizDropItem(url: url, bytes: 7)
}
}
private actor Attempts {
var sources: [URL] = []
var languages: [DictationLanguage] = []
func record(_ source: URL, language: DictationLanguage) -> Bool {
sources.append(source)
languages.append(language)
return source.lastPathComponent == "second.wav" && sources.filter { $0 == source }.count == 1
}
}
@@ -305,6 +305,12 @@ struct SettingsModelTests {
model.addCorrection(source: "a", replacement: "b")
model.promptModeEnabled = true
model.promptRecipient = .generic
model.retentionDays = 0
model.hudShowsHints = true
model.hudSize = .large
model.promptGuidanceInstructions = "Собирай короткий план"
model.promptGuidanceExamples = [PromptUserExample(spoken: "проверь", wanted: "Проверь проект")]
model.speechEngine = .whisperTurbo
model.codexPath = "/custom/codex"
model.setHotkey(recordableHotkeyChoice(forKeycode: 97, modifiers: .maskCommand)!, for: .prompt)
fixture.dictationSettings.promptOnboardingOffered = true
@@ -316,6 +322,17 @@ struct SettingsModelTests {
#expect(model.layoutMode == .fixing)
#expect(model.launchAtLogin)
#expect(model.corrections.isEmpty)
#expect(model.retentionDays == DICTATION_RETENTION_DEFAULT_DAYS)
#expect(!model.hudShowsHints)
#expect(model.hudSize == DICTATION_HUD_DEFAULT_SIZE)
#expect(model.promptGuidanceInstructions.isEmpty)
#expect(model.promptGuidanceExamples.isEmpty)
#expect(model.speechEngine == .multilingualV3)
#expect(fixture.dictationSettings.speechEngine == .multilingualV3)
#expect(fixture.dictationSettings.dictationRetentionDays == DICTATION_RETENTION_DEFAULT_DAYS)
#expect(!fixture.dictationSettings.dictationHUDShowsHints)
#expect(fixture.dictationSettings.dictationHUDSize == DICTATION_HUD_DEFAULT_SIZE)
#expect(fixture.dictationSettings.promptUserGuidance == .none)
#expect(!model.promptModeEnabled)
#expect(model.promptRecipient == .codex)
#expect(model.codexPath.isEmpty)
@@ -439,6 +456,30 @@ struct SettingsModelTests {
#expect(model.save())
#expect(fixture.launchAtLogin)
}
@Test func modelReadinessTracksSelectedEngineAndInstallationKeepsOtherDrafts() {
let fixture = Fixture()
fixture.dictationSettings.speechEngine = .whisperTurbo
let model = fixture.makeModel(speechModelProbe: { $0 == .multilingualV3 })
#expect(!model.speechModelInstalled)
#expect(model.canUseInstalledParakeet)
model.speechEngine = .multilingualV3
#expect(model.speechModelInstalled)
#expect(!model.canUseInstalledParakeet)
model.speechEngine = .whisperLargeV3
#expect(!model.speechModelInstalled)
model.enterDelayText = "987"
model.promptGuidanceInstructions = "Unsaved user draft"
let savedDelay = fixture.dictationSettings.enterDelayMilliseconds
model.applyInstalledSpeechModel(.multilingualV3)
#expect(model.speechEngine == .multilingualV3)
#expect(model.speechModelInstalled)
#expect(model.enterDelayText == "987")
#expect(model.promptGuidanceInstructions == "Unsaved user draft")
#expect(fixture.dictationSettings.enterDelayMilliseconds == savedDelay)
#expect(fixture.dictationSettings.speechEngine == .whisperTurbo)
}
}
@MainActor
@@ -462,7 +503,8 @@ private final class Fixture {
func makeModel(
codexDetector: @escaping (String) -> URL? = {
DictationSettings.detectCodexExecutable(storedPath: $0)
}
},
speechModelProbe: @escaping (SpeechModelProfile) -> Bool = { _ in false }
) -> SettingsModel {
SettingsModel(
dictationSettings: dictationSettings,
@@ -473,7 +515,9 @@ private final class Fixture {
writeLaunchAtLogin: { self.launchAtLogin = $0 }
),
layoutHotkeys: .settings(layoutHotkeySettings),
codexDetector: codexDetector
codexDetector: codexDetector,
installedSpeechEngine: { .multilingualV3 },
speechModelProbe: speechModelProbe
)
}
}