Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions FirebaseAI/Tests/Unit/ChatTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,104 @@ final class ChatTests: XCTestCase {
XCTAssertEqual(chat.history[1], assembledExpectation)
#endif // os(watchOS)
}

func testSendMessage_unary_appendsHistory() async throws {
let expectedInput = "Test input"
MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
forResource: "unary-success-basic-reply-short",
withExtension: "json",
subdirectory: "mock-responses/googleai"
)
let model = GenerativeModel(
modelName: modelName,
modelResourceName: modelResourceName,
firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
apiConfig: FirebaseAI.defaultVertexAIAPIConfig,
tools: nil,
requestOptions: RequestOptions(),
urlSession: urlSession
)
let chat = model.startChat()

// Pre-condition: History should be empty.
XCTAssertTrue(chat.history.isEmpty)

let response = try await chat.sendMessage(expectedInput)

XCTAssertNotNil(response.text)
let text = try XCTUnwrap(response.text)
XCTAssertFalse(text.isEmpty)

// Post-condition: History should have the user's message and the model's response.
XCTAssertEqual(chat.history.count, 2)
let userInput = try XCTUnwrap(chat.history.first)
XCTAssertEqual(userInput.role, "user")
XCTAssertEqual(userInput.parts.count, 1)
let userInputText = try XCTUnwrap(userInput.parts.first as? TextPart)
XCTAssertEqual(userInputText.text, expectedInput)

let modelResponse = try XCTUnwrap(chat.history.last)
XCTAssertEqual(modelResponse.role, "model")
XCTAssertEqual(modelResponse.parts.count, 1)
let modelResponseText = try XCTUnwrap(modelResponse.parts.first as? TextPart)
XCTAssertFalse(modelResponseText.text.isEmpty)
}

func testSendMessageStream_error_doesNotAppendHistory() async throws {
MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
forResource: "streaming-failure-finish-reason-safety",
withExtension: "txt",
subdirectory: "mock-responses/vertexai"
)
let model = GenerativeModel(
modelName: modelName,
modelResourceName: modelResourceName,
firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
apiConfig: FirebaseAI.defaultVertexAIAPIConfig,
tools: nil,
requestOptions: RequestOptions(),
urlSession: urlSession
)
let chat = model.startChat()
let input = "Test input"

// Pre-condition: History should be empty.
XCTAssertTrue(chat.history.isEmpty)

do {
let stream = try chat.sendMessageStream(input)
for try await _ in stream {
// Consume the stream.
}
XCTFail("Should have thrown a responseStoppedEarly error.")
} catch let GenerateContentError.responseStoppedEarly(reason, _) {
XCTAssertEqual(reason, .safety)
} catch {
XCTFail("Unexpected error thrown: \(error)")
}

// Post-condition: History should still be empty.
XCTAssertEqual(chat.history.count, 0)
}

func testStartChat_withHistory_initializesCorrectly() async throws {
let history = [
ModelContent(role: "user", parts: "Question 1"),
ModelContent(role: "model", parts: "Answer 1"),
]
let model = GenerativeModel(
modelName: modelName,
modelResourceName: modelResourceName,
firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
apiConfig: FirebaseAI.defaultVertexAIAPIConfig,
tools: nil,
requestOptions: RequestOptions(),
urlSession: urlSession
)

let chat = model.startChat(history: history)

XCTAssertEqual(chat.history.count, 2)
XCTAssertEqual(chat.history, history)
}
}
88 changes: 88 additions & 0 deletions FirebaseAI/Tests/Unit/JSONValueTests+Coverage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2025 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 XCTest

@testable import FirebaseAI

@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
extension JSONValueTests {
func testDecodeNestedObject() throws {
let nestedObject: JSONObject = [
"nestedKey": .string("nestedValue"),
]
let expectedObject: JSONObject = [
"numberKey": .number(numberValue),
"objectKey": .object(nestedObject),
]
let json = """
{
"numberKey": \(numberValue),
"objectKey": {
"nestedKey": "nestedValue"
}
}
"""
let jsonData = try XCTUnwrap(json.data(using: .utf8))

let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))

