ChatTests.swift 3.0 KB

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