Files
schihei a78572adc3 feat(m4-m6): session, panel, Replace, settings and onboarding
M4: TranslationSession actor with epochs/one-in-flight streaming, PanelViewModel, non-activating NSPanel, streaming panel UI, searchable/recent target languages, warnings/errors and Copy/Retry/Replace actions.

M5: InsertionCoordinator with target capture, AX direct replacement, secure-field/own-PID refusal, frontmost and selected-text verification, snapshot/⌘V/restore fallback, cancellation-safe restore and blocking-warning gate.

M6: versioned AppSettings/SettingsStore, onboarding for Ollama/model/target/Accessibility, General/Model/Translation/Privacy/Diagnostics settings, launch-at-login via SMAppService, status menu (target, copy/replace last, setup, diagnostics, about), text-free diagnostics and first-run setup.

Docs: PROGRESS M4–M6, ARCHITECTURE §10–§12, ADR-019/020, SELECTION_AND_CLIPBOARD and TESTING. Manual 10-app matrices, real AX/paste delivery, VoiceOver and focus checks remain open.

Verified: make verify green (format, strict lint, scripts, privacy, Debug+Release signatures, 217 package tests); app launches and a two-copy clipboard smoke test opens the panel.
2026-11-26 14:03:16 +01:00

298 lines
9.5 KiB
Swift

