Skip to content

Commit 0f96548

Browse files
committed
fix remaining tests
1 parent b0787d1 commit 0f96548

File tree

4 files changed

+31
-69
lines changed

4 files changed

+31
-69
lines changed

Sources/Converse/ContentBlocks/JSON.swift

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public enum JSONValue: Codable, Sendable {
5656
case let v as [JSONValue]:
5757
self = .array(v)
5858
break
59+
case let v as JSONValue:
60+
self = v
61+
break
5962
default:
6063
fatalError("JSONValue: Unsupported type: \(type(of: value))")
6164
}
@@ -146,24 +149,6 @@ public struct JSON: Codable, Sendable {
146149
}
147150
}
148151

149-
public func getValue<T>(_ key: String) -> T? {
150-
if case let .object(dictionary) = value {
151-
guard let v = dictionary[key] else {
152-
return nil
153-
}
154-
switch v {
155-
case .int(let val): return val as? T
156-
case .double(let val): return val as? T
157-
case .string(let val): return val as? T
158-
case .bool(let val): return val as? T
159-
case .array(let val): return val as? T
160-
case .object(let val): return val as? T
161-
case .null: return nil
162-
}
163-
}
164-
return nil
165-
}
166-
167152
public func getValue<T>() -> T? {
168153
switch value {
169154
case .int(let v): return v as? T

Tests/Converse/ConverseToolTests.swift

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,14 @@ extension BedrockServiceTests {
3232
.withTool(tool)
3333
let reply = try await bedrock.converse(with: builder)
3434
#expect(reply.textReply == nil)
35-
let id: String
36-
let name: String
37-
let input: JSON
35+
3836
if let toolUse = reply.toolUse {
39-
id = toolUse.id
40-
name = toolUse.name
41-
input = toolUse.input
37+
#expect(toolUse.id == "toolId")
38+
#expect(toolUse.name == "toolName")
39+
#expect(toolUse.input["value"]?["code"] == "string")
4240
} else {
43-
id = ""
44-
name = ""
45-
input = JSON(with: .object(["code": .string("wrong")]))
41+
Issue.record("Tool use is nil")
4642
}
47-
#expect(id == "toolId")
48-
#expect(name == "toolName")
49-
#expect(input.getValue("code") == "abc")
5043
}
5144

5245
@Test("Request tool usage with reused builder")
@@ -67,23 +60,15 @@ extension BedrockServiceTests {
6760

6861
#expect(reply.textReply == nil)
6962

70-
let id: String
71-
let name: String
72-
let input: JSON
7363
if let toolUse = reply.toolUse {
74-
id = toolUse.id
75-
name = toolUse.name
76-
input = toolUse.input
64+
print(toolUse)
65+
#expect(toolUse.id == "toolId")
66+
#expect(toolUse.name == "toolName")
67+
#expect(toolUse.input["value"]?["code"] == "string")
7768
} else {
78-
id = ""
79-
name = ""
80-
input = JSON(with: .object(["code": .string("wrong")]))
69+
Issue.record("ToolUse is nil")
8170
}
8271

83-
#expect(id == "toolId")
84-
#expect(name == "toolName")
85-
#expect(input.getValue("code") == "abc")
86-
8772
builder = try ConverseRequestBuilder(from: builder, with: reply)
8873
.withToolResult("Information from Tool")
8974

Tests/Converse/JSONTests.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,32 @@ struct JSONTests {
2525
func jsonGetValueFromValidJSONString() async throws {
2626

2727
let json = try jsonFromString()
28-
#expect(json.getValue("name") == "Jane Doe")
29-
#expect(json.getValue("age") == 30)
30-
#expect(json.getValue("isMember") == true)
31-
let t: String? = json.getValue("nonExistentKey")
28+
#expect(json["name"] == "Jane Doe")
29+
#expect(json["age"] == 30)
30+
#expect(json["isMember"] == true)
31+
let t: String? = json["nonExistentKey"]
3232
#expect(t == nil)
3333
}
3434

3535
@Test("JSON getValue from [String:JSONValue]")
3636
func jsonGetValueFromDictionary() async throws {
3737
let json = try jsonFromDictionary()
3838

39-
#expect(json.getValue("name") == "Jane Doe")
40-
#expect(json.getValue("age") == 30)
41-
#expect(json.getValue("isMember") == true)
42-
let t: String? = json.getValue("nonExistentKey")
39+
#expect(json["name"] == "Jane Doe")
40+
#expect(json["age"] == 30)
41+
#expect(json["isMember"] == true)
42+
let t: String? = json["nonExistentKey"]
4343
#expect(t == nil)
4444
}
4545

4646
@Test("JSON getValue nested")
4747
func jsonGetValueNested() async throws {
4848

4949
let json = try jsonFromDictionaryWithNested()
50-
#expect(json.getValue("name") == "Jane Doe")
51-
#expect(json.getValue("age") == 30)
52-
#expect(json.getValue("isMember") == true)
53-
let t: String? = json.getValue("nonExistentKey")
50+
#expect(json["name"] == "Jane Doe")
51+
#expect(json["age"] == 30)
52+
#expect(json["isMember"] == true)
53+
let t: String? = json["nonExistentKey"]
5454
#expect(t == nil)
5555
#expect(json["address"]?["street"] == "123 Main St")
5656
#expect(json["address"]?["city"] == "Anytown")
@@ -114,7 +114,7 @@ struct JSONTests {
114114
func emptyJSON() async throws {
115115
#expect(throws: Never.self) {
116116
let json = try JSON(from: "")
117-
let t: String? = json.getValue("nonExistentKey")
117+
let t: String? = json["nonExistentKey"]
118118
#expect(t == nil)
119119
}
120120
}

Tests/Converse/ToolResultBlockTests.swift

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,13 @@ extension BedrockServiceTests {
3939

4040
@Test("ToolResultBlock Initializer with ID and JSON Content")
4141
func toolResultBlockInitializerWithJSON() async throws {
42-
let json = JSON(with: .object(["code": .string("string")]))
42+
let json = JSON(with: .object(["key": .string("string")]))
4343
let block = ToolResultBlock(json, id: "block2")
4444
#expect(block.id == "block2")
4545
#expect(block.content.count == 1)
46-
var value = ""
4746
if case .json(let json) = block.content.first {
48-
value = json.getValue("key") ?? ""
47+
#expect(json["key"] == "string")
4948
}
50-
#expect(value == "value")
5149
#expect(block.status == .success)
5250
}
5351

@@ -90,11 +88,9 @@ extension BedrockServiceTests {
9088

9189
#expect(block.id == "block5")
9290
#expect(block.content.count == 1)
93-
var value = ""
9491
if case .json(let json) = block.content.first {
95-
value = json.getValue("key") ?? ""
92+
#expect(json["key"] == "value")
9693
}
97-
#expect(value == "value")
9894
#expect(block.status == block.status)
9995
}
10096

@@ -108,14 +104,10 @@ extension BedrockServiceTests {
108104
let block = try ToolResultBlock(object, id: "block6")
109105
#expect(block.id == "block6")
110106
#expect(block.content.count == 1)
111-
var name = ""
112-
var age = 0
113107
if case .json(let jsonContent) = block.content.first {
114-
name = jsonContent.getValue("name") ?? ""
115-
age = jsonContent.getValue("age") ?? 0
108+
#expect(jsonContent["name"] == "Jane")
109+
#expect(jsonContent["age"] == 30)
116110
}
117-
#expect(name == "Jane")
118-
#expect(age == 30)
119111
}
120112

121113
@Test("ToolResultBlock Initializer with Invalid Data Throws Error")

0 commit comments

Comments
 (0)