PartTests.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2024 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. @testable import FirebaseAI
  17. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  18. final class PartTests: XCTestCase {
  19. let decoder = JSONDecoder()
  20. let encoder = JSONEncoder()
  21. override func setUp() {
  22. encoder.outputFormatting = .init(
  23. arrayLiteral: .prettyPrinted, .sortedKeys, .withoutEscapingSlashes
  24. )
  25. }
  26. // MARK: - Part Decoding
  27. func testDecodeTextPart() throws {
  28. let expectedText = "Hello, world!"
  29. let json = """
  30. {
  31. "text" : "\(expectedText)"
  32. }
  33. """
  34. let jsonData = try XCTUnwrap(json.data(using: .utf8))
  35. let part = try decoder.decode(TextPart.self, from: jsonData)
  36. XCTAssertEqual(part.text, expectedText)
  37. }
  38. func testDecodeInlineDataPart() throws {
  39. let imageBase64 = try PartTests.blueSquareImage()
  40. let mimeType = "image/png"
  41. let json = """
  42. {
  43. "inlineData" : {
  44. "data" : "\(imageBase64)",
  45. "mimeType" : "\(mimeType)"
  46. }
  47. }
  48. """
  49. let jsonData = try XCTUnwrap(json.data(using: .utf8))
  50. let part = try decoder.decode(InlineDataPart.self, from: jsonData)
  51. XCTAssertEqual(part.data, Data(base64Encoded: imageBase64))
  52. XCTAssertEqual(part.mimeType, mimeType)
  53. }
  54. func testDecodeFunctionResponsePart() throws {
  55. let functionName = "test-function-name"
  56. let resultParameter = "test-result-parameter"
  57. let resultValue = "test-result-value"
  58. let json = """
  59. {
  60. "functionResponse" : {
  61. "name" : "\(functionName)",
  62. "response" : {
  63. "\(resultParameter)" : "\(resultValue)"
  64. }
  65. }
  66. }
  67. """
  68. let jsonData = try XCTUnwrap(json.data(using: .utf8))
  69. let part = try decoder.decode(FunctionResponsePart.self, from: jsonData)
  70. let functionResponse = part.functionResponse
  71. XCTAssertEqual(functionResponse.name, functionName)
  72. XCTAssertEqual(functionResponse.response, [resultParameter: .string(resultValue)])
  73. }
  74. // MARK: - Part Encoding
  75. func testEncodeTextPart() throws {
  76. let expectedText = "Hello, world!"
  77. let textPart = TextPart(expectedText)
  78. let jsonData = try encoder.encode(textPart)
  79. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  80. XCTAssertEqual(json, """
  81. {
  82. "text" : "\(expectedText)"
  83. }
  84. """)
  85. }
  86. func testEncodeInlineDataPart() throws {
  87. let mimeType = "image/png"
  88. let imageBase64 = try PartTests.blueSquareImage()
  89. let imageBase64Data = Data(base64Encoded: imageBase64)
  90. let inlineDataPart = InlineDataPart(data: imageBase64Data!, mimeType: mimeType)
  91. let jsonData = try encoder.encode(inlineDataPart)
  92. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  93. XCTAssertEqual(json, """
  94. {
  95. "inlineData" : {
  96. "data" : "\(imageBase64)",
  97. "mimeType" : "\(mimeType)"
  98. }
  99. }
  100. """)
  101. }
  102. func testEncodeFileDataPart() throws {
  103. let mimeType = "image/jpeg"
  104. let fileURI = "gs://test-bucket/image.jpg"
  105. let fileDataPart = FileDataPart(uri: fileURI, mimeType: mimeType)
  106. let jsonData = try encoder.encode(fileDataPart)
  107. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  108. XCTAssertEqual(json, """
  109. {
  110. "fileData" : {
  111. "fileUri" : "\(fileURI)",
  112. "mimeType" : "\(mimeType)"
  113. }
  114. }
  115. """)
  116. }
  117. // MARK: - Helpers
  118. private static func bundle() -> Bundle {
  119. #if SWIFT_PACKAGE
  120. return Bundle.module
  121. #else // SWIFT_PACKAGE
  122. return Bundle(for: Self.self)
  123. #endif // SWIFT_PACKAGE
  124. }
  125. private static func blueSquareImage() throws -> String {
  126. let imageURL = bundle().url(forResource: "blue", withExtension: "png")!
  127. let imageData = try Data(contentsOf: imageURL)
  128. return imageData.base64EncodedString()
  129. }
  130. }