Files
Sprachmittler/Packages/SprachmittlerKit/Tests/SprachmittlerCaptureTests/PasteboardWatcherTests.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

150 lines
5.3 KiB
Swift

import Foundation
import SprachmittlerCore
import SprachmittlerTestSupport
import Testing
@testable import SprachmittlerCapture
@Suite("Pasteboard watcher")
@MainActor
struct PasteboardWatcherTests {
private let start = Date(timeIntervalSince1970: 2000)
@Test func readsOnlyAfterAChange() {
let pasteboard = FakePasteboard(text: "Hello")
let watcher = PasteboardWatcher(pasteboard: pasteboard)
#expect(watcher.poll(at: start) == nil)
#expect(pasteboard.stringReadCount == 0)
}
@Test func twoIdenticalCopiesAreCaptured() {
let pasteboard = FakePasteboard()
let watcher = PasteboardWatcher(pasteboard: pasteboard)
pasteboard.simulateCopy("Hello")
#expect(watcher.poll(at: start) == nil)
pasteboard.simulateCopy("Hello")
let event = watcher.poll(at: start.addingTimeInterval(0.1))
guard case let .captured(captured) = event else {
Issue.record("expected a capture")
return
}
#expect(captured.text == "Hello")
#expect(captured.source == .doubleCopy)
#expect(captured.capturedAt == start.addingTimeInterval(0.1))
}
@Test func slowCopiesDoNotTrigger() {
let pasteboard = FakePasteboard()
let watcher = PasteboardWatcher(pasteboard: pasteboard)
pasteboard.simulateCopy("Hello")
#expect(watcher.poll(at: start) == nil)
pasteboard.simulateCopy("Hello")
#expect(watcher.poll(at: start.addingTimeInterval(0.6)) == nil)
}
@Test func tooLongTextUsesTheScriptAwareLimit() {
let latin = FakePasteboard()
let latinWatcher = PasteboardWatcher(pasteboard: latin)
latin.simulateCopy(String(repeating: "a", count: 4001))
_ = latinWatcher.poll(at: start)
latin.simulateCopy(String(repeating: "a", count: 4001))
#expect(
latinWatcher.poll(at: start.addingTimeInterval(0.1))
== .textTooLong(characters: 4001, limit: 4000)
)
let dense = FakePasteboard()
let denseWatcher = PasteboardWatcher(pasteboard: dense)
let cjk = String(repeating: "你", count: 2001)
dense.simulateCopy(cjk)
_ = denseWatcher.poll(at: start)
dense.simulateCopy(cjk)
#expect(
denseWatcher.poll(at: start.addingTimeInterval(0.1))
== .textTooLong(characters: 2001, limit: 2000)
)
}
@Test func configuredPartsAcceptLongerText() {
let pasteboard = FakePasteboard()
let watcher = PasteboardWatcher(pasteboard: pasteboard, limits: CaptureLimits(maximumParts: 3))
let accepted = String(repeating: "a", count: 12000)
pasteboard.simulateCopy(accepted)
_ = watcher.poll(at: start)
pasteboard.simulateCopy(accepted)
guard case let .captured(captured) = watcher.poll(at: start.addingTimeInterval(0.1)) else {
Issue.record("expected a capture")
return
}
#expect(captured.text.count == 12000)
let tooLong = String(repeating: "a", count: 12001)
watcher.clear()
pasteboard.simulateCopy(tooLong)
_ = watcher.poll(at: start.addingTimeInterval(0.2))
pasteboard.simulateCopy(tooLong)
#expect(
watcher.poll(at: start.addingTimeInterval(0.3))
== .textTooLong(characters: 12001, limit: 12000)
)
}
@Test func ownWritesAreSuppressedFor750Milliseconds() {
let pasteboard = FakePasteboard()
let watcher = PasteboardWatcher(pasteboard: pasteboard)
pasteboard.simulateOwnMarkerCopy("Hallo")
#expect(watcher.poll(at: start) == .ignoredOwnWrite)
pasteboard.simulateCopy("Hello")
#expect(watcher.poll(at: start.addingTimeInterval(0.1)) == .ignoredOwnWrite)
pasteboard.simulateCopy("Hello")
#expect(watcher.poll(at: start.addingTimeInterval(0.8)) == nil)
pasteboard.simulateCopy("Hello")
let event = watcher.poll(at: start.addingTimeInterval(0.9))
#expect(event != nil)
}
@Test func noteOwnWriteSuppressesTheNextChange() {
let pasteboard = FakePasteboard()
let watcher = PasteboardWatcher(pasteboard: pasteboard)
watcher.noteOwnWrite(at: start)
pasteboard.simulateCopy("Hallo")
#expect(watcher.poll(at: start.addingTimeInterval(0.1)) == .ignoredOwnWrite)
}
@Test func clearForgetsTheFirstCopy() {
let pasteboard = FakePasteboard()
let watcher = PasteboardWatcher(pasteboard: pasteboard)
pasteboard.simulateCopy("Hello")
#expect(watcher.poll(at: start) == nil)
watcher.clear()
pasteboard.simulateCopy("Hello")
#expect(watcher.poll(at: start.addingTimeInterval(0.1)) == nil)
}
@Test func startAndStopDoNotLeakThePollTask() async {
let pasteboard = FakePasteboard()
let ticker = ManualPasteboardTicker()
let watcher = PasteboardWatcher(pasteboard: pasteboard, ticker: ticker)
watcher.start { _ in }
#expect(watcher.isRunning)
for _ in 0 ..< 1000 where !ticker.isStarted {
await Task.yield()
}
watcher.stop()
#expect(!watcher.isRunning)
for _ in 0 ..< 1000 where !ticker.isTerminated {
await Task.yield()
}
#expect(ticker.isTerminated)
}
}