TemplateChatSessionTests.swift 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2025 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. @testable import FirebaseAI
  15. import FirebaseCore
  16. import XCTest
  17. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  18. final class TemplateChatSessionTests: XCTestCase {
  19. var model: TemplateGenerativeModel!
  20. override func setUp() {
  21. super.setUp()
  22. let firebaseInfo = GenerativeModelTestUtil.testFirebaseInfo()
  23. let generativeAIService = GenerativeAIService(
  24. firebaseInfo: firebaseInfo,
  25. urlSession: GenAIURLSession.default
  26. )
  27. let apiConfig = APIConfig(service: .googleAI(endpoint: .firebaseProxyProd), version: .v1beta)
  28. model = TemplateGenerativeModel(generativeAIService: generativeAIService, apiConfig: apiConfig)
  29. }
  30. func testSendMessage() async throws {
  31. let chat = model.startChat(template: "test-template")
  32. let response = try await chat.sendMessage("Hello", variables: ["name": "test"])
  33. XCTAssertEqual(chat.history.count, 2)
  34. XCTAssertEqual(chat.history[0].role, "user")
  35. XCTAssertEqual((chat.history[0].parts.first as? TextPart)?.text, "Hello")
  36. XCTAssertEqual(chat.history[1].role, "model")
  37. XCTAssertEqual(response.candidates.count, 1)
  38. }
  39. }