feat(m4-m6): session, panel, Replace, settings and onboarding
M4: TranslationSession actor with epochs/one-in-flight streaming, PanelViewModel, non-activating NSPanel, streaming panel UI, searchable/recent target languages, warnings/errors and Copy/Retry/Replace actions. M5: InsertionCoordinator with target capture, AX direct replacement, secure-field/own-PID refusal, frontmost and selected-text verification, snapshot/⌘V/restore fallback, cancellation-safe restore and blocking-warning gate. M6: versioned AppSettings/SettingsStore, onboarding for Ollama/model/target/Accessibility, General/Model/Translation/Privacy/Diagnostics settings, launch-at-login via SMAppService, status menu (target, copy/replace last, setup, diagnostics, about), text-free diagnostics and first-run setup. Docs: PROGRESS M4–M6, ARCHITECTURE §10–§12, ADR-019/020, SELECTION_AND_CLIPBOARD and TESTING. Manual 10-app matrices, real AX/paste delivery, VoiceOver and focus checks remain open. Verified: make verify green (format, strict lint, scripts, privacy, Debug+Release signatures, 217 package tests); app launches and a two-copy clipboard smoke test opens the panel.
This commit is contained in:
1 parent
64078e2f04
commit
a78572adc3
47 files changed
+4963
-79
No files matched your search
@@ -0,0 +1,273 @@
|
||||
import Foundation
|
||||
import SprachmittlerEngines
|
||||
import Testing
|
||||
@testable import SprachmittlerCore
|
||||
|
||||
@Suite("Panel view model")
|
||||
@MainActor
|
||||
struct PanelViewModelTests {
|
||||
private let captured = CapturedText(
|
||||
text: "Hello.",
|
||||
capturedAt: Date(timeIntervalSince1970: 0),
|
||||
source: .doubleCopy
|
||||
)
|
||||
|
||||
@Test func streamsIntoThePanelAndCopyKeepsItOpen() async {
|
||||
let (panel, copies, _) = makePanel(
|
||||
streaming: ImmediateStreaming(deltas: ["Hal", "lo"], result: makeResult("Hallo"))
|
||||
)
|
||||
await panel.begin(captured)
|
||||
await waitFor { panel.phase == .ready }
|
||||
|
||||
#expect(panel.translation == "Hallo")
|
||||
#expect(panel.canCopy)
|
||||
#expect(!panel.didCopy)
|
||||
|
||||
panel.copyTranslation()
|
||||
#expect(panel.didCopy)
|
||||
#expect(copies.text == "Hallo")
|
||||
#expect(panel.phase == .ready)
|
||||
}
|
||||
|
||||
@Test func blockingWarningsDisableReplace() async {
|
||||
let replaceBox = ReplaceBox()
|
||||
let (panel, _, _) = makePanel(
|
||||
streaming: ImmediateStreaming(
|
||||
deltas: ["Hallo"],
|
||||
result: makeResult("Hallo", warnings: [.truncated])
|
||||
),
|
||||
replacement: {
|
||||
replaceBox.called = true
|
||||
return .replaced(using: .accessibility)
|
||||
}
|
||||
)
|
||||
await panel.begin(captured)
|
||||
await waitFor { panel.phase == .ready }
|
||||
|
||||
#expect(panel.blockingWarnings.contains(.truncated))
|
||||
#expect(!panel.canReplace)
|
||||
await panel.replaceSelection()
|
||||
#expect(!replaceBox.called)
|
||||
}
|
||||
|
||||
@Test func cleanResultEnablesReplaceAndClosesOnSuccess() async {
|
||||
let closeBox = CloseBox()
|
||||
let (panel, _, _) = makePanel(
|
||||
streaming: ImmediateStreaming(deltas: ["Hallo"], result: makeResult("Hallo")),
|
||||
replacement: { .replaced(using: .accessibility) },
|
||||
onClose: { closeBox.closed = true }
|
||||
)
|
||||
await panel.begin(captured)
|
||||
await waitFor { panel.phase == .ready }
|
||||
|
||||
#expect(panel.canReplace)
|
||||
await panel.replaceSelection()
|
||||
#expect(closeBox.closed)
|
||||
}
|
||||
|
||||
@Test func sameLanguageWarningCanBeDismissed() async {
|
||||
let warning = TranslationWarning.sameLanguage(source: "de", target: "de")
|
||||
let (panel, _, _) = makePanel(
|
||||
streaming: ImmediateStreaming(
|
||||
deltas: ["Hallo"],
|
||||
result: makeResult("Hallo", warnings: [warning])
|
||||
)
|
||||
)
|
||||
await panel.begin(captured)
|
||||
await waitFor { panel.phase == .ready }
|
||||
|
||||
#expect(panel.sameLanguageWarning == warning)
|
||||
panel.dismissSameLanguageWarning()
|
||||
#expect(panel.sameLanguageWarning == nil)
|
||||
}
|
||||
|
||||
@Test func changingTargetRetranslatesAndNotifies() async {
|
||||
let streaming = EchoStreaming()
|
||||
let (panel, _, targets) = makePanel(streaming: streaming)
|
||||
await panel.begin(captured)
|
||||
await waitFor { panel.phase == .ready }
|
||||
#expect(panel.translation == "de")
|
||||
|
||||
await panel.setTarget(HyMT3Languages.english)
|
||||
await waitFor { panel.phase == .ready && panel.translation == "en" }
|
||||
|
||||
#expect(panel.configuration.target == HyMT3Languages.english)
|
||||
#expect(targets.codes == ["en"])
|
||||
#expect(await streaming.callCount == 2)
|
||||
}
|
||||
|
||||
@Test func retryUsesTheLastCapture() async {
|
||||
let streaming = CountingStreaming(result: makeResult("Hallo"))
|
||||
let (panel, _, _) = makePanel(streaming: streaming)
|
||||
await panel.begin(captured)
|
||||
await waitFor { panel.phase == .ready }
|
||||
|
||||
await panel.retry()
|
||||
for _ in 0 ..< 1000 {
|
||||
if await streaming.callCount == 2 {
|
||||
break
|
||||
}
|
||||
await Task.yield()
|
||||
}
|
||||
await waitFor { panel.phase == .ready }
|
||||
|
||||
#expect(panel.translation == "Hallo")
|
||||
#expect(await streaming.callCount == 2)
|
||||
}
|
||||
|
||||
@Test func failureShowsAUserMessage() async {
|
||||
let error = SprachmittlerError.modelNotInstalled(model: "m")
|
||||
let (panel, _, _) = makePanel(streaming: FailingStreaming(error: error))
|
||||
await panel.begin(captured)
|
||||
await waitFor { panel.phase == .failed }
|
||||
|
||||
#expect(panel.error == error)
|
||||
#expect(panel.errorMessage == error.userMessage)
|
||||
#expect(panel.recoveryAction == .pullModel("m"))
|
||||
}
|
||||
|
||||
@Test func closeClearsStateAndCancels() async {
|
||||
let (panel, _, _) = makePanel(
|
||||
streaming: ImmediateStreaming(deltas: ["Hallo"], result: makeResult("Hallo"))
|
||||
)
|
||||
await panel.begin(captured)
|
||||
await waitFor { panel.phase == .ready }
|
||||
|
||||
await panel.close()
|
||||
#expect(panel.phase == .idle)
|
||||
#expect(panel.source == nil)
|
||||
#expect(panel.translation.isEmpty)
|
||||
#expect(panel.warnings.isEmpty)
|
||||
}
|
||||
|
||||
// The helper returns the panel and two small test captures; a struct would add noise here.
|
||||
// swiftlint:disable large_tuple
|
||||
private func makePanel(
|
||||
streaming: any TranslationStreaming,
|
||||
replacement: (@MainActor () async -> InsertionOutcome)? = nil,
|
||||
onClose: @escaping @MainActor () -> Void = {}
|
||||
) -> (PanelViewModel, CopiedTextBox, TargetBox) {
|
||||
let session = TranslationSession(pipelineFactory: { streaming })
|
||||
let copies = CopiedTextBox()
|
||||
let targets = TargetBox()
|
||||
let panel = PanelViewModel(
|
||||
session: session,
|
||||
configuration: PanelConfiguration(
|
||||
target: HyMT3Languages.german,
|
||||
model: "test-hy-mt3",
|
||||
keepAlive: "30m"
|
||||
),
|
||||
copy: { copies.text = $0 },
|
||||
replace: replacement,
|
||||
onCloseRequest: onClose,
|
||||
onTargetChanged: { targets.codes.append($0.code) }
|
||||
)
|
||||
panel.startObserving()
|
||||
return (panel, copies, targets)
|
||||
}
|
||||
|
||||
// swiftlint:enable large_tuple
|
||||
|
||||
private func waitFor(_ condition: @MainActor () -> Bool) async {
|
||||
for _ in 0 ..< 10000 {
|
||||
if condition() {
|
||||
return
|
||||
}
|
||||
await Task.yield()
|
||||
}
|
||||
Issue.record("panel condition was not reached")
|
||||
}
|
||||
|
||||
private func makeResult(
|
||||
_ translation: String,
|
||||
warnings: [TranslationWarning] = []
|
||||
) -> PipelineResult {
|
||||
PipelineResult(
|
||||
translation: translation,
|
||||
warnings: warnings,
|
||||
sourceLanguage: "en",
|
||||
timing: TranslationTiming(firstTokenMs: 1, totalMs: 2, promptTokens: 3, outputTokens: 4),
|
||||
chunkCount: 1
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private struct ImmediateStreaming: TranslationStreaming {
|
||||
let deltas: [String]
|
||||
let result: PipelineResult
|
||||
|
||||
func translate(
|
||||
_ request: TranslationSessionRequest,
|
||||
onDelta: (@Sendable (String) async -> Void)?
|
||||
) async throws -> PipelineResult {
|
||||
for delta in deltas {
|
||||
await onDelta?(delta)
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
private struct FailingStreaming: TranslationStreaming {
|
||||
let error: SprachmittlerError
|
||||
|
||||
func translate(
|
||||
_ request: TranslationSessionRequest,
|
||||
onDelta: (@Sendable (String) async -> Void)?
|
||||
) async throws -> PipelineResult {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
private actor EchoStreaming: TranslationStreaming {
|
||||
private(set) var callCount = 0
|
||||
|
||||
func translate(
|
||||
_ request: TranslationSessionRequest,
|
||||
onDelta: (@Sendable (String) async -> Void)?
|
||||
) async throws -> PipelineResult {
|
||||
callCount += 1
|
||||
let result = PipelineResult(
|
||||
translation: request.target.code,
|
||||
warnings: [],
|
||||
sourceLanguage: "en",
|
||||
timing: TranslationTiming(firstTokenMs: 1, totalMs: 2, promptTokens: 3, outputTokens: 4),
|
||||
chunkCount: 1
|
||||
)
|
||||
await onDelta?(result.translation)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
private actor CountingStreaming: TranslationStreaming {
|
||||
private(set) var callCount = 0
|
||||
let result: PipelineResult
|
||||
|
||||
init(result: PipelineResult) {
|
||||
self.result = result
|
||||
}
|
||||
|
||||
func translate(
|
||||
_ request: TranslationSessionRequest,
|
||||
onDelta: (@Sendable (String) async -> Void)?
|
||||
) async throws -> PipelineResult {
|
||||
callCount += 1
|
||||
await onDelta?(result.translation)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
private final class CopiedTextBox: @unchecked Sendable {
|
||||
var text: String?
|
||||
}
|
||||
|
||||
private final class TargetBox: @unchecked Sendable {
|
||||
var codes: [String] = []
|
||||
}
|
||||
|
||||
private final class ReplaceBox: @unchecked Sendable {
|
||||
var called = false
|
||||
}
|
||||
|
||||
private final class CloseBox: @unchecked Sendable {
|
||||
var closed = false
|
||||
}
|
||||
Reference in new issue
Block a user