TemplateChatSessionTests.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 FirebaseAILogic
  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. var urlSession: URLSession!
  21. override func setUp() {
  22. super.setUp()
  23. let configuration = URLSessionConfiguration.default
  24. configuration.protocolClasses = [MockURLProtocol.self]
  25. urlSession = URLSession(configuration: configuration)
  26. let firebaseInfo = GenerativeModelTestUtil.testFirebaseInfo()
  27. let generativeAIService = GenerativeAIService(
  28. firebaseInfo: firebaseInfo,
  29. urlSession: urlSession
  30. )
  31. let apiConfig = APIConfig(service: .googleAI(endpoint: .firebaseProxyProd), version: .v1beta)
  32. model = TemplateGenerativeModel(generativeAIService: generativeAIService, apiConfig: apiConfig)
  33. }
  34. func testSendMessage() async throws {
  35. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  36. forResource: "unary-success-basic-reply-short",
  37. withExtension: "json",
  38. subdirectory: "mock-responses/googleai",
  39. isTemplateRequest: true
  40. )
  41. let chat = model.startChat(templateID: "test-template")
  42. let response = try await chat.sendMessage("Hello", inputs: ["name": "test"])
  43. XCTAssertEqual(chat.history.count, 2)
  44. XCTAssertEqual(chat.history[0].role, "user")
  45. XCTAssertEqual((chat.history[0].parts.first as? TextPart)?.text, "Hello")
  46. XCTAssertEqual(chat.history[1].role, "model")
  47. XCTAssertEqual(
  48. (chat.history[1].parts.first as? TextPart)?.text,
  49. "Google's headquarters, also known as the Googleplex, is located in **Mountain View, California**.\n"
  50. )
  51. XCTAssertEqual(response.candidates.count, 1)
  52. }
  53. func testSendMessageStream() async throws {
  54. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  55. forResource: "streaming-success-basic-reply-short",
  56. withExtension: "txt",
  57. subdirectory: "mock-responses/googleai",
  58. isTemplateRequest: true
  59. )
  60. let chat = model.startChat(templateID: "test-template")
  61. let stream = try chat.sendMessageStream("Hello", inputs: ["name": "test"])
  62. let content = try await GenerativeModelTestUtil.collectTextFromStream(stream)
  63. XCTAssertEqual(content, "The capital of Wyoming is **Cheyenne**.\n")
  64. XCTAssertEqual(chat.history.count, 2)
  65. XCTAssertEqual(chat.history[0].role, "user")
  66. XCTAssertEqual((chat.history[0].parts.first as? TextPart)?.text, "Hello")
  67. XCTAssertEqual(chat.history[1].role, "model")
  68. }
  69. func testSendMessageWithModelContent() async throws {
  70. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  71. forResource: "unary-success-basic-reply-short",
  72. withExtension: "json",
  73. subdirectory: "mock-responses/googleai",
  74. isTemplateRequest: true
  75. )
  76. let chat = model.startChat(templateID: "test-template")
  77. let response = try await chat.sendMessage(
  78. [ModelContent(parts: [TextPart("Hello")])],
  79. inputs: ["name": "test"]
  80. )
  81. XCTAssertEqual(chat.history.count, 2)
  82. XCTAssertEqual(chat.history[0].role, "user")
  83. XCTAssertEqual((chat.history[0].parts.first as? TextPart)?.text, "Hello")
  84. XCTAssertEqual(chat.history[1].role, "model")
  85. XCTAssertEqual(
  86. (chat.history[1].parts.first as? TextPart)?.text,
  87. "Google's headquarters, also known as the Googleplex, is located in **Mountain View, California**.\n"
  88. )
  89. XCTAssertEqual(response.candidates.count, 1)
  90. }
  91. func testSendMessageStreamWithModelContent() async throws {
  92. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  93. forResource: "streaming-success-basic-reply-short",
  94. withExtension: "txt",
  95. subdirectory: "mock-responses/googleai",
  96. isTemplateRequest: true
  97. )
  98. let chat = model.startChat(templateID: "test-template")
  99. let stream = try chat.sendMessageStream(
  100. [ModelContent(parts: [TextPart("Hello")])],
  101. inputs: ["name": "test"]
  102. )
  103. let content = try await GenerativeModelTestUtil.collectTextFromStream(stream)
  104. XCTAssertEqual(content, "The capital of Wyoming is **Cheyenne**.\n")
  105. XCTAssertEqual(chat.history.count, 2)
  106. XCTAssertEqual(chat.history[0].role, "user")
  107. XCTAssertEqual((chat.history[0].parts.first as? TextPart)?.text, "Hello")
  108. XCTAssertEqual(chat.history[1].role, "model")
  109. }
  110. }