TemplateGenerativeModelTests.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2025 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. @testable import FirebaseAILogic
  15. import FirebaseCore
  16. import XCTest
  17. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  18. final class TemplateGenerativeModelTests: XCTestCase {
  19. var urlSession: URLSession!
  20. var model: TemplateGenerativeModel!
  21. override func setUp() {
  22. super.setUp()
  23. let configuration = URLSessionConfiguration.default
  24. configuration.protocolClasses = [MockURLProtocol.self]
  25. urlSession = URLSession(configuration: configuration)
  26. let firebaseInfo = GenerativeModelTestUtil.testFirebaseInfo()
  27. let generativeAIService = GenerativeAIService(
  28. firebaseInfo: firebaseInfo,
  29. urlSession: urlSession
  30. )
  31. let apiConfig = APIConfig(service: .googleAI(endpoint: .firebaseProxyProd), version: .v1beta)
  32. model = TemplateGenerativeModel(generativeAIService: generativeAIService, apiConfig: apiConfig)
  33. }
  34. func testGenerateContent() async throws {
  35. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  36. forResource: "unary-success-basic-reply-short",
  37. withExtension: "json",
  38. subdirectory: "mock-responses/googleai",
  39. isTemplateRequest: true
  40. )
  41. let response = try await model.generateContent(
  42. templateID: "test-template",
  43. inputs: ["name": "test"]
  44. )
  45. XCTAssertEqual(
  46. response.text,
  47. "Google's headquarters, also known as the Googleplex, is located in **Mountain View, California**.\n"
  48. )
  49. }
  50. func testGenerateContentStream() async throws {
  51. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  52. forResource: "streaming-success-basic-reply-short",
  53. withExtension: "txt",
  54. subdirectory: "mock-responses/googleai",
  55. isTemplateRequest: true
  56. )
  57. let stream = try model.generateContentStream(
  58. templateID: "test-template",
  59. inputs: ["name": "test"]
  60. )
  61. let content = try await GenerativeModelTestUtil.collectTextFromStream(stream)
  62. XCTAssertEqual(content, "The capital of Wyoming is **Cheyenne**.\n")
  63. }
  64. }