Files
Andrew Heard bc9f9ea1da feat(ai): add FoundationModelsFake for GLM tests (#16995)
Add FoundationModelsFake, a nested Swift package that fakes Apple's
Foundation Models framework, so that GeminiLanguageModel (GLM) can build
and run its unit tests on Linux and on OS versions without the real
framework. It's used only for development and testing.

- GLM depends on the fake through a `FoundationModelsFake` package
  trait, which is on by default where Darwin isn't available (e.g.,
  Linux). With the trait off, the fake isn't built. The root
  `Package.swift` and podspecs can't reach it.
- The fake mirrors Apple's public API and availability in the 27.2
  SDKs, including `@Generable` macro expansions. It omits
  PrivateCloudComputeLanguageModel, OCRTool, BarcodeReaderTool, the
  adapter and feedback APIs, and deprecated APIs.
  SystemLanguageModel is always unavailable. Below Swift 6.4 it
  compiles to an empty module.
- Decoding rejects transcript images with `file:` URLs and schemas
  whose references never resolve, instead of reading local files or
  overflowing the stack.
- Without ImageIO, GLM passes PNG, JPEG, WebP, HEIC, and HEIF images
  through to Gemini after checking their signatures, and rejects
  images that would need re-encoding.
- Add CI jobs that run both packages' tests against the fake: one on
  Linux in a digest-pinned `swift:6.4.2` container, and one on macOS
  with Xcode 27.2. Skip `sdk.ai` for changes that only touch the fake.
2026-11-05 14:13:50 -05:00

535 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 compiler(>=6.4) && (FoundationModelsFake || canImport(FoundationModels))
import Foundation
#if FoundationModelsFake
import FoundationModelsFake
#else
import FoundationModels
#endif
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 // compiler(>=6.4) && (FoundationModelsFake || canImport(FoundationModels))