Files
Sprachmittler/Packages/SprachmittlerKit/Tests/SprachmittlerCoreTests/PanelTestSupport.swift
T
schihei 2a601058f4 feat(capture): translate long selections in parts
The capture gate and the chunk size were the same number, so a selection above one chunk was rejected before the pipeline could split it; the Settings sliders moved both thresholds together. Add maximumPartsPerSelection (1-8, default 1, so existing behaviour is unchanged). The capture limit is now part size x parts and travels on TranslationSessionRequest, so the watcher, CapturePolicy and TranslationSession agree for that capture. TranslationStreaming gained an optional per-part callback; the session emits chunkStarted and the panel shows 'Part 2 of 4' while streaming and '2 parts' when finished. textTooLong now points at Settings and offers an Open Settings button. Lower the dense-script part-size maximum to 4000 because 5000 dense characters plus a 4096-token output can overflow the 8192-token context. Tests cover capture limits, watcher limits, pipeline part callbacks, session events and limits, panel progress, settings clamping and migration. Docs updated: ADR-011, TRANSLATION_OUTPUT, SELECTION_AND_CLIPBOARD, ARCHITECTURE, LANGUAGES and README. Verified: make verify green; live 4355-character German text showed Part 1 of 2, Part 2 of 2 and '2 parts'; with one part the panel showed the too-long message and Open Settings.
2026-11-27 20:21:30 +01:00

199 lines
5.6 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] = [],
captureLimits: CaptureLimits = CaptureLimits()
) -> (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,
captureLimits: captureLimits
),
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
}
}
/// A fake that reports three parts and waits after the first until the test releases it.
actor GatedMultiPartStreaming: TranslationStreaming {
private(set) var requests: [TranslationSessionRequest] = []
private var releaseContinuation: CheckedContinuation<Void, Never>?
func translate(
_ request: TranslationSessionRequest,
onDelta: (@Sendable (String) async -> Void)?
) async throws -> PipelineResult {
try await translate(request, onDelta: onDelta, onChunk: nil)
}
func translate(
_ request: TranslationSessionRequest,
onDelta: (@Sendable (String) async -> Void)?,
onChunk: (@Sendable (Int, Int) async -> Void)?
) async throws -> PipelineResult {
requests.append(request)
await onChunk?(1, 3)
await onDelta?("part1 ")
await withCheckedContinuation { releaseContinuation = $0 }
await onChunk?(2, 3)
await onDelta?("part2 ")
await onChunk?(3, 3)
await onDelta?("part3")
return PipelineResult(
translation: "part1 part2 part3",
warnings: [],
sourceLanguage: "en",
timing: TranslationTiming(firstTokenMs: 1, totalMs: 2, promptTokens: 3, outputTokens: 4),
chunkCount: 3
)
}
func release() async {
while releaseContinuation == nil {
await Task.yield()
}
releaseContinuation?.resume()
releaseContinuation = nil
}
}