feat(m2-m3): adapter, text pipeline, double-copy capture and Services (T2.1-T2.7, T3.1-T3.4)
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.
This commit is contained in:
1 parent
26b493e7e5
commit
64078e2f04
80 files changed
+4090
-325
No files matched your search
+78
@@ -0,0 +1,78 @@
|
||||
import Foundation
|
||||
import SprachmittlerCapture
|
||||
import SprachmittlerTestSupport
|
||||
import Testing
|
||||
|
||||
@Suite("Clipboard snapshot")
|
||||
@MainActor
|
||||
struct ClipboardSnapshotTests {
|
||||
@Test func snapshotCapturesEveryItemAndType() throws {
|
||||
let text = Data("Hello".utf8)
|
||||
let rich = Data("rich".utf8)
|
||||
let image = Data([0x01, 0x02, 0x03])
|
||||
let pasteboard = FakePasteboard(
|
||||
changeCount: 3,
|
||||
text: "Hello",
|
||||
items: [
|
||||
ClipboardItemData(data: ["public.utf8-plain-text": text, "public.rtf": rich]),
|
||||
ClipboardItemData(data: ["public.png": image]),
|
||||
]
|
||||
)
|
||||
|
||||
let snapshot = try #require(pasteboard.snapshot())
|
||||
#expect(snapshot.changeCount == 3)
|
||||
#expect(snapshot.items.count == 2)
|
||||
#expect(snapshot.items[0].data["public.utf8-plain-text"] == text)
|
||||
#expect(snapshot.items[0].data["public.rtf"] == rich)
|
||||
#expect(snapshot.items[1].data["public.png"] == image)
|
||||
}
|
||||
|
||||
@Test func restoreOnlyWhenChangeCountMatches() throws {
|
||||
let text = Data("Hello".utf8)
|
||||
let pasteboard = FakePasteboard(
|
||||
changeCount: 3,
|
||||
text: "Hello",
|
||||
items: [ClipboardItemData(data: ["public.utf8-plain-text": text])]
|
||||
)
|
||||
let snapshot = try #require(pasteboard.snapshot())
|
||||
#expect(pasteboard.restore(snapshot, onlyIfChangeCountMatches: 3))
|
||||
#expect(pasteboard.text == "Hello")
|
||||
#expect(!pasteboard.hasOwnWriteMarkers)
|
||||
}
|
||||
|
||||
@Test func anotherAppChangingTheClipboardPreventsRestore() throws {
|
||||
let text = Data("Hello".utf8)
|
||||
let pasteboard = FakePasteboard(
|
||||
changeCount: 3,
|
||||
text: "Hello",
|
||||
items: [ClipboardItemData(data: ["public.utf8-plain-text": text])]
|
||||
)
|
||||
let snapshot = try #require(pasteboard.snapshot())
|
||||
|
||||
pasteboard.simulateCopy("Other")
|
||||
#expect(!pasteboard.restore(snapshot, onlyIfChangeCountMatches: 3))
|
||||
#expect(pasteboard.text == "Other")
|
||||
}
|
||||
|
||||
@Test func restoreFailureIsReported() throws {
|
||||
let pasteboard = FakePasteboard(changeCount: 1, text: "Hello")
|
||||
let snapshot = try #require(pasteboard.snapshot())
|
||||
pasteboard.restoreSucceeds = false
|
||||
#expect(!pasteboard.restore(snapshot, onlyIfChangeCountMatches: 1))
|
||||
}
|
||||
|
||||
@Test func snapshotFailureReturnsNil() {
|
||||
let pasteboard = FakePasteboard()
|
||||
pasteboard.snapshotSucceeds = false
|
||||
#expect(pasteboard.snapshot() == nil)
|
||||
}
|
||||
|
||||
@Test func writeAddsOwnAndTransientMarkers() {
|
||||
let pasteboard = FakePasteboard()
|
||||
let newCount = pasteboard.write("Hallo")
|
||||
#expect(newCount == 1)
|
||||
#expect(pasteboard.text == "Hallo")
|
||||
#expect(pasteboard.hasOwnWriteMarkers)
|
||||
#expect(pasteboard.writtenTexts == ["Hallo"])
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable import SprachmittlerCapture
|
||||
|
||||
@Suite("Double-copy detector")
|
||||
struct DoubleCopyDetectorTests {
|
||||
private let start = Date(timeIntervalSince1970: 1000)
|
||||
|
||||
@Test func twoIdenticalCopiesWithinTheWindowTrigger() {
|
||||
var detector = DoubleCopyDetector()
|
||||
#expect(detector.record(changeCount: 1, text: "Hello", at: start) == .none)
|
||||
#expect(detector.record(changeCount: 2, text: "Hello", at: start.addingTimeInterval(0.3))
|
||||
== .captured("Hello"))
|
||||
}
|
||||
|
||||
@Test func slowCopiesDoNotTrigger() {
|
||||
var detector = DoubleCopyDetector()
|
||||
#expect(detector.record(changeCount: 1, text: "Hello", at: start) == .none)
|
||||
#expect(detector.record(changeCount: 2, text: "Hello", at: start.addingTimeInterval(0.6)) == .none)
|
||||
#expect(detector.record(changeCount: 3, text: "Hello", at: start.addingTimeInterval(0.7))
|
||||
== .captured("Hello"))
|
||||
}
|
||||
|
||||
@Test func differentTextDoesNotTrigger() {
|
||||
var detector = DoubleCopyDetector()
|
||||
#expect(detector.record(changeCount: 1, text: "Hello", at: start) == .none)
|
||||
#expect(detector.record(changeCount: 2, text: "World", at: start.addingTimeInterval(0.1)) == .none)
|
||||
#expect(detector.record(changeCount: 3, text: "World", at: start.addingTimeInterval(0.2))
|
||||
== .captured("World"))
|
||||
}
|
||||
|
||||
@Test func emptyOrWhitespaceTextResets() {
|
||||
var detector = DoubleCopyDetector()
|
||||
#expect(detector.record(changeCount: 1, text: "Hello", at: start) == .none)
|
||||
#expect(detector.record(changeCount: 2, text: " ", at: start.addingTimeInterval(0.1)) == .empty)
|
||||
#expect(detector.record(changeCount: 3, text: "Hello", at: start.addingTimeInterval(0.2)) == .none)
|
||||
}
|
||||
|
||||
@Test func duplicateChangeCountIsIgnored() {
|
||||
var detector = DoubleCopyDetector()
|
||||
#expect(detector.record(changeCount: 1, text: "Hello", at: start) == .none)
|
||||
#expect(detector.record(changeCount: 1, text: "Hello", at: start.addingTimeInterval(0.1)) == .none)
|
||||
}
|
||||
|
||||
@Test func comparisonTrimsAndNormalisesLineEndingsOnly() {
|
||||
var detector = DoubleCopyDetector()
|
||||
#expect(detector.record(changeCount: 1, text: " Hello\r\nworld ", at: start) == .none)
|
||||
#expect(detector.record(changeCount: 2, text: "Hello\nworld", at: start.addingTimeInterval(0.1))
|
||||
== .captured("Hello\nworld"))
|
||||
}
|
||||
|
||||
@Test func internalWhitespaceIsSignificant() {
|
||||
var detector = DoubleCopyDetector()
|
||||
#expect(detector.record(changeCount: 1, text: "a b", at: start) == .none)
|
||||
#expect(detector.record(changeCount: 2, text: "a b", at: start.addingTimeInterval(0.1)) == .none)
|
||||
}
|
||||
|
||||
@Test func resetForgetsTheRememberedCopy() {
|
||||
var detector = DoubleCopyDetector()
|
||||
#expect(detector.record(changeCount: 1, text: "Hello", at: start) == .none)
|
||||
detector.reset()
|
||||
#expect(detector.record(changeCount: 2, text: "Hello", at: start.addingTimeInterval(0.1)) == .none)
|
||||
}
|
||||
|
||||
@Test func expireClearsACandidateAfterTheWindow() {
|
||||
var detector = DoubleCopyDetector()
|
||||
#expect(detector.record(changeCount: 1, text: "Hello", at: start) == .none)
|
||||
detector.expire(at: start.addingTimeInterval(0.6))
|
||||
#expect(detector.record(changeCount: 2, text: "Hello", at: start.addingTimeInterval(0.7)) == .none)
|
||||
}
|
||||
}
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import AppKit
|
||||
import Foundation
|
||||
import SprachmittlerCore
|
||||
import SprachmittlerTestSupport
|
||||
import Testing
|
||||
@testable import SprachmittlerCapture
|
||||
|
||||
@Suite("Services provider")
|
||||
@MainActor
|
||||
struct ServicesProviderTests {
|
||||
@Test func deliversTheSelectedTextWithoutAccessibility() throws {
|
||||
let pasteboard = NSPasteboard(name: NSPasteboard.Name("SprachmittlerTests-\(UUID().uuidString)"))
|
||||
pasteboard.clearContents()
|
||||
pasteboard.setString("Hello from Services", forType: .string)
|
||||
|
||||
let box = CapturedBox()
|
||||
let provider = ServicesProvider { box.record($0) }
|
||||
var error: NSString = ""
|
||||
provider.translateSelection(pasteboard, userData: "", error: &error)
|
||||
|
||||
let captured = try #require(box.captured)
|
||||
#expect(captured.text == "Hello from Services")
|
||||
#expect(captured.source == .services)
|
||||
}
|
||||
|
||||
@Test func emptySelectionReturnsAnError() {
|
||||
let pasteboard = NSPasteboard(name: NSPasteboard.Name("SprachmittlerTests-\(UUID().uuidString)"))
|
||||
pasteboard.clearContents()
|
||||
|
||||
let box = CapturedBox()
|
||||
let provider = ServicesProvider { box.record($0) }
|
||||
var error: NSString = ""
|
||||
provider.translateSelection(pasteboard, userData: "", error: &error)
|
||||
|
||||
#expect(error.length > 0)
|
||||
#expect(box.captured == nil)
|
||||
}
|
||||
|
||||
@Test func ownWriteMarkersAreIgnored() {
|
||||
let pasteboard = NSPasteboard(name: NSPasteboard.Name("SprachmittlerTests-\(UUID().uuidString)"))
|
||||
pasteboard.clearContents()
|
||||
pasteboard.setString("Hallo", forType: .string)
|
||||
pasteboard.setData(
|
||||
Data(),
|
||||
forType: NSPasteboard.PasteboardType("com.schihei.sprachmittler.own")
|
||||
)
|
||||
|
||||
let box = CapturedBox()
|
||||
let provider = ServicesProvider { box.record($0) }
|
||||
var error: NSString = ""
|
||||
provider.translateSelection(pasteboard, userData: "", error: &error)
|
||||
|
||||
#expect(error.length > 0)
|
||||
#expect(box.captured == nil)
|
||||
}
|
||||
}
|
||||
|
||||
/// @unchecked Sendable is safe because every field is guarded by `lock`.
|
||||
private final class CapturedBox: @unchecked Sendable {
|
||||
private let lock = NSLock()
|
||||
private var value: CapturedText?
|
||||
|
||||
var captured: CapturedText? {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
return value
|
||||
}
|
||||
|
||||
func record(_ captured: CapturedText) {
|
||||
lock.lock()
|
||||
value = captured
|
||||
lock.unlock()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import AppKit
|
||||
import Foundation
|
||||
import SprachmittlerCapture
|
||||
import Testing
|
||||
|
||||
@Suite("System pasteboard adapter")
|
||||
@MainActor
|
||||
struct SystemPasteboardTests {
|
||||
private func makePasteboard() -> (NSPasteboard, SystemPasteboard) {
|
||||
let pasteboard = NSPasteboard(name: NSPasteboard.Name("SprachmittlerTests-\(UUID().uuidString)"))
|
||||
pasteboard.clearContents()
|
||||
return (pasteboard, SystemPasteboard(pasteboard: pasteboard))
|
||||
}
|
||||
|
||||
@Test func writeAddsTextAndMarkers() {
|
||||
let (pasteboard, adapter) = makePasteboard()
|
||||
let before = adapter.changeCount
|
||||
|
||||
let after = adapter.write("Hallo")
|
||||
|
||||
#expect(after > before)
|
||||
#expect(pasteboard.string(forType: .string) == "Hallo")
|
||||
#expect(adapter.containsOwnWriteMarkers())
|
||||
}
|
||||
|
||||
@Test func snapshotAndRestoreRoundTripMultipleTypes() throws {
|
||||
let (pasteboard, adapter) = makePasteboard()
|
||||
let rtf = Data("rich".utf8)
|
||||
pasteboard.setString("Hello", forType: .string)
|
||||
pasteboard.setData(rtf, forType: .rtf)
|
||||
|
||||
let snapshot = try #require(adapter.snapshot())
|
||||
_ = adapter.write("Translation")
|
||||
|
||||
#expect(adapter.restore(snapshot, onlyIfChangeCountMatches: adapter.changeCount))
|
||||
#expect(pasteboard.string(forType: .string) == "Hello")
|
||||
#expect(pasteboard.data(forType: .rtf) == rtf)
|
||||
}
|
||||
|
||||
@Test func restoreIsRefusedAfterAnotherChange() throws {
|
||||
let (pasteboard, adapter) = makePasteboard()
|
||||
pasteboard.setString("Hello", forType: .string)
|
||||
let snapshot = try #require(adapter.snapshot())
|
||||
|
||||
pasteboard.clearContents()
|
||||
pasteboard.setString("Other", forType: .string)
|
||||
|
||||
#expect(!adapter.restore(snapshot, onlyIfChangeCountMatches: snapshot.changeCount))
|
||||
#expect(pasteboard.string(forType: .string) == "Other")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user