VertexComponentTests.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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, watchOS 8.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. // MARK: Interoperability Tests
  33. /// Tests that the right number of components are being provided for the container.
  34. func testComponentsBeingRegistered() throws {
  35. let components = VertexAIComponent.componentsToRegister()
  36. XCTAssert(components.count == 1)
  37. }
  38. /// Tests that a vertex instance can be created properly by the VertexAIComponent.
  39. func testVertexInstanceCreation() throws {
  40. let app = try XCTUnwrap(VertexComponentTests.app)
  41. let component = VertexAIComponent(app: app)
  42. let vertex = component.vertexAI("my-location")
  43. XCTAssertNotNil(vertex)
  44. }
  45. /// Tests that the component container caches instances of VertexAIComponent.
  46. func testMultipleComponentInstancesCreated() throws {
  47. let registrants = NSMutableSet(array: [VertexAIComponent.self])
  48. let container = FirebaseComponentContainer(
  49. app: VertexComponentTests.app,
  50. registrants: registrants
  51. )
  52. let provider1 = ComponentType<VertexAIProvider>.instance(for: VertexAIProvider.self,
  53. in: container)
  54. XCTAssertNotNil(provider1)
  55. let provider2 = ComponentType<VertexAIProvider>.instance(for: VertexAIProvider.self,
  56. in: container)
  57. XCTAssertNotNil(provider2)
  58. // Ensure they're the same instance.
  59. XCTAssert(provider1 === provider2)
  60. }
  61. /// Tests that instances of vertex created are different.
  62. func testMultipleVertexInstancesCreated() throws {
  63. let app = try XCTUnwrap(VertexComponentTests.app)
  64. let registrants = NSMutableSet(array: [VertexAIComponent.self])
  65. let container = FirebaseComponentContainer(app: app, registrants: registrants)
  66. let provider = ComponentType<VertexAIProvider>.instance(for: VertexAIProvider.self,
  67. in: container)
  68. XCTAssertNotNil(provider)
  69. let vertex1 = provider?.vertexAI("randomLocation")
  70. let vertex2 = provider?.vertexAI("randomLocation")
  71. XCTAssertNotNil(vertex1)
  72. // Ensure they're the same instance.
  73. XCTAssert(vertex1 === vertex2)
  74. let vertex3 = provider?.vertexAI("differentLocation")
  75. XCTAssertNotNil(vertex3)
  76. XCTAssert(vertex1 !== vertex3)
  77. }
  78. /// Test that vertex instances get deallocated.
  79. func testVertexLifecycle() throws {
  80. weak var weakApp: FirebaseApp?
  81. weak var weakVertex: VertexAI?
  82. try autoreleasepool {
  83. let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
  84. gcmSenderID: "00000000000000000-00000000000-000000000")
  85. options.projectID = "myProjectID"
  86. let app1 = FirebaseApp(instanceWithName: "transitory app", options: options)
  87. weakApp = try XCTUnwrap(app1)
  88. let vertex = VertexAI(app: app1, location: "transitory location")
  89. weakVertex = vertex
  90. XCTAssertNotNil(weakVertex)
  91. }
  92. XCTAssertNil(weakApp)
  93. XCTAssertNil(weakVertex)
  94. }
  95. }