ChatTests.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #if SWIFT_PACKAGE
  30. let bundle = Bundle.module
  31. #else // SWIFT_PACKAGE
  32. let bundle = Bundle(for: Self.self)
  33. #endif // SWIFT_PACKAGE
  34. let fileURL = try XCTUnwrap(bundle.url(
  35. forResource: "streaming-success-basic-reply-parts",
  36. withExtension: "txt"
  37. ))
  38. // Skip tests using MockURLProtocol on watchOS; unsupported in watchOS 2 and later, see
  39. // https://developer.apple.com/documentation/foundation/urlprotocol for details.
  40. #if os(watchOS)
  41. throw XCTSkip("Custom URL protocols are unsupported in watchOS 2 and later.")
  42. #endif // os(watchOS)
  43. MockURLProtocol.requestHandler = { request in
  44. let response = HTTPURLResponse(
  45. url: request.url!,
  46. statusCode: 200,
  47. httpVersion: nil,
  48. headerFields: nil
  49. )!
  50. return (response, fileURL.lines)
  51. }
  52. let model = GenerativeModel(
  53. name: "my-model",
  54. projectID: "my-project-id",
  55. apiKey: "API_KEY",
  56. tools: nil,
  57. requestOptions: RequestOptions(),
  58. appCheck: nil,
  59. auth: nil,
  60. urlSession: urlSession
  61. )
  62. let chat = Chat(model: model, history: [])
  63. let input = "Test input"
  64. let stream = try chat.sendMessageStream(input)
  65. // Ensure the values are parsed correctly
  66. for try await value in stream {
  67. XCTAssertNotNil(value.text)
  68. }
  69. XCTAssertEqual(chat.history.count, 2)
  70. let part = try XCTUnwrap(chat.history[0].parts[0])
  71. let textPart = try XCTUnwrap(part as? TextPart)
  72. XCTAssertEqual(textPart.text, input)
  73. let finalText = "1 2 3 4 5 6 7 8"
  74. let assembledExpectation = ModelContent(role: "model", parts: finalText)
  75. XCTAssertEqual(chat.history[1], assembledExpectation)
  76. }
  77. }