package thin import ( "bufio" "bytes" "context" "encoding/json" "io" "net" "slices" "strings" "testing" "amadan.net/oficina/equipo/internal/runner/connect" "amadan.net/oficina/equipo/internal/wire" ) // A Codex step approves only its own tools, and only the call Codex // started with those exact arguments; shell, patch and anything else the // app-server asks is refused. func TestCodexStepApprovals(t *testing.T) { var out bytes.Buffer rpc := connect.NewAppServer(strings.NewReader(""), &out) items := map[string]codexItem{ "a": {ID: "a", Type: "mcpToolCall", Server: "equipo", Tool: "submit_review", Arguments: json.RawMessage(`{"verdict":"approve","headline":"Approved.","summary":"ok"}`)}, "b": {ID: "b", Type: "mcpToolCall", Server: "equipo", Tool: "status", Arguments: json.RawMessage(`{}`)}, } names := []string{"submit_review", "ask_person"} elicit := func(server, args string) json.RawMessage { return json.RawMessage(`{"serverName":"` + server + `","mode":"form","_meta":{"codex_approval_kind":"mcp_tool_call","tool_params":` + args + `}}`) } for _, tc := range []struct { name, method string params json.RawMessage want string }{ {"own tool, same arguments", "mcpServer/elicitation/request", elicit("equipo", `{"summary":"ok","verdict":"approve","headline":"Approved."}`), `"accept"`}, {"own tool, other arguments", "mcpServer/elicitation/request", elicit("equipo", `{"verdict":"changes_requested","headline":"x","summary":"ok"}`), `"decline"`}, {"a tool the step does not have", "mcpServer/elicitation/request", elicit("equipo", `{}`), `"decline"`}, {"another server", "mcpServer/elicitation/request", elicit("other", `{"verdict":"approve","headline":"Approved.","summary":"ok"}`), `"decline"`}, {"shell", "item/commandExecution/requestApproval", json.RawMessage(`{"command":"curl example.com"}`), `refused by policy`}, {"patch", "item/fileChange/requestApproval", json.RawMessage(`{}`), `refused by policy`}, } { t.Run(tc.name, func(t *testing.T) { out.Reset() accepted := answerApproval(rpc, connect.Message{ID: json.RawMessage(`"s1"`), Method: tc.method, Params: tc.params}, items, names) if !strings.Contains(out.String(), tc.want) || tc.want == `"decline"` && strings.Contains(out.String(), `"accept"`) { t.Fatalf("answer %s, want %s", out.String(), tc.want) } // A refusal is counted by the step (its reason when it then // commits nothing is tools_refused). if accepted != (tc.want == `"accept"`) { t.Fatalf("accepted %v for %s", accepted, out.String()) } }) } } // A Codex step's tool socket lists only the step's tools: Codex asks // through the app-server, so there is no permission tool to call. func TestCodexStepSocketHasNoPermission(t *testing.T) { a, b := net.Pipe() defer a.Close() call := func(context.Context, string, string, json.RawMessage) (string, bool) { return "Recorded.", false } go serveMCP(context.Background(), b, toolSet{list: reviewTools, names: []string{"submit_review", "ask_person"}}, textOnly(call)) r := bufio.NewReader(a) ask := func(q string) map[string]any { t.Helper() if _, err := io.WriteString(a, q+"\n"); err != nil { t.Fatal(err) } line, err := r.ReadBytes('\n') var v map[string]any if err != nil || json.Unmarshal(line, &v) != nil { t.Fatal(err) } return v } ask(`{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18"}}`) var names []string for _, tool := range ask(`{"jsonrpc":"2.0","id":2,"method":"tools/list"}`)["result"].(map[string]any)["tools"].([]any) { names = append(names, tool.(map[string]any)["name"].(string)) } if !slices.Equal(names, []string{"submit_review", "ask_person"}) { t.Fatalf("tools %v", names) } if v := ask(`{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"permission","arguments":{"tool_name":"Bash","input":{}}}}`); v["error"] == nil { t.Fatalf("permission answered on a Codex step: %v", v) } } // What a Codex step shows while it runs: commands and file changes, // without the /workspace prefix, and the tools it calls. func TestCodexStepActivity(t *testing.T) { edit := codexItem{Type: "fileChange"} edit.Changes = append(edit.Changes, struct { Path string `json:"path"` }{Path: "/workspace/README.md"}) for _, tc := range []struct { item codexItem kind, subject, line string }{ {codexItem{Type: "commandExecution", Command: "go test ./...\nsecond line"}, wire.ActivityRun, "go test ./...", "Bash go test ./..."}, {edit, wire.ActivityEdit, "README.md", "Edit README.md"}, {codexItem{Type: "mcpToolCall", Server: "equipo", Tool: "submit_candidate"}, wire.ActivityOther, "", "submit_candidate"}, {codexItem{Type: "reasoning", Text: "secret plan"}, "", "", ""}, } { kind, subject, line := itemActivity(tc.item) if kind != tc.kind || subject != tc.subject || line != tc.line { t.Fatalf("%+v: %q %q %q", tc.item, kind, subject, line) } } }