ChatTests.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 FirebaseCore
  15. import Foundation
  16. import XCTest
  17. @testable import FirebaseAI
  18. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  19. final class ChatTests: XCTestCase {
  20. let modelName = "test-model-name"
  21. let modelResourceName = "projects/my-project/locations/us-central1/models/test-model-name"
  22. var urlSession: URLSession!
  23. override func setUp() {
  24. let configuration = URLSessionConfiguration.default
  25. configuration.protocolClasses = [MockURLProtocol.self]
  26. urlSession = URLSession(configuration: configuration)
  27. }
  28. override func tearDown() {
  29. MockURLProtocol.requestHandler = nil
  30. }
  31. func testMergingText() async throws {
  32. let bundle = BundleTestUtil.bundle()
  33. let fileURL = try XCTUnwrap(bundle.url(
  34. forResource: "streaming-success-basic-reply-parts",
  35. withExtension: "txt",
  36. subdirectory: "vertexai"
  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 app = FirebaseApp(instanceWithName: "testApp",
  53. options: FirebaseOptions(googleAppID: "ignore",
  54. gcmSenderID: "ignore"))
  55. let model = GenerativeModel(
  56. modelName: modelName,
  57. modelResourceName: modelResourceName,
  58. firebaseInfo: FirebaseInfo(
  59. projectID: "my-project-id",
  60. apiKey: "API_KEY",
  61. firebaseAppID: "My app ID",
  62. firebaseApp: app
  63. ),
  64. apiConfig: FirebaseAI.defaultVertexAIAPIConfig,
  65. tools: nil,
  66. requestOptions: RequestOptions(),
  67. urlSession: urlSession
  68. )
  69. let chat = Chat(model: model, history: [])
  70. let input = "Test input"
  71. let stream = try chat.sendMessageStream(input)
  72. // Ensure the values are parsed correctly
  73. for try await value in stream {
  74. XCTAssertNotNil(value.text)
  75. }
  76. XCTAssertEqual(chat.history.count, 2)
  77. let part = try XCTUnwrap(chat.history[0].parts[0])
  78. let textPart = try XCTUnwrap(part as? TextPart)
  79. XCTAssertEqual(textPart.text, input)
  80. let finalText = "1 2 3 4 5 6 7 8"
  81. let assembledExpectation = ModelContent(role: "model", parts: finalText)
  82. XCTAssertEqual(chat.history[1], assembledExpectation)
  83. }
  84. }