@@ -106,4 +106,136 @@ final class MessagesRequestTests: XCTestCase {
106
106
XCTAssertNil ( request. body? . tools)
107
107
XCTAssertNil ( request. body? . toolChoice)
108
108
}
109
+
110
+ func testEncoding( ) throws {
111
+ // Prepare test data
112
+ let message = Message ( role: . user, content: [ . text( " Hello " ) ] )
113
+ let systemPrompt : SystemPrompt = . text( " You are a helpful assistant " , nil )
114
+
115
+ // Define various tools for testing
116
+ let tools = [
117
+ // Computer tool
118
+ Tool . computer ( . init(
119
+ name: " computer " ,
120
+ displayWidthPx: 1024 ,
121
+ displayHeightPx: 768 ,
122
+ displayNumber: 1
123
+ ) ) ,
124
+
125
+ // Text editor tool
126
+ Tool . textEditor ( . init(
127
+ name: " str_replace_editor "
128
+ ) ) ,
129
+
130
+ // Bash tool
131
+ Tool . bash ( . init(
132
+ name: " bash "
133
+ ) ) ,
134
+
135
+ // Function tool (weather)
136
+ Tool . function ( . init(
137
+ name: " get_weather " ,
138
+ description: " Get weather information " ,
139
+ inputSchema: . init(
140
+ type: . object,
141
+ format: nil ,
142
+ description: " Weather query parameters " ,
143
+ nullable: nil ,
144
+ enumValues: nil ,
145
+ items: nil ,
146
+ properties: [
147
+ " location " : . init(
148
+ type: . string,
149
+ format: nil ,
150
+ description: " City name " ,
151
+ nullable: nil ,
152
+ enumValues: nil ,
153
+ items: nil ,
154
+ properties: nil ,
155
+ requiredProperties: nil
156
+ )
157
+ ] ,
158
+ requiredProperties: [ " location " ]
159
+ )
160
+ ) )
161
+ ]
162
+
163
+ let sut = MessagesRequestBody (
164
+ model: . claude_3_Opus,
165
+ messages: [ message] ,
166
+ system: [ systemPrompt] ,
167
+ maxTokens: 1000 ,
168
+ metaData: . init( userId: " test-user " ) ,
169
+ stopSequences: [ " STOP " ] ,
170
+ stream: true ,
171
+ temperature: 0.7 ,
172
+ topP: 0.9 ,
173
+ topK: 10 ,
174
+ tools: tools,
175
+ toolChoice: . auto
176
+ )
177
+
178
+ // Encode to JSON
179
+ let data = try anthropicJSONEncoder. encode ( sut)
180
+ let json = try JSONSerialization . jsonObject ( with: data) as! [ String : Any ]
181
+
182
+ // Verify basic properties
183
+ XCTAssertEqual ( json [ " model " ] as? String , " claude-3-opus-20240229 " )
184
+ XCTAssertEqual ( ( json [ " messages " ] as? [ [ String : Any ] ] ) ? . count, 1 )
185
+ XCTAssertEqual ( ( json [ " system " ] as? [ [ String : Any ] ] ) ? . count, 1 )
186
+ XCTAssertEqual ( json [ " max_tokens " ] as? Int , 1000 )
187
+ XCTAssertNotNil ( json [ " meta_data " ] )
188
+ XCTAssertEqual ( ( json [ " stop_sequences " ] as? [ String ] ) , [ " STOP " ] )
189
+ XCTAssertEqual ( json [ " stream " ] as? Bool , true )
190
+ XCTAssertEqual ( json [ " temperature " ] as? Double , 0.7 )
191
+ XCTAssertEqual ( json [ " top_p " ] as? Double , 0.9 )
192
+ XCTAssertEqual ( json [ " top_k " ] as? Int , 10 )
193
+
194
+ // Verify tools array
195
+ let encodedTools = json [ " tools " ] as? [ [ String : Any ] ]
196
+ XCTAssertEqual ( encodedTools? . count, 4 )
197
+
198
+ // Verify computer tool
199
+ let computerTool = encodedTools ? [ 0 ]
200
+ XCTAssertEqual ( computerTool ? [ " type " ] as? String , " computer_20241022 " )
201
+ XCTAssertEqual ( computerTool ? [ " name " ] as? String , " computer " )
202
+ XCTAssertEqual ( computerTool ? [ " display_width_px " ] as? Int , 1024 )
203
+ XCTAssertEqual ( computerTool ? [ " display_height_px " ] as? Int , 768 )
204
+
205
+ // Verify text editor tool
206
+ let textEditorTool = encodedTools ? [ 1 ]
207
+ XCTAssertEqual ( textEditorTool ? [ " type " ] as? String , " textEditor_20241022 " )
208
+ XCTAssertEqual ( textEditorTool ? [ " name " ] as? String , " str_replace_editor " )
209
+
210
+ // Verify bash tool
211
+ let bashTool = encodedTools ? [ 2 ]
212
+ XCTAssertEqual ( bashTool ? [ " type " ] as? String , " bash_20241022 " )
213
+ XCTAssertEqual ( bashTool ? [ " name " ] as? String , " bash " )
214
+
215
+ // Verify function tool
216
+ let functionTool = encodedTools ? [ 3 ]
217
+ XCTAssertEqual ( functionTool ? [ " name " ] as? String , " get_weather " )
218
+ XCTAssertNotNil ( functionTool ? [ " description " ] )
219
+ XCTAssertNotNil ( functionTool ? [ " input_schema " ] )
220
+
221
+ XCTAssertNotNil ( json [ " tool_choice " ] )
222
+ }
223
+
224
+ func testEncodingWithMinimalParameters( ) throws {
225
+ // Test with only required parameters
226
+ let message = Message ( role: . user, content: [ . text( " Hello " ) ] )
227
+ let sut = MessagesRequestBody (
228
+ messages: [ message] ,
229
+ maxTokens: 1000
230
+ )
231
+
232
+ let data = try anthropicJSONEncoder. encode ( sut)
233
+ let json = try JSONSerialization . jsonObject ( with: data) as! [ String : Any ]
234
+
235
+ // Verify minimal configuration
236
+ XCTAssertEqual ( json [ " model " ] as? String , " claude-3-opus-20240229 " )
237
+ XCTAssertEqual ( ( json [ " messages " ] as? [ [ String : Any ] ] ) ? . count, 1 )
238
+ XCTAssertEqual ( json [ " max_tokens " ] as? Int , 1000 )
239
+ XCTAssertNil ( json [ " tool_choice " ] )
240
+ }
109
241
}
0 commit comments