oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package codex
2
3import (
4 "context"
5 "encoding/json"
6 "io"
7 "strings"
8 "sync"
9 "testing"
10 "time"
11
12 "amadan.net/oficina/equipo/internal/runner/provider"
13)
14
15type rpcInput struct {
16 mu sync.Mutex
17 writes [][]byte
18 sent chan []byte
19}
20
21func (w *rpcInput) Write(b []byte) (int, error) {
22 w.mu.Lock()
23 w.writes = append(w.writes, append([]byte(nil), b...))
24 w.mu.Unlock()
25 if w.sent != nil {
26 w.sent <- append([]byte(nil), b...)
27 }
28 return len(b), nil
29}
30func (w *rpcInput) Close() error { return nil }
31func TestCodexRPCInterleaving(t *testing.T) {
32 ctx, cancel := context.WithTimeout(context.Background(), time.Second)
33 defer cancel()
34 frames := make(chan provider.Frame, 8)
35 done := make(chan error, 1)
36 w := &rpcInput{sent: make(chan []byte, 4)}
37 var mu sync.Mutex
38 var seen []string
39 r := newRPC(ctx, &provider.Capture{Input: w, Frames: frames, Done: done}, func(raw []byte) error { mu.Lock(); defer mu.Unlock(); seen = append(seen, string(raw)); return nil })
40 defer r.close()
41 result := make(chan error, 1)
42 go func() {
43 v, e := r.Call(ctx, "account/read", json.RawMessage(`{"refreshToken":false}`))
44 if e == nil && string(v) != `{"account":null}` {
45 e = io.ErrUnexpectedEOF
46 }
47 result <- e
48 }()
49 req := <-w.sent
50 var call struct{ ID json.RawMessage }
51 json.Unmarshal(req, &call)
52 raws := []string{`{"method":"thread/started","params":{"thread":{"id":"t"}}}`, `{"id":1,"method":"item/commandExecution/requestApproval","params":{"threadId":"t","turnId":"u"}}`, `{"id":` + string(call.ID) + `,"method":"item/tool/requestUserInput","params":{"threadId":"t","turnId":"u"}}`, `{"id":` + string(call.ID) + `,"result":{"account":null}}`}
53 for i, raw := range raws {
54 frames <- provider.Frame{Sequence: int64(i + 1), Bytes: []byte(raw)}
55 }
56 if e := <-result; e != nil {
57 t.Fatal(e)
58 }
59 mu.Lock()
60 if len(seen) != 3 {
61 t.Fatal("lost interleaved frames", seen)
62 }
63 mu.Unlock()
64 go func() { _, e := r.Call(ctx, "config/read", json.RawMessage(`{}`)); result <- e }()
65 <-w.sent
66 close(frames)
67 done <- nil
68 close(done)
69 if e := <-result; e == nil {
70 t.Fatal("EOF fabricated a reply")
71 }
72}
73func TestCodexRPCRefusesOverrides(t *testing.T) {
74 w := &rpcInput{}
75 ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond)
76 defer cancel()
77 r := newRPC(ctx, &provider.Capture{Input: w, Frames: make(chan provider.Frame), Done: make(chan error)}, func([]byte) error { return nil })
78 defer r.close()
79 for _, field := range []string{"sandboxPolicy", "sandbox", "permissions", "approvalPolicy", "approvalsReviewer", "model", "config", "collaborationMode", "cwd", "runtimeWorkspaceRoots"} {
80 _, e := r.Call(ctx, "turn/start", json.RawMessage(`{"threadId":"t","input":[],"`+field+`":"override"}`))
81 if e == nil {
82 t.Fatal("accepted", field)
83 }
84 }
85 if _, e := r.Call(ctx, "command/exec", json.RawMessage(`{}`)); e == nil {
86 t.Fatal("general RPC exposed")
87 }
88 w.mu.Lock()
89 defer w.mu.Unlock()
90 if len(w.writes) != 0 {
91 t.Fatal("override reached stdin")
92 }
93}
94func TestCodexRPCBadSequence(t *testing.T) {
95 for _, raw := range []string{`{"id":"unknown","result":{}}`, `{"method":"x","params":{}}`} {
96 ctx, cancel := context.WithTimeout(context.Background(), time.Second)
97 frames := make(chan provider.Frame, 1)
98 w := &rpcInput{sent: make(chan []byte, 1)}
99 r := newRPC(ctx, &provider.Capture{Input: w, Frames: frames, Done: make(chan error)}, func([]byte) error { return nil })
100 result := make(chan error, 1)
101 go func() { _, e := r.Call(ctx, "account/read", json.RawMessage(`{}`)); result <- e }()
102 <-w.sent
103 seq := int64(1)
104 if strings.Contains(raw, `"method"`) {
105 seq = 0
106 }
107 frames <- provider.Frame{Sequence: seq, Bytes: []byte(raw)}
108 if e := <-result; e != errProtocol {
109 t.Fatal("invalid stream did not fail immediately", e)
110 }
111 r.close()
112 cancel()
113 }
114}
115
116type shortInput struct {
117 closed bool
118 mu sync.Mutex
119}
120
121func (w *shortInput) Write(b []byte) (int, error) { return len(b) - 1, nil }
122func (w *shortInput) Close() error { w.mu.Lock(); defer w.mu.Unlock(); w.closed = true; return nil }
123func TestCodexRPCPartialWrite(t *testing.T) {
124 ctx, cancel := context.WithTimeout(context.Background(), time.Second)
125 defer cancel()
126 w := &shortInput{}
127 r := newRPC(ctx, &provider.Capture{Input: w, Frames: make(chan provider.Frame), Done: make(chan error)}, func([]byte) error { return nil })
128 _, e := r.Call(ctx, "account/read", json.RawMessage(`{}`))
129 if e != io.ErrShortWrite {
130 t.Fatal("partial write accepted", e)
131 }
132 r.close()
133 <-r.done
134 if !w.closed {
135 t.Fatal("partial write left stdin open")
136 }
137}
138