mirror of
https://github.com/firebase/firebase-ios-sdk
synced 2026-11-10 22:45:59 +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.
138 lines
4.6 KiB
Swift
138 lines
4.6 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)
|
|
import Foundation
|
|
import Testing
|
|
|
|
@testable import FoundationModelsFake
|
|
|
|
@Suite("Transcript Codable")
|
|
struct TranscriptCodableTests {
|
|
// MARK: - Encoding constructed transcripts
|
|
|
|
@Test
|
|
func toolCallArgumentsEncodeAsJSONString() throws {
|
|
let arguments = GeneratedContent(properties: ["city": "Paris", "units": "celsius"])
|
|
let call = Transcript.ToolCall(id: "call-1", toolName: "getWeather", arguments: arguments)
|
|
let transcript = Transcript(entries: [.toolCalls(Transcript.ToolCalls(id: "c", [call]))])
|
|
|
|
let encoded = try Self.encode(transcript)
|
|
|
|
let object = try #require(
|
|
JSONSerialization.jsonObject(with: encoded) as? [String: Any]
|
|
)
|
|
let entries = try #require(
|
|
(object["transcript"] as? [String: Any])?["entries"] as? [[String: Any]]
|
|
)
|
|
let calls = try #require(entries.first?["toolCalls"] as? [[String: Any]])
|
|
#expect(calls.first?["arguments"] as? String == #"{"city": "Paris", "units": "celsius"}"#)
|
|
#expect(calls.first?["metadata"] == nil)
|
|
}
|
|
|
|
@Test
|
|
func structuredContentIsEmbeddedAsJSON() throws {
|
|
let content = GeneratedContent(properties: ["title": "Headline", "rank": 1])
|
|
let segment = Transcript.Segment.structure(
|
|
Transcript.StructuredSegment(id: "s", schemaName: "SearchResult", content: content)
|
|
)
|
|
let transcript = Transcript(entries: [
|
|
.toolOutput(Transcript.ToolOutput(id: "call-2", toolName: "search", segments: [segment]))
|
|
])
|
|
let expected = try Self.canonical(
|
|
Data(
|
|
#"""
|
|
{"type":"FoundationModels.Transcript","version":"1.1","transcript":{"entries":[
|
|
{"role":"tool","id":"call-2","toolCallID":"call-2","toolName":"search","contents":[
|
|
{"type":"structure","id":"s","structure":{"source":"SearchResult",
|
|
"content":{"title":"Headline","rank":1}}}]}]}}
|
|
"""#.utf8
|
|
)
|
|
)
|
|
|
|
let encoded = try Self.encode(transcript)
|
|
|
|
#expect(try Self.canonical(encoded) == expected)
|
|
}
|
|
|
|
// MARK: - Decoded and constructed equality
|
|
|
|
@Test
|
|
func decodedStructuredSegmentDoesNotEqualConstructedOne() throws {
|
|
let constructed = Transcript(entries: [
|
|
.response(
|
|
Transcript.Response(
|
|
id: "r",
|
|
segments: [
|
|
.structure(
|
|
Transcript.StructuredSegment(
|
|
id: "s",
|
|
schemaName: "SearchResult",
|
|
content: GeneratedContent(properties: ["title": "Headline"])
|
|
)
|
|
)
|
|
]
|
|
)
|
|
)
|
|
])
|
|
|
|
let decoded = try JSONDecoder().decode(
|
|
Transcript.self,
|
|
from: Self.encode(constructed)
|
|
)
|
|
|
|
#expect(decoded != constructed)
|
|
#expect(try Self.encode(decoded) == Self.encode(constructed))
|
|
}
|
|
|
|
// MARK: - Decoding errors
|
|
|
|
@Test
|
|
func rejectsUnknownEntryRole() throws {
|
|
let input = #"""
|
|
{"type":"FoundationModels.Transcript","version":"1.1",
|
|
"transcript":{"entries":[{"role":"mystery","id":"x"}]}}
|
|
"""#
|
|
|
|
#expect(throws: DecodingError.self) {
|
|
try JSONDecoder().decode(Transcript.self, from: Data(input.utf8))
|
|
}
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
/// Encodes a transcript with sorted keys.
|
|
///
|
|
/// - Parameter transcript: The transcript to encode.
|
|
/// - Returns: The encoded JSON.
|
|
static func encode(_ transcript: Transcript) throws -> Data {
|
|
let encoder = JSONEncoder()
|
|
encoder.outputFormatting = [.sortedKeys]
|
|
return try encoder.encode(transcript)
|
|
}
|
|
|
|
/// Re-serializes JSON with sorted keys so that two encodings can be compared.
|
|
///
|
|
/// - Parameter data: The JSON to canonicalize.
|
|
/// - Returns: The JSON with sorted keys.
|
|
static func canonical(_ data: Data) throws -> Data {
|
|
let object = try JSONSerialization.jsonObject(with: data, options: [.fragmentsAllowed])
|
|
return try JSONSerialization.data(
|
|
withJSONObject: object,
|
|
options: [.sortedKeys, .fragmentsAllowed]
|
|
)
|
|
}
|
|
}
|
|
#endif // compiler(>=6.4)
|