M2: TranslationAdapter/AdapterRegistry with documented Hy-MT3 prompts and publisher sampling; all 102 languages with BCP-47 variants, RTL and dense-script flags; normalisation, NLLanguage detection, content classification and exact-join script-aware chunking; protected-token scanning and output validation; prompt policy with one documented fallback; shared TranslationPipeline with Core error mapping. The CLI now reports sourceLanguage and warning codes. M3: pure DoubleCopyDetector, 120 ms PasteboardWatcher with 750 ms own-write suppression, SystemPasteboard snapshot/restore and markers, CapturePolicy for the 4000/2000 limits, ServicesProvider and the menu-bar action. T3.5 (10-app manual matrix) remains intentionally open. Docs: ARCHITECTURE, TRANSLATION_OUTPUT, HYMT3_MODEL (§7 spike), SELECTION_AND_CLIPBOARD, PROGRESS; ADR-016 superseded by ADR-017/018. Verified: make verify green (format, strict lint, scripts, privacy, Debug+Release signatures, package tests); live 7B CLI en→de/de→en/Markdown/JSON/placeholders; empty input exits 2 and unreachable exits 3.
125 lines
4.3 KiB
Swift
125 lines
4.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 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)
|
|
}
|
|
}
|