SafetyTests.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. import XCTest
  15. @testable import FirebaseAILogic
  16. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  17. final class SafetyTests: XCTestCase {
  18. let decoder = JSONDecoder()
  19. let encoder = JSONEncoder()
  20. override func setUp() {
  21. encoder.outputFormatting = .init(
  22. arrayLiteral: .prettyPrinted, .sortedKeys, .withoutEscapingSlashes
  23. )
  24. }
  25. // MARK: - SafetyRating Decoding
  26. func testDecodeSafetyRating_allFieldsPresent() throws {
  27. let json = """
  28. {
  29. "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
  30. "probability": "NEGLIGIBLE",
  31. "probabilityScore": 0.1,
  32. "severity": "HARM_SEVERITY_LOW",
  33. "severityScore": 0.2,
  34. "blocked": true
  35. }
  36. """
  37. let jsonData = try XCTUnwrap(json.data(using: .utf8))
  38. let rating = try decoder.decode(SafetyRating.self, from: jsonData)
  39. XCTAssertEqual(rating.category, .dangerousContent)
  40. XCTAssertEqual(rating.probability, .negligible)
  41. XCTAssertEqual(rating.probabilityScore, 0.1)
  42. XCTAssertEqual(rating.severity, .low)
  43. XCTAssertEqual(rating.severityScore, 0.2)
  44. XCTAssertTrue(rating.blocked)
  45. }
  46. func testDecodeSafetyRating_missingOptionalFields() throws {
  47. let json = """
  48. {
  49. "category": "HARM_CATEGORY_HARASSMENT",
  50. "probability": "LOW"
  51. }
  52. """
  53. let jsonData = try XCTUnwrap(json.data(using: .utf8))
  54. let rating = try decoder.decode(SafetyRating.self, from: jsonData)
  55. XCTAssertEqual(rating.category, .harassment)
  56. XCTAssertEqual(rating.probability, .low)
  57. XCTAssertEqual(rating.probabilityScore, 0.0)
  58. XCTAssertEqual(rating.severity, .unspecified)
  59. XCTAssertEqual(rating.severityScore, 0.0)
  60. XCTAssertFalse(rating.blocked)
  61. }
  62. func testDecodeSafetyRating_unknownEnums() throws {
  63. let json = """
  64. {
  65. "category": "HARM_CATEGORY_UNKNOWN",
  66. "probability": "UNKNOWN_PROBABILITY",
  67. "severity": "UNKNOWN_SEVERITY"
  68. }
  69. """
  70. let jsonData = try XCTUnwrap(json.data(using: .utf8))
  71. let rating = try decoder.decode(SafetyRating.self, from: jsonData)
  72. XCTAssertEqual(rating.category.rawValue, "HARM_CATEGORY_UNKNOWN")
  73. XCTAssertEqual(rating.probability.rawValue, "UNKNOWN_PROBABILITY")
  74. XCTAssertEqual(rating.severity.rawValue, "UNKNOWN_SEVERITY")
  75. }
  76. // MARK: - SafetySetting Encoding
  77. func testEncodeSafetySetting_allFields() throws {
  78. let setting = SafetySetting(
  79. harmCategory: .hateSpeech,
  80. threshold: .blockMediumAndAbove,
  81. method: .severity
  82. )
  83. let jsonData = try encoder.encode(setting)
  84. let jsonString = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  85. XCTAssertEqual(jsonString, """
  86. {
  87. "category" : "HARM_CATEGORY_HATE_SPEECH",
  88. "method" : "SEVERITY",
  89. "threshold" : "BLOCK_MEDIUM_AND_ABOVE"
  90. }
  91. """)
  92. }
  93. func testEncodeSafetySetting_nilMethod() throws {
  94. let setting = SafetySetting(
  95. harmCategory: .sexuallyExplicit,
  96. threshold: .blockOnlyHigh
  97. )
  98. let jsonData = try encoder.encode(setting)
  99. let jsonString = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  100. XCTAssertEqual(jsonString, """
  101. {
  102. "category" : "HARM_CATEGORY_SEXUALLY_EXPLICIT",
  103. "threshold" : "BLOCK_ONLY_HIGH"
  104. }
  105. """)
  106. }
  107. }