Skip to content

Commit 217b06c

Browse files
committed
Add remaining InternalPartTests
1 parent 86f06be commit 217b06c

File tree

1 file changed

+204
-4
lines changed

1 file changed

+204
-4
lines changed

FirebaseAI/Tests/Unit/Types/InternalPartTests.swift

Lines changed: 204 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ final class InternalPartTests: XCTestCase {
2727
}
2828
"""
2929
let jsonData = try XCTUnwrap(json.data(using: .utf8))
30+
3031
let part = try decoder.decode(InternalPart.self, from: jsonData)
3132

3233
XCTAssertEqual(part.isThought, true)
@@ -44,6 +45,7 @@ final class InternalPartTests: XCTestCase {
4445
}
4546
"""
4647
let jsonData = try XCTUnwrap(json.data(using: .utf8))
48+
4749
let part = try decoder.decode(InternalPart.self, from: jsonData)
4850

4951
XCTAssertNil(part.isThought)
@@ -68,6 +70,7 @@ final class InternalPartTests: XCTestCase {
6870
}
6971
"""
7072
let jsonData = try XCTUnwrap(json.data(using: .utf8))
73+
7174
let part = try decoder.decode(InternalPart.self, from: jsonData)
7275

7376
XCTAssertEqual(part.isThought, true)
@@ -79,8 +82,205 @@ final class InternalPartTests: XCTestCase {
7982
XCTAssertEqual(inlineData.data, Data(base64Encoded: imageBase64))
8083
}
8184

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+
}
86286
}

0 commit comments

Comments
 (0)