1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- import XCTest
16-
1715import FirebaseAI
18-
19- final class ServerPromptTemplateIntegrationTests : XCTestCase {
20- override func setUp( ) {
21- super. setUp ( )
22- continueAfterFailure = false
23- }
24-
25- func testGenerateContentWithText( ) async throws {
26- let model = FirebaseAI . firebaseAI ( backend: . vertexAI( location: " global " ) )
27- . templateGenerativeModel ( )
16+ import Testing
17+ #if canImport(UIKit)
18+ import UIKit
19+ #endif
20+
21+ struct ServerPromptTemplateIntegrationTests {
22+ private static let testConfigs : [ InstanceConfig ] = [
23+ . vertexAI_v1beta,
24+ . vertexAI_v1beta_global,
25+ ]
26+ private static let imageGenerationTestConfigs : [ InstanceConfig ] = [ . vertexAI_v1beta]
27+
28+ @Test ( arguments: [
29+ // The "greeting2" template is only available in the `global` location.
30+ InstanceConfig . vertexAI_v1beta_global,
31+ ] )
32+ func generateContentWithText( _ config: InstanceConfig ) async throws {
33+ let model = FirebaseAI . componentInstance ( config) . templateGenerativeModel ( )
2834 let userName = " paul "
2935 let response = try await model. generateContent (
3036 template: " greeting2 " ,
@@ -33,12 +39,13 @@ final class ServerPromptTemplateIntegrationTests: XCTestCase {
3339 " language " : " Spanish " ,
3440 ]
3541 )
36- let text = try XCTUnwrap ( response. text)
37- XCTAssert ( text. contains ( " Paul " ) )
42+ let text = try #require ( response. text)
43+ #expect ( text. contains ( " Paul " ) )
3844 }
3945
40- func testGenerateContentStream( ) async throws {
41- let model = FirebaseAI . firebaseAI ( backend: . vertexAI( ) ) . templateGenerativeModel ( )
46+ @Test ( arguments: testConfigs)
47+ func generateContentStream( _ config: InstanceConfig ) async throws {
48+ let model = FirebaseAI . componentInstance ( config) . templateGenerativeModel ( )
4249 let userName = " paul "
4350 let stream = try model. generateContentStream (
4451 template: " greeting.prompt " ,
@@ -53,25 +60,30 @@ final class ServerPromptTemplateIntegrationTests: XCTestCase {
5360 resultText += text
5461 }
5562 }
56- XCTAssert ( resultText. contains ( " Paul " ) )
63+ #expect ( resultText. contains ( " Paul " ) )
5764 }
5865
59- func testGenerateImages( ) async throws {
60- let imagenModel = FirebaseAI . firebaseAI ( backend: . vertexAI( ) ) . templateImagenModel ( )
66+ @Test ( arguments: imageGenerationTestConfigs)
67+ func generateImages( _ config: InstanceConfig ) async throws {
68+ let imagenModel = FirebaseAI . componentInstance ( config) . templateImagenModel ( )
6169 let imagenPrompt = " A cat picture "
6270 let response = try await imagenModel. generateImages (
6371 template: " generate_images.prompt " ,
6472 variables: [
6573 " prompt " : imagenPrompt,
6674 ]
6775 )
68- XCTAssertEqual ( response. images. count, 3 )
76+ #expect ( response. images. count == 3 )
6977 }
7078
71- func testGenerateContentWithMedia( ) async throws {
72- let model = FirebaseAI . firebaseAI ( backend: . vertexAI( ) ) . templateGenerativeModel ( )
73- let image = UIImage ( systemName: " photo " ) !
74- if let imageBytes = image. jpegData ( compressionQuality: 0.8 ) {
79+ #if canImport(UIKit)
80+ @Test ( arguments: testConfigs)
81+ func generateContentWithMedia( _ config: InstanceConfig ) async throws {
82+ let model = FirebaseAI . componentInstance ( config) . templateGenerativeModel ( )
83+ let image = UIImage ( systemName: " photo " ) !
84+ let imageBytes = try #require(
85+ image. jpegData ( compressionQuality: 0.8 ) , " Could not get image data. "
86+ )
7587 let base64Image = imageBytes. base64EncodedString ( )
7688
7789 let response = try await model. generateContent (
@@ -84,16 +96,19 @@ final class ServerPromptTemplateIntegrationTests: XCTestCase {
8496 ] ,
8597 ]
8698 )
87- XCTAssert ( response. text? . isEmpty == false )
88- } else {
89- XCTFail ( " Could not get image data. " )
99+ let text = try #require( response. text)
100+ #expect( !text. isEmpty)
90101 }
91- }
92-
93- func testGenerateContentStreamWithMedia( ) async throws {
94- let model = FirebaseAI . firebaseAI ( backend: . vertexAI( ) ) . templateGenerativeModel ( )
95- let image = UIImage ( systemName: " photo " ) !
96- if let imageBytes = image. jpegData ( compressionQuality: 0.8 ) {
102+ #endif // canImport(UIKit)
103+
104+ #if canImport(UIKit)
105+ @Test ( arguments: testConfigs)
106+ func generateContentStreamWithMedia( _ config: InstanceConfig ) async throws {
107+ let model = FirebaseAI . componentInstance ( config) . templateGenerativeModel ( )
108+ let image = UIImage ( systemName: " photo " ) !
109+ let imageBytes = try #require(
110+ image. jpegData ( compressionQuality: 0.8 ) , " Could not get image data. "
111+ )
97112 let base64Image = imageBytes. base64EncodedString ( )
98113
99114 let stream = try model. generateContentStream (
@@ -112,14 +127,13 @@ final class ServerPromptTemplateIntegrationTests: XCTestCase {
112127 resultText += text
113128 }
114129 }
115- XCTAssert ( resultText. isEmpty == false )
116- } else {
117- XCTFail ( " Could not get image data. " )
130+ #expect( !resultText. isEmpty)
118131 }
119- }
132+ #endif // canImport(UIKit)
120133
121- func testChat( ) async throws {
122- let model = FirebaseAI . firebaseAI ( backend: . vertexAI( ) ) . templateGenerativeModel ( )
134+ @Test ( arguments: testConfigs)
135+ func chat( _ config: InstanceConfig ) async throws {
136+ let model = FirebaseAI . componentInstance ( config) . templateGenerativeModel ( )
123137 let initialHistory = [
124138 ModelContent ( role: " user " , parts: " Hello! " ) ,
125139 ModelContent ( role: " model " , parts: " Hi there! How can I help? " ) ,
@@ -132,13 +146,16 @@ final class ServerPromptTemplateIntegrationTests: XCTestCase {
132146 userMessage,
133147 variables: [ " message " : userMessage]
134148 )
135- XCTAssert ( response. text? . isEmpty == false )
136- XCTAssertEqual ( chatSession. history. count, 4 )
137- XCTAssertEqual ( ( chatSession. history [ 2 ] . parts. first as? TextPart ) ? . text, userMessage)
149+ let text = try #require( response. text)
150+ #expect( !text. isEmpty)
151+ #expect( chatSession. history. count == 4 )
152+ let textPart = try #require( chatSession. history [ 2 ] . parts. first as? TextPart )
153+ #expect( textPart. text == userMessage)
138154 }
139155
140- func testChatStream( ) async throws {
141- let model = FirebaseAI . firebaseAI ( backend: . vertexAI( ) ) . templateGenerativeModel ( )
156+ @Test ( arguments: testConfigs)
157+ func chatStream( _ config: InstanceConfig ) async throws {
158+ let model = FirebaseAI . componentInstance ( config) . templateGenerativeModel ( )
142159 let initialHistory = [
143160 ModelContent ( role: " user " , parts: " Hello! " ) ,
144161 ModelContent ( role: " model " , parts: " Hi there! How can I help? " ) ,
@@ -157,8 +174,9 @@ final class ServerPromptTemplateIntegrationTests: XCTestCase {
157174 resultText += text
158175 }
159176 }
160- XCTAssert ( resultText. isEmpty == false )
161- XCTAssertEqual ( chatSession. history. count, 4 )
162- XCTAssertEqual ( ( chatSession. history [ 2 ] . parts. first as? TextPart ) ? . text, userMessage)
177+ #expect( !resultText. isEmpty)
178+ #expect( chatSession. history. count == 4 )
179+ let textPart = try #require( chatSession. history [ 2 ] . parts. first as? TextPart )
180+ #expect( textPart. text == userMessage)
163181 }
164182}
0 commit comments