Files
pipelines/internal/codex/approvals_test.go
T

185 lines
7.0 KiB
Go

package codex
import (
"context"
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
"github.com/coder/websocket"
"github.com/coder/websocket/wsjson"
)
func approvalChat(t *testing.T) (*Chat, <-chan rpcMessage) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
responses := make(chan rpcMessage, 8)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
conn, err := websocket.Accept(w, r, nil)
if err != nil {
return
}
defer conn.CloseNow()
for {
var msg rpcMessage
if wsjson.Read(ctx, conn, &msg) != nil {
return
}
responses <- msg
}
}))
conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http"), nil)
if err != nil {
t.Fatal(err)
}
c := &Chat{conn: conn, ctx: ctx, cancel: cancel, forms: map[string]pendingForm{}}
t.Cleanup(func() { cancel(); conn.CloseNow(); srv.Close() })
return c, responses
}
func TestApprovalCallbacksAreDistinctAndSessionOwned(t *testing.T) {
c, replies := approvalChat(t)
for _, id := range []string{`1`, `"second-callback"`} {
c.receiveApproval(rpcMessage{ID: json.RawMessage(id), Method: "item/commandExecution/requestApproval", Params: json.RawMessage(`{"threadId":"thread-a","itemId":"shared-item","command":"echo test","cwd":"/work"}`)})
}
pending, _ := c.Permissions(context.Background(), "thread-a")
if len(pending) != 2 || pending[0].RequestID == pending[1].RequestID {
t.Fatalf("callbacks collapsed: %+v", pending)
}
for i, p := range pending {
if err := c.Reply(context.Background(), protocol.ChatReplyRequest{SessionID: "foreign", RequestID: p.RequestID, Decision: protocol.ChatDecisionOnce}); err == nil {
t.Fatal("cross-session approval accepted")
}
decision := protocol.ChatDecisionOnce
want := "accept"
if i == 1 {
decision = protocol.ChatDecisionAlways
want = "acceptForSession"
}
if err := c.Reply(context.Background(), protocol.ChatReplyRequest{SessionID: "thread-a", RequestID: p.RequestID, Decision: decision}); err != nil {
t.Fatal(err)
}
select {
case msg := <-replies:
var result struct {
Decision string `json:"decision"`
}
json.Unmarshal(msg.Result, &result)
if result.Decision != want || string(msg.ID) != string(capturedApprovalID(p.RequestID)) {
t.Fatalf("wrong reply: %+v", msg)
}
case <-time.After(time.Second):
t.Fatal("approval response not delivered")
}
}
pending, _ = c.Permissions(context.Background(), "thread-a")
if len(pending) != 0 {
t.Fatal("completed approvals retained")
}
}
func capturedApprovalID(id string) []byte {
raw, _ := base64.RawURLEncoding.DecodeString(strings.TrimPrefix(id, "codex_"))
return raw
}
func TestPermissionGrantNeverAddsUnrequestedAccess(t *testing.T) {
for _, decision := range []string{protocol.ChatDecisionOnce, protocol.ChatDecisionAlways, protocol.ChatDecisionReject} {
t.Run(decision, func(t *testing.T) {
c, replies := approvalChat(t)
c.receiveApproval(rpcMessage{ID: json.RawMessage(`99`), Method: "item/permissions/requestApproval", Params: json.RawMessage(`{"threadId":"thread-a","itemId":"item","permissions":{"network":{"enabled":true},"fileSystem":null,"unrecognized":"never grant"}}`)})
pending, _ := c.Permissions(context.Background(), "thread-a")
if err := c.Reply(context.Background(), protocol.ChatReplyRequest{SessionID: "thread-a", RequestID: pending[0].RequestID, Decision: decision}); err != nil {
t.Fatal(err)
}
msg := <-replies
var response struct {
Permissions map[string]any `json:"permissions"`
Scope string `json:"scope"`
}
json.Unmarshal(msg.Result, &response)
if _, ok := response.Permissions["unrecognized"]; ok {
t.Fatal("granted unknown access")
}
want := 1
if decision == protocol.ChatDecisionReject {
want = 0
}
if len(response.Permissions) != want {
t.Fatalf("grant=%s", msg.Result)
}
if (response.Scope == "session") != (decision == protocol.ChatDecisionAlways) {
t.Fatalf("wrong scope: %s", msg.Result)
}
})
}
}
func TestCodexQuestionReadbackAndOwnership(t *testing.T) {
c, _ := approvalChat(t)
c.forms["question"] = pendingForm{threadID: "thread-a", form: protocol.ChatForm{ID: "question", SessionID: "thread-a"}}
forms, _ := c.Forms(context.Background(), "thread-a")
if len(forms) != 1 {
t.Fatal("pending question not recoverable")
}
if err := c.Answer(context.Background(), protocol.ChatAnswerRequest{SessionID: "foreign", FormID: "question", Answers: map[string]any{"a": "no"}}); err == nil {
t.Fatal("foreign session answered question")
}
forms, _ = c.Forms(context.Background(), "thread-a")
if len(forms) != 1 {
t.Fatal("refused reply consumed pending question")
}
}
func TestMCPElicitationUsesTypedAnswersAndExplicitCancellation(t *testing.T) {
for _, action := range []string{"accept", "cancel", "decline"} {
t.Run(action, func(t *testing.T) {
c, replies := approvalChat(t)
c.handleServerRequest(rpcMessage{ID: json.RawMessage(`"mcp-request"`), Method: "mcpServer/elicitation/request", Params: json.RawMessage(`{"threadId":"thread-a","mode":"form","message":"MCP input","requestedSchema":{"type":"object","properties":{"enabled":{"type":"boolean"},"count":{"type":"integer","minimum":0}},"required":["enabled","count"]}}`)})
forms, err := c.Forms(context.Background(), "thread-a")
if err != nil || len(forms) != 1 || len(forms[0].Fields) != 2 {
t.Fatalf("forms=%+v err=%v", forms, err)
}
if err := c.Answer(context.Background(), protocol.ChatAnswerRequest{SessionID: "thread-a", FormID: forms[0].ID, Action: action, Answers: map[string]any{"enabled": false, "count": 0}}); err != nil {
t.Fatal(err)
}
msg := <-replies
var result struct {
Action string `json:"action"`
Content map[string]any `json:"content"`
}
json.Unmarshal(msg.Result, &result)
if result.Action != action {
t.Fatalf("wrong action: %s", msg.Result)
}
if action == "accept" {
if result.Content["enabled"] != false || result.Content["count"] != float64(0) {
t.Fatalf("lost typed values: %s", msg.Result)
}
} else if result.Content != nil {
t.Fatal("cancel/decline returned content")
}
})
}
}
func TestResolvedCallbackClearsOnlyItsOwner(t *testing.T) {
c, _ := approvalChat(t)
c.receiveApproval(rpcMessage{ID: json.RawMessage(`7`), Method: "item/fileChange/requestApproval", Params: json.RawMessage(`{"threadId":"thread-a","itemId":"item","grantRoot":"/work"}`)})
c.handleNotification(rpcMessage{Method: "serverRequest/resolved", Params: json.RawMessage(`{"threadId":"foreign","requestId":7}`)})
pending, _ := c.Permissions(context.Background(), "thread-a")
if len(pending) != 1 {
t.Fatal("foreign notification consumed approval")
}
c.handleNotification(rpcMessage{Method: "serverRequest/resolved", Params: json.RawMessage(`{"threadId":"thread-a","requestId":7}`)})
pending, _ = c.Permissions(context.Background(), "thread-a")
if len(pending) != 0 {
t.Fatal("resolved callback not removed")
}
}