package codex import ( "context" "encoding/json" "io" "strings" "sync" "testing" "time" "amadan.net/oficina/equipo/internal/runner/provider" ) type rpcInput struct { mu sync.Mutex writes [][]byte sent chan []byte } func (w *rpcInput) Write(b []byte) (int, error) { w.mu.Lock() w.writes = append(w.writes, append([]byte(nil), b...)) w.mu.Unlock() if w.sent != nil { w.sent <- append([]byte(nil), b...) } return len(b), nil } func (w *rpcInput) Close() error { return nil } func TestCodexRPCInterleaving(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() frames := make(chan provider.Frame, 8) done := make(chan error, 1) w := &rpcInput{sent: make(chan []byte, 4)} var mu sync.Mutex var seen []string 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 }) defer r.close() result := make(chan error, 1) go func() { v, e := r.Call(ctx, "account/read", json.RawMessage(`{"refreshToken":false}`)) if e == nil && string(v) != `{"account":null}` { e = io.ErrUnexpectedEOF } result <- e }() req := <-w.sent var call struct{ ID json.RawMessage } json.Unmarshal(req, &call) 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}}`} for i, raw := range raws { frames <- provider.Frame{Sequence: int64(i + 1), Bytes: []byte(raw)} } if e := <-result; e != nil { t.Fatal(e) } mu.Lock() if len(seen) != 3 { t.Fatal("lost interleaved frames", seen) } mu.Unlock() go func() { _, e := r.Call(ctx, "config/read", json.RawMessage(`{}`)); result <- e }() <-w.sent close(frames) done <- nil close(done) if e := <-result; e == nil { t.Fatal("EOF fabricated a reply") } } func TestCodexRPCRefusesOverrides(t *testing.T) { w := &rpcInput{} ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond) defer cancel() r := newRPC(ctx, &provider.Capture{Input: w, Frames: make(chan provider.Frame), Done: make(chan error)}, func([]byte) error { return nil }) defer r.close() for _, field := range []string{"sandboxPolicy", "sandbox", "permissions", "approvalPolicy", "approvalsReviewer", "model", "config", "collaborationMode", "cwd", "runtimeWorkspaceRoots"} { _, e := r.Call(ctx, "turn/start", json.RawMessage(`{"threadId":"t","input":[],"`+field+`":"override"}`)) if e == nil { t.Fatal("accepted", field) } } if _, e := r.Call(ctx, "command/exec", json.RawMessage(`{}`)); e == nil { t.Fatal("general RPC exposed") } w.mu.Lock() defer w.mu.Unlock() if len(w.writes) != 0 { t.Fatal("override reached stdin") } } func TestCodexRPCBadSequence(t *testing.T) { for _, raw := range []string{`{"id":"unknown","result":{}}`, `{"method":"x","params":{}}`} { ctx, cancel := context.WithTimeout(context.Background(), time.Second) frames := make(chan provider.Frame, 1) w := &rpcInput{sent: make(chan []byte, 1)} r := newRPC(ctx, &provider.Capture{Input: w, Frames: frames, Done: make(chan error)}, func([]byte) error { return nil }) result := make(chan error, 1) go func() { _, e := r.Call(ctx, "account/read", json.RawMessage(`{}`)); result <- e }() <-w.sent seq := int64(1) if strings.Contains(raw, `"method"`) { seq = 0 } frames <- provider.Frame{Sequence: seq, Bytes: []byte(raw)} if e := <-result; e != errProtocol { t.Fatal("invalid stream did not fail immediately", e) } r.close() cancel() } } type shortInput struct { closed bool mu sync.Mutex } func (w *shortInput) Write(b []byte) (int, error) { return len(b) - 1, nil } func (w *shortInput) Close() error { w.mu.Lock(); defer w.mu.Unlock(); w.closed = true; return nil } func TestCodexRPCPartialWrite(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() w := &shortInput{} r := newRPC(ctx, &provider.Capture{Input: w, Frames: make(chan provider.Frame), Done: make(chan error)}, func([]byte) error { return nil }) _, e := r.Call(ctx, "account/read", json.RawMessage(`{}`)) if e != io.ErrShortWrite { t.Fatal("partial write accepted", e) } r.close() <-r.done if !w.closed { t.Fatal("partial write left stdin open") } }