Ver Fonte

[AI] Update empty parts check for urlContextMetadata (#15355)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Paul Beusterien há 6 meses atrás
pai
commit
0259023920

+ 2 - 1
FirebaseAI/Sources/GenerateContentResponse.swift

@@ -179,7 +179,8 @@ public struct Candidate: Sendable {
   // Returns `true` if the candidate contains no information that a developer could use.
   var isEmpty: Bool {
     content.parts
-      .isEmpty && finishReason == nil && citationMetadata == nil && groundingMetadata == nil
+      .isEmpty && finishReason == nil && citationMetadata == nil && groundingMetadata == nil &&
+      urlContextMetadata == nil
   }
 }
 

+ 37 - 1
FirebaseAI/Tests/Unit/Types/GenerateContentResponseTests.swift

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-import FirebaseAI
+@testable import FirebaseAI
 import XCTest
 
 @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
@@ -157,4 +157,40 @@ final class GenerateContentResponseTests: XCTestCase {
     XCTAssertFalse(textPart.isThought)
     XCTAssertEqual(candidate.finishReason, .stop)
   }
+
+  // MARK: - Candidate.isEmpty
+
+  func testCandidateIsEmpty_allEmpty_isTrue() throws {
+    let candidate = Candidate(
+      content: ModelContent(parts: []),
+      safetyRatings: [],
+      finishReason: nil,
+      citationMetadata: nil,
+      groundingMetadata: nil,
+      urlContextMetadata: nil
+    )
+
+    XCTAssertTrue(candidate.isEmpty, "A candidate with no content should be empty.")
+  }
+
+  func testCandidateIsEmpty_withURLContextMetadata_isFalse() throws {
+    let urlMetadata = try URLMetadata(
+      retrievedURL: XCTUnwrap(URL(string: "https://google.com")),
+      retrievalStatus: .success
+    )
+    let urlContextMetadata = URLContextMetadata(urlMetadata: [urlMetadata])
+    let candidate = Candidate(
+      content: ModelContent(parts: []),
+      safetyRatings: [],
+      finishReason: nil,
+      citationMetadata: nil,
+      groundingMetadata: nil,
+      urlContextMetadata: urlContextMetadata
+    )
+
+    XCTAssertFalse(
+      candidate.isEmpty,
+      "A candidate with only `urlContextMetadata` should not be empty."
+    )
+  }
 }