ChatCompletionsTests+ErrorHandling.swift 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // This source file is part of the Foundation Models open source project.
  4. //
  5. // Copyright © 2024-2027 Apple Inc. and the Foundation Models project authors.
  6. //
  7. // Licensed under the Apache License v2.0
  8. //
  9. // See LICENSE.txt for license information
  10. //
  11. //===----------------------------------------------------------------------===//
  12. import Foundation
  13. import FoundationModels
  14. @testable import FoundationModelsUtilities
  15. import Testing
  16. extension ChatCompletionsTests {
  17. @Suite struct ErrorHandling {
  18. init() { MockSSEProtocol.reset() }
  19. @Test func `throws on HTTP error`() async throws {
  20. MockSSEProtocol.handler = { _ in
  21. (429, Data("Rate limited".utf8))
  22. }
  23. let session = LanguageModelSession(model: makeMockModel())
  24. await #expect(throws: (any Error).self) {
  25. try await session.respond(to: "test")
  26. }
  27. }
  28. @Test func `throws on API error embedded in SSE stream`() async throws {
  29. MockSSEProtocol.handler = { _ in
  30. (200, MockSSE.apiError(message: "Rate limit exceeded"))
  31. }
  32. let session = LanguageModelSession(model: makeMockModel())
  33. await #expect(throws: (any Error).self) {
  34. try await session.respond(to: "test")
  35. }
  36. }
  37. }
  38. }