Skip to content

Commit 323e33b

Browse files
committed
Add InternalPartTests
1 parent 86a725b commit 323e33b

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
@testable import FirebaseAI
16+
import XCTest
17+
18+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
19+
final class InternalPartTests: XCTestCase {
20+
let decoder = JSONDecoder()
21+
22+
func testDecodeTextPartWithThought() throws {
23+
let json = """
24+
{
25+
"text": "This is a thought.",
26+
"thought": true
27+
}
28+
"""
29+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
30+
let part = try decoder.decode(InternalPart.self, from: jsonData)
31+
32+
XCTAssertEqual(part.isThought, true)
33+
guard case let .text(text) = part.data else {
34+
XCTFail("Decoded part is not a text part.")
35+
return
36+
}
37+
XCTAssertEqual(text, "This is a thought.")
38+
}
39+
40+
func testDecodeTextPartWithoutThought() throws {
41+
let json = """
42+
{
43+
"text": "This is not a thought."
44+
}
45+
"""
46+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
47+
let part = try decoder.decode(InternalPart.self, from: jsonData)
48+
49+
XCTAssertNil(part.isThought)
50+
guard case let .text(text) = part.data else {
51+
XCTFail("Decoded part is not a text part.")
52+
return
53+
}
54+
XCTAssertEqual(text, "This is not a thought.")
55+
}
56+
57+
func testDecodeInlineDataPartWithThought() throws {
58+
let imageBase64 =
59+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+P+/HgAFhAJ/wlseKgAAAABJRU5ErkJggg=="
60+
let mimeType = "image/png"
61+
let json = """
62+
{
63+
"inlineData": {
64+
"mimeType": "\(mimeType)",
65+
"data": "\(imageBase64)"
66+
},
67+
"thought": true
68+
}
69+
"""
70+
let jsonData = try XCTUnwrap(json.data(using: .utf8))
71+
let part = try decoder.decode(InternalPart.self, from: jsonData)
72+
73+
XCTAssertEqual(part.isThought, true)
74+
guard case let .inlineData(inlineData) = part.data else {
75+
XCTFail("Decoded part is not an inlineData part.")
76+
return
77+
}
78+
XCTAssertEqual(inlineData.mimeType, mimeType)
79+
XCTAssertEqual(inlineData.data, Data(base64Encoded: imageBase64))
80+
}
81+
82+
// TODO(andrewheard): Add testDecodeInlineDataPartWithoutThought
83+
// TODO(andrewheard): Add testDecodeFunctionCallPartWithThought
84+
// TODO(andrewheard): Add testDecodeFunctionCallPartWithThoughtSignature
85+
// TODO(andrewheard): Add testDecodeFunctionCallPartWithoutThought
86+
}

0 commit comments

Comments
 (0)