VertexComponentTests.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 11.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 var app: FirebaseApp?
  24. let location = "test-location"
  25. override class func setUp() {
  26. super.setUp()
  27. if app == nil {
  28. let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
  29. gcmSenderID: "00000000000000000-00000000000-000000000")
  30. options.projectID = VertexComponentTests.projectID
  31. options.apiKey = VertexComponentTests.apiKey
  32. FirebaseApp.configure(options: options)
  33. app = FirebaseApp(instanceWithName: "test", options: options)
  34. }
  35. }
  36. /// Test that the objc class is available for the component system to update the user agent.
  37. func testComponentsBeingRegistered() throws {
  38. XCTAssertNotNil(NSClassFromString("FIRVertexAIComponent"))
  39. }
  40. /// Tests that a vertex instance can be created properly.
  41. func testVertexInstanceCreation() throws {
  42. let app = try XCTUnwrap(VertexComponentTests.app)
  43. let vertex = VertexAI.vertexAI(app: app, location: location)
  44. XCTAssertNotNil(vertex)
  45. XCTAssertEqual(vertex.projectID, VertexComponentTests.projectID)
  46. XCTAssertEqual(vertex.apiKey, VertexComponentTests.apiKey)
  47. XCTAssertEqual(vertex.location, location)
  48. }
  49. /// Tests that a vertex instances are reused properly.
  50. func testMultipleComponentInstancesCreated() throws {
  51. let app = try XCTUnwrap(VertexComponentTests.app)
  52. let vertex1 = VertexAI.vertexAI(app: app, location: location)
  53. let vertex2 = VertexAI.vertexAI(app: app, location: location)
  54. // Ensure they're the same instance.
  55. XCTAssert(vertex1 === vertex2)
  56. let vertex3 = VertexAI.vertexAI(app: app, location: "differentLocation")
  57. XCTAssert(vertex1 !== vertex3)
  58. }
  59. /// Test that vertex instances get deallocated.
  60. func testVertexLifecycle() throws {
  61. weak var weakApp: FirebaseApp?
  62. weak var weakVertex: VertexAI?
  63. try autoreleasepool {
  64. let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
  65. gcmSenderID: "00000000000000000-00000000000-000000000")
  66. options.projectID = VertexComponentTests.projectID
  67. options.apiKey = VertexComponentTests.apiKey
  68. let app1 = FirebaseApp(instanceWithName: "transitory app", options: options)
  69. weakApp = try XCTUnwrap(app1)
  70. let vertex = VertexAI(app: app1, location: "transitory location")
  71. weakVertex = vertex
  72. XCTAssertNotNil(weakVertex)
  73. }
  74. XCTAssertNil(weakApp)
  75. XCTAssertNil(weakVertex)
  76. }
  77. func testModelResourceName() throws {
  78. let app = try XCTUnwrap(VertexComponentTests.app)
  79. let vertex = VertexAI.vertexAI(app: app, location: location)
  80. let model = "test-model-name"
  81. let modelResourceName = vertex.modelResourceName(modelName: model)
  82. XCTAssertEqual(
  83. modelResourceName,
  84. "projects/\(vertex.projectID)/locations/\(vertex.location)/publishers/google/models/\(model)"
  85. )
  86. }
  87. func testGenerativeModel() async throws {
  88. let app = try XCTUnwrap(VertexComponentTests.app)
  89. let vertex = VertexAI.vertexAI(app: app, location: location)
  90. let modelName = "test-model-name"
  91. let modelResourceName = vertex.modelResourceName(modelName: modelName)
  92. let systemInstruction = ModelContent(role: "system", parts: "test-system-instruction-prompt")
  93. let generativeModel = vertex.generativeModel(
  94. modelName: modelName,
  95. systemInstruction: systemInstruction
  96. )
  97. XCTAssertEqual(generativeModel.modelResourceName, modelResourceName)
  98. XCTAssertEqual(generativeModel.systemInstruction, systemInstruction)
  99. }
  100. }