ChatTests.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2023 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 XCTest
  16. @testable import FirebaseVertexAI
  17. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  18. final class ChatTests: XCTestCase {
  19. var urlSession: URLSession!
  20. override func setUp() {
  21. let configuration = URLSessionConfiguration.default
  22. configuration.protocolClasses = [MockURLProtocol.self]
  23. urlSession = URLSession(configuration: configuration)
  24. }
  25. override func tearDown() {
  26. MockURLProtocol.requestHandler = nil
  27. }
  28. func testMergingText() async throws {
  29. let bundle = BundleTestUtil.bundle()
  30. let fileURL = try XCTUnwrap(bundle.url(
  31. forResource: "streaming-success-basic-reply-parts",
  32. withExtension: "txt"
  33. ))
  34. // Skip tests using MockURLProtocol on watchOS; unsupported in watchOS 2 and later, see
  35. // https://developer.apple.com/documentation/foundation/urlprotocol for details.
  36. #if os(watchOS)
  37. throw XCTSkip("Custom URL protocols are unsupported in watchOS 2 and later.")
  38. #endif // os(watchOS)
  39. MockURLProtocol.requestHandler = { request in
  40. let response = HTTPURLResponse(
  41. url: request.url!,
  42. statusCode: 200,
  43. httpVersion: nil,
  44. headerFields: nil
  45. )!
  46. return (response, fileURL.lines)
  47. }
  48. let model = GenerativeModel(
  49. name: "my-model",
  50. projectID: "my-project-id",
  51. apiKey: "API_KEY",
  52. tools: nil,
  53. requestOptions: RequestOptions(),
  54. appCheck: nil,
  55. auth: nil,
  56. urlSession: urlSession
  57. )
  58. let chat = Chat(model: model, history: [])
  59. let input = "Test input"
  60. let stream = try chat.sendMessageStream(input)
  61. // Ensure the values are parsed correctly
  62. for try await value in stream {
  63. XCTAssertNotNil(value.text)
  64. }
  65. XCTAssertEqual(chat.history.count, 2)
  66. let part = try XCTUnwrap(chat.history[0].parts[0])
  67. let textPart = try XCTUnwrap(part as? TextPart)
  68. XCTAssertEqual(textPart.text, input)
  69. let finalText = "1 2 3 4 5 6 7 8"
  70. let assembledExpectation = ModelContent(role: "model", parts: finalText)
  71. XCTAssertEqual(chat.history[1], assembledExpectation)
  72. }
  73. }