forked from agentclientprotocol/kotlin-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionConfigSelectOptionsSerializerTest.kt
More file actions
667 lines (614 loc) · 26.9 KB
/
SessionConfigSelectOptionsSerializerTest.kt
File metadata and controls
667 lines (614 loc) · 26.9 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
@file:OptIn(com.agentclientprotocol.annotations.UnstableApi::class)
package com.agentclientprotocol.model
import com.agentclientprotocol.rpc.ACPJson
import com.agentclientprotocol.rpc.JsonRpcNotification
import com.agentclientprotocol.rpc.JsonRpcRequest
import com.agentclientprotocol.rpc.JsonRpcResponse
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.jsonObject
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
import kotlin.test.assertNotNull
class SessionConfigSelectOptionsSerializerTest {
@Test
fun `decode session-new response params with config options`() {
val json = """
{
"_direction": "incoming",
"_type": "response",
"id": 1,
"method": "session/new",
"params": {
"sessionId": "019c1edc-aae9-70d0-ba38-024381e6c6c3",
"modes": {
"currentModeId": "auto",
"availableModes": [
{
"id": "read-only",
"name": "Read Only",
"description": "Codex can read files in the current workspace. Approval is required to edit files or access the internet."
},
{
"id": "auto",
"name": "Default",
"description": "Codex can read and edit files in the current workspace, and run commands. Approval is required to access the internet or edit other files. (Identical to Agent mode)"
},
{
"id": "full-access",
"name": "Full Access",
"description": "Codex can edit files outside this workspace and access the internet without asking for approval. Exercise caution when using."
}
]
},
"models": {
"currentModelId": "gpt-5.2-codex/xhigh",
"availableModels": [
{
"modelId": "gpt-5.2-codex/low",
"name": "gpt-5.2-codex (low)",
"description": "Latest frontier agentic coding model. Fast responses with lighter reasoning"
}
]
},
"configOptions": [
{
"id": "mode",
"name": "Approval Preset",
"description": "Choose an approval and sandboxing preset for your session",
"category": "mode",
"type": "select",
"currentValue": "auto",
"options": [
{
"value": "read-only",
"name": "Read Only",
"description": "Codex can read files in the current workspace. Approval is required to edit files or access the internet."
},
{
"value": "auto",
"name": "Default",
"description": "Codex can read and edit files in the current workspace, and run commands. Approval is required to access the internet or edit other files. (Identical to Agent mode)"
},
{
"value": "full-access",
"name": "Full Access",
"description": "Codex can edit files outside this workspace and access the internet without asking for approval. Exercise caution when using."
}
]
}
]
}
}
""".trimIndent()
val request = ACPJson.decodeFromString(JsonRpcRequest.serializer(), json)
assertEquals("session/new", request.method.name)
val response = ACPJson.decodeFromJsonElement(
NewSessionResponse.serializer(),
request.params ?: JsonNull
)
assertEquals(SessionId("019c1edc-aae9-70d0-ba38-024381e6c6c3"), response.sessionId)
assertEquals(SessionModeId("auto"), response.modes?.currentModeId)
assertEquals(ModelId("gpt-5.2-codex/xhigh"), response.models?.currentModelId)
val configOptions = response.configOptions
assertNotNull(configOptions)
val first = configOptions.first()
val select = assertIs<SessionConfigOption.Select>(first)
val options = assertIs<SessionConfigSelectOptions.Flat>(select.options)
assertEquals(SessionConfigValueId("auto"), select.currentValue)
assertEquals(3, options.options.size)
}
@Test
fun `decode session-new response params with grouped config options`() {
val json = """
{
"id": "models",
"name": "Model",
"currentValue": "ask",
"type": "select",
"options": [
{
"group": "Provider A",
"options": [
{
"value": "model-1",
"name": "Model 1",
"description": "The fastest model"
}
]
},
{
"group": "Provider B",
"options": [
{
"value": "model-2",
"name": "Model 2",
"description": "The most powerful model"
}
]
}
]
}
""".trimIndent()
val option = ACPJson.decodeFromString(SessionConfigOption.serializer(), json)
val select = assertIs<SessionConfigOption.Select>(option)
val options = assertIs<SessionConfigSelectOptions.Grouped>(select.options)
assertEquals(SessionConfigValueId("ask"), select.currentValue)
assertEquals(2, options.groups.size)
assertEquals("Provider A", options.groups[0].group.value)
assertEquals(1, options.groups[0].options.size)
assertEquals(SessionConfigValueId("model-1"), options.groups[0].options[0].value)
}
@Test
fun `decode rfd session-new response example with config options`() {
val json = """
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"sessionId": "sess_abc123def456",
"configOptions": [
{
"id": "mode",
"name": "Session Mode",
"description": "Optional description for the Client to display to the user.",
"category": "mode",
"type": "select",
"currentValue": "ask",
"options": [
{
"value": "ask",
"name": "Ask",
"description": "Request permission before making any changes"
},
{
"value": "code",
"name": "Code",
"description": "Write and modify code with full tool access"
}
]
},
{
"id": "models",
"name": "Model",
"category": "model",
"type": "select",
"currentValue": "ask",
"options": [
{
"value": "model-1",
"name": "Model 1",
"description": "The fastest model"
},
{
"value": "model-2",
"name": "Model 2",
"description": "The most powerful model"
}
]
}
]
}
}
""".trimIndent()
val response = ACPJson.decodeFromString(JsonRpcResponse.serializer(), json)
val result = ACPJson.decodeFromJsonElement(
NewSessionResponse.serializer(),
response.result ?: JsonNull
)
val configOptions = result.configOptions
assertNotNull(configOptions)
assertEquals(2, configOptions.size)
val first = assertIs<SessionConfigOption.Select>(configOptions[0])
val firstOptions = assertIs<SessionConfigSelectOptions.Flat>(first.options)
assertEquals(SessionConfigValueId("ask"), first.currentValue)
assertEquals(2, firstOptions.options.size)
}
@Test
fun `decode rfd session-set-config-option request example`() {
val json = """
{
"jsonrpc": "2.0",
"id": 2,
"method": "session/set_config_option",
"params": {
"sessionId": "sess_abc123def456",
"configId": "mode",
"value": "code"
}
}
""".trimIndent()
val request = ACPJson.decodeFromString(JsonRpcRequest.serializer(), json)
assertEquals("session/set_config_option", request.method.name)
val params = ACPJson.decodeFromJsonElement(
SetSessionConfigOptionRequest.serializer(),
request.params ?: JsonNull
)
assertEquals(SessionId("sess_abc123def456"), params.sessionId)
assertEquals(SessionConfigId("mode"), params.configId)
val stringValue = assertIs<SessionConfigOptionValue.StringValue>(params.value)
assertEquals("code", stringValue.value)
}
@Test
fun `decode rfd session-set-config-option response example`() {
val json = """
{
"jsonrpc": "2.0",
"id": 2,
"result": {
"configOptions": [
{
"id": "mode",
"name": "Session Mode",
"type": "select",
"currentValue": "ask",
"options": [
{
"value": "ask",
"name": "Ask",
"description": "Request permission before making any changes"
},
{
"value": "code",
"name": "Code",
"description": "Write and modify code with full tool access"
}
]
},
{
"id": "models",
"name": "Model",
"type": "select",
"currentValue": "ask",
"options": [
{
"value": "model-1",
"name": "Model 1",
"description": "The fastest model"
},
{
"value": "model-2",
"name": "Model 2",
"description": "The most powerful model"
}
]
}
]
}
}
""".trimIndent()
val response = ACPJson.decodeFromString(JsonRpcResponse.serializer(), json)
val result = ACPJson.decodeFromJsonElement(
SetSessionConfigOptionResponse.serializer(),
response.result ?: JsonNull
)
assertEquals(2, result.configOptions.size)
val first = assertIs<SessionConfigOption.Select>(result.configOptions[0])
val options = assertIs<SessionConfigSelectOptions.Flat>(first.options)
assertEquals(SessionConfigValueId("ask"), first.currentValue)
assertEquals(2, options.options.size)
}
@Test
fun `decode rfd config-option-update notification example`() {
val json = """
{
"jsonrpc": "2.0",
"method": "session/update",
"params": {
"sessionId": "sess_abc123def456",
"update": {
"sessionUpdate": "config_option_update",
"configOptions": [
{
"id": "mode",
"name": "Session Mode",
"type": "select",
"currentValue": "ask",
"options": [
{
"value": "ask",
"name": "Ask",
"description": "Request permission before making any changes"
},
{
"value": "code",
"name": "Code",
"description": "Write and modify code with full tool access"
}
]
},
{
"id": "models",
"name": "Model",
"type": "select",
"currentValue": "ask",
"options": [
{
"value": "model-1",
"name": "Model 1",
"description": "The fastest model"
},
{
"value": "model-2",
"name": "Model 2",
"description": "The most powerful model"
}
]
}
]
}
}
}
""".trimIndent()
val notification = ACPJson.decodeFromString(JsonRpcNotification.serializer(), json)
assertEquals("session/update", notification.method.name)
val params = ACPJson.decodeFromJsonElement(
SessionNotification.serializer(),
notification.params ?: JsonNull
)
assertEquals(SessionId("sess_abc123def456"), params.sessionId)
val update = assertIs<SessionUpdate.ConfigOptionUpdate>(params.update)
assertEquals(2, update.configOptions.size)
val first = assertIs<SessionConfigOption.Select>(update.configOptions[0])
val options = assertIs<SessionConfigSelectOptions.Flat>(first.options)
assertEquals(2, options.options.size)
}
@Test
fun `decode boolean config option`() {
val json = """
{
"id": "auto_approve",
"name": "Auto Approve",
"description": "Automatically approve all tool calls",
"type": "boolean",
"currentValue": true
}
""".trimIndent()
val option = ACPJson.decodeFromString(SessionConfigOption.serializer(), json)
val boolOption = assertIs<SessionConfigOption.BooleanOption>(option)
assertEquals(SessionConfigId("auto_approve"), boolOption.id)
assertEquals("Auto Approve", boolOption.name)
assertEquals("Automatically approve all tool calls", boolOption.description)
assertEquals(true, boolOption.currentValue)
}
@Test
fun `decode boolean config option with false value`() {
val json = """
{
"id": "verbose",
"name": "Verbose",
"type": "boolean",
"currentValue": false
}
""".trimIndent()
val option = ACPJson.decodeFromString(SessionConfigOption.serializer(), json)
val boolOption = assertIs<SessionConfigOption.BooleanOption>(option)
assertEquals(SessionConfigId("verbose"), boolOption.id)
assertEquals(false, boolOption.currentValue)
assertEquals(null, boolOption.description)
}
@Test
fun `encode boolean config option roundtrip`() {
val original = SessionConfigOption.BooleanOption(
id = SessionConfigId("auto_approve"),
name = "Auto Approve",
description = "Automatically approve all tool calls",
currentValue = true
)
val encoded = ACPJson.encodeToString(SessionConfigOption.serializer(), original)
val decoded = ACPJson.decodeFromString(SessionConfigOption.serializer(), encoded)
val boolOption = assertIs<SessionConfigOption.BooleanOption>(decoded)
assertEquals(original.id, boolOption.id)
assertEquals(original.name, boolOption.name)
assertEquals(original.description, boolOption.description)
assertEquals(original.currentValue, boolOption.currentValue)
}
@Test
fun `decode session-new response with mixed select and boolean config options`() {
val json = """
{
"sessionId": "sess_mixed",
"configOptions": [
{
"id": "mode",
"name": "Session Mode",
"type": "select",
"currentValue": "ask",
"options": [
{
"value": "ask",
"name": "Ask"
}
]
},
{
"id": "auto_approve",
"name": "Auto Approve",
"type": "boolean",
"currentValue": true
}
]
}
""".trimIndent()
val response = ACPJson.decodeFromString(NewSessionResponse.serializer(), json)
val configOptions = response.configOptions
assertNotNull(configOptions)
assertEquals(2, configOptions.size)
assertIs<SessionConfigOption.Select>(configOptions[0])
val boolOption = assertIs<SessionConfigOption.BooleanOption>(configOptions[1])
assertEquals(true, boolOption.currentValue)
}
@Test
fun `decode set config option request with boolean value`() {
val json = """
{
"jsonrpc": "2.0",
"id": 3,
"method": "session/set_config_option",
"params": {
"sessionId": "sess_abc123def456",
"configId": "auto_approve",
"type": "boolean",
"value": true
}
}
""".trimIndent()
val request = ACPJson.decodeFromString(JsonRpcRequest.serializer(), json)
val params = ACPJson.decodeFromJsonElement(
SetSessionConfigOptionRequest.serializer(),
request.params ?: JsonNull
)
assertEquals(SessionId("sess_abc123def456"), params.sessionId)
assertEquals(SessionConfigId("auto_approve"), params.configId)
val boolValue = assertIs<SessionConfigOptionValue.BoolValue>(params.value)
assertEquals(true, boolValue.value)
}
@Test
fun `encode set config option request with boolean value roundtrip`() {
val original = SetSessionConfigOptionRequest(
sessionId = SessionId("sess_test"),
configId = SessionConfigId("verbose"),
value = SessionConfigOptionValue.BoolValue(false)
)
val encoded = ACPJson.encodeToString(SetSessionConfigOptionRequest.serializer(), original)
// Verify the encoded JSON contains "type":"boolean" at top level
val encodedJson = ACPJson.parseToJsonElement(encoded)
assertEquals("boolean", encodedJson.jsonObject["type"]?.let { (it as? JsonPrimitive)?.content })
val decoded = ACPJson.decodeFromString(SetSessionConfigOptionRequest.serializer(), encoded)
assertEquals(original.sessionId, decoded.sessionId)
assertEquals(original.configId, decoded.configId)
val boolValue = assertIs<SessionConfigOptionValue.BoolValue>(decoded.value)
assertEquals(false, boolValue.value)
}
@Test
fun `encode set config option request with string value roundtrip`() {
val original = SetSessionConfigOptionRequest(
sessionId = SessionId("sess_test"),
configId = SessionConfigId("mode"),
value = SessionConfigOptionValue.StringValue("code")
)
val encoded = ACPJson.encodeToString(SetSessionConfigOptionRequest.serializer(), original)
// Verify the encoded JSON does NOT contain "type" field for string values
val encodedJson = ACPJson.parseToJsonElement(encoded)
assertEquals(null, encodedJson.jsonObject["type"])
val decoded = ACPJson.decodeFromString(SetSessionConfigOptionRequest.serializer(), encoded)
assertEquals(original.sessionId, decoded.sessionId)
assertEquals(original.configId, decoded.configId)
val stringValue = assertIs<SessionConfigOptionValue.StringValue>(decoded.value)
assertEquals("code", stringValue.value)
}
// --- Edge case tests ---
@Test
fun `quoted string true is deserialized as StringValue not BoolValue`() {
val json = """{"sessionId":"s","configId":"c","value":"true"}"""
val request = ACPJson.decodeFromString(SetSessionConfigOptionRequest.serializer(), json)
val stringValue = assertIs<SessionConfigOptionValue.StringValue>(request.value)
assertEquals("true", stringValue.value)
}
@Test
fun `quoted string false is deserialized as StringValue not BoolValue`() {
val json = """{"sessionId":"s","configId":"c","value":"false"}"""
val request = ACPJson.decodeFromString(SetSessionConfigOptionRequest.serializer(), json)
val stringValue = assertIs<SessionConfigOptionValue.StringValue>(request.value)
assertEquals("false", stringValue.value)
}
@Test
fun `decode set config option request without type field is backward compatible`() {
// Old format without "type" field should still work as StringValue
val json = """{"sessionId":"s","configId":"c","value":"model-1"}"""
val request = ACPJson.decodeFromString(SetSessionConfigOptionRequest.serializer(), json)
assertEquals(SessionId("s"), request.sessionId)
assertEquals(SessionConfigId("c"), request.configId)
val stringValue = assertIs<SessionConfigOptionValue.StringValue>(request.value)
assertEquals("model-1", stringValue.value)
}
@Test
fun `decode set config option request with type boolean`() {
val json = """{"sessionId":"s","configId":"c","type":"boolean","value":true}"""
val request = ACPJson.decodeFromString(SetSessionConfigOptionRequest.serializer(), json)
val boolValue = assertIs<SessionConfigOptionValue.BoolValue>(request.value)
assertEquals(true, boolValue.value)
}
@Test
fun `decode set config option request with unknown type falls back to UnknownValue`() {
val json = """{"sessionId":"s","configId":"c","type":"multi_select","value":["a","b"]}"""
val request = ACPJson.decodeFromString(SetSessionConfigOptionRequest.serializer(), json)
assertIs<SessionConfigOptionValue.UnknownValue>(request.value)
}
@Test
fun `numeric value in config option without type deserializes as UnknownValue`() {
val json = """{"sessionId":"s","configId":"c","value":42}"""
val request = ACPJson.decodeFromString(SetSessionConfigOptionRequest.serializer(), json)
val unknown = assertIs<SessionConfigOptionValue.UnknownValue>(request.value)
assertEquals(JsonPrimitive(42), unknown.rawElement)
}
@Test
fun `UnknownValue roundtrip preserves raw element`() {
val json = """{"sessionId":"s","configId":"c","value":42}"""
val request = ACPJson.decodeFromString(SetSessionConfigOptionRequest.serializer(), json)
val encoded = ACPJson.encodeToString(SetSessionConfigOptionRequest.serializer(), request)
val decoded = ACPJson.decodeFromString(SetSessionConfigOptionRequest.serializer(), encoded)
val unknown = assertIs<SessionConfigOptionValue.UnknownValue>(decoded.value)
assertEquals(JsonPrimitive(42), unknown.rawElement)
}
// --- Factory method and extension function tests ---
@Test
fun `SessionConfigOption boolean factory creates BooleanOption`() {
val option = SessionConfigOption.boolean("verbose", "Verbose", true, "Enable verbose logging")
assertIs<SessionConfigOption.BooleanOption>(option)
assertEquals(SessionConfigId("verbose"), option.id)
assertEquals("Verbose", option.name)
assertEquals("Enable verbose logging", option.description)
assertEquals(true, option.currentValue)
}
@Test
fun `SessionConfigOption select factory creates Select`() {
val options = SessionConfigSelectOptions.Flat(listOf(
SessionConfigSelectOption(SessionConfigValueId("a"), "Option A")
))
val option = SessionConfigOption.select("mode", "Mode", "a", options, "Pick a mode")
assertIs<SessionConfigOption.Select>(option)
assertEquals(SessionConfigId("mode"), option.id)
assertEquals("Mode", option.name)
assertEquals("Pick a mode", option.description)
assertEquals(SessionConfigValueId("a"), option.currentValue)
}
@Test
fun `SessionConfigOptionValue of String creates StringValue`() {
val value = SessionConfigOptionValue.of("code")
val stringValue = assertIs<SessionConfigOptionValue.StringValue>(value)
assertEquals("code", stringValue.value)
}
@Test
fun `SessionConfigOptionValue of Boolean creates BoolValue`() {
val value = SessionConfigOptionValue.of(true)
val boolValue = assertIs<SessionConfigOptionValue.BoolValue>(value)
assertEquals(true, boolValue.value)
}
@Test
fun `boolean value without type field deserializes as BoolValue`() {
val json = """{"sessionId":"s","configId":"c","value":true}"""
val request = ACPJson.decodeFromString(SetSessionConfigOptionRequest.serializer(), json)
val boolValue = assertIs<SessionConfigOptionValue.BoolValue>(request.value)
assertEquals(true, boolValue.value)
}
@Test
fun `false value without type field deserializes as BoolValue`() {
val json = """{"sessionId":"s","configId":"c","value":false}"""
val request = ACPJson.decodeFromString(SetSessionConfigOptionRequest.serializer(), json)
val boolValue = assertIs<SessionConfigOptionValue.BoolValue>(request.value)
assertEquals(false, boolValue.value)
}
@Test
fun `unknown type roundtrip preserves type and value`() {
val json = """{"sessionId":"s","configId":"c","type":"multi_select","value":["a","b"]}"""
val request = ACPJson.decodeFromString(SetSessionConfigOptionRequest.serializer(), json)
assertIs<SessionConfigOptionValue.UnknownValue>(request.value)
// Re-serialize and verify the type and value are preserved
val encoded = ACPJson.encodeToString(SetSessionConfigOptionRequest.serializer(), request)
val reDecoded = ACPJson.parseToJsonElement(encoded).jsonObject
assertEquals("multi_select", (reDecoded["type"] as? JsonPrimitive)?.content)
assertNotNull(reDecoded["value"])
}
}