@@ -17,6 +17,8 @@ import XCTest
17
17
18
18
@available ( iOS 15 . 0 , macOS 12 . 0 , macCatalyst 15 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
19
19
final class GenerateContentResponseTests : XCTestCase {
20
+ let jsonDecoder = JSONDecoder ( )
21
+
20
22
// MARK: - GenerateContentResponse Computed Properties
21
23
22
24
func testGenerateContentResponse_inlineDataParts_success( ) throws {
@@ -107,22 +109,52 @@ final class GenerateContentResponseTests: XCTestCase {
107
109
)
108
110
}
109
111
110
- func testURLContextMetadata_withEmptyURLMetadata_isNil( ) throws {
112
+ // MARK: - Decoding Tests
113
+
114
+ func testDecodeCandidate_emptyURLMetadata_urlContextMetadataIsNil( ) throws {
111
115
let json = """
112
116
{
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 " : [] }
120
120
}
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)
122
125
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
+ }
124
137
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)
127
159
}
128
160
}
0 commit comments