Files
Andrew Heard b82cdca747 feat(ai): add image input support to GLM (#16895)
Declare the vision capability in GeminiLanguageModel and translate
prompt image attachments into Gemini inlineData parts. Previously,
any attachment segment in a transcript threw an error.

Key changes and implementation details:

- Pass-through optimization: PNG, JPEG, WebP, HEIC, and HEIF file URLs
  that are already upright are sent unchanged without re-encoding.
  Multi-image files are only passed through if the primary image is
  the first image.
- Re-encoding and normalization: All other image sources (CGImage,
  CIImage, CVPixelBuffer, other formats, or rotated files) are rotated
  to display orientation and encoded as JPEG (quality 0.9, matching
  Transcript Codable conformance). Grayscale, extended-range, and HDR
  images are converted to sRGB, compositing transparency over black.
- Platform portability: Re-encoding relies solely on CoreGraphics,
  ImageIO, and UniformTypeIdentifiers to maintain watchOS support.
- Scoping and validation: Image attachments are supported only in
  prompt entries. Non-file URLs are rejected by scheme to avoid
  leaking tokens or dumping long payloads.
- Ordering: Prompt segments retain their order, with adjacent text runs
  merged into single text parts.
- Tests: Add extensive unit and integration tests covering formats,
  orientations, color spaces, and backend image understanding.
2026-10-30 16:57:47 -04:00

2.8 KiB

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:

  1. Unit Tests (Default):

    • Use MockHTTPURLProtocol for deterministic, zero-network, sub-second execution.
    • Run cleanly in offline, CI, and local development environments without any credentials or background daemons.
  2. 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.

Tip

For details on configuring credentials and parameterizing integration tests, see the Integration Tests Guide.