-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathgenerated_rpc_union_test.go
More file actions
386 lines (348 loc) · 13.6 KB
/
Copy pathgenerated_rpc_union_test.go
File metadata and controls
386 lines (348 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package rpc
import (
"encoding/json"
"io"
"testing"
"github.com/github/copilot-sdk/go/internal/jsonrpc2"
)
func TestExternalToolResultJSONUnion(t *testing.T) {
var stringResult ExternalToolResult = ExternalToolStringResult("tool result")
raw, err := json.Marshal(stringResult)
if err != nil {
t.Fatalf("marshal string result: %v", err)
}
if string(raw) != `"tool result"` {
t.Fatalf("marshal string result = %s", raw)
}
decodedString, err := unmarshalExternalToolResult([]byte(`"tool result"`))
if err != nil {
t.Fatalf("unmarshal string result: %v", err)
}
decodedStringValue, ok := decodedString.(ExternalToolStringResult)
if !ok || string(decodedStringValue) != "tool result" {
t.Fatalf("unmarshal string result = %#v", decodedString)
}
var objectResult ExternalToolResult = &ExternalToolTextResultForLlm{TextResultForLlm: "expanded"}
raw, err = json.Marshal(objectResult)
if err != nil {
t.Fatalf("marshal object result: %v", err)
}
if string(raw) != `{"textResultForLlm":"expanded"}` {
t.Fatalf("marshal object result = %s", raw)
}
decodedObject, err := unmarshalExternalToolResult([]byte(`{"textResultForLlm":"expanded"}`))
if err != nil {
t.Fatalf("unmarshal object result: %v", err)
}
decodedObjectValue, ok := decodedObject.(*ExternalToolTextResultForLlm)
if !ok || decodedObjectValue.TextResultForLlm != "expanded" {
t.Fatalf("unmarshal object result = %#v", decodedObject)
}
}
func TestOptionalNullableFieldsPreserveOmittedAndNull(t *testing.T) {
unset, err := json.Marshal(TaskClientUpdateProgress{})
if err != nil {
t.Fatalf("marshal unset progress: %v", err)
}
if string(unset) != `{"kind":"progress"}` {
t.Fatalf("marshal unset progress = %s", unset)
}
var clearedPercentage *float64
var clearedPhase *string
cleared, err := json.Marshal(TaskClientUpdateProgress{
Percentage: &clearedPercentage,
Phase: &clearedPhase,
})
if err != nil {
t.Fatalf("marshal cleared progress: %v", err)
}
if string(cleared) != `{"kind":"progress","percentage":null,"phase":null}` {
t.Fatalf("marshal cleared progress = %s", cleared)
}
}
func TestFilterMappingJSONUnion(t *testing.T) {
var mapping FilterMapping = FilterMappingEnumMap{"secret": ContentFilterModeHiddenCharacters}
raw, err := json.Marshal(mapping)
if err != nil {
t.Fatalf("marshal filter mapping map: %v", err)
}
if string(raw) != `{"secret":"hidden_characters"}` {
t.Fatalf("marshal filter mapping map = %s", raw)
}
decodedMap, err := unmarshalFilterMapping([]byte(`{"secret":"hidden_characters"}`))
if err != nil {
t.Fatalf("unmarshal filter mapping map: %v", err)
}
decodedMapValue, ok := decodedMap.(FilterMappingEnumMap)
if !ok || decodedMapValue["secret"] != ContentFilterModeHiddenCharacters {
t.Fatalf("unmarshal filter mapping map = %#v", decodedMap)
}
var enumValue FilterMapping = ContentFilterModeMarkdown
raw, err = json.Marshal(enumValue)
if err != nil {
t.Fatalf("marshal filter mapping enum: %v", err)
}
if string(raw) != `"markdown"` {
t.Fatalf("marshal filter mapping enum = %s", raw)
}
decodedEnum, err := unmarshalFilterMapping([]byte(`"markdown"`))
if err != nil {
t.Fatalf("unmarshal filter mapping enum: %v", err)
}
decodedEnumValue, ok := decodedEnum.(ContentFilterMode)
if !ok || decodedEnumValue != ContentFilterModeMarkdown {
t.Fatalf("unmarshal filter mapping enum = %#v", decodedEnum)
}
}
func TestMCPServerConfigJSONUnion(t *testing.T) {
var localConfig MCPServerConfig = &MCPServerConfigStdio{
Args: []string{"-v"},
Command: "node",
}
raw, err := json.Marshal(localConfig)
if err != nil {
t.Fatalf("marshal local config: %v", err)
}
if string(raw) != `{"args":["-v"],"command":"node"}` {
t.Fatalf("marshal local config = %s", raw)
}
decodedLocal, err := unmarshalMCPServerConfig([]byte(`{"args":["-v"],"command":"node"}`))
if err != nil {
t.Fatalf("unmarshal local config: %v", err)
}
decodedLocalValue, ok := decodedLocal.(*MCPServerConfigStdio)
if !ok || decodedLocalValue.Command != "node" || len(decodedLocalValue.Args) != 1 || decodedLocalValue.Args[0] != "-v" {
t.Fatalf("unmarshal local config = %#v", decodedLocal)
}
var httpConfig MCPServerConfig = &MCPServerConfigHTTP{URL: "https://example.com/mcp"}
raw, err = json.Marshal(httpConfig)
if err != nil {
t.Fatalf("marshal HTTP config: %v", err)
}
if string(raw) != `{"url":"https://example.com/mcp"}` {
t.Fatalf("marshal HTTP config = %s", raw)
}
decodedHTTP, err := unmarshalMCPServerConfig([]byte(`{"url":"https://example.com/mcp"}`))
if err != nil {
t.Fatalf("unmarshal HTTP config: %v", err)
}
decodedHTTPValue, ok := decodedHTTP.(*MCPServerConfigHTTP)
if !ok || decodedHTTPValue.URL != "https://example.com/mcp" {
t.Fatalf("unmarshal HTTP config = %#v", decodedHTTP)
}
decodedRaw, err := unmarshalMCPServerConfig([]byte(`{"name":"future"}`))
if err != nil {
t.Fatalf("unmarshal raw config: %v", err)
}
if _, ok := decodedRaw.(*RawMCPServerConfigData); !ok {
t.Fatalf("unmarshal raw config = %T, want *RawMCPServerConfigData", decodedRaw)
}
}
func TestTaskProgressUnmarshalsTaskAgentProgressVariants(t *testing.T) {
agentProgress, err := unmarshalTaskProgress([]byte(`{"type":"agent","recentActivity":[],"latestIntent":"Summarizing"}`))
if err != nil {
t.Fatalf("unmarshal agent task progress: %v", err)
}
agentValue, ok := agentProgress.(*TaskAgentProgress)
if !ok {
t.Fatalf("agent task progress = %T, want *TaskAgentProgress", agentProgress)
}
if agentValue.LatestIntent == nil || *agentValue.LatestIntent != "Summarizing" {
t.Fatalf("agent latest intent = %v, want Summarizing", agentValue.LatestIntent)
}
shellProgress, err := unmarshalTaskProgress([]byte(`{"type":"shell","recentOutput":"building","pid":123}`))
if err != nil {
t.Fatalf("unmarshal shell task progress: %v", err)
}
shellValue, ok := shellProgress.(*TaskShellProgress)
if !ok {
t.Fatalf("shell task progress = %T, want *TaskShellProgress", shellProgress)
}
if shellValue.RecentOutput != "building" {
t.Fatalf("shell recent output = %q, want building", shellValue.RecentOutput)
}
if shellValue.Pid == nil || *shellValue.Pid != 123 {
t.Fatalf("shell pid = %v, want 123", shellValue.Pid)
}
}
func TestCommandsInvokeUnmarshalsSlashCommandInvocationResult(t *testing.T) {
clientToServerReader, clientToServerWriter := io.Pipe()
serverToClientReader, serverToClientWriter := io.Pipe()
client := jsonrpc2.NewClient(clientToServerWriter, serverToClientReader)
server := jsonrpc2.NewClient(serverToClientWriter, clientToServerReader)
server.SetRequestHandler("session.commands.invoke", func(params json.RawMessage) (json.RawMessage, *jsonrpc2.Error) {
var request struct {
Input string `json:"input"`
Name string `json:"name"`
SessionID string `json:"sessionId"`
}
if err := json.Unmarshal(params, &request); err != nil {
return nil, &jsonrpc2.Error{Code: -32602, Message: err.Error()}
}
if request.SessionID != "session-1" || request.Name != "help" || request.Input != "details" {
return nil, &jsonrpc2.Error{Code: -32602, Message: "unexpected invoke request"}
}
return json.RawMessage(`{"kind":"text","text":"hello","markdown":true}`), nil
})
client.Start()
server.Start()
t.Cleanup(func() {
client.Stop()
server.Stop()
_ = clientToServerWriter.Close()
_ = clientToServerReader.Close()
_ = serverToClientWriter.Close()
_ = serverToClientReader.Close()
})
input := "details"
result, err := NewSessionRPC(client, "session-1").Commands.Invoke(t.Context(), &CommandsInvokeRequest{
Input: &input,
Name: "help",
})
if err != nil {
t.Fatalf("invoke command: %v", err)
}
textResult, ok := result.(*SlashCommandTextResult)
if !ok {
t.Fatalf("invoke result = %T, want *SlashCommandTextResult", result)
}
if textResult.Text != "hello" {
t.Fatalf("invoke result text = %q, want hello", textResult.Text)
}
if textResult.Markdown == nil || !*textResult.Markdown {
t.Fatalf("invoke result markdown = %v, want true", textResult.Markdown)
}
}
func TestQueuedCommandResultBoolDiscriminatorJSONUnion(t *testing.T) {
stopProcessingQueue := true
var handled QueuedCommandResult = &QueuedCommandHandled{StopProcessingQueue: &stopProcessingQueue}
raw, err := json.Marshal(handled)
if err != nil {
t.Fatalf("marshal handled result: %v", err)
}
if string(raw) != `{"handled":true,"stopProcessingQueue":true}` {
t.Fatalf("marshal handled result = %s", raw)
}
decodedHandled, err := unmarshalQueuedCommandResult([]byte(`{"handled":true,"stopProcessingQueue":true}`))
if err != nil {
t.Fatalf("unmarshal handled result: %v", err)
}
decodedHandledValue, ok := decodedHandled.(*QueuedCommandHandled)
if !ok {
t.Fatalf("unmarshal handled result = %T, want *QueuedCommandHandled", decodedHandled)
}
if decodedHandledValue.StopProcessingQueue == nil || !*decodedHandledValue.StopProcessingQueue {
t.Fatalf("unmarshal handled stopProcessingQueue = %v, want true", decodedHandledValue.StopProcessingQueue)
}
var notHandled QueuedCommandResult = &QueuedCommandNotHandled{}
raw, err = json.Marshal(notHandled)
if err != nil {
t.Fatalf("marshal not handled result: %v", err)
}
if string(raw) != `{"handled":false}` {
t.Fatalf("marshal not handled result = %s", raw)
}
decodedNotHandled, err := unmarshalQueuedCommandResult([]byte(`{"handled":false}`))
if err != nil {
t.Fatalf("unmarshal not handled result: %v", err)
}
if _, ok := decodedNotHandled.(*QueuedCommandNotHandled); !ok {
t.Fatalf("unmarshal not handled result = %T, want *QueuedCommandNotHandled", decodedNotHandled)
}
}
func TestUIElicitationFieldValueJSONUnion(t *testing.T) {
raw, err := json.Marshal(UIElicitationBooleanValue(true))
if err != nil {
t.Fatalf("marshal bool value: %v", err)
}
if string(raw) != `true` {
t.Fatalf("marshal bool value = %s", raw)
}
var response UIElicitationResponse
if err := json.Unmarshal([]byte(`{"action":"accept","content":{"choices":["a","b"]}}`), &response); err != nil {
t.Fatalf("unmarshal response with string array value: %v", err)
}
decodedArray, ok := response.Content["choices"].(UIElicitationStringArrayValue)
if !ok {
t.Fatalf("unmarshal string array value = %T, want UIElicitationStringArrayValue", response.Content["choices"])
}
if len(decodedArray) != 2 || decodedArray[0] != "a" || decodedArray[1] != "b" {
t.Fatalf("unmarshal string array value = %#v", decodedArray)
}
}
func TestUIElicitationSchemaPropertyJSONUnion(t *testing.T) {
var schema UIElicitationSchema
if err := json.Unmarshal([]byte(`{
"type":"object",
"properties":{
"confirmed":{"type":"boolean","default":true},
"choice":{"type":"string","enum":["a","b"]},
"freeform":{"type":"string","minLength":1},
"count":{"type":"integer","minimum":0},
"arrayChoice":{"type":"array","items":{"type":"string","enum":["a","b"]}},
"arrayAnyOf":{"type":"array","items":{"anyOf":[{"const":"a","title":"A"}]}}
},
"required":["confirmed"]
}`), &schema); err != nil {
t.Fatalf("unmarshal elicitation schema: %v", err)
}
confirmed, ok := schema.Properties["confirmed"].(*UIElicitationSchemaPropertyBoolean)
if !ok {
t.Fatalf("confirmed property = %T, want *UIElicitationSchemaPropertyBoolean", schema.Properties["confirmed"])
}
if confirmed.Default == nil || !*confirmed.Default {
t.Fatalf("confirmed default = %v, want true", confirmed.Default)
}
choice, ok := schema.Properties["choice"].(*UIElicitationStringEnumField)
if !ok {
t.Fatalf("choice property = %T, want *UIElicitationStringEnumField", schema.Properties["choice"])
}
if len(choice.Enum) != 2 || choice.Enum[0] != "a" || choice.Enum[1] != "b" {
t.Fatalf("choice enum = %#v", choice.Enum)
}
freeform, ok := schema.Properties["freeform"].(*UIElicitationSchemaPropertyString)
if !ok {
t.Fatalf("freeform property = %T, want *UIElicitationSchemaPropertyString", schema.Properties["freeform"])
}
if freeform.MinLength == nil || *freeform.MinLength != 1 {
t.Fatalf("freeform minLength = %v, want 1", freeform.MinLength)
}
count, ok := schema.Properties["count"].(*UIElicitationSchemaPropertyNumber)
if !ok {
t.Fatalf("count property = %T, want *UIElicitationSchemaPropertyNumber", schema.Properties["count"])
}
if count.Discriminator != UIElicitationSchemaPropertyNumberTypeInteger {
t.Fatalf("count type = %q, want %q", count.Discriminator, UIElicitationSchemaPropertyNumberTypeInteger)
}
arrayChoice, ok := schema.Properties["arrayChoice"].(*UIElicitationArrayEnumField)
if !ok {
t.Fatalf("arrayChoice property = %T, want *UIElicitationArrayEnumField", schema.Properties["arrayChoice"])
}
if len(arrayChoice.Items.Enum) != 2 || arrayChoice.Items.Enum[0] != "a" || arrayChoice.Items.Enum[1] != "b" {
t.Fatalf("arrayChoice items enum = %#v", arrayChoice.Items.Enum)
}
arrayAnyOf, ok := schema.Properties["arrayAnyOf"].(*UIElicitationArrayAnyOfField)
if !ok {
t.Fatalf("arrayAnyOf property = %T, want *UIElicitationArrayAnyOfField", schema.Properties["arrayAnyOf"])
}
if len(arrayAnyOf.Items.AnyOf) != 1 || arrayAnyOf.Items.AnyOf[0].Const != "a" || arrayAnyOf.Items.AnyOf[0].Title != "A" {
t.Fatalf("arrayAnyOf items anyOf = %#v", arrayAnyOf.Items.AnyOf)
}
defaultValue := true
encoded, err := json.Marshal(UIElicitationSchema{
Type: UIElicitationSchemaTypeObject,
Properties: map[string]UIElicitationSchemaProperty{
"confirmed": &UIElicitationSchemaPropertyBoolean{Default: &defaultValue},
},
})
if err != nil {
t.Fatalf("marshal elicitation schema: %v", err)
}
var roundTrip UIElicitationSchema
if err := json.Unmarshal(encoded, &roundTrip); err != nil {
t.Fatalf("unmarshal marshaled elicitation schema: %v", err)
}
if _, ok := roundTrip.Properties["confirmed"].(*UIElicitationSchemaPropertyBoolean); !ok {
t.Fatalf("round-trip confirmed property = %T, want *UIElicitationSchemaPropertyBoolean", roundTrip.Properties["confirmed"])
}
}