@@ -27,6 +27,7 @@ final class InternalPartTests: XCTestCase {
27
27
}
28
28
"""
29
29
let jsonData = try XCTUnwrap ( json. data ( using: . utf8) )
30
+
30
31
let part = try decoder. decode ( InternalPart . self, from: jsonData)
31
32
32
33
XCTAssertEqual ( part. isThought, true )
@@ -44,6 +45,7 @@ final class InternalPartTests: XCTestCase {
44
45
}
45
46
"""
46
47
let jsonData = try XCTUnwrap ( json. data ( using: . utf8) )
48
+
47
49
let part = try decoder. decode ( InternalPart . self, from: jsonData)
48
50
49
51
XCTAssertNil ( part. isThought)
@@ -68,6 +70,7 @@ final class InternalPartTests: XCTestCase {
68
70
}
69
71
"""
70
72
let jsonData = try XCTUnwrap ( json. data ( using: . utf8) )
73
+
71
74
let part = try decoder. decode ( InternalPart . self, from: jsonData)
72
75
73
76
XCTAssertEqual ( part. isThought, true )
@@ -79,8 +82,205 @@ final class InternalPartTests: XCTestCase {
79
82
XCTAssertEqual ( inlineData. data, Data ( base64Encoded: imageBase64) )
80
83
}
81
84
82
- // TODO(andrewheard): Add testDecodeInlineDataPartWithoutThought
83
- // TODO(andrewheard): Add testDecodeFunctionCallPartWithThought
84
- // TODO(andrewheard): Add testDecodeFunctionCallPartWithThoughtSignature
85
- // TODO(andrewheard): Add testDecodeFunctionCallPartWithoutThought
85
+ func testDecodeInlineDataPartWithoutThought( ) throws {
86
+ let imageBase64 = " aGVsbG8= "
87
+ let mimeType = " image/png "
88
+ let json = """
89
+ {
90
+ " inlineData " : {
91
+ " mimeType " : " \( mimeType) " ,
92
+ " data " : " \( imageBase64) "
93
+ }
94
+ }
95
+ """
96
+ let jsonData = try XCTUnwrap ( json. data ( using: . utf8) )
97
+
98
+ let part = try decoder. decode ( InternalPart . self, from: jsonData)
99
+
100
+ XCTAssertNil ( part. isThought)
101
+ guard case let . inlineData( inlineData) = part. data else {
102
+ XCTFail ( " Decoded part is not an inlineData part. " )
103
+ return
104
+ }
105
+ XCTAssertEqual ( inlineData. mimeType, mimeType)
106
+ XCTAssertEqual ( inlineData. data, Data ( base64Encoded: imageBase64) )
107
+ }
108
+
109
+ func testDecodeFileDataPartWithThought( ) throws {
110
+ let uri = " file:///path/to/file.mp3 "
111
+ let mimeType = " audio/mpeg "
112
+ let json = """
113
+ {
114
+ " fileData " : {
115
+ " fileUri " : " \( uri) " ,
116
+ " mimeType " : " \( mimeType) "
117
+ },
118
+ " thought " : true
119
+ }
120
+ """
121
+ let jsonData = try XCTUnwrap ( json. data ( using: . utf8) )
122
+
123
+ let part = try decoder. decode ( InternalPart . self, from: jsonData)
124
+
125
+ XCTAssertEqual ( part. isThought, true )
126
+ guard case let . fileData( fileData) = part. data else {
127
+ XCTFail ( " Decoded part is not a fileData part. " )
128
+ return
129
+ }
130
+ XCTAssertEqual ( fileData. fileURI, uri)
131
+ XCTAssertEqual ( fileData. mimeType, mimeType)
132
+ }
133
+
134
+ func testDecodeFileDataPartWithoutThought( ) throws {
135
+ let uri = " file:///path/to/file.mp3 "
136
+ let mimeType = " audio/mpeg "
137
+ let json = """
138
+ {
139
+ " fileData " : {
140
+ " fileUri " : " \( uri) " ,
141
+ " mimeType " : " \( mimeType) "
142
+ }
143
+ }
144
+ """
145
+ let jsonData = try XCTUnwrap ( json. data ( using: . utf8) )
146
+
147
+ let part = try decoder. decode ( InternalPart . self, from: jsonData)
148
+
149
+ XCTAssertNil ( part. isThought)
150
+ guard case let . fileData( fileData) = part. data else {
151
+ XCTFail ( " Decoded part is not a fileData part. " )
152
+ return
153
+ }
154
+ XCTAssertEqual ( fileData. fileURI, uri)
155
+ XCTAssertEqual ( fileData. mimeType, mimeType)
156
+ }
157
+
158
+ func testDecodeFunctionCallPartWithThoughtSignature( ) throws {
159
+ let functionName = " someFunction "
160
+ let expectedThoughtSignature = " some_signature "
161
+ let json = """
162
+ {
163
+ " functionCall " : {
164
+ " name " : " \( functionName) " ,
165
+ " args " : {
166
+ " arg1 " : " value1 "
167
+ },
168
+ },
169
+ " thoughtSignature " : " \( expectedThoughtSignature) "
170
+ }
171
+ """
172
+ let jsonData = try XCTUnwrap ( json. data ( using: . utf8) )
173
+
174
+ let part = try decoder. decode ( InternalPart . self, from: jsonData)
175
+
176
+ let thoughtSignature = try XCTUnwrap ( part. thoughtSignature)
177
+ XCTAssertEqual ( thoughtSignature, expectedThoughtSignature)
178
+ XCTAssertNil ( part. isThought)
179
+ guard case let . functionCall( functionCall) = part. data else {
180
+ XCTFail ( " Decoded part is not a functionCall part. " )
181
+ return
182
+ }
183
+ XCTAssertEqual ( functionCall. name, functionName)
184
+ XCTAssertEqual ( functionCall. args, [ " arg1 " : . string( " value1 " ) ] )
185
+ }
186
+
187
+ func testDecodeFunctionCallPartWithoutThoughtSignature( ) throws {
188
+ let functionName = " someFunction "
189
+ let json = """
190
+ {
191
+ " functionCall " : {
192
+ " name " : " \( functionName) " ,
193
+ " args " : {
194
+ " arg1 " : " value1 "
195
+ }
196
+ }
197
+ }
198
+ """
199
+ let jsonData = try XCTUnwrap ( json. data ( using: . utf8) )
200
+
201
+ let part = try decoder. decode ( InternalPart . self, from: jsonData)
202
+
203
+ XCTAssertNil ( part. isThought)
204
+ XCTAssertNil ( part. thoughtSignature)
205
+ guard case let . functionCall( functionCall) = part. data else {
206
+ XCTFail ( " Decoded part is not a functionCall part. " )
207
+ return
208
+ }
209
+ XCTAssertEqual ( functionCall. name, functionName)
210
+ XCTAssertEqual ( functionCall. args, [ " arg1 " : . string( " value1 " ) ] )
211
+ }
212
+
213
+ func testDecodeFunctionCallPartWithoutArgs( ) throws {
214
+ let functionName = " someFunction "
215
+ let json = """
216
+ {
217
+ " functionCall " : {
218
+ " name " : " \( functionName) "
219
+ }
220
+ }
221
+ """
222
+ let jsonData = try XCTUnwrap ( json. data ( using: . utf8) )
223
+
224
+ let part = try decoder. decode ( InternalPart . self, from: jsonData)
225
+
226
+ XCTAssertNil ( part. isThought)
227
+ XCTAssertNil ( part. thoughtSignature)
228
+ guard case let . functionCall( functionCall) = part. data else {
229
+ XCTFail ( " Decoded part is not a functionCall part. " )
230
+ return
231
+ }
232
+ XCTAssertEqual ( functionCall. name, functionName)
233
+ XCTAssertEqual ( functionCall. args, JSONObject ( ) )
234
+ }
235
+
236
+ func testDecodeFunctionResponsePartWithThought( ) throws {
237
+ let functionName = " someFunction "
238
+ let json = """
239
+ {
240
+ " functionResponse " : {
241
+ " name " : " \( functionName) " ,
242
+ " response " : {
243
+ " output " : " someValue "
244
+ }
245
+ },
246
+ " thought " : true
247
+ }
248
+ """
249
+ let jsonData = try XCTUnwrap ( json. data ( using: . utf8) )
250
+
251
+ let part = try decoder. decode ( InternalPart . self, from: jsonData)
252
+
253
+ XCTAssertEqual ( part. isThought, true )
254
+ guard case let . functionResponse( functionResponse) = part. data else {
255
+ XCTFail ( " Decoded part is not a functionResponse part. " )
256
+ return
257
+ }
258
+ XCTAssertEqual ( functionResponse. name, functionName)
259
+ XCTAssertEqual ( functionResponse. response, [ " output " : . string( " someValue " ) ] )
260
+ }
261
+
262
+ func testDecodeFunctionResponsePartWithoutThought( ) throws {
263
+ let functionName = " someFunction "
264
+ let json = """
265
+ {
266
+ " functionResponse " : {
267
+ " name " : " \( functionName) " ,
268
+ " response " : {
269
+ " output " : " someValue "
270
+ }
271
+ }
272
+ }
273
+ """
274
+ let jsonData = try XCTUnwrap ( json. data ( using: . utf8) )
275
+
276
+ let part = try decoder. decode ( InternalPart . self, from: jsonData)
277
+
278
+ XCTAssertNil ( part. isThought)
279
+ guard case let . functionResponse( functionResponse) = part. data else {
280
+ XCTFail ( " Decoded part is not a functionResponse part. " )
281
+ return
282
+ }
283
+ XCTAssertEqual ( functionResponse. name, functionName)
284
+ XCTAssertEqual ( functionResponse. response, [ " output " : . string( " someValue " ) ] )
285
+ }
86
286
}
0 commit comments