import Foundation import SprachmittlerOllama import Testing @testable import SprachmittlerCore @Suite("User-facing errors") struct ErrorMessageTests { @Test func everyErrorHasAMessageAndARecoveryAction() throws { let url = try #require(URL(string: "http://127.0.0.1:11434")) let errors: [SprachmittlerError] = [ .noSelection, .emptyText, .textTooLong(characters: 5000, limit: 4000), .pasteboardUnavailable, .ollamaUnreachable(url: url), .ollamaTooOld(found: "0.1", required: "0.40"), .endpointRejected(reason: .remoteNotAllowed), .modelNotInstalled(model: "m"), .noAdapter(model: "m"), .badRequest(message: "raw body"), .server(status: 500, message: "raw body"), .timeout(phase: .firstToken), .emptyOutput, .truncated, .repetitionLoop, .preambleDetected, .protectedTokenLost(token: "https://example.com"), .accessibilityNotTrusted, .accessibilityUnsupported(app: "App"), .insertionTargetChanged, .pasteboardRestoreFailed, .cancelled, ] for error in errors { #expect(!error.userMessage.isEmpty) #expect(!error.userMessage.contains("raw body")) _ = error.recoveryAction } #expect(SprachmittlerError.ollamaUnreachable(url: url).recoveryAction == .startOllama) #expect(SprachmittlerError.accessibilityNotTrusted.recoveryAction == .openSettings) } } @Suite("Diagnostics store") struct DiagnosticsStoreTests { @Test func keepsTheLastFiftyEventsAndClears() async { let store = DiagnosticsStore(clock: { Date(timeIntervalSince1970: 0) }) for index in 0 ..< 60 { await store.record(kind: "event-\(index)", durationMs: index, detail: "code") } let events = await store.snapshot() #expect(events.count == DiagnosticsStore.capacity) #expect(events.first?.kind == "event-10") #expect(events.last?.kind == "event-59") #expect(events.allSatisfy { $0.detail == "code" }) await store.clear() let cleared = await store.snapshot() #expect(cleared.isEmpty) } }