ChatTests.swift 2.9 KB

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