VertexComponentTests.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. import Foundation
  15. import FirebaseCore
  16. @testable import FirebaseVertexAI
  17. import SharedTestUtilities
  18. import XCTest
  19. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, *)
  20. class VertexComponentTests: XCTestCase {
  21. static var app: FirebaseApp!
  22. override class func setUp() {
  23. super.setUp()
  24. if app == nil {
  25. let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
  26. gcmSenderID: "00000000000000000-00000000000-000000000")
  27. options.projectID = "myProjectID"
  28. FirebaseApp.configure(options: options)
  29. app = FirebaseApp(instanceWithName: "test", options: options)
  30. }
  31. }
  32. /// Test that the objc class is available for the component system to update the user agent.
  33. func testComponentsBeingRegistered() throws {
  34. XCTAssertNotNil(NSClassFromString("FIRVertexAIComponent"))
  35. }
  36. /// Tests that a vertex instance can be created properly.
  37. func testVertexInstanceCreation() throws {
  38. let app = try XCTUnwrap(VertexComponentTests.app)
  39. let vertex = VertexAI.vertexAI(app: app, location: "my-location")
  40. XCTAssertNotNil(vertex)
  41. }
  42. /// Tests that a vertex instances are reused properly.
  43. func testMultipleComponentInstancesCreated() throws {
  44. let app = try XCTUnwrap(VertexComponentTests.app)
  45. let vertex1 = VertexAI.vertexAI(app: app, location: "my-location")
  46. let vertex2 = VertexAI.vertexAI(app: app, location: "my-location")
  47. // Ensure they're the same instance.
  48. XCTAssert(vertex1 === vertex2)
  49. let vertex3 = VertexAI.vertexAI(app: app, location: "differentLocation")
  50. XCTAssert(vertex1 !== vertex3)
  51. }
  52. /// Test that vertex instances get deallocated.
  53. func testVertexLifecycle() throws {
  54. weak var weakApp: FirebaseApp?
  55. weak var weakVertex: VertexAI?
  56. try autoreleasepool {
  57. let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
  58. gcmSenderID: "00000000000000000-00000000000-000000000")
  59. options.projectID = "myProjectID"
  60. let app1 = FirebaseApp(instanceWithName: "transitory app", options: options)
  61. weakApp = try XCTUnwrap(app1)
  62. let vertex = VertexAI(app: app1, location: "transitory location")
  63. weakVertex = vertex
  64. XCTAssertNotNil(weakVertex)
  65. }
  66. XCTAssertNil(weakApp)
  67. XCTAssertNil(weakVertex)
  68. }
  69. }