VertexComponentTests.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 FirebaseCore
  15. import Foundation
  16. import XCTest
  17. @_implementationOnly import FirebaseCoreExtension
  18. @testable import FirebaseVertexAI
  19. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  20. class VertexComponentTests: XCTestCase {
  21. static let projectID = "test-project-id"
  22. static let apiKey = "test-api-key"
  23. static let options = {
  24. let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
  25. gcmSenderID: "00000000000000000-00000000000-000000000")
  26. options.projectID = VertexComponentTests.projectID
  27. options.apiKey = VertexComponentTests.apiKey
  28. return options
  29. }()
  30. static let app = {
  31. FirebaseApp.configure(options: options)
  32. return FirebaseApp(instanceWithName: "test", options: options)
  33. }()
  34. let location = "test-location"
  35. /// Test that the objc class is available for the component system to update the user agent.
  36. func testComponentsBeingRegistered() throws {
  37. XCTAssertNotNil(NSClassFromString("FIRVertexAIComponent"))
  38. }
  39. /// Tests that a vertex instance can be created properly.
  40. func testVertexInstanceCreation() throws {
  41. let vertex = VertexAI.vertexAI(app: VertexComponentTests.app, location: location)
  42. XCTAssertNotNil(vertex)
  43. XCTAssertEqual(vertex.projectID, VertexComponentTests.projectID)
  44. XCTAssertEqual(vertex.apiKey, VertexComponentTests.apiKey)
  45. XCTAssertEqual(vertex.location, location)
  46. }
  47. /// Tests that Vertex instances are reused properly.
  48. func testSameAppAndLocation_instanceReused() throws {
  49. let app = try XCTUnwrap(VertexComponentTests.app)
  50. let vertex1 = VertexAI.vertexAI(app: app, location: location)
  51. let vertex2 = VertexAI.vertexAI(app: app, location: location)
  52. // Ensure they're the same instance.
  53. XCTAssert(vertex1 === vertex2)
  54. }
  55. func testSameAppAndDifferentLocation_newInstanceCreated() throws {
  56. let vertex1 = VertexAI.vertexAI(app: VertexComponentTests.app, location: location)
  57. let vertex2 = VertexAI.vertexAI(app: VertexComponentTests.app, location: "differentLocation")
  58. // Ensure they are different instances.
  59. XCTAssert(vertex1 !== vertex2)
  60. }
  61. func testDifferentAppAndSameLocation_newInstanceCreated() throws {
  62. FirebaseApp.configure(name: "test-2", options: VertexComponentTests.options)
  63. let app2 = FirebaseApp(instanceWithName: "test-2", options: VertexComponentTests.options)
  64. addTeardownBlock { await app2.delete() }
  65. let vertex1 = VertexAI.vertexAI(app: VertexComponentTests.app, location: location)
  66. let vertex2 = VertexAI.vertexAI(app: app2, location: location)
  67. XCTAssert(VertexComponentTests.app != app2)
  68. XCTAssert(vertex1 !== vertex2) // Ensure they are different instances.
  69. }
  70. func testDifferentAppAndDifferentLocation_newInstanceCreated() throws {
  71. FirebaseApp.configure(name: "test-2", options: VertexComponentTests.options)
  72. let app2 = FirebaseApp(instanceWithName: "test-2", options: VertexComponentTests.options)
  73. addTeardownBlock { await app2.delete() }
  74. let vertex1 = VertexAI.vertexAI(app: VertexComponentTests.app, location: location)
  75. let vertex2 = VertexAI.vertexAI(app: app2, location: "differentLocation")
  76. XCTAssert(VertexComponentTests.app != app2)
  77. XCTAssert(vertex1 !== vertex2) // Ensure they are different instances.
  78. }
  79. /// Test that vertex instances get deallocated.
  80. func testVertexLifecycle() throws {
  81. weak var weakApp: FirebaseApp?
  82. weak var weakVertex: VertexAI?
  83. try autoreleasepool {
  84. let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
  85. gcmSenderID: "00000000000000000-00000000000-000000000")
  86. options.projectID = VertexComponentTests.projectID
  87. options.apiKey = VertexComponentTests.apiKey
  88. let app1 = FirebaseApp(instanceWithName: "transitory app", options: options)
  89. weakApp = try XCTUnwrap(app1)
  90. let vertex = VertexAI(app: app1, location: "transitory location")
  91. weakVertex = vertex
  92. XCTAssertNotNil(weakVertex)
  93. }
  94. XCTAssertNil(weakApp)
  95. XCTAssertNil(weakVertex)
  96. }
  97. func testModelResourceName() throws {
  98. let app = try XCTUnwrap(VertexComponentTests.app)
  99. let vertex = VertexAI.vertexAI(app: app, location: location)
  100. let model = "test-model-name"
  101. let modelResourceName = vertex.modelResourceName(modelName: model)
  102. XCTAssertEqual(
  103. modelResourceName,
  104. "projects/\(vertex.projectID)/locations/\(vertex.location)/publishers/google/models/\(model)"
  105. )
  106. }
  107. func testGenerativeModel() async throws {
  108. let app = try XCTUnwrap(VertexComponentTests.app)
  109. let vertex = VertexAI.vertexAI(app: app, location: location)
  110. let modelName = "test-model-name"
  111. let modelResourceName = vertex.modelResourceName(modelName: modelName)
  112. let systemInstruction = ModelContent(role: "system", parts: "test-system-instruction-prompt")
  113. let generativeModel = vertex.generativeModel(
  114. modelName: modelName,
  115. systemInstruction: systemInstruction
  116. )
  117. XCTAssertEqual(generativeModel.modelResourceName, modelResourceName)
  118. XCTAssertEqual(generativeModel.systemInstruction, systemInstruction)
  119. }
  120. }