The header language picker set the effective target and then re-ran prepare, where TranslationDirectionPolicy only knew a Bool override and returned the mother tongue. Any picked language other than the mother tongue was discarded, so the panel appeared not to change. The policy now takes the override target itself, and PanelViewModel stores it, so a picker choice wins over a rule for that capture; a new capture returns to the rule/mother tongue. Move the shared panel test scaffolding into PanelTestSupport and the routing tests into PanelRoutingTests, and add picker and rule-override tests. Verified: make verify green; live checks with a German -> English rule show the picker switching to French, the next capture back to English with the banner, and the banner override to German.
144 lines
4.7 KiB
Swift
144 lines
4.7 KiB
Swift
import Foundation
|
|
import SprachmittlerEngines
|
|
import SprachmittlerText
|
|
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)
|
|
}
|
|
}
|