| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- // 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.
- @preconcurrency import FirebaseCore
- internal import FirebaseCoreExtension
- import Foundation
- import XCTest
- @testable import FirebaseAI
- @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
- class VertexComponentTests: XCTestCase {
- static let projectID = "test-project-id"
- static let apiKey = "test-api-key"
- static let options = {
- let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
- gcmSenderID: "00000000000000000-00000000000-000000000")
- options.projectID = VertexComponentTests.projectID
- options.apiKey = VertexComponentTests.apiKey
- return options
- }()
- static let app = FirebaseApp(instanceWithName: "test", options: options)
- let location = "test-location"
- let modelName = "test-model-name"
- let systemInstruction = ModelContent(role: "system", parts: "test-system-instruction-prompt")
- override class func setUp() {
- FirebaseApp.configure(options: options)
- guard FirebaseApp.app() != nil else {
- fatalError("The default app does not exist.")
- }
- }
- /// Test that the objc class is available for the component system to update the user agent.
- func testComponentsBeingRegistered() throws {
- XCTAssertNotNil(NSClassFromString("FIRVertexAIComponent"))
- }
- /// Tests that a vertex instance can be created properly using the default Firebase app.
- func testVertexInstanceCreation_defaultApp() throws {
- let vertex = FirebaseAI.firebaseAI(backend: .vertexAI())
- XCTAssertNotNil(vertex)
- XCTAssertEqual(vertex.firebaseInfo.projectID, VertexComponentTests.projectID)
- XCTAssertEqual(vertex.firebaseInfo.apiKey, VertexComponentTests.apiKey)
- XCTAssertEqual(vertex.location, "us-central1")
- XCTAssertEqual(vertex.apiConfig.service, .vertexAI(endpoint: .firebaseProxyProd))
- XCTAssertEqual(vertex.apiConfig.service.endpoint, .firebaseProxyProd)
- XCTAssertEqual(vertex.apiConfig.version, .v1beta)
- }
- /// Tests that a vertex instance can be created properly using the default Firebase app and custom
- /// location.
- func testVertexInstanceCreation_defaultApp_customLocation() throws {
- let vertex = FirebaseAI.firebaseAI(backend: .vertexAI(location: location))
- XCTAssertNotNil(vertex)
- XCTAssertEqual(vertex.firebaseInfo.projectID, VertexComponentTests.projectID)
- XCTAssertEqual(vertex.firebaseInfo.apiKey, VertexComponentTests.apiKey)
- XCTAssertEqual(vertex.location, location)
- XCTAssertEqual(vertex.apiConfig.service, .vertexAI(endpoint: .firebaseProxyProd))
- XCTAssertEqual(vertex.apiConfig.service.endpoint, .firebaseProxyProd)
- XCTAssertEqual(vertex.apiConfig.version, .v1beta)
- }
- /// Tests that a vertex instance can be created properly.
- func testVertexInstanceCreation_customApp() throws {
- let vertex = FirebaseAI.firebaseAI(
- app: VertexComponentTests.app,
- backend: .vertexAI(location: location)
- )
- XCTAssertNotNil(vertex)
- XCTAssertEqual(vertex.firebaseInfo.projectID, VertexComponentTests.projectID)
- XCTAssertEqual(vertex.firebaseInfo.apiKey, VertexComponentTests.apiKey)
- XCTAssertEqual(vertex.location, location)
- XCTAssertEqual(vertex.apiConfig.service, .vertexAI(endpoint: .firebaseProxyProd))
- XCTAssertEqual(vertex.apiConfig.service.endpoint, .firebaseProxyProd)
- XCTAssertEqual(vertex.apiConfig.version, .v1beta)
- }
- /// Tests that Vertex instances are reused properly.
- func testSameAppAndLocation_instanceReused() throws {
- let app = try XCTUnwrap(VertexComponentTests.app)
- let vertex1 = FirebaseAI.firebaseAI(app: app, backend: .vertexAI(location: location))
- let vertex2 = FirebaseAI.firebaseAI(app: app, backend: .vertexAI(location: location))
- // Ensure they're the same instance.
- XCTAssert(vertex1 === vertex2)
- }
- func testSameAppAndDifferentLocation_newInstanceCreated() throws {
- let vertex1 = FirebaseAI.firebaseAI(
- app: VertexComponentTests.app,
- backend: .vertexAI(location: location)
- )
- let vertex2 = FirebaseAI.firebaseAI(
- app: VertexComponentTests.app,
- backend: .vertexAI(location: "differentLocation")
- )
- // Ensure they are different instances.
- XCTAssert(vertex1 !== vertex2)
- }
- func testDifferentAppAndSameLocation_newInstanceCreated() throws {
- FirebaseApp.configure(name: "test-2", options: VertexComponentTests.options)
- let app2 = FirebaseApp(instanceWithName: "test-2", options: VertexComponentTests.options)
- addTeardownBlock { await app2.delete() }
- let vertex1 = FirebaseAI.firebaseAI(
- app: VertexComponentTests.app,
- backend: .vertexAI(location: location)
- )
- let vertex2 = FirebaseAI.firebaseAI(app: app2, backend: .vertexAI(location: location))
- XCTAssert(VertexComponentTests.app != app2)
- XCTAssert(vertex1 !== vertex2) // Ensure they are different instances.
- }
- func testDifferentAppAndDifferentLocation_newInstanceCreated() throws {
- FirebaseApp.configure(name: "test-2", options: VertexComponentTests.options)
- let app2 = FirebaseApp(instanceWithName: "test-2", options: VertexComponentTests.options)
- addTeardownBlock { await app2.delete() }
- let vertex1 = FirebaseAI.firebaseAI(
- app: VertexComponentTests.app,
- backend: .vertexAI(location: location)
- )
- let vertex2 = FirebaseAI.firebaseAI(
- app: app2,
- backend: .vertexAI(location: "differentLocation")
- )
- XCTAssert(VertexComponentTests.app != app2)
- XCTAssert(vertex1 !== vertex2) // Ensure they are different instances.
- }
- func testSameAppAndDifferentAPI_newInstanceCreated() throws {
- let vertex1 = FirebaseAI.createInstance(
- app: VertexComponentTests.app,
- location: location,
- apiConfig: APIConfig(service: .vertexAI(endpoint: .firebaseProxyProd), version: .v1beta),
- useLimitedUseAppCheckTokens: false
- )
- let vertex2 = FirebaseAI.createInstance(
- app: VertexComponentTests.app,
- location: location,
- apiConfig: APIConfig(service: .vertexAI(endpoint: .firebaseProxyProd), version: .v1),
- useLimitedUseAppCheckTokens: false
- )
- // Ensure they are different instances.
- XCTAssert(vertex1 !== vertex2)
- }
- /// Test that vertex instances get deallocated.
- func testVertexLifecycle() throws {
- weak var weakApp: FirebaseApp?
- weak var weakVertex: FirebaseAI?
- try autoreleasepool {
- let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
- gcmSenderID: "00000000000000000-00000000000-000000000")
- options.projectID = VertexComponentTests.projectID
- options.apiKey = VertexComponentTests.apiKey
- let app1 = FirebaseApp(instanceWithName: "transitory app", options: options)
- weakApp = try XCTUnwrap(app1)
- let vertex = FirebaseAI(
- app: app1,
- location: "transitory location",
- apiConfig: FirebaseAI.defaultVertexAIAPIConfig,
- useLimitedUseAppCheckTokens: false
- )
- weakVertex = vertex
- XCTAssertNotNil(weakVertex)
- }
- XCTAssertNil(weakApp)
- XCTAssertNil(weakVertex)
- }
- func testModelResourceName_vertexAI() throws {
- let app = try XCTUnwrap(VertexComponentTests.app)
- let vertex = FirebaseAI.firebaseAI(app: app, backend: .vertexAI(location: location))
- let model = "test-model-name"
- let projectID = vertex.firebaseInfo.projectID
- let modelResourceName = vertex.modelResourceName(modelName: model)
- let location = try XCTUnwrap(vertex.location)
- XCTAssertEqual(
- modelResourceName,
- "projects/\(projectID)/locations/\(location)/publishers/google/models/\(model)"
- )
- }
- func testModelResourceName_developerAPI_generativeLanguage() throws {
- let app = try XCTUnwrap(VertexComponentTests.app)
- let apiConfig = APIConfig(service: .googleAI(endpoint: .googleAIBypassProxy), version: .v1beta)
- let vertex = FirebaseAI.createInstance(
- app: app,
- location: nil,
- apiConfig: apiConfig,
- useLimitedUseAppCheckTokens: false
- )
- let model = "test-model-name"
- let modelResourceName = vertex.modelResourceName(modelName: model)
- XCTAssertEqual(modelResourceName, "models/\(model)")
- }
- func testModelResourceName_developerAPI_firebaseVertexAI() throws {
- let app = try XCTUnwrap(VertexComponentTests.app)
- let apiConfig = APIConfig(
- service: .googleAI(endpoint: .firebaseProxyStaging),
- version: .v1beta
- )
- let vertex = FirebaseAI.createInstance(
- app: app,
- location: nil,
- apiConfig: apiConfig,
- useLimitedUseAppCheckTokens: false
- )
- let model = "test-model-name"
- let projectID = vertex.firebaseInfo.projectID
- let modelResourceName = vertex.modelResourceName(modelName: model)
- XCTAssertEqual(modelResourceName, "projects/\(projectID)/models/\(model)")
- }
- func testGenerativeModel_vertexAI() async throws {
- let app = try XCTUnwrap(VertexComponentTests.app)
- let vertex = FirebaseAI.firebaseAI(app: app, backend: .vertexAI(location: location))
- let modelResourceName = vertex.modelResourceName(modelName: modelName)
- let expectedSystemInstruction = ModelContent(role: nil, parts: systemInstruction.parts)
- let generativeModel = vertex.generativeModel(
- modelName: modelName,
- systemInstruction: systemInstruction
- )
- XCTAssertEqual(generativeModel.modelResourceName, modelResourceName)
- XCTAssertEqual(generativeModel.systemInstruction, expectedSystemInstruction)
- XCTAssertEqual(generativeModel.apiConfig, FirebaseAI.defaultVertexAIAPIConfig)
- }
- func testGenerativeModel_developerAPI() async throws {
- let app = try XCTUnwrap(VertexComponentTests.app)
- let apiConfig = APIConfig(
- service: .googleAI(endpoint: .firebaseProxyStaging),
- version: .v1beta
- )
- let vertex = FirebaseAI.createInstance(
- app: app,
- location: nil,
- apiConfig: apiConfig,
- useLimitedUseAppCheckTokens: false
- )
- let modelResourceName = vertex.modelResourceName(modelName: modelName)
- let expectedSystemInstruction = ModelContent(role: nil, parts: systemInstruction.parts)
- let generativeModel = vertex.generativeModel(
- modelName: modelName,
- systemInstruction: systemInstruction
- )
- XCTAssertEqual(generativeModel.modelResourceName, modelResourceName)
- XCTAssertEqual(generativeModel.systemInstruction, expectedSystemInstruction)
- XCTAssertEqual(generativeModel.apiConfig, apiConfig)
- }
- }
|