XCTAssertEqual(jsonObject, .object(expectedObject))
}

func testDecodeNestedArray() throws {
let nestedArray: [JSONValue] = [.string("a"), .string("b")]
let expectedObject: JSONObject = [
"numberKey": .number(numberValue),
"arrayKey": .array(nestedArray),
]
let json = """
{
"numberKey": \(numberValue),
"arrayKey": ["a", "b"]
}
"""
let jsonData = try XCTUnwrap(json.data(using: .utf8))

let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))

XCTAssertEqual(jsonObject, .object(expectedObject))
}

func testEncodeNestedObject() throws {
let nestedObject: JSONObject = [
"nestedKey": .string("nestedValue"),
]
let objectValue: JSONObject = [
"numberKey": .number(numberValue),
"objectKey": .object(nestedObject),
]

let jsonData = try encoder.encode(JSONValue.object(objectValue))
let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
XCTAssertEqual(jsonObject, .object(objectValue))
}

func testEncodeNestedArray() throws {
let nestedArray: [JSONValue] = [.string("a"), .string("b")]
let objectValue: JSONObject = [
"numberKey": .number(numberValue),
"arrayKey": .array(nestedArray),
]

let jsonData = try encoder.encode(JSONValue.object(objectValue))
let jsonObject = try XCTUnwrap(decoder.decode(JSONValue.self, from: jsonData))
XCTAssertEqual(jsonObject, .object(objectValue))
}
}
76 changes: 76 additions & 0 deletions FirebaseAI/Tests/Unit/PartsRepresentableTests+Coverage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2025 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 XCTest
#if canImport(UIKit)
import UIKit
#elseif canImport(AppKit)
import AppKit
#endif

@testable import FirebaseAI

@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
extension PartsRepresentableTests {
func testMixedParts() throws {
let text = "This is a test"
let data = try XCTUnwrap("This is some data".data(using: .utf8))
let inlineData = InlineDataPart(data: data, mimeType: "text/plain")

let parts: [any PartsRepresentable] = [text, inlineData]
let modelContent = ModelContent(parts: parts)

XCTAssertEqual(modelContent.parts.count, 2)
let textPart = try XCTUnwrap(modelContent.parts[0] as? TextPart)
XCTAssertEqual(textPart.text, text)
let dataPart = try XCTUnwrap(modelContent.parts[1] as? InlineDataPart)
XCTAssertEqual(dataPart, inlineData)
}

#if canImport(UIKit)
func testMixedParts_withImage() throws {
let text = "This is a test"
let image = try XCTUnwrap(UIImage(systemName: "star"))
let parts: [any PartsRepresentable] = [text, image]
let modelContent = ModelContent(parts: parts)

XCTAssertEqual(modelContent.parts.count, 2)
let textPart = try XCTUnwrap(modelContent.parts[0] as? TextPart)
XCTAssertEqual(textPart.text, text)
let imagePart = try XCTUnwrap(modelContent.parts[1] as? InlineDataPart)
XCTAssertEqual(imagePart.mimeType, "image/jpeg")
XCTAssertFalse(imagePart.data.isEmpty)
}

#elseif canImport(AppKit)
func testMixedParts_withImage() throws {
let text = "This is a test"
let coreImage = CIImage(color: CIColor.blue)
.cropped(to: CGRect(origin: CGPoint.zero, size: CGSize(width: 16, height: 16)))
let rep = NSCIImageRep(ciImage: coreImage)
let image = NSImage(size: rep.size)
image.addRepresentation(rep)

let parts: [any PartsRepresentable] = [text, image]
let modelContent = ModelContent(parts: parts)

XCTAssertEqual(modelContent.parts.count, 2)
let textPart = try XCTUnwrap(modelContent.parts[0] as? TextPart)
XCTAssertEqual(textPart.text, text)
let imagePart = try XCTUnwrap(modelContent.parts[1] as? InlineDataPart)
XCTAssertEqual(imagePart.mimeType, "image/jpeg")
XCTAssertFalse(imagePart.data.isEmpty)
}
#endif
}
112 changes: 112 additions & 0 deletions FirebaseAI/Tests/Unit/SafetyTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2025 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 XCTest

