Files
firebase-ios-sdk/GeminiLanguageModel/Tests/GeminiLanguageModelTests/GeminiErrorMapperTests.swift
Andrew Heard ff6c2620f1 feat(ai): regenerate GeminiAPIDataModels with updated generator (#16881)
Regenerate GenerateContent/ with the GeminiLanguageModel/scripts/typegen
generator from Discovery Document revision 20260929, fixing the drift
reported by the check-data-models CI job.

Generated model changes:
- Structs use optional `var` properties, an empty `init()` and
  `Buildable` instead of memberwise initializers.
- Acronyms are capitalized in Swift names (e.g. `responseMIMEType`,
  `URLContext`). JSON schema properties are typed as `JSONObject`, and
  `Schema`, `DataType` and `VideoMetadata` are no longer generated.
- Enums combine both backends' cases and decode unknown values to
  `.unrecognized`.
- Every generated file has a "DO NOT EDIT" header.

GeminiLanguageModel changes:
- Build requests with `Buildable` closures and adopt the renamed
  properties. The encoded JSON is unchanged.
- Add `GeminiLanguageModel.Error.invalidResponse(InvalidResponse)`.
  Responses that fail to decode or contain a function call with no
  name now throw `invalidResponse` instead of `networkFailure`, and
  `InvalidResponse.underlyingError` exposes the original error.
- Map the `MODEL_ARMOR` and `JAILBREAK` block reasons and the
  `MODEL_ARMOR` finish reason to `guardrailViolation`.
- Exclude the GeminiLanguageModel test READMEs from the root SwiftPM
  manifest so builds with `-warnings-as-errors` succeed.
2026-10-13 18:44:02 -04:00

531 lines
20 KiB
Swift

// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#if canImport(FoundationModels) && compiler(>=6.4)
import Foundation
import FoundationModels
import GeminiAPIClient
import GeminiAPIDataModels
import GeminiTestUtilities
import Testing
@testable import GeminiLanguageModel
@Suite("GeminiErrorMapper Tests", .requireFoundationModels)
struct GeminiErrorMapperTests {
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func mapPassesThroughLanguageModelError() throws {
let originalError = LanguageModelError.timeout(
LanguageModelError.Timeout(debugDescription: "Operation timed out")
)
let mappedError = GeminiErrorMapper.map(originalError)
#expect(mappedError is LanguageModelError)
if case LanguageModelError.timeout(let timeout) = mappedError {
#expect(timeout.debugDescription == originalError.debugDescription)
} else {
Issue.record("Expected LanguageModelError.timeout")
}
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func mapPassesThroughCancellationError() {
let originalError = CancellationError()
let mappedError = GeminiErrorMapper.map(originalError)
#expect(mappedError is CancellationError)
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func mapPassesThroughGeminiLanguageModelError() throws {
let originalError = GeminiLanguageModel.Error.serviceUnavailable(
GeminiLanguageModel.Error.ServiceUnavailable(debugDescription: "Service down")
)
let mappedError = GeminiErrorMapper.map(originalError)
let geminiError = try #require(
mappedError as? GeminiLanguageModel.Error,
"Expected GeminiLanguageModel.Error, got: \(type(of: mappedError))"
)
guard case .serviceUnavailable(let serviceUnavailable) = geminiError else {
Issue.record("Expected GeminiLanguageModel.Error.serviceUnavailable, got: \(mappedError)")
return
}
#expect(serviceUnavailable.debugDescription == originalError.debugDescription)
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func mapRateLimit429WithRetryDelay() throws {
let apiError = GoogleCloudAPIError(
code: 429,
message: "Resource exhausted",
status: .resourceExhausted,
retryDelay: .seconds(60)
)
let mappedError = GeminiErrorMapper.map(GeminiAPIError.apiError(apiError))
if case LanguageModelError.rateLimited(let rateLimited) = mappedError {
#expect(rateLimited.debugDescription == apiError.message)
let resetDate = try #require(rateLimited.resetDate)
#expect(resetDate > Date.now)
} else {
Issue.record("Expected LanguageModelError.rateLimited")
}
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func mapRateLimitResourceExhaustedWithoutRetryDelay() {
let apiError = GoogleCloudAPIError(
code: 429,
message: "Quota exceeded",
status: .resourceExhausted
)
let mappedError = GeminiErrorMapper.map(GeminiAPIError.apiError(apiError))
if case LanguageModelError.rateLimited(let rateLimited) = mappedError {
#expect(rateLimited.debugDescription == apiError.message)
#expect(rateLimited.resetDate == nil)
} else {
Issue.record("Expected LanguageModelError.rateLimited")
}
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func mapServiceUnavailableFromAPIError() throws {
let apiError = GoogleCloudAPIError(
code: 503,
message: "Backend service unavailable",
status: .unavailable
)
let mappedError = GeminiErrorMapper.map(GeminiAPIError.apiError(apiError))
let geminiError = try #require(
mappedError as? GeminiLanguageModel.Error,
"Expected GeminiLanguageModel.Error, got: \(type(of: mappedError))"
)
guard case .serviceUnavailable(let serviceUnavailable) = geminiError else {
Issue.record("Expected GeminiLanguageModel.Error.serviceUnavailable, got: \(mappedError)")
return
}
#expect(serviceUnavailable.debugDescription == apiError.message)
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func mapServiceUnavailableFromHTTPError503() throws {
let httpError = GeminiAPIError.httpError(statusCode: 503, body: "Overloaded")
let mappedError = GeminiErrorMapper.map(httpError)
let geminiError = try #require(
mappedError as? GeminiLanguageModel.Error,
"Expected GeminiLanguageModel.Error, got: \(type(of: mappedError))"
)
guard case .serviceUnavailable(let serviceUnavailable) = geminiError else {
Issue.record("Expected GeminiLanguageModel.Error.serviceUnavailable, got: \(mappedError)")
return
}
if case GeminiAPIError.httpError(_, let body) = httpError {
#expect(serviceUnavailable.debugDescription.contains(body))
}
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func mapModelNotFoundFrom404Message() throws {
let apiError = GoogleCloudAPIError(
code: 404,
message: "models/gemini-invalid is not found",
status: .notFound
)
let mappedError = GeminiErrorMapper.map(GeminiAPIError.apiError(apiError))
let geminiError = try #require(
mappedError as? GeminiLanguageModel.Error,
"Expected GeminiLanguageModel.Error, got: \(type(of: mappedError))"
)
guard case .modelNotFound(let modelNotFound) = geminiError else {
Issue.record("Expected GeminiLanguageModel.Error.modelNotFound, got: \(mappedError)")
return
}
#expect(modelNotFound.debugDescription == apiError.message)
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func mapGeneralAPIError() throws {
let apiError = GoogleCloudAPIError(
code: 400,
message: "Invalid field value",
status: .invalidArgument
)
let mappedError = GeminiErrorMapper.map(GeminiAPIError.apiError(apiError))
let geminiError = try #require(
mappedError as? GeminiLanguageModel.Error,
"Expected GeminiLanguageModel.Error, got: \(type(of: mappedError))"
)
guard case .apiError(let geminiAPIError) = geminiError else {
Issue.record("Expected GeminiLanguageModel.Error.apiError, got: \(mappedError)")
return
}
#expect(geminiAPIError.code == apiError.status?.rawValue)
#expect(geminiAPIError.statusCode == apiError.code)
#expect(geminiAPIError.message == apiError.message)
#expect(geminiAPIError.metadata["status"] as? String == apiError.status?.rawValue)
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func mapNetworkFailureFromHTTPError() throws {
let httpError = GeminiAPIError.httpError(statusCode: 502, body: "Bad Gateway")
let mappedError = GeminiErrorMapper.map(httpError)
let geminiError = try #require(
mappedError as? GeminiLanguageModel.Error,
"Expected GeminiLanguageModel.Error, got: \(type(of: mappedError))"
)
guard case .networkFailure(let networkFailure) = geminiError else {
Issue.record("Expected GeminiLanguageModel.Error.networkFailure, got: \(mappedError)")
return
}
if case GeminiAPIError.httpError(_, let body) = httpError {
#expect(networkFailure.debugDescription.contains(body))
}
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func mapTimeoutFromURLError() {
let urlError = URLError(.timedOut)
let mappedError = GeminiErrorMapper.map(urlError)
#expect(mappedError is LanguageModelError)
if case LanguageModelError.timeout(let timeout) = mappedError {
#expect(timeout.debugDescription == urlError.localizedDescription)
} else {
Issue.record("Expected LanguageModelError.timeout")
}
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func mapNetworkFailureFromGenericURLError() throws {
let urlError = URLError(.cannotConnectToHost)
let mappedError = GeminiErrorMapper.map(urlError)
let geminiError = try #require(
mappedError as? GeminiLanguageModel.Error,
"Expected GeminiLanguageModel.Error, got: \(type(of: mappedError))"
)
guard case .networkFailure(let networkFailure) = geminiError else {
Issue.record("Expected GeminiLanguageModel.Error.networkFailure, got: \(mappedError)")
return
}
#expect(networkFailure.debugDescription == urlError.localizedDescription)
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func mapInvalidResponseFromTypeMismatchDecodingError() throws {
let json = #"{"candidates": [{"content": {"parts": "not an array"}}]}"#
let decodingError = try #require(makeDecodingError(decoding: json))
let mappedError = GeminiErrorMapper.map(decodingError)
let geminiError = try #require(
mappedError as? GeminiLanguageModel.Error,
"Expected GeminiLanguageModel.Error, got: \(type(of: mappedError))"
)
guard case .invalidResponse(let invalidResponse) = geminiError else {
Issue.record("Expected GeminiLanguageModel.Error.invalidResponse, got: \(mappedError)")
return
}
#expect(invalidResponse.debugDescription.contains("`candidates[0].content.parts`"))
let underlyingError = try #require(invalidResponse.underlyingError as? DecodingError)
guard case .typeMismatch = underlyingError else {
Issue.record("Expected DecodingError.typeMismatch, got: \(underlyingError)")
return
}
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func mapInvalidResponseFromKeyNotFoundDecodingError() throws {
struct Response: Decodable {
let candidates: [Candidate]
}
let decodingError = try #require(makeDecodingError(decoding: "{}", as: Response.self))
let mappedError = GeminiErrorMapper.map(decodingError)
guard case GeminiLanguageModel.Error.invalidResponse(let invalidResponse) = mappedError else {
Issue.record("Expected GeminiLanguageModel.Error.invalidResponse, got: \(mappedError)")
return
}
#expect(invalidResponse.debugDescription.contains("`candidates`"))
#expect(invalidResponse.underlyingError is DecodingError)
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func mapInvalidResponseFromMalformedJSONDecodingError() throws {
let decodingError = try #require(makeDecodingError(decoding: "not json"))
let mappedError = GeminiErrorMapper.map(decodingError)
guard case GeminiLanguageModel.Error.invalidResponse(let invalidResponse) = mappedError else {
Issue.record("Expected GeminiLanguageModel.Error.invalidResponse, got: \(mappedError)")
return
}
#expect(!invalidResponse.debugDescription.contains("`"))
#expect(invalidResponse.underlyingError is DecodingError)
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func checkGuardrailsThrowsForPromptSafety() {
let promptFeedback = PromptFeedback { $0.blockReason = .safety }
let chunk = GenerateContentResponse { $0.promptFeedback = promptFeedback }
#expect(throws: LanguageModelError.self) {
try GeminiErrorMapper.checkGuardrails(in: chunk)
}
}
@Test(arguments: [PromptFeedback.BlockReason.modelArmor, .jailbreak])
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func checkGuardrailsThrowsGuardrailViolationForPromptBlockReason(
_ blockReason: PromptFeedback.BlockReason
) {
let promptFeedback = PromptFeedback { $0.blockReason = blockReason }
let chunk = GenerateContentResponse { $0.promptFeedback = promptFeedback }
do {
try GeminiErrorMapper.checkGuardrails(in: chunk)
Issue.record("Expected guardrailViolation error to be thrown")
} catch let LanguageModelError.guardrailViolation(violation) {
#expect(violation.debugDescription.contains("\(blockReason)"))
} catch {
Issue.record("Unexpected error thrown: \(error)")
}
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func checkGuardrailsThrowsRefusalForPromptOther() async throws {
let blockReason = PromptFeedback.BlockReason.other
let promptFeedback = PromptFeedback { $0.blockReason = blockReason }
let chunk = GenerateContentResponse { $0.promptFeedback = promptFeedback }
do {
try GeminiErrorMapper.checkGuardrails(in: chunk)
Issue.record("Expected refusal error to be thrown")
} catch let LanguageModelError.refusal(refusal) {
let content = try await refusal.explanation.content
#expect(content.contains("\(blockReason)"))
} catch {
Issue.record("Unexpected error thrown: \(error)")
}
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func checkGuardrailsThrowsForCandidateSafety() {
let finishMessage = "Safety policy triggered"
let candidate = Candidate {
$0.finishReason = .safety
$0.finishMessage = finishMessage
}
let chunk = GenerateContentResponse { $0.candidates = [candidate] }
do {
try GeminiErrorMapper.checkGuardrails(in: chunk)
Issue.record("Expected guardrailViolation error to be thrown")
} catch let LanguageModelError.guardrailViolation(violation) {
#expect(violation.debugDescription.contains(finishMessage))
} catch {
Issue.record("Unexpected error thrown: \(error)")
}
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func checkGuardrailsThrowsForCandidateModelArmor() {
let finishMessage = "Model Armor blocked the response"
let candidate = Candidate {
$0.finishReason = .modelArmor
$0.finishMessage = finishMessage
}
let chunk = GenerateContentResponse { $0.candidates = [candidate] }
do {
try GeminiErrorMapper.checkGuardrails(in: chunk)
Issue.record("Expected guardrailViolation error to be thrown")
} catch let LanguageModelError.guardrailViolation(violation) {
#expect(violation.debugDescription.contains(finishMessage))
} catch {
Issue.record("Unexpected error thrown: \(error)")
}
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func checkGuardrailsThrowsRefusalForCandidateRecitation() async throws {
let finishMessage = "Recitation check failed"
let candidate = Candidate {
$0.finishReason = .recitation
$0.finishMessage = finishMessage
}
let chunk = GenerateContentResponse { $0.candidates = [candidate] }
do {
try GeminiErrorMapper.checkGuardrails(in: chunk)
Issue.record("Expected refusal error to be thrown")
} catch let LanguageModelError.refusal(refusal) {
let content = try await refusal.explanation.content
#expect(content.contains(finishMessage))
} catch {
Issue.record("Unexpected error thrown: \(error)")
}
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func checkGuardrailsThrowsUnsupportedLanguageForCandidateLanguage() {
let finishMessage = "Language not supported"
let candidate = Candidate {
$0.finishReason = .language
$0.finishMessage = finishMessage
}
let chunk = GenerateContentResponse { $0.candidates = [candidate] }
do {
try GeminiErrorMapper.checkGuardrails(in: chunk)
Issue.record("Expected unsupportedLanguageOrLocale error to be thrown")
} catch let LanguageModelError.unsupportedLanguageOrLocale(unsupported) {
#expect(unsupported.languageCode == Locale.LanguageCode("und"))
#expect(unsupported.debugDescription == finishMessage)
} catch {
Issue.record("Unexpected error thrown: \(error)")
}
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func checkGuardrailsPassesForSafeContent() throws {
let candidate = Candidate { $0.finishReason = .stop }
let chunk = GenerateContentResponse { $0.candidates = [candidate] }
try GeminiErrorMapper.checkGuardrails(in: chunk)
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func apiErrorDebugDescription() {
let errorWithCode = GeminiLanguageModel.Error.APIError(
code: "invalid_request",
statusCode: 400,
message: "Invalid parameter"
)
let errorWithoutCode = GeminiLanguageModel.Error.APIError(
code: "invalid_request",
message: "Invalid parameter"
)
#expect(errorWithCode.debugDescription.contains("HTTP 400"))
#expect(!errorWithoutCode.debugDescription.contains("HTTP"))
#expect(errorWithCode.code == "invalid_request")
#expect(errorWithCode.statusCode == 400)
#expect(errorWithCode.message == "Invalid parameter")
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func mapRateLimitFromHTTP429() {
let mappedError = GeminiErrorMapper.map(
GeminiAPIError.httpError(statusCode: 429, body: "Too many requests")
)
if case LanguageModelError.rateLimited(let rateLimited) = mappedError {
#expect(rateLimited.debugDescription.contains("429"))
} else {
Issue.record("Expected LanguageModelError.rateLimited")
}
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func mapModelNotFoundFromHTTP404() {
let mappedError = GeminiErrorMapper.map(
GeminiAPIError.httpError(statusCode: 404, body: "Not found")
)
if case GeminiLanguageModel.Error.modelNotFound(let modelNotFound) = mappedError {
#expect(modelNotFound.debugDescription.contains("404"))
} else {
Issue.record("Expected GeminiLanguageModel.Error.modelNotFound")
}
}
@Test
@available(macOS 27.0, iOS 27.0, watchOS 27.0, visionOS 27.0, *)
func checkGuardrailsThrowsRefusalForCandidateOtherWithoutMessage() {
let candidate = Candidate { $0.finishReason = .other }
let chunk = GenerateContentResponse { $0.candidates = [candidate] }
#expect(throws: LanguageModelError.self) {
try GeminiErrorMapper.checkGuardrails(in: chunk)
}
}
// MARK: - Helpers
/// Decodes the given JSON and returns the resulting `DecodingError`, if any.
///
/// - Parameters:
/// - json: The JSON string to decode.
/// - type: The type to decode the JSON as.
/// - Returns: The `DecodingError` thrown while decoding, or `nil` if decoding succeeded.
private func makeDecodingError<T: Decodable>(
decoding json: String,
as type: T.Type = GenerateContentResponse.self
) -> DecodingError? {
do {
_ = try JSONDecoder().decode(type, from: Data(json.utf8))
return nil
} catch {
return error as? DecodingError
}
}
}
#endif // canImport(FoundationModels) && compiler(>=6.4)