-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(FirebaseAI): Add missing Part type handlers in ModelContent init #15793
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
base: main
Are you sure you want to change the base?
Changes from all commits
f147dd1
9a731da
6eba765
826c080
8f2248e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,115 @@ | ||||||
| // 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 Foundation | ||||||
| import XCTest | ||||||
|
|
||||||
| @testable import FirebaseAILogic | ||||||
|
|
||||||
| @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||||||
| final class ModelContentTests: XCTestCase { | ||||||
| // MARK: - ModelContent Initialization with Part Types | ||||||
|
|
||||||
| func testInitWithExecutableCodePart() { | ||||||
| let executableCodePart = ExecutableCodePart(language: .python, code: "print('hello')") | ||||||
|
|
||||||
| let content = ModelContent(role: "model", parts: [executableCodePart]) | ||||||
|
|
||||||
| XCTAssertEqual(content.role, "model") | ||||||
| XCTAssertEqual(content.parts.count, 1) | ||||||
| guard let resultPart = content.parts.first as? ExecutableCodePart else { | ||||||
| XCTFail("Expected ExecutableCodePart") | ||||||
| return | ||||||
| } | ||||||
| XCTAssertEqual(resultPart.language, .python) | ||||||
| XCTAssertEqual(resultPart.code, "print('hello')") | ||||||
| } | ||||||
|
|
||||||
| func testInitWithCodeExecutionResultPart() { | ||||||
| let codeExecutionResultPart = CodeExecutionResultPart(outcome: .ok, output: "hello") | ||||||
|
|
||||||
| let content = ModelContent(role: "model", parts: [codeExecutionResultPart]) | ||||||
|
|
||||||
| XCTAssertEqual(content.role, "model") | ||||||
| XCTAssertEqual(content.parts.count, 1) | ||||||
| guard let resultPart = content.parts.first as? CodeExecutionResultPart else { | ||||||
| XCTFail("Expected CodeExecutionResultPart") | ||||||
| return | ||||||
| } | ||||||
| XCTAssertEqual(resultPart.outcome, .ok) | ||||||
| XCTAssertEqual(resultPart.output, "hello") | ||||||
| } | ||||||
|
|
||||||
| func testInitWithMixedPartsIncludingExecutableCode() { | ||||||
| let textPart = TextPart("Here is some code:") | ||||||
| let executableCodePart = ExecutableCodePart(language: .python, code: "x = 1 + 2") | ||||||
| let codeExecutionResultPart = CodeExecutionResultPart(outcome: .ok, output: "3") | ||||||
|
|
||||||
| let content = ModelContent( | ||||||
| role: "model", | ||||||
| parts: [textPart, executableCodePart, codeExecutionResultPart] | ||||||
| ) | ||||||
|
|
||||||
| XCTAssertEqual(content.role, "model") | ||||||
| XCTAssertEqual(content.parts.count, 3) | ||||||
|
|
||||||
| // Verify each part type | ||||||
| XCTAssertTrue(content.parts[0] is TextPart) | ||||||
| XCTAssertTrue(content.parts[1] is ExecutableCodePart) | ||||||
| XCTAssertTrue(content.parts[2] is CodeExecutionResultPart) | ||||||
| } | ||||||
|
|
||||||
| func testInitWithExecutableCodePartPreservesThoughtMetadata() { | ||||||
| // Test that thought-related metadata is preserved through the conversion | ||||||
| let executableCode = ExecutableCode( | ||||||
| language: ExecutableCode.Language(kind: .python), | ||||||
| code: "print('test')" | ||||||
| ) | ||||||
| let internalExecutableCodePart = ExecutableCodePart( | ||||||
| executableCode, | ||||||
| isThought: true, | ||||||
| thoughtSignature: "some-signature" | ||||||
| ) | ||||||
|
|
||||||
| // Verify metadata is set correctly on the part itself | ||||||
| XCTAssertTrue(internalExecutableCodePart.isThought) | ||||||
| XCTAssertEqual(internalExecutableCodePart.thoughtSignature, "some-signature") | ||||||
|
|
||||||
| let content = ModelContent(role: "model", parts: [internalExecutableCodePart]) | ||||||
|
|
||||||
| guard let resultPart = content.parts.first as? ExecutableCodePart else { | ||||||
| XCTFail("Expected ExecutableCodePart") | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| // Verify the part maintains its properties after round-trip | ||||||
| XCTAssertEqual(resultPart.language, .python) | ||||||
| XCTAssertEqual(resultPart.code, "print('test')") | ||||||
| XCTAssertTrue(resultPart.isThought) | ||||||
| XCTAssertEqual(resultPart.thoughtSignature, "some-signature") | ||||||
| } | ||||||
|
|
||||||
| func testInitWithCodeExecutionResultPartWithDeadlockedOutcome() { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test name
Suggested change
|
||||||
| let codeExecutionResultPart = CodeExecutionResultPart(outcome: .deadlineExceeded, output: nil) | ||||||
|
Check failure on line 104 in FirebaseAI/Tests/Unit/ModelContentTests.swift
|
||||||
|
|
||||||
| let content = ModelContent(role: "model", parts: [codeExecutionResultPart]) | ||||||
|
|
||||||
| guard let resultPart = content.parts.first as? CodeExecutionResultPart else { | ||||||
| XCTFail("Expected CodeExecutionResultPart") | ||||||
| return | ||||||
| } | ||||||
| XCTAssertEqual(resultPart.outcome, .deadlineExceeded) | ||||||
|
Check failure on line 112 in FirebaseAI/Tests/Unit/ModelContentTests.swift
|
||||||
| XCTAssertNil(resultPart.output) | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable name
internalExecutableCodePartis a bit confusing as it refers to an instance of the publicExecutableCodeParttype. Renaming it toexecutableCodePartwould improve clarity.Additionally, the assertions on lines 86-87 verify the initial state of the part before it's processed by
ModelContent. Since the test's main purpose is to confirm thatModelContentpreserves these properties, these initial checks are redundant with the final assertions on lines 99-100. Removing them would make the test more focused on the behavior ofModelContentinitialization.