import Darwin
import Foundation
import SprachmittlerCore
import SprachmittlerTestSupport
import Testing
@testable import SprachmittlerCapture
@Suite("Insertion coordinator")
@MainActor
struct InsertionCoordinatorTests {
private let source = CapturedText(
text: "Hello.",
capturedAt: Date(timeIntervalSince1970: 0),
source: .doubleCopy
)
private let target = InsertionTarget(
pid: 1234,
bundleID: "com.example.app",
selectedText: nil,
capturedAt: Date(timeIntervalSince1970: 0)
)
@Test func accessibilityDirectReplacementDoesNotTouchTheClipboard() async throws {
let pasteboard = makePasteboard()
let accessibility = FakeAccessibilityEditor(
trusted: true,
selected: "Hello.",
secure: false,
setResult: true
)
let poster = FakeEventPoster(result: true)
let coordinator = makeCoordinator(pasteboard: pasteboard, accessibility: accessibility, poster: poster)
let outcome = try await coordinator.replace(selection: source, with: "Hallo", target: target)
#expect(outcome == .replaced(using: .accessibility))
#expect(await accessibility.setCalls == ["Hallo"])
#expect(pasteboard.writtenTexts.isEmpty)
#expect(poster.pids.isEmpty)
}
@Test func unsupportedAccessibilityFallsBackToPasteAndRestoresTheClipboard() async throws {
let pasteboard = makePasteboard()
let accessibility = FakeAccessibilityEditor(
trusted: true,
selected: nil,
secure: false,
setResult: false
)
let poster = FakeEventPoster(result: true)
let coordinator = makeCoordinator(pasteboard: pasteboard, accessibility: accessibility, poster: poster)
let outcome = try await coordinator.replace(selection: source, with: "Hallo", target: target)
#expect(outcome == .replaced(using: .paste))
#expect(pasteboard.writtenTexts == ["Hallo"])
#expect(pasteboard.text == "Original")
#expect(!pasteboard.hasOwnWriteMarkers)
#expect(poster.pids == [1234])
}
@Test func wrongFrontmostAppAbortsBeforeTouchingAnything() async {
let pasteboard = makePasteboard()
let accessibility = FakeAccessibilityEditor(
trusted: true,
selected: "Hello.",
secure: false,
setResult: true
)
let coordinator = makeCoordinator(
pasteboard: pasteboard,
accessibility: accessibility,
poster: FakeEventPoster(result: true),
frontmost: 999
)
await #expect(throws: SprachmittlerError.insertionTargetChanged) {
_ = try await coordinator.replace(selection: source, with: "Hallo", target: target)
}
#expect(pasteboard.writtenTexts.isEmpty)
}
@Test func changedSelectionAborts() async {
let accessibility = FakeAccessibilityEditor(
trusted: true,
selected: "Different.",
secure: false,
setResult: true
)
let coordinator = makeCoordinator(
pasteboard: makePasteboard(),
accessibility: accessibility,
poster: FakeEventPoster(result: true)
)
await #expect(throws: SprachmittlerError.insertionTargetChanged) {
_ = try await coordinator.replace(selection: source, with: "Hallo", target: target)
}
}
@Test func deniedAccessibilityAborts() async {
let accessibility = FakeAccessibilityEditor(
trusted: false,
selected: nil,
secure: false,
setResult: false
)
let coordinator = makeCoordinator(
pasteboard: makePasteboard(),
accessibility: accessibility,
poster: FakeEventPoster(result: true)
)
await #expect(throws: SprachmittlerError.accessibilityNotTrusted) {
_ = try await coordinator.replace(selection: source, with: "Hallo", target: target)
}
}
@Test func secureFieldIsRefused() async {
let accessibility = FakeAccessibilityEditor(
trusted: true,
selected: nil,
secure: true,
setResult: false
)
let coordinator = makeCoordinator(
pasteboard: makePasteboard(),
accessibility: accessibility,
poster: FakeEventPoster(result: true)
)
await #expect(throws: SprachmittlerError.accessibilityUnsupported(app: "com.example.app")) {
_ = try await coordinator.replace(selection: source, with: "Hallo", target: target)
}
}
@Test func restoreFailureIsReported() async {
let pasteboard = makePasteboard()
pasteboard.restoreSucceeds = false
let accessibility = FakeAccessibilityEditor(
trusted: true,
selected: nil,
secure: false,
setResult: false
)
let coordinator = makeCoordinator(
pasteboard: pasteboard,
accessibility: accessibility,
poster: FakeEventPoster(result: true)
)
await #expect(throws: SprachmittlerError.pasteboardRestoreFailed) {
_ = try await coordinator.replace(selection: source, with: "Hallo", target: target)
}
#expect(pasteboard.text == "Hallo")
}
@Test func cancelledSettleDelayStillRestoresTheClipboard() async {
let pasteboard = makePasteboard()
let accessibility = FakeAccessibilityEditor(
trusted: true,
selected: nil,
secure: false,
setResult: false
)
let coordinator = makeCoordinator(
pasteboard: pasteboard,
accessibility: accessibility,
poster: FakeEventPoster(result: true),
delay: { _ in throw CancellationError() }
)
await #expect(throws: CancellationError.self) {
_ = try await coordinator.replace(selection: source, with: "Hallo", target: target)
}
#expect(pasteboard.text == "Original")
#expect(!pasteboard.hasOwnWriteMarkers)
}
@Test func eventPostingFailureRestoresAndReportsUnsupported() async {
let pasteboard = makePasteboard()
let accessibility = FakeAccessibilityEditor(
trusted: true,
selected: nil,
secure: false,
setResult: false
)
let coordinator = makeCoordinator(
pasteboard: pasteboard,
accessibility: accessibility,
poster: FakeEventPoster(result: false)
)
await #expect(throws: SprachmittlerError.accessibilityUnsupported(app: "com.example.app")) {
_ = try await coordinator.replace(selection: source, with: "Hallo", target: target)
}
#expect(pasteboard.text == "Original")
}
@Test func refusesToInsertIntoItsOwnApp() async {
let own = InsertionTarget(
pid: getpid(),
bundleID: "de.schihei.sprachmittler",
selectedText: nil,
capturedAt: Date()
)
let coordinator = makeCoordinator(
pasteboard: makePasteboard(),
accessibility: FakeAccessibilityEditor(
trusted: true,
selected: nil,
secure: false,
setResult: false
),
poster: FakeEventPoster(result: true)
)
await #expect(throws: SprachmittlerError.insertionTargetChanged) {
_ = try await coordinator.replace(selection: source, with: "Hallo", target: own)
}
}
private func makePasteboard() -> FakePasteboard {
let original = Data("Original".utf8)
return FakePasteboard(
changeCount: 1,
text: "Original",
items: [ClipboardItemData(data: ["public.utf8-plain-text": original])]
)
}
private func makeCoordinator(
pasteboard: FakePasteboard,
accessibility: FakeAccessibilityEditor,
poster: FakeEventPoster,
frontmost: pid_t = 1234,
delay: @escaping @Sendable (Duration) async throws -> Void = { _ in }
) -> InsertionCoordinator {
InsertionCoordinator(
pasteboard: pasteboard,
accessibility: accessibility,
poster: poster,
delay: delay,
ownWrite: {},
frontmostPID: { frontmost }
)
}
}
private actor FakeAccessibilityEditor: AccessibilityTextEditing {
let trusted: Bool
let selected: String?
let secure: Bool
let setResult: Bool
private(set) var setCalls: [String] = []
init(trusted: Bool, selected: String?, secure: Bool, setResult: Bool) {
self.trusted = trusted
self.selected = selected
self.secure = secure
self.setResult = setResult
}
func isTrusted() async -> Bool {
trusted
}
func selectedText(pid: pid_t) async -> String? {
selected
}
func isSecureField(pid: pid_t) async -> Bool {
secure
}
func setSelectedText(pid: pid_t, to text: String) async -> Bool {
setCalls.append(text)
return setResult
}
}
private final class FakeEventPoster: EventPosting, @unchecked Sendable {
private let lock = NSLock()
private(set) var pids: [pid_t] = []
let result: Bool
init(result: Bool) {
self.result = result
}
func postPaste(to pid: pid_t) -> Bool {
lock.lock()
pids.append(pid)
lock.unlock()
return result
}
}