mirror of
https://github.com/firebase/firebase-ios-sdk
synced 2026-11-09 22:36:01 +05:45
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.
GeminiLanguageModel Tests
Unit and integration test suites validating GeminiLanguageModel protocol
conformance, streaming response generation, transcript translation, error
mapping, and backend integration.
Quick Start
# Run all unit tests (fast, ~0.04s, skips integration tests)
swift test --skip IntegrationTests
# Run all tests in this target (including integration tests)
swift test --filter GeminiLanguageModelTests
Test Suites
| Suite / File | Scope | Strategy |
|---|---|---|
GeminiLanguageModelTests.swift |
Protocol conformance, single/multi-turn responses, streaming, tool calling | Unit (MockHTTPURLProtocol) |
GenerationSchema+GeminiTests.swift |
JSON Schema encoding and property ordering conversion | Unit (pure transformation) |
GeminiRequestTranslatorTests.swift |
Request, generation config, tool declarations, and tool mode mapping | Unit (pure transformation) |
GeminiTranscriptTranslatorTests.swift |
Apple Transcript <-> Gemini payload mapping (tools, reasoning, prompts, image parts) |
Unit (pure transformation) |
GeminiImageTranslatorTests.swift |
Image attachment encoding, orientation, file pass-through, and URL handling | Unit (images generated in code) |
TestImages.swift |
Helpers that generate, encode, and sample test images without bundled resources | Test utility |
GeminiErrorMapperTests.swift |
HTTP and API error mapping to LanguageModelError |
Unit (exhaustive mapping) |
IntegrationTestingBackendTests.swift |
Endpoint resolution and credential discovery | Unit / Infrastructure |
IntegrationTests/ |
End-to-end Gemini and Firebase AI Logic integration tests | Integration (see IntegrationTests/README.md) |
Testing Architecture
This target separates tests into two distinct tiers:
-
Unit Tests (Default):
- Use
MockHTTPURLProtocolfor deterministic, zero-network, sub-second execution. - Run cleanly in offline, CI, and local development environments without any credentials or background daemons.
- Use
-
Integration Tests (
IntegrationTests/):- Tagged with
.tags(.integration)and gated by.requireIntegrationTestingBackend. - Exercise endpoints on Google Cloud and Firebase using configured credentials.
- Isolated in the
IntegrationTests/subfolder so unit test runs are never slowed down by network timeouts or external dependencies.
- Tagged with
Tip
For details on configuring credentials and parameterizing integration tests, see the Integration Tests Guide.