@testable import FirebaseAI

@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
final class SafetyTests: XCTestCase {
let decoder = JSONDecoder()
let encoder = JSONEncoder()

// MARK: - SafetyRating Decoding

func testDecodeSafetyRating_allFieldsPresent() throws {
let json = """
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"probability": "NEGLIGIBLE",
"probabilityScore": 0.1,
"severity": "HARM_SEVERITY_LOW",
"severityScore": 0.2,
"blocked": true
}
"""
let jsonData = try XCTUnwrap(json.data(using: .utf8))
let rating = try decoder.decode(SafetyRating.self, from: jsonData)

XCTAssertEqual(rating.category, .dangerousContent)
XCTAssertEqual(rating.probability, .negligible)
XCTAssertEqual(rating.probabilityScore, 0.1)
XCTAssertEqual(rating.severity, .low)
XCTAssertEqual(rating.severityScore, 0.2)
XCTAssertTrue(rating.blocked)
}

func testDecodeSafetyRating_missingOptionalFields() throws {
let json = """
{
"category": "HARM_CATEGORY_HARASSMENT",
"probability": "LOW"
}
"""
let jsonData = try XCTUnwrap(json.data(using: .utf8))
let rating = try decoder.decode(SafetyRating.self, from: jsonData)

XCTAssertEqual(rating.category, .harassment)
XCTAssertEqual(rating.probability, .low)
XCTAssertEqual(rating.probabilityScore, 0.0)
XCTAssertEqual(rating.severity, .unspecified)
XCTAssertEqual(rating.severityScore, 0.0)
XCTAssertFalse(rating.blocked)
}

func testDecodeSafetyRating_unknownEnums() throws {
let json = """
{
"category": "HARM_CATEGORY_UNKNOWN",
"probability": "UNKNOWN_PROBABILITY",
"severity": "UNKNOWN_SEVERITY"
}
"""
let jsonData = try XCTUnwrap(json.data(using: .utf8))
let rating = try decoder.decode(SafetyRating.self, from: jsonData)

XCTAssertEqual(rating.category.rawValue, "HARM_CATEGORY_UNKNOWN")
XCTAssertEqual(rating.probability.rawValue, "UNKNOWN_PROBABILITY")
XCTAssertEqual(rating.severity.rawValue, "UNKNOWN_SEVERITY")
}

// MARK: - SafetySetting Encoding

func testEncodeSafetySetting_allFields() throws {
let setting = SafetySetting(
harmCategory: .hateSpeech,
threshold: .blockMediumAndAbove,
method: .severity
)
encoder.outputFormatting = .sortedKeys
let jsonData = try encoder.encode(setting)
let jsonString = try XCTUnwrap(String(data: jsonData, encoding: .utf8))

XCTAssertEqual(jsonString, """
{"category":"HARM_CATEGORY_HATE_SPEECH","method":"SEVERITY","threshold":"BLOCK_MEDIUM_AND_ABOVE"}
""")
}

func testEncodeSafetySetting_nilMethod() throws {
let setting = SafetySetting(
harmCategory: .sexuallyExplicit,
threshold: .blockOnlyHigh
)
encoder.outputFormatting = .sortedKeys
let jsonData = try encoder.encode(setting)
let jsonString = try XCTUnwrap(String(data: jsonData, encoding: .utf8))

XCTAssertEqual(jsonString, """
{"category":"HARM_CATEGORY_SEXUALLY_EXPLICIT","threshold":"BLOCK_ONLY_HIGH"}
""")
}
}
Loading