-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Increase Firebase AI Logic unit test coverage #15126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+342
−0
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
37273e4
Part 1
paulb777 8dcfc18
part 2
paulb777 d769436
Review
paulb777 1eef613
address review comments
paulb777 76bf729
fix tests
paulb777 7c3cb3a
[Firebase AI] Merge `+Coverage` files into existing test files (#15136)
andrewheard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
FirebaseAI/Tests/Unit/GenerativeModelGoogleAITests+Coverage.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// 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 FirebaseAppCheckInterop | ||
import FirebaseAuthInterop | ||
import FirebaseCore | ||
import XCTest | ||
|
||
@testable import FirebaseAI | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
extension GenerativeModelGoogleAITests { | ||
// MARK: - Tool/Function Calling | ||
|
||
func testGenerateContent_success_functionCall_emptyArguments() async throws { | ||
MockURLProtocol | ||
.requestHandler = try GenerativeModelTestUtil.httpRequestHandler( | ||
forResource: "unary-success-function-call-empty-arguments", | ||
withExtension: "json", | ||
subdirectory: "mock-responses/vertexai" | ||
paulb777 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
) | ||
|
||
let response = try await model.generateContent(testPrompt) | ||
|
||
XCTAssertEqual(response.candidates.count, 1) | ||
let candidate = try XCTUnwrap(response.candidates.first) | ||
XCTAssertEqual(candidate.content.parts.count, 1) | ||
let part = try XCTUnwrap(candidate.content.parts.first) | ||
guard let functionCall = part as? FunctionCallPart else { | ||
XCTFail("Part is not a FunctionCall.") | ||
return | ||
} | ||
XCTAssertEqual(functionCall.name, "current_time") | ||
XCTAssertTrue(functionCall.args.isEmpty) | ||
XCTAssertEqual(response.functionCalls, [functionCall]) | ||
} | ||
|
||
func testGenerateContent_success_functionCall_withArguments() async throws { | ||
MockURLProtocol | ||
.requestHandler = try GenerativeModelTestUtil.httpRequestHandler( | ||
forResource: "unary-success-function-call-with-arguments", | ||
withExtension: "json", | ||
subdirectory: "mock-responses/vertexai" | ||
) | ||
|
||
let response = try await model.generateContent(testPrompt) | ||
|
||
XCTAssertEqual(response.candidates.count, 1) | ||
let candidate = try XCTUnwrap(response.candidates.first) | ||
XCTAssertEqual(candidate.content.parts.count, 1) | ||
let part = try XCTUnwrap(candidate.content.parts.first) | ||
guard let functionCall = part as? FunctionCallPart else { | ||
XCTFail("Part is not a FunctionCall.") | ||
return | ||
} | ||
XCTAssertEqual(functionCall.name, "sum") | ||
XCTAssertEqual(functionCall.args.count, 2) | ||
let argX = try XCTUnwrap(functionCall.args["x"]) | ||
XCTAssertEqual(argX, .number(4)) | ||
let argY = try XCTUnwrap(functionCall.args["y"]) | ||
XCTAssertEqual(argY, .number(5)) | ||
XCTAssertEqual(response.functionCalls, [functionCall]) | ||
} | ||
|
||
func testGenerateContent_success_functionCall_parallelCalls() async throws { | ||
MockURLProtocol | ||
.requestHandler = try GenerativeModelTestUtil.httpRequestHandler( | ||
forResource: "unary-success-function-call-parallel-calls", | ||
withExtension: "json", | ||
subdirectory: "mock-responses/vertexai" | ||
) | ||
|
||
let response = try await model.generateContent(testPrompt) | ||
|
||
XCTAssertEqual(response.candidates.count, 1) | ||
let candidate = try XCTUnwrap(response.candidates.first) | ||
XCTAssertEqual(candidate.content.parts.count, 3) | ||
let functionCalls = response.functionCalls | ||
XCTAssertEqual(functionCalls.count, 3) | ||
} | ||
|
||
func testGenerateContent_success_functionCall_mixedContent() async throws { | ||
MockURLProtocol | ||
.requestHandler = try GenerativeModelTestUtil.httpRequestHandler( | ||
forResource: "unary-success-function-call-mixed-content", | ||
withExtension: "json", | ||
subdirectory: "mock-responses/vertexai" | ||
) | ||
|
||
let response = try await model.generateContent(testPrompt) | ||
|
||
XCTAssertEqual(response.candidates.count, 1) | ||
let candidate = try XCTUnwrap(response.candidates.first) | ||
XCTAssertEqual(candidate.content.parts.count, 4) | ||
let functionCalls = response.functionCalls | ||
XCTAssertEqual(functionCalls.count, 2) | ||
let text = try XCTUnwrap(response.text) | ||
XCTAssertEqual(text, "The sum of [1, 2, 3] is") | ||
} | ||
|
||
// MARK: - Count Tokens | ||
|
||
func testCountTokens_success() async throws { | ||
MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler( | ||
forResource: "unary-success-total-tokens", | ||
withExtension: "json", | ||
subdirectory: "mock-responses/vertexai" | ||
) | ||
|
||
let response = try await model.countTokens("Why is the sky blue?") | ||
XCTAssertEqual(response.totalTokens, 6) | ||
} | ||
} |
paulb777 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// 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 json = try XCTUnwrap(String(data: jsonData, encoding: .utf8)) | ||
XCTAssertEqual( | ||
json, | ||
"{\"numberKey\":\(numberValueEncoded),\"objectKey\":{\"nestedKey\":\"nestedValue\"}}" | ||
) | ||
paulb777 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
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 json = try XCTUnwrap(String(data: jsonData, encoding: .utf8)) | ||
XCTAssertEqual( | ||
json, | ||
"{\"arrayKey\":[\"a\",\"b\"],\"numberKey\":\(numberValueEncoded)}" | ||
) | ||
paulb777 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.