-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcodex_test.go
More file actions
984 lines (883 loc) · 33.6 KB
/
Copy pathcodex_test.go
File metadata and controls
984 lines (883 loc) · 33.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
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
package codex
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"log/slog"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
"time"
"github.com/pmenglund/codex-sdk-go/protocol"
"github.com/pmenglund/codex-sdk-go/rpc"
)
func TestThreadStartOptionsToParams(t *testing.T) {
ephemeral := true
opts := ThreadStartOptions{
Model: "gpt-test",
ModelProvider: "openai",
ServiceTier: "priority",
Cwd: "/tmp/project",
ApprovalPolicy: "never",
SandboxPolicy: map[string]any{"type": "readOnly"},
Config: map[string]any{"foo": "bar"},
ServiceName: "codex-sdk-go",
BaseInstructions: "base",
DeveloperInstructions: "dev",
Ephemeral: &ephemeral,
}
params, err := opts.toParams()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
assertEqual(t, "model", params.Model, stringPtr("gpt-test"))
assertEqual(t, "modelProvider", params.ModelProvider, stringPtr("openai"))
assertEqual(t, "serviceTier", params.ServiceTier, stringPtr("priority"))
assertEqual(t, "cwd", params.Cwd, stringPtr("/tmp/project"))
assertRawEqual(t, "approvalPolicy", params.ApprovalPolicy, MustJSON("never"))
assertRawEqual(t, "sandbox", params.Sandbox, MustJSON(map[string]any{"type": "readOnly"}))
if params.Config == nil {
t.Fatalf("expected config")
}
assertEqual(t, "config", *params.Config, map[string]any{"foo": "bar"})
assertEqual(t, "serviceName", params.ServiceName, stringPtr("codex-sdk-go"))
assertEqual(t, "baseInstructions", params.BaseInstructions, stringPtr("base"))
assertEqual(t, "developerInstructions", params.DeveloperInstructions, stringPtr("dev"))
assertEqual(t, "ephemeral", params.Ephemeral, &ephemeral)
}
func TestNewRejectsTypedNilTransport(t *testing.T) {
var transport *rpc.ReplayTransport
if _, err := New(context.Background(), Options{Transport: transport}); err == nil {
t.Fatal("expected typed-nil transport error")
}
}
func TestThreadStartOptionsRejectExperimentalRawEvents(t *testing.T) {
_, err := (ThreadStartOptions{ExperimentalRawEvents: true}).toParams()
if err == nil {
t.Fatalf("expected experimental raw events error")
}
}
func TestThreadResumeOptionsToParams(t *testing.T) {
opts := ThreadResumeOptions{
ThreadID: "thr_123",
Model: "gpt-test",
ModelProvider: "openai",
ServiceTier: "priority",
Cwd: "/tmp/project",
ApprovalPolicy: "never",
Sandbox: map[string]any{"type": "readOnly"},
Config: map[string]any{"foo": "bar"},
BaseInstructions: "base",
DeveloperInstructions: "dev",
}
params, err := opts.toParams()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
assertEqual(t, "threadId", params.ThreadID, "thr_123")
assertEqual(t, "model", params.Model, stringPtr("gpt-test"))
assertEqual(t, "modelProvider", params.ModelProvider, stringPtr("openai"))
assertEqual(t, "serviceTier", params.ServiceTier, stringPtr("priority"))
assertEqual(t, "cwd", params.Cwd, stringPtr("/tmp/project"))
assertRawEqual(t, "approvalPolicy", params.ApprovalPolicy, MustJSON("never"))
assertRawEqual(t, "sandbox", params.Sandbox, MustJSON(map[string]any{"type": "readOnly"}))
if params.Config == nil {
t.Fatalf("expected config")
}
assertEqual(t, "config", *params.Config, map[string]any{"foo": "bar"})
assertEqual(t, "baseInstructions", params.BaseInstructions, stringPtr("base"))
assertEqual(t, "developerInstructions", params.DeveloperInstructions, stringPtr("dev"))
}
func TestThreadResumeOptionsRejectEmptyThreadID(t *testing.T) {
_, err := (ThreadResumeOptions{}).toParams()
if err == nil {
t.Fatalf("expected empty thread id error")
}
}
func TestThreadResumeOptionsRejectHistory(t *testing.T) {
_, err := (ThreadResumeOptions{
ThreadID: "thr_123",
History: []ThreadResumeHistoryElem{MustJSON("h1")},
}).toParams()
if err == nil {
t.Fatalf("expected history resume error")
}
}
func TestThreadResumeOptionsRejectPath(t *testing.T) {
_, err := (ThreadResumeOptions{
ThreadID: "thr_123",
Path: "/tmp/rollout",
}).toParams()
if err == nil {
t.Fatalf("expected path resume error")
}
}
func TestBuildTurnParams(t *testing.T) {
opts := &TurnOptions{
ClientUserMessageID: "msg_123",
Cwd: "/tmp",
ApprovalPolicy: "never",
SandboxPolicy: map[string]any{"type": "readOnly"},
Model: "gpt-test",
ServiceTier: "priority",
Effort: "medium",
Summary: "short",
OutputSchema: map[string]any{"type": "object"},
}
params, err := buildTurnParams("thr_123", []Input{TextInput("hello")}, opts)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
assertEqual(t, "threadId", params.ThreadID, "thr_123")
assertEqual(t, "input", params.Input, []protocol.TurnStartParamsInputElem{TextInput("hello")})
assertEqual(t, "clientUserMessageId", params.ClientUserMessageID, stringPtr("msg_123"))
assertEqual(t, "cwd", params.Cwd, stringPtr("/tmp"))
assertRawEqual(t, "approvalPolicy", params.ApprovalPolicy, MustJSON("never"))
assertRawEqual(t, "sandboxPolicy", params.SandboxPolicy, MustJSON(map[string]any{"type": "readOnly"}))
assertEqual(t, "model", params.Model, stringPtr("gpt-test"))
assertEqual(t, "serviceTier", params.ServiceTier, stringPtr("priority"))
assertRawEqual(t, "effort", params.Effort, MustJSON("medium"))
assertRawEqual(t, "summary", params.Summary, MustJSON("short"))
assertRawEqual(t, "outputSchema", params.OutputSchema, MustJSON(map[string]any{"type": "object"}))
}
func TestBuildTurnParamsRejectCollaborationMode(t *testing.T) {
_, err := buildTurnParams("thr_123", []Input{TextInput("hello")}, &TurnOptions{CollaborationMode: "default"})
if err == nil {
t.Fatalf("expected collaboration mode error")
}
}
func TestBuildTurnParamsRejectInvalidInputs(t *testing.T) {
tests := []struct {
name string
input Input
}{
{name: "unknown type", input: Input{Type: "bogus"}},
{name: "empty text", input: Input{Type: InputTypeText}},
{name: "empty image url", input: ImageInput("")},
{name: "empty local image path", input: LocalImageInput("")},
{name: "empty audio url", input: AudioInput("")},
{name: "empty local audio path", input: LocalAudioInput("")},
{name: "empty skill name", input: SkillInput("", "/tmp/skill")},
{name: "empty skill path", input: SkillInput("skill", "")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if _, err := buildTurnParams("thr_123", []Input{tt.input}, nil); err == nil {
t.Fatalf("expected invalid input error")
}
})
}
}
func TestThreadResponseID(t *testing.T) {
response := protocol.ThreadStartResponse{Thread: &protocol.Thread{ID: "thr_1"}}
id, err := threadIDFromResponse(response.ThreadID, response.Thread)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if id != "thr_1" {
t.Fatalf("expected thread id thr_1, got %q", id)
}
if _, err := threadIDFromResponse("", nil); err == nil {
t.Fatalf("expected error for missing thread id")
}
}
func TestJSONHelpers(t *testing.T) {
if raw, err := JSON(nil); err != nil || raw != nil {
t.Fatalf("expected nil JSON, got %v err=%v", raw, err)
}
raw, err := JSON(json.RawMessage(`{"ok":true}`))
if err != nil || string(raw) != `{"ok":true}` {
t.Fatalf("unexpected raw JSON: %s err=%v", string(raw), err)
}
if _, err := JSON(json.RawMessage("{bad")); err == nil {
t.Fatalf("expected error for invalid raw JSON")
}
if _, err := normalizeJSONValue("value", json.RawMessage("{bad")); err == nil {
t.Fatalf("expected normalize error for invalid raw JSON")
}
if raw := MustJSON(map[string]any{"ok": true}); !json.Valid(raw) {
t.Fatalf("expected valid JSON")
}
}
func TestStartThreadInvalidJSONOptions(t *testing.T) {
ctx := context.Background()
c := &Codex{logger: slog.New(slog.NewTextHandler(io.Discard, nil))}
if _, err := c.StartThread(ctx, ThreadStartOptions{ApprovalPolicy: json.RawMessage("{bad")}); err == nil {
t.Fatalf("expected error for invalid approval policy")
}
}
func TestResumeThreadInvalidJSONOptions(t *testing.T) {
ctx := context.Background()
c := &Codex{logger: slog.New(slog.NewTextHandler(io.Discard, nil))}
if _, err := c.ResumeThread(ctx, ThreadResumeOptions{ThreadID: "thr_123", ApprovalPolicy: json.RawMessage("{bad")}); err == nil {
t.Fatalf("expected error for invalid approval policy")
}
}
func TestRunStreamedInvalidJSONOptions(t *testing.T) {
ctx := context.Background()
client, err := New(ctx, Options{Transport: rpc.NewReplayTransport(initializeTranscript())})
if err != nil {
t.Fatalf("new client error: %v", err)
}
defer client.Close()
thread := &Thread{client: client.Client(), id: "thr_123", logger: slog.New(slog.NewTextHandler(io.Discard, nil))}
if _, err := thread.RunStreamed(ctx, []Input{TextInput("hi")}, &TurnOptions{ApprovalPolicy: json.RawMessage("{bad")}); err == nil {
t.Fatalf("expected error for invalid approval policy")
}
}
func TestExtractTextFromItemRaw(t *testing.T) {
raw := MustJSON(map[string]any{"type": "agentMessage", "text": "hello"})
if text, ok := extractTextFromItemRaw(raw); !ok || text != "hello" {
t.Fatalf("expected text from raw")
}
raw = MustJSON(map[string]any{"wrapped": map[string]any{"type": "agentMessage", "text": "inner"}})
if text, ok := extractTextFromItemRaw(raw); !ok || text != "inner" {
t.Fatalf("expected text from nested raw")
}
if _, ok := extractTextFromItemRaw(MustJSON(map[string]any{"type": "plan", "text": "not final"})); ok {
t.Fatalf("plan text must not be treated as a final response")
}
}
func TestNotificationError(t *testing.T) {
note := rpc.Notification{Method: "error", Raw: MustJSON(map[string]any{"willRetry": true})}
if err := notificationError(note); err != nil {
t.Fatalf("expected nil error for willRetry")
}
note = rpc.Notification{Method: "error", Raw: MustJSON(map[string]any{"error": map[string]any{"message": "boom"}})}
if err := notificationError(note); err == nil || err.Error() != "boom" {
t.Fatalf("expected error boom, got %v", err)
}
note = rpc.Notification{Method: "turn/completed", Raw: MustJSON(map[string]any{"turn": map[string]any{"status": "failed", "error": map[string]any{"message": "fail"}}})}
if err := notificationError(note); err == nil || err.Error() != "fail" {
t.Fatalf("expected error fail, got %v", err)
}
note = rpc.Notification{Method: "turn/failed", Raw: MustJSON(map[string]any{"threadId": "thr_123", "turn": map[string]any{"status": "failed", "error": map[string]any{"message": "failed hard"}}})}
if err := notificationError(note); err == nil || err.Error() != "failed hard" {
t.Fatalf("expected error failed hard, got %v", err)
}
note = rpc.Notification{Method: "error", Raw: json.RawMessage("{bad")}
if err := notificationError(note); err == nil || err.Error() != "turn error" {
t.Fatalf("expected generic turn error for malformed payload, got %v", err)
}
note = rpc.Notification{Method: "turn/completed", Raw: MustJSON(map[string]any{"turn": map[string]any{"status": "failed"}})}
if err := notificationError(note); err == nil || err.Error() != "turn failed" {
t.Fatalf("expected generic completed failure, got %v", err)
}
note = rpc.Notification{Method: "turn/failed", Raw: MustJSON(map[string]any{"error": map[string]any{"message": "payload boom"}})}
if err := notificationError(note); err == nil || err.Error() != "payload boom" {
t.Fatalf("expected payload error message, got %v", err)
}
note = rpc.Notification{Method: "turn/failed", Raw: json.RawMessage("{bad")}
if err := notificationError(note); err == nil || err.Error() != "turn failed" {
t.Fatalf("expected generic turn failed for malformed payload, got %v", err)
}
}
func TestParseTurnNotificationTypedParams(t *testing.T) {
willRetry := true
itemRaw := MustJSON(map[string]any{"type": "agentMessage", "id": "item_1", "text": "done"})
var item protocol.ThreadItem
if err := json.Unmarshal(itemRaw, &item); err != nil {
t.Fatalf("decode thread item: %v", err)
}
tests := []struct {
name string
note rpc.Notification
want turnNotificationPayload
}{
{
name: "turn value",
note: rpc.Notification{Params: protocol.TurnNotification{
ThreadID: "thr_1",
Turn: &protocol.Turn{ID: "turn_1"},
}},
want: turnNotificationPayload{ThreadID: "thr_1", Turn: &protocol.Turn{ID: "turn_1"}},
},
{
name: "turn pointer",
note: rpc.Notification{Params: &protocol.TurnNotification{
ThreadID: "thr_2",
Turn: &protocol.Turn{ID: "turn_2"},
}},
want: turnNotificationPayload{ThreadID: "thr_2", Turn: &protocol.Turn{ID: "turn_2"}},
},
{
name: "item value",
note: rpc.Notification{Params: protocol.ItemCompletedNotification{
ThreadID: "thr_3",
Item: item,
}},
want: turnNotificationPayload{ThreadID: "thr_3", Item: itemRaw},
},
{
name: "item pointer",
note: rpc.Notification{Params: &protocol.ItemCompletedNotification{
ThreadID: "thr_4",
Item: item,
}},
want: turnNotificationPayload{ThreadID: "thr_4", Item: itemRaw},
},
{
name: "error value",
note: rpc.Notification{Params: protocol.ErrorNotification{
ThreadID: "thr_5",
WillRetry: willRetry,
Error: protocol.TurnError{Message: "retrying"},
}},
want: turnNotificationPayload{
ThreadID: "thr_5",
WillRetry: &willRetry,
Error: &protocol.TurnError{Message: "retrying"},
},
},
{
name: "error pointer",
note: rpc.Notification{Params: &protocol.ErrorNotification{
ThreadID: "thr_6",
WillRetry: willRetry,
Error: protocol.TurnError{Message: "retrying"},
}},
want: turnNotificationPayload{
ThreadID: "thr_6",
WillRetry: &willRetry,
Error: &protocol.TurnError{Message: "retrying"},
},
},
{
name: "thread goal cleared value",
note: rpc.Notification{Params: protocol.ThreadGoalClearedNotification{
ThreadID: "thr_7",
}},
want: turnNotificationPayload{ThreadID: "thr_7"},
},
{
name: "thread goal cleared pointer",
note: rpc.Notification{Params: &protocol.ThreadGoalClearedNotification{
ThreadID: "thr_8",
}},
want: turnNotificationPayload{ThreadID: "thr_8"},
},
{
name: "thread goal updated value",
note: rpc.Notification{Params: protocol.ThreadGoalUpdatedNotification{
ThreadID: "thr_9",
Goal: protocol.ThreadGoal{
ThreadID: "thr_9",
Objective: "ship",
Status: protocol.ThreadGoalStatusActive,
},
}},
want: turnNotificationPayload{ThreadID: "thr_9"},
},
{
name: "thread goal updated pointer",
note: rpc.Notification{Params: &protocol.ThreadGoalUpdatedNotification{
ThreadID: "thr_10",
Goal: protocol.ThreadGoal{
ThreadID: "thr_10",
Objective: "ship",
Status: protocol.ThreadGoalStatusActive,
},
}},
want: turnNotificationPayload{ThreadID: "thr_10"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseTurnNotification(tt.note)
if err != nil {
t.Fatalf("parseTurnNotification error: %v", err)
}
assertEqual(t, "payload", got, tt.want)
})
}
}
func TestTurnStreamNilAndClose(t *testing.T) {
var stream *TurnStream
if _, err := stream.Next(context.Background()); err == nil {
t.Fatalf("expected nil stream error")
}
stream.Close()
stream = &TurnStream{}
if _, err := stream.Next(context.Background()); err == nil {
t.Fatalf("expected uninitialized stream error")
}
stream.Close()
}
func TestResolveLogger(t *testing.T) {
logger := resolveLogger(nil)
if logger == nil {
t.Fatalf("expected non-nil logger")
}
logger.Info("silenced")
}
func TestAttachApprovalLogger(t *testing.T) {
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
handler := AutoApproveHandler{}
attached := attachApprovalLogger(handler, logger)
typed, ok := attached.(AutoApproveHandler)
if !ok {
t.Fatalf("expected AutoApproveHandler")
}
if typed.Logger == nil {
t.Fatalf("expected logger to be attached")
}
ptr := &AutoApproveHandler{}
attached = attachApprovalLogger(ptr, logger)
if attached == ptr {
t.Fatalf("expected pointer handler to be copied")
}
if ptr.Logger != nil {
t.Fatalf("caller-owned pointer was mutated")
}
if copied, ok := attached.(*AutoApproveHandler); !ok || copied.Logger == nil {
t.Fatalf("expected copied pointer logger to be attached: %#v", attached)
}
customLogger := slog.New(slog.NewTextHandler(io.Discard, nil))
ptr = &AutoApproveHandler{Logger: customLogger}
attached = attachApprovalLogger(ptr, logger)
if attached == ptr || ptr.Logger != customLogger {
t.Fatalf("expected existing pointer logger to be copied without mutation")
}
gotNil := attachApprovalLogger((*AutoApproveHandler)(nil), logger)
if gotNil != nil {
t.Fatalf("expected typed nil handler to normalize to nil, got %#v", gotNil)
}
var typedNilCustom *testServerRequestHandler
if got := attachApprovalLogger(typedNilCustom, logger); got != nil {
t.Fatalf("expected custom typed nil handler to normalize to nil, got %#v", got)
}
other := &testServerRequestHandler{}
if got := attachApprovalLogger(other, logger); got != other {
t.Fatalf("expected non-auto-approve handler to pass through")
}
}
func TestAutoApproveResponses(t *testing.T) {
handler := AutoApproveHandler{}
resp, err := handler.ItemCommandExecutionRequestApproval(context.Background(), protocol.CommandExecutionRequestApprovalParams{ItemID: "item", ThreadID: "thr", TurnID: "turn"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resp == nil {
t.Fatalf("expected response")
}
permissions := map[string]any{"sandbox": "workspace-write"}
permResp, err := handler.ItemPermissionsRequestApproval(context.Background(), protocol.PermissionsRequestApprovalParams{
ItemID: "item",
ThreadID: "thr",
TurnID: "turn",
Permissions: permissions,
})
if err != nil {
t.Fatalf("unexpected permissions error: %v", err)
}
assertEqual(t, "permissions", permResp.Permissions, permissions)
}
func TestAutoApproveLegacyResponses(t *testing.T) {
handler := AutoApproveHandler{}
if _, err := handler.ItemFileChangeRequestApproval(context.Background(), protocol.FileChangeRequestApprovalParams{ItemID: "item", ThreadID: "thr", TurnID: "turn"}); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if _, err := handler.ApplyPatchApproval(context.Background(), protocol.ApplyPatchApprovalParams{CallID: "call", ConversationID: "thr", FileChanges: map[string]any{}}); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if _, err := handler.ExecCommandApproval(context.Background(), protocol.ExecCommandApprovalParams{CallID: "call", ConversationID: "thr", Command: []string{"echo"}}); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if _, err := handler.ItemToolRequestUserInput(context.Background(), protocol.ToolRequestUserInputParams{ItemID: "item", ThreadID: "thr", TurnID: "turn"}); err == nil {
t.Fatalf("expected error for tool user input")
}
if _, err := handler.ItemToolCall(context.Background(), protocol.DynamicToolCallParams{}); err == nil {
t.Fatalf("expected error for dynamic tool call")
}
if _, err := handler.McpServerElicitationRequest(context.Background(), protocol.MCPServerElicitationRequestParams(nil)); err == nil {
t.Fatalf("expected error for mcp elicitation")
}
if _, err := handler.AccountChatgptAuthTokensRefresh(context.Background(), protocol.ChatgptAuthTokensRefreshParams{}); err == nil {
t.Fatalf("expected error for auth token refresh")
}
}
func TestNewUsesDefaultClientInfo(t *testing.T) {
ctx := context.Background()
client, err := New(ctx, Options{
Transport: rpc.NewReplayTransport(initializeTranscript()),
})
if err != nil {
t.Fatalf("new client error: %v", err)
}
if client.Client() == nil {
t.Fatalf("expected rpc client")
}
_ = client.Close()
}
func TestNewSpawnError(t *testing.T) {
ctx := context.Background()
_, err := New(ctx, Options{
Spawn: SpawnOptions{CodexPath: "codex-missing-binary"},
})
if err == nil {
t.Fatalf("expected spawn error")
}
}
func TestNewSpawnSurvivesInitContextCancellation(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("spawn script test is unix-only")
}
ctx, cancel := context.WithCancel(context.Background())
// Compatibility probing is covered separately; this test isolates whether
// canceling the initialization context terminates the spawned app-server.
client, err := New(ctx, Options{
Spawn: SpawnOptions{CodexPath: writeFakeCodexBinary(t)},
Logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
CompatibilityPolicy: Ignore,
})
if err != nil {
t.Fatalf("new client error: %v", err)
}
defer client.Close()
cancel()
time.Sleep(100 * time.Millisecond)
thread, err := client.StartThread(context.Background(), ThreadStartOptions{})
if err != nil {
t.Fatalf("start thread after init context cancel failed: %v", err)
}
if thread.ID() != "thr_test" {
t.Fatalf("unexpected thread id: %s", thread.ID())
}
}
func TestStartThreadOnUninitializedClient(t *testing.T) {
_, err := (&Codex{}).StartThread(context.Background(), ThreadStartOptions{})
if err == nil || err.Error() != "codex client is not initialized" {
t.Fatalf("unexpected error: %v", err)
}
}
func TestResumeThreadOnUninitializedClient(t *testing.T) {
_, err := (&Codex{}).ResumeThread(context.Background(), ThreadResumeOptions{ThreadID: "thr_123"})
if err == nil || err.Error() != "codex client is not initialized" {
t.Fatalf("unexpected error: %v", err)
}
}
func TestThreadRunOnUninitializedThread(t *testing.T) {
_, err := (&Thread{}).Run(context.Background(), "hi", nil)
if err == nil || err.Error() != "thread client is not initialized" {
t.Fatalf("unexpected error: %v", err)
}
}
func TestNilThreadRun(t *testing.T) {
var thread *Thread
_, err := thread.Run(context.Background(), "hi", nil)
if err == nil || err.Error() != "thread is nil" {
t.Fatalf("unexpected error: %v", err)
}
}
func TestRunStreamedStartCallError(t *testing.T) {
client := rpc.NewClient(rpc.NewReplayTransport([]rpc.TranscriptEntry{
writeLine(rpc.JSONRPCRequest{
ID: rpc.NewIntRequestID(1),
Method: "turn/start",
Params: mustRaw(turnStartParams("hello")),
}),
readLine(rpc.JSONRPCError{
ID: rpc.NewIntRequestID(1),
Error: rpc.JSONRPCErrorDetail{
Code: -1,
Message: "start failed",
},
}),
}), rpc.ClientOptions{})
defer client.Close()
thread := &Thread{client: client, id: "thr_123", logger: slog.New(slog.NewTextHandler(io.Discard, nil))}
if _, err := thread.RunStreamed(context.Background(), []Input{TextInput("hello")}, nil); err == nil || !strings.Contains(err.Error(), "start failed") {
t.Fatalf("expected start failed error, got %v", err)
}
}
func TestNewLogsCodexVersionMismatch(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("spawn script test is unix-only")
}
var logs bytes.Buffer
logger := slog.New(slog.NewTextHandler(&logs, nil))
client, err := New(context.Background(), Options{
Spawn: SpawnOptions{CodexPath: writeFakeCodexBinaryWithVersion(t, "999.999.999")},
Logger: logger,
CompatibilityPolicy: Warn,
})
if err != nil {
t.Fatalf("new client error: %v", err)
}
defer client.Close()
logOutput := logs.String()
if !strings.Contains(logOutput, "codex binary compatibility could not be guaranteed") {
t.Fatalf("expected version mismatch warning, got %s", logOutput)
}
if !strings.Contains(logOutput, "generated_version="+protocol.GeneratedCodexVersion) {
t.Fatalf("expected generated version in warning, got %s", logOutput)
}
}
func TestWarnIfCodexVersionCannotBeVerified(t *testing.T) {
var logs bytes.Buffer
logger := slog.New(slog.NewTextHandler(&logs, nil))
if err := checkCodexCompatibility(context.Background(), logger, filepath.Join(t.TempDir(), "missing-codex"), Warn); err != nil {
t.Fatalf("warn policy returned error: %v", err)
}
logOutput := logs.String()
if !strings.Contains(logOutput, "level=WARN") {
t.Fatalf("expected warning level, got %s", logOutput)
}
if !strings.Contains(logOutput, "codex binary compatibility could not be guaranteed") {
t.Fatalf("expected verification warning, got %s", logOutput)
}
if !strings.Contains(logOutput, "generated_version="+protocol.GeneratedCodexVersion) {
t.Fatalf("expected generated version in warning, got %s", logOutput)
}
}
func TestSpawnLogsDoNotExposeArgumentValues(t *testing.T) {
const secret = "spawn-secret-marker"
var logs bytes.Buffer
client, err := New(context.Background(), Options{
Logger: slog.New(slog.NewTextHandler(&logs, nil)),
Spawn: SpawnOptions{
CodexPath: writeFakeCodexBinary(t),
ConfigOverrides: []string{"provider_token=" + secret},
ExtraArgs: []string{"--credential", secret},
},
})
if err != nil {
t.Fatalf("new client: %v", err)
}
defer client.Close()
if strings.Contains(logs.String(), secret) {
t.Fatalf("spawn logs exposed argument values: %s", logs.String())
}
}
func TestRequireMajorMinorRejectsMismatch(t *testing.T) {
_, err := New(context.Background(), Options{
Spawn: SpawnOptions{CodexPath: writeFakeCodexBinaryWithVersion(t, "999.999.999")},
})
var compatibilityErr *CodexCompatibilityError
if !errors.As(err, &compatibilityErr) {
t.Fatalf("expected CodexCompatibilityError, got %v", err)
}
if compatibilityErr.RuntimeVersion != "999.999.999" || compatibilityErr.GeneratedVersion != protocol.GeneratedCodexVersion {
t.Fatalf("unexpected compatibility error: %#v", compatibilityErr)
}
}
func TestCompatibilityPoliciesAndPatchTolerance(t *testing.T) {
parts := strings.Split(protocol.GeneratedCodexVersion, ".")
if len(parts) < 3 {
t.Fatalf("generated version lacks patch: %q", protocol.GeneratedCodexVersion)
}
patchVersion := strings.Join([]string{parts[0], parts[1], "999"}, ".")
for _, tt := range []struct {
name string
version string
policy CompatibilityPolicy
}{
{name: "same major minor patch", version: patchVersion, policy: RequireMajorMinor},
{name: "warn unparseable", version: "dev", policy: Warn},
{name: "ignore unparseable", version: "dev", policy: Ignore},
} {
t.Run(tt.name, func(t *testing.T) {
client, err := New(context.Background(), Options{
Spawn: SpawnOptions{CodexPath: writeFakeCodexBinaryWithVersion(t, tt.version)},
CompatibilityPolicy: tt.policy,
})
if err != nil {
t.Fatalf("new client error: %v", err)
}
defer client.Close()
})
}
}
func TestRequireMajorMinorRejectsUnprobeableAndUnparseable(t *testing.T) {
for _, path := range []string{
filepath.Join(t.TempDir(), "missing-codex"),
writeFakeCodexBinaryWithVersion(t, "dev"),
} {
err := checkCodexCompatibility(context.Background(), nil, path, RequireMajorMinor)
var compatibilityErr *CodexCompatibilityError
if !errors.As(err, &compatibilityErr) {
t.Fatalf("expected typed compatibility error for %q, got %v", path, err)
}
if compatibilityErr.Cause != nil && !strings.Contains(err.Error(), compatibilityErr.Cause.Error()) {
t.Fatalf("compatibility error omitted its underlying cause: %v", err)
}
}
}
func TestCustomTransportSkipsCompatibilityProbe(t *testing.T) {
client, err := New(context.Background(), Options{
Transport: rpc.NewReplayTransport(initializeTranscript()),
Spawn: SpawnOptions{CodexPath: filepath.Join(t.TempDir(), "missing-codex")},
})
if err != nil {
t.Fatalf("custom transport should skip probe: %v", err)
}
defer client.Close()
}
func TestParseCodexVersionOutput(t *testing.T) {
if got := parseCodexVersionOutput("codex-cli 0.137.0\n"); got != "0.137.0" {
t.Fatalf("unexpected version: %q", got)
}
if got := parseCodexVersionOutput("warning\ncodex-cli v1.2.3\n"); got != "1.2.3" {
t.Fatalf("unexpected version with prefix: %q", got)
}
if got := parseCodexVersionOutput("codex-cli dev\n"); got != "" {
t.Fatalf("expected empty version, got %q", got)
}
}
func initializeTranscript() []rpc.TranscriptEntry {
info := defaultClientInfo()
return []rpc.TranscriptEntry{
writeLine(rpc.JSONRPCRequest{
ID: rpc.NewIntRequestID(1),
Method: "initialize",
Params: mustRaw(protocol.InitializeParams{ClientInfo: info}),
}),
readLine(rpc.JSONRPCResponse{
ID: rpc.NewIntRequestID(1),
Result: mustRaw(map[string]any{}),
}),
writeLine(rpc.JSONRPCNotification{Method: "initialized"}),
}
}
func writeFakeCodexBinary(t *testing.T) string {
return writeFakeCodexBinaryWithVersion(t, protocol.GeneratedCodexVersion)
}
func writeFakeCodexBinaryWithVersion(t *testing.T, version string) string {
t.Helper()
path := filepath.Join(t.TempDir(), "fake-codex")
script := `#!/bin/sh
if [ "$1" = "--version" ]; then
printf 'codex-cli ` + version + `\n'
exit 0
fi
extract_id() {
printf '%s\n' "$1" | sed -n 's/.*"id":\([0-9][0-9]*\).*/\1/p'
}
while IFS= read -r line; do
case "$line" in
*'"method":"initialize"'*)
id=$(extract_id "$line")
if [ -z "$id" ]; then id=1; fi
printf '{"jsonrpc":"2.0","id":%s,"result":{}}\n' "$id"
;;
*'"method":"thread/start"'*)
id=$(extract_id "$line")
if [ -z "$id" ]; then id=2; fi
printf '{"jsonrpc":"2.0","id":%s,"result":{"threadId":"thr_test"}}\n' "$id"
;;
esac
done
`
if err := os.WriteFile(path, []byte(script), 0o755); err != nil {
t.Fatalf("write fake codex: %v", err)
}
return path
}
func TestInputHelpers(t *testing.T) {
if input := TextInput("hi"); input.Type != InputTypeText || input.Text != "hi" {
t.Fatalf("unexpected text input: %#v", input)
}
if input := ImageInput("https://example.com"); input.Type != InputTypeImage || input.URL != "https://example.com" {
t.Fatalf("unexpected image input: %#v", input)
}
if input := LocalImageInput("/tmp/img.png"); input.Type != InputTypeLocalImage || input.Path != "/tmp/img.png" {
t.Fatalf("unexpected local image input: %#v", input)
}
if input := AudioInput("https://example.com/audio.mp3"); input.Type != InputTypeAudio || input.URL != "https://example.com/audio.mp3" {
t.Fatalf("unexpected audio input: %#v", input)
}
if input := LocalAudioInput("/tmp/audio.mp3"); input.Type != InputTypeLocalAudio || input.Path != "/tmp/audio.mp3" {
t.Fatalf("unexpected local audio input: %#v", input)
}
if input := SkillInput("skill", "/tmp/skill"); input.Type != InputTypeSkill || input.Name != "skill" || input.Path != "/tmp/skill" {
t.Fatalf("unexpected skill input: %#v", input)
}
mention := MentionInput("thread")
if mention.Text != "@thread" || len(mention.TextElements) != 1 {
t.Fatalf("unexpected mention input: %#v", mention)
}
if err := mention.validate(); err != nil {
t.Fatalf("mention should validate: %v", err)
}
invalid := Input{Type: InputTypeText, Text: "x", TextElements: []protocol.TextElement{{
ByteRange: protocol.TextElementByteRange{Start: 2, End: 3},
}}}
if err := invalid.validate(); err == nil {
t.Fatalf("expected invalid text element range")
}
}
func TestMatchThreadID(t *testing.T) {
note := rpc.Notification{Raw: MustJSON(map[string]any{"threadId": "thr_1"})}
if !matchesThreadID(note, "thr_1") {
t.Fatalf("expected matching thread id")
}
if matchesThreadID(note, "thr_2") {
t.Fatalf("expected non-matching thread id")
}
typed := rpc.Notification{Params: protocol.ThreadGoalUpdatedNotification{ThreadID: "thr_3"}}
if !matchesThreadID(typed, "thr_3") {
t.Fatalf("expected typed goal notification to match thread id")
}
if matchesThreadID(typed, "thr_4") {
t.Fatalf("expected typed goal notification not to match another thread id")
}
empty := rpc.Notification{Raw: MustJSON(map[string]any{})}
if !matchesThreadID(empty, "thr_1") {
t.Fatalf("expected match when thread id missing")
}
}
type testServerRequestHandler struct{}
func (h *testServerRequestHandler) AccountChatgptAuthTokensRefresh(ctx context.Context, params protocol.ChatgptAuthTokensRefreshParams) (*protocol.ChatgptAuthTokensRefreshResponse, error) {
return nil, nil
}
func (h *testServerRequestHandler) ApplyPatchApproval(ctx context.Context, params protocol.ApplyPatchApprovalParams) (*protocol.ApplyPatchApprovalResponse, error) {
return nil, nil
}
func (h *testServerRequestHandler) AttestationGenerate(ctx context.Context, params protocol.AttestationGenerateParams) (*protocol.AttestationGenerateResponse, error) {
return nil, nil
}
func (h *testServerRequestHandler) ExecCommandApproval(ctx context.Context, params protocol.ExecCommandApprovalParams) (*protocol.ExecCommandApprovalResponse, error) {
return nil, nil
}
func (h *testServerRequestHandler) ItemCommandExecutionRequestApproval(ctx context.Context, params protocol.CommandExecutionRequestApprovalParams) (*protocol.CommandExecutionRequestApprovalResponse, error) {
return nil, nil
}
func (h *testServerRequestHandler) ItemFileChangeRequestApproval(ctx context.Context, params protocol.FileChangeRequestApprovalParams) (*protocol.FileChangeRequestApprovalResponse, error) {
return nil, nil
}
func (h *testServerRequestHandler) ItemPermissionsRequestApproval(ctx context.Context, params protocol.PermissionsRequestApprovalParams) (*protocol.PermissionsRequestApprovalResponse, error) {
return nil, nil
}
func (h *testServerRequestHandler) ItemToolCall(ctx context.Context, params protocol.DynamicToolCallParams) (*protocol.DynamicToolCallResponse, error) {
return nil, nil
}
func (h *testServerRequestHandler) ItemToolRequestUserInput(ctx context.Context, params protocol.ToolRequestUserInputParams) (*protocol.ToolRequestUserInputResponse, error) {
return nil, nil
}
func (h *testServerRequestHandler) McpServerElicitationRequest(ctx context.Context, params protocol.MCPServerElicitationRequestParams) (*protocol.MCPServerElicitationRequestResponse, error) {
return nil, nil
}
func assertEqual(t *testing.T, name string, got, want any) {
t.Helper()
if !reflect.DeepEqual(got, want) {
t.Fatalf("unexpected %s: %#v (want %#v)", name, got, want)
}
}
func assertRawEqual(t *testing.T, name string, got any, want json.RawMessage) {
t.Helper()
if got == nil {
t.Fatalf("expected %s to be set", name)
}
raw, ok := got.(json.RawMessage)
if !ok {
t.Fatalf("expected %s to be json.RawMessage, got %T", name, got)
}
if string(raw) != string(want) {
t.Fatalf("unexpected %s: %s (want %s)", name, string(raw), string(want))
}
}