|
| 1 | +import Foundation |
| 2 | +import Testing |
| 3 | +@testable import Kaset |
| 4 | + |
| 5 | +// MARK: - AIErrorTests |
| 6 | + |
| 7 | +/// Tests for AIError enum and its properties. |
| 8 | +@available(macOS 26.0, *) |
| 9 | +@Suite("AIError", .tags(.api)) |
| 10 | +struct AIErrorTests { |
| 11 | + // MARK: - Error Description Tests |
| 12 | + |
| 13 | + @Test("contextWindowExceeded has correct description") |
| 14 | + func contextWindowExceededDescription() { |
| 15 | + let error = AIError.contextWindowExceeded |
| 16 | + #expect(error.errorDescription?.contains("too complex") == true) |
| 17 | + } |
| 18 | + |
| 19 | + @Test("contentBlocked has correct description") |
| 20 | + func contentBlockedDescription() { |
| 21 | + let error = AIError.contentBlocked |
| 22 | + #expect(error.errorDescription?.contains("can't help") == true) |
| 23 | + } |
| 24 | + |
| 25 | + @Test("cancelled has correct description") |
| 26 | + func cancelledDescription() { |
| 27 | + let error = AIError.cancelled |
| 28 | + #expect(error.errorDescription?.contains("cancelled") == true) |
| 29 | + } |
| 30 | + |
| 31 | + @Test("notAvailable has correct description with reason") |
| 32 | + func notAvailableDescription() { |
| 33 | + let error = AIError.notAvailable(reason: "Device not supported") |
| 34 | + #expect(error.errorDescription?.contains("unavailable") == true) |
| 35 | + #expect(error.errorDescription?.contains("Device not supported") == true) |
| 36 | + } |
| 37 | + |
| 38 | + @Test("modelNotReady has correct description") |
| 39 | + func modelNotReadyDescription() { |
| 40 | + let error = AIError.modelNotReady |
| 41 | + #expect(error.errorDescription?.contains("loading") == true) |
| 42 | + } |
| 43 | + |
| 44 | + @Test("sessionBusy has correct description") |
| 45 | + func sessionBusyDescription() { |
| 46 | + let error = AIError.sessionBusy |
| 47 | + #expect(error.errorDescription?.contains("wait") == true) |
| 48 | + } |
| 49 | + |
| 50 | + @Test("unknown has correct description with underlying error") |
| 51 | + func unknownDescription() { |
| 52 | + let underlying = NSError(domain: "test", code: 42, userInfo: [ |
| 53 | + NSLocalizedDescriptionKey: "Something failed", |
| 54 | + ]) |
| 55 | + let error = AIError.unknown(underlying: underlying) |
| 56 | + #expect(error.errorDescription?.contains("Something failed") == true) |
| 57 | + } |
| 58 | + |
| 59 | + // MARK: - Recovery Suggestion Tests |
| 60 | + |
| 61 | + @Test("contextWindowExceeded has recovery suggestion") |
| 62 | + func contextWindowExceededRecovery() { |
| 63 | + let error = AIError.contextWindowExceeded |
| 64 | + #expect(error.recoverySuggestion != nil) |
| 65 | + #expect(error.recoverySuggestion?.contains("shorter") == true) |
| 66 | + } |
| 67 | + |
| 68 | + @Test("contentBlocked has recovery suggestion") |
| 69 | + func contentBlockedRecovery() { |
| 70 | + let error = AIError.contentBlocked |
| 71 | + #expect(error.recoverySuggestion != nil) |
| 72 | + #expect(error.recoverySuggestion?.contains("rephras") == true) |
| 73 | + } |
| 74 | + |
| 75 | + @Test("cancelled has no recovery suggestion") |
| 76 | + func cancelledNoRecovery() { |
| 77 | + let error = AIError.cancelled |
| 78 | + #expect(error.recoverySuggestion == nil) |
| 79 | + } |
| 80 | + |
| 81 | + @Test("notAvailable has recovery suggestion") |
| 82 | + func notAvailableRecovery() { |
| 83 | + let error = AIError.notAvailable(reason: "Not enabled") |
| 84 | + #expect(error.recoverySuggestion != nil) |
| 85 | + #expect(error.recoverySuggestion?.contains("System Settings") == true) |
| 86 | + } |
| 87 | + |
| 88 | + @Test("modelNotReady has recovery suggestion") |
| 89 | + func modelNotReadyRecovery() { |
| 90 | + let error = AIError.modelNotReady |
| 91 | + #expect(error.recoverySuggestion != nil) |
| 92 | + #expect(error.recoverySuggestion?.contains("downloading") == true) |
| 93 | + } |
| 94 | + |
| 95 | + @Test("sessionBusy has no recovery suggestion") |
| 96 | + func sessionBusyNoRecovery() { |
| 97 | + let error = AIError.sessionBusy |
| 98 | + #expect(error.recoverySuggestion == nil) |
| 99 | + } |
| 100 | + |
| 101 | + @Test("unknown has recovery suggestion") |
| 102 | + func unknownRecovery() { |
| 103 | + let error = AIError.unknown(underlying: NSError(domain: "test", code: 1)) |
| 104 | + #expect(error.recoverySuggestion != nil) |
| 105 | + #expect(error.recoverySuggestion?.contains("restart") == true) |
| 106 | + } |
| 107 | + |
| 108 | + // MARK: - shouldDisplay Tests |
| 109 | + |
| 110 | + @Test("contextWindowExceeded should display") |
| 111 | + func contextWindowExceededShouldDisplay() { |
| 112 | + let error = AIError.contextWindowExceeded |
| 113 | + #expect(error.shouldDisplay == true) |
| 114 | + } |
| 115 | + |
| 116 | + @Test("contentBlocked should display") |
| 117 | + func contentBlockedShouldDisplay() { |
| 118 | + let error = AIError.contentBlocked |
| 119 | + #expect(error.shouldDisplay == true) |
| 120 | + } |
| 121 | + |
| 122 | + @Test("cancelled should NOT display") |
| 123 | + func cancelledShouldNotDisplay() { |
| 124 | + let error = AIError.cancelled |
| 125 | + #expect(error.shouldDisplay == false) |
| 126 | + } |
| 127 | + |
| 128 | + @Test("notAvailable should display") |
| 129 | + func notAvailableShouldDisplay() { |
| 130 | + let error = AIError.notAvailable(reason: "test") |
| 131 | + #expect(error.shouldDisplay == true) |
| 132 | + } |
| 133 | + |
| 134 | + @Test("modelNotReady should display") |
| 135 | + func modelNotReadyShouldDisplay() { |
| 136 | + let error = AIError.modelNotReady |
| 137 | + #expect(error.shouldDisplay == true) |
| 138 | + } |
| 139 | + |
| 140 | + @Test("sessionBusy should display") |
| 141 | + func sessionBusyShouldDisplay() { |
| 142 | + let error = AIError.sessionBusy |
| 143 | + #expect(error.shouldDisplay == true) |
| 144 | + } |
| 145 | + |
| 146 | + @Test("unknown should display") |
| 147 | + func unknownShouldDisplay() { |
| 148 | + let error = AIError.unknown(underlying: NSError(domain: "test", code: 1)) |
| 149 | + #expect(error.shouldDisplay == true) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +// MARK: - AIErrorHandlerTests |
| 154 | + |
| 155 | +/// Tests for AIErrorHandler utility methods. |
| 156 | +@available(macOS 26.0, *) |
| 157 | +@Suite("AIErrorHandler", .tags(.api)) |
| 158 | +struct AIErrorHandlerTests { |
| 159 | + // MARK: - handle() Tests |
| 160 | + |
| 161 | + @Test("Handle CancellationError returns cancelled") |
| 162 | + func handleCancellationError() { |
| 163 | + let error = CancellationError() |
| 164 | + let result = AIErrorHandler.handle(error) |
| 165 | + |
| 166 | + if case .cancelled = result { |
| 167 | + // Success |
| 168 | + } else { |
| 169 | + Issue.record("Expected .cancelled, got \(result)") |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @Test("Handle unknown error wraps it") |
| 174 | + func handleUnknownError() { |
| 175 | + let underlying = NSError(domain: "test", code: 123) |
| 176 | + let result = AIErrorHandler.handle(underlying) |
| 177 | + |
| 178 | + if case let .unknown(wrapped) = result { |
| 179 | + #expect((wrapped as NSError).code == 123) |
| 180 | + } else { |
| 181 | + Issue.record("Expected .unknown, got \(result)") |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + // MARK: - userMessage() Tests |
| 186 | + |
| 187 | + @Test("userMessage includes description") |
| 188 | + func userMessageIncludesDescription() { |
| 189 | + let error = AIError.contextWindowExceeded |
| 190 | + let message = AIErrorHandler.userMessage(for: error) |
| 191 | + |
| 192 | + #expect(message.contains("too complex")) |
| 193 | + } |
| 194 | + |
| 195 | + @Test("userMessage includes recovery suggestion when available") |
| 196 | + func userMessageIncludesRecovery() { |
| 197 | + let error = AIError.contentBlocked |
| 198 | + let message = AIErrorHandler.userMessage(for: error) |
| 199 | + |
| 200 | + #expect(message.contains("rephras")) |
| 201 | + } |
| 202 | + |
| 203 | + @Test("userMessage works for cancelled (no recovery)") |
| 204 | + func userMessageForCancelled() { |
| 205 | + let error = AIError.cancelled |
| 206 | + let message = AIErrorHandler.userMessage(for: error) |
| 207 | + |
| 208 | + #expect(message.contains("cancelled")) |
| 209 | + } |
| 210 | + |
| 211 | + // MARK: - handleAndMessage() Tests |
| 212 | + |
| 213 | + @Test("handleAndMessage returns nil for cancelled") |
| 214 | + func handleAndMessageCancelledReturnsNil() { |
| 215 | + let error = CancellationError() |
| 216 | + let message = AIErrorHandler.handleAndMessage(error, context: "test") |
| 217 | + |
| 218 | + #expect(message == nil) |
| 219 | + } |
| 220 | + |
| 221 | + @Test("handleAndMessage returns message for displayable errors") |
| 222 | + func handleAndMessageReturnsMessage() { |
| 223 | + let error = NSError(domain: "test", code: 1, userInfo: [ |
| 224 | + NSLocalizedDescriptionKey: "Test error", |
| 225 | + ]) |
| 226 | + let message = AIErrorHandler.handleAndMessage(error, context: "test operation") |
| 227 | + |
| 228 | + #expect(message != nil) |
| 229 | + #expect(message?.contains("Test error") == true) |
| 230 | + } |
| 231 | +} |
0 commit comments