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.
154 lines
4.0 KiB
Swift
154 lines
4.0 KiB
Swift
import Foundation
|
|
import SprachmittlerCore
|
|
import SprachmittlerEngines
|
|
import SprachmittlerText
|
|
import Testing
|
|
|
|
// Shared scaffolding for the panel test suites. The fakes are small and local to the tests.
|
|
|
|
// The helper returns the panel plus two small test boxes; a struct would add noise here.
|
|
// swiftlint:disable large_tuple
|
|
@MainActor
|
|
func makePanel(
|
|
streaming: any TranslationStreaming,
|
|
replacement: (@MainActor () async -> InsertionOutcome)? = nil,
|
|
onClose: @escaping @MainActor () -> Void = {},
|
|
detector: any LanguageDetecting = FixedLanguageDetector(detection: nil),
|
|
target: ModelLanguage = HyMT3Languages.german,
|
|
routes: [LanguageRoute] = []
|
|
) -> (PanelViewModel, CopiedTextBox, TargetBox) {
|
|
let session = TranslationSession(pipelineFactory: { streaming })
|
|
let copies = CopiedTextBox()
|
|
let targets = TargetBox()
|
|
let panel = PanelViewModel(
|
|
session: session,
|
|
configuration: PanelConfiguration(
|
|
target: target,
|
|
model: "test-hy-mt3",
|
|
keepAlive: "30m",
|
|
languageRoutes: routes
|
|
),
|
|
copy: { copies.text = $0 },
|
|
replace: replacement,
|
|
onCloseRequest: onClose,
|
|
onTargetChanged: { targets.codes.append($0.code) },
|
|
detector: detector
|
|
)
|
|
panel.startObserving()
|
|
return (panel, copies, targets)
|
|
}
|
|
|
|
// swiftlint:enable large_tuple
|
|
|
|
@MainActor
|
|
func waitFor(_ condition: @MainActor () -> Bool) async {
|
|
for _ in 0 ..< 10000 {
|
|
if condition() {
|
|
return
|
|
}
|
|
await Task.yield()
|
|
}
|
|
Issue.record("panel condition was not reached")
|
|
}
|
|
|
|
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
|
|
)
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
struct FailingStreaming: TranslationStreaming {
|
|
let error: SprachmittlerError
|
|
|
|
func translate(
|
|
_ request: TranslationSessionRequest,
|
|
onDelta: (@Sendable (String) async -> Void)?
|
|
) async throws -> PipelineResult {
|
|
throw error
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
final class CopiedTextBox: @unchecked Sendable {
|
|
var text: String?
|
|
}
|
|
|
|
final class TargetBox: @unchecked Sendable {
|
|
var codes: [String] = []
|
|
}
|
|
|
|
final class ReplaceBox: @unchecked Sendable {
|
|
var called = false
|
|
}
|
|
|
|
final class CloseBox: @unchecked Sendable {
|
|
var closed = false
|
|
}
|
|
|
|
struct FixedLanguageDetector: LanguageDetecting {
|
|
let detection: LanguageDetection?
|
|
|
|
func detect(_ text: String) -> LanguageDetection? {
|
|
detection
|
|
}
|
|
}
|