TemplateGenerativeModelTests.swift 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 FirebaseAI
  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-response",
  37. withExtension: "json",
  38. subdirectory: "mock-responses",
  39. isTemplateRequest: true
  40. )
  41. let response = try await model.generateContent(
  42. template: "test-template",
  43. variables: ["name": "test"]
  44. )
  45. XCTAssertEqual(response.text, "Hello, world!")
  46. }
  47. }