-
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 3 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,100 @@ | ||||||
| // Copyright 2024 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 executableCodePart = ExecutableCodePart(language: .python, code: "print('test')") | ||||||
|
|
||||||
| let content = ModelContent(role: "model", parts: [executableCodePart]) | ||||||
|
|
||||||
| 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, executableCodePart.language) | ||||||
| XCTAssertEqual(resultPart.code, executableCodePart.code) | ||||||
| } | ||||||
18-RAJAT marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| 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 89 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 97 in FirebaseAI/Tests/Unit/ModelContentTests.swift
|
||||||
| XCTAssertNil(resultPart.output) | ||||||
| } | ||||||
| } | ||||||
18-RAJAT marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
Uh oh!
There was an error while loading. Please reload this page.