|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import Foundation |
| 16 | + |
| 17 | +import FirebaseCore |
| 18 | +@testable import FirebaseVertexAI |
| 19 | + |
| 20 | +import SharedTestUtilities |
| 21 | + |
| 22 | +import XCTest |
| 23 | + |
| 24 | +@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *) |
| 25 | +class VertexComponentTests: XCTestCase { |
| 26 | + static var app: FirebaseApp! |
| 27 | + |
| 28 | + override class func setUp() { |
| 29 | + super.setUp() |
| 30 | + if app == nil { |
| 31 | + let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000", |
| 32 | + gcmSenderID: "00000000000000000-00000000000-000000000") |
| 33 | + options.projectID = "myProjectID" |
| 34 | + FirebaseApp.configure(options: options) |
| 35 | + app = FirebaseApp(instanceWithName: "test", options: options) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /// Test that the objc class is available for the component system to update the user agent. |
| 40 | + func testComponentsBeingRegistered() throws { |
| 41 | + XCTAssertNotNil(NSClassFromString("FIRVertexAIComponent")) |
| 42 | + } |
| 43 | + |
| 44 | + /// Tests that a vertex instance can be created properly. |
| 45 | + func testVertexInstanceCreation() throws { |
| 46 | + let app = try XCTUnwrap(VertexComponentTests.app) |
| 47 | + let vertex = VertexAI.vertexAI(app: app, location: "my-location") |
| 48 | + XCTAssertNotNil(vertex) |
| 49 | + } |
| 50 | + |
| 51 | + /// Tests that a vertex instances are reused properly. |
| 52 | + func testMultipleComponentInstancesCreated() throws { |
| 53 | + let app = try XCTUnwrap(VertexComponentTests.app) |
| 54 | + let vertex1 = VertexAI.vertexAI(app: app, location: "my-location") |
| 55 | + let vertex2 = VertexAI.vertexAI(app: app, location: "my-location") |
| 56 | + |
| 57 | + // Ensure they're the same instance. |
| 58 | + XCTAssert(vertex1 === vertex2) |
| 59 | + |
| 60 | + let vertex3 = VertexAI.vertexAI(app: app, location: "differentLocation") |
| 61 | + XCTAssert(vertex1 !== vertex3) |
| 62 | + } |
| 63 | + |
| 64 | + /// Test that vertex instances get deallocated. |
| 65 | + func testVertexLifecycle() throws { |
| 66 | + weak var weakApp: FirebaseApp? |
| 67 | + weak var weakVertex: VertexAI? |
| 68 | + try autoreleasepool { |
| 69 | + let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000", |
| 70 | + gcmSenderID: "00000000000000000-00000000000-000000000") |
| 71 | + options.projectID = "myProjectID" |
| 72 | + let app1 = FirebaseApp(instanceWithName: "transitory app", options: options) |
| 73 | + weakApp = try XCTUnwrap(app1) |
| 74 | + let vertex = VertexAI(app: app1, location: "transitory location") |
| 75 | + weakVertex = vertex |
| 76 | + XCTAssertNotNil(weakVertex) |
| 77 | + } |
| 78 | + XCTAssertNil(weakApp) |
| 79 | + XCTAssertNil(weakVertex) |
| 80 | + } |
| 81 | +} |
0 commit comments