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

200 lines
5.8 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.
import SwiftDiagnostics
import SwiftSyntaxMacrosGenericTestSupport
import Testing
/// Expansion tests for less common `@Generable` declarations.
@Suite
struct GenerableMacroEdgeCaseTests {
/// An internal `Hashable` struct with a non-`GenerationID` `id` property: members get no
/// access modifier, `PartiallyGenerated` repeats `Hashable`, and it doesn't synthesize
/// `Identifiable`.
@Test
func internalHashableStructWithIDProperty() throws {
let source = """
@Generable
struct InternalThing: Hashable {
var id: String
var value: Int
}
"""
try assertExpansion(source, matchesSnapshot: "EdgeCases/InternalHashableWithID")
}
/// An `id: GenerationID` property maps to the content's generation ID instead of a schema
/// property.
@Test
func generationIDProperty() throws {
let source = """
@Generable
struct SearchTerm {
var id: GenerationID
@Guide(description: "A two- or three- word search term")
var searchTerm: String
}
"""
try assertExpansion(source, matchesSnapshot: "EdgeCases/GenerationID")
}
/// An `id: GenerationID?` property maps to the content's generation ID without a fallback.
@Test
func optionalGenerationIDProperty() throws {
let source = """
@Generable
struct Draft {
var id: GenerationID?
var text: String
}
"""
try assertExpansion(source, matchesSnapshot: "EdgeCases/OptionalGenerationID")
}
/// A `package` enum with several unlabeled associated values, which are named `value`,
/// `value1`, and so on.
@Test
func packageEnumWithUnlabeledAssociatedValues() throws {
let source = """
@Generable
package enum Payload {
case pair(Int, Int)
case single(Int)
}
"""
try assertExpansion(source, matchesSnapshot: "EdgeCases/PackageUnlabeled")
}
/// An enum whose cases mix labeled and unlabeled associated values. Like Apple's macro, an
/// unlabeled value is named after its position among all of the case's values.
@Test
func enumWithMixedAssociatedValueLabels() throws {
let source = """
@Generable
enum Mixed {
case mix(Int, label: String, Double)
case labeledFirst(x: Int, String)
}
"""
try assertExpansion(source, matchesSnapshot: "EdgeCases/MixedAssociatedValueLabels")
}
/// A `public` struct whose properties have their own access levels. Like Apple's macro, each
/// `PartiallyGenerated` property repeats its property's modifiers.
@Test
func publicStructWithMixedPropertyAccess() throws {
let source = """
@Generable
public struct Mixed {
public var shown: String
var hidden: Int
private var secret: Int
public private(set) var readOnly: String
}
"""
try assertExpansion(source, matchesSnapshot: "EdgeCases/MixedPropertyAccess")
}
/// Associated value labels that are keywords are escaped where they're used as identifiers.
@Test
func keywordAssociatedValueLabels() throws {
let source = """
@Generable
enum Keyword {
case range(default: Int, in: String)
case plain
}
"""
try assertExpansion(source, matchesSnapshot: "EdgeCases/KeywordLabels")
}
/// Backticked names, multiple bindings, observers, and implicitly unwrapped optionals are
/// included; constants with a value, `lazy`, static, and computed properties are skipped.
@Test
func storedPropertyForms() throws {
let source = """
@Generable
struct Forms {
var `default`: Int
var a, b: Int
var observed: Int { didSet {} }
var implicit: String!
let constant: Int = 3
lazy var cached: Int = 3
static var shared: Int = 1
var computed: Int { a + b }
}
"""
try assertExpansion(source, matchesSnapshot: "EdgeCases/StoredPropertyForms")
}
/// `@Generable(name:)` on an enum calls the macro-only `GenerationSchema(__name:anyOf:)`.
@Test
func namedEnum() throws {
let source = """
@Generable(name: "Mood")
public enum NamedMood {
case happy
case sad
}
"""
try assertExpansion(source, matchesSnapshot: "EdgeCases/NamedEnum")
}
/// An enum's description and `representNilExplicitlyInGeneratedContent` are repeated by its
/// `Discriminated*` structs.
@Test
func explicitNilEnum() throws {
let source = """
@Generable(description: "D", representNilExplicitlyInGeneratedContent: true)
enum ExplicitEnum {
case a(Int?)
}
"""
try assertExpansion(source, matchesSnapshot: "EdgeCases/ExplicitNilEnum")
}
/// A property without a type annotation is excluded with a warning.
@Test
func propertyWithoutTypeAnnotation() throws {
let source = """
@Generable
struct Untyped {
var count = 5
var name: String
}
"""
let warning = DiagnosticSpec(
message: "Property 'count' has no type annotation, so '@Generable' excludes it from the "
+ "generation schema; add an explicit type to include it.",
line: 3,
column: 7,
severity: .warning
)
try assertExpansion(
source, matchesSnapshot: "EdgeCases/MissingType", diagnostics: [warning])
}
}