Skip to content

Commit bd2501a

Browse files
committed
Add Candidate decoding tests for empty/nil urlMetadata cases
1 parent d537c1e commit bd2501a

File tree

1 file changed

+44
-12
lines changed

1 file changed

+44
-12
lines changed

FirebaseAI/Tests/Unit/Types/GenerateContentResponseTests.swift

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import XCTest
1717

1818
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
1919
final class GenerateContentResponseTests: XCTestCase {
20+
let jsonDecoder = JSONDecoder()
21+
2022
// MARK: - GenerateContentResponse Computed Properties
2123

2224
func testGenerateContentResponse_inlineDataParts_success() throws {
@@ -107,22 +109,52 @@ final class GenerateContentResponseTests: XCTestCase {
107109
)
108110
}
109111

110-
func testURLContextMetadata_withEmptyURLMetadata_isNil() throws {
112+
// MARK: - Decoding Tests
113+
114+
func testDecodeCandidate_emptyURLMetadata_urlContextMetadataIsNil() throws {
111115
let json = """
112116
{
113-
"candidates": [
114-
{
115-
"content": { "role": "model", "parts": [ { "text": "Some text." } ] },
116-
"finishReason": "STOP",
117-
"urlContextMetadata": { "urlMetadata": [] }
118-
}
119-
]
117+
"content": { "role": "model", "parts": [ { "text": "Some text." } ] },
118+
"finishReason": "STOP",
119+
"urlContextMetadata": { "urlMetadata": [] }
120120
}
121-
""".data(using: .utf8)!
121+
"""
122+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
123+
124+
let candidate = try jsonDecoder.decode(Candidate.self, from: jsonData)
122125

123-
let response = try JSONDecoder().decode(GenerateContentResponse.self, from: json)
126+
XCTAssertNil(
127+
candidate.urlContextMetadata,
128+
"urlContextMetadata should be nil if the `urlMetadata` array is empty in the candidate."
129+
)
130+
XCTAssertEqual(candidate.content.role, "model")
131+
let part = try XCTUnwrap(candidate.content.parts.first)
132+
let textPart = try XCTUnwrap(part as? TextPart)
133+
XCTAssertEqual(textPart.text, "Some text.")
134+
XCTAssertFalse(textPart.isThought)
135+
XCTAssertEqual(candidate.finishReason, .stop)
136+
}
124137

125-
let candidate = try XCTUnwrap(response.candidates.first)
126-
XCTAssertNil(candidate.urlContextMetadata)
138+
func testDecodeCandidate_missingURLMetadata_urlContextMetadataIsNil() throws {
139+
let json = """
140+
{
141+
"content": { "role": "model", "parts": [ { "text": "Some text." } ] },
142+
"finishReason": "STOP"
143+
}
144+
"""
145+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
146+
147+
let candidate = try jsonDecoder.decode(Candidate.self, from: jsonData)
148+
149+
XCTAssertNil(
150+
candidate.urlContextMetadata,
151+
"urlContextMetadata should be nil if `urlMetadata` is not provided in the candidate."
152+
)
153+
XCTAssertEqual(candidate.content.role, "model")
154+
let part = try XCTUnwrap(candidate.content.parts.first)
155+
let textPart = try XCTUnwrap(part as? TextPart)
156+
XCTAssertEqual(textPart.text, "Some text.")
157+
XCTAssertFalse(textPart.isThought)
158+
XCTAssertEqual(candidate.finishReason, .stop)
127159
}
128160
}

0 commit comments

Comments
 (0)