Files
patterm/internal/mcp/protocol_test.go
Harry Bayliss 3622c41fd0 Land staged session/MCP/chrome work + sidebar clear-J fix
This batches the in-flight [Unreleased] block from CHANGELOG.md into a
single commit. Highlights:

- Real MCP protocol layer (initialize / tools/list / tools/call) so
  vendor MCP clients can complete the handshake against the per-PID
  socket. Legacy direct-dispatch preserved for the harness.
- New mcp_injection kinds — cli_override for codex, config_env for
  opencode — joining the existing env-var and config_file paths so
  patterm can slot into more agents without touching their real
  config or auth.
- Ctrl+A/D and Ctrl+W/S focus navigation across tabs and intra-tab
  process lists, recognised in legacy / kitty CSI u / xterm
  modifyOtherKeys encodings.
- Palette macros (sw / k / sp ) and reordering so open sessions
  surface above spawn-new entries.
- Two-row tab bar, sidebar/tabbar/status chrome cache, viewport-wipe
  on agent spawn, CR-terminated orchestrator injections, and split-
  Enter PTY writes so paste-detecting TUIs see Enter as a key event.

Also fixes the bug logged in TODO: claude's Ctrl+O tool-call expansion
emits CSI 0 J, which the viewport renderer was forwarding verbatim —
wiping the sidebar to the right of the cursor and leaving the chrome
cache convinced nothing had changed. CSI 0 J and CSI 1 J are now
translated into per-row ECH sequences clamped to the viewport, same
as CSI 2 J and CSI K already were.

Agent guides (CLAUDE.md / AGENTS.md) now spell out the
TODO->CHANGELOG workflow so completed items land in the changelog
rather than as ticked entries left behind in TODO.
2026-05-14 19:09:35 +01:00

129 lines
3.7 KiB
Go

package mcp
import (
"encoding/json"
"testing"
)
func TestInitializeReturnsCapabilities(t *testing.T) {
s := &Server{}
req := []byte(`{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"claude","version":"1.0"}}}`)
resp := s.dispatch("", req)
if resp == nil {
t.Fatal("expected response for initialize")
}
var parsed struct {
JSONRPC string `json:"jsonrpc"`
ID json.RawMessage `json:"id"`
Result map[string]interface{} `json:"result"`
Error *struct {
Code int `json:"code"`
} `json:"error"`
}
if err := json.Unmarshal(resp, &parsed); err != nil {
t.Fatalf("parse: %v\n%s", err, resp)
}
if parsed.Error != nil {
t.Fatalf("initialize returned error: %+v", parsed.Error)
}
if parsed.Result["protocolVersion"] == nil {
t.Fatalf("missing protocolVersion: %+v", parsed.Result)
}
caps, ok := parsed.Result["capabilities"].(map[string]interface{})
if !ok {
t.Fatalf("capabilities not object: %+v", parsed.Result)
}
if caps["tools"] == nil {
t.Fatalf("tools capability missing: %+v", caps)
}
}
func TestInitializedNotificationSuppressesResponse(t *testing.T) {
s := &Server{}
req := []byte(`{"jsonrpc":"2.0","method":"notifications/initialized"}`)
resp := s.dispatch("", req)
if resp != nil {
t.Fatalf("notification produced a response: %s", resp)
}
}
func TestToolsListReturnsConcreteSchemas(t *testing.T) {
s := &Server{}
req := []byte(`{"jsonrpc":"2.0","id":2,"method":"tools/list"}`)
resp := s.dispatch("", req)
if resp == nil {
t.Fatal("expected response for tools/list")
}
var parsed struct {
Result map[string]interface{} `json:"result"`
Error *struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"error"`
}
if err := json.Unmarshal(resp, &parsed); err != nil {
t.Fatalf("parse: %v\n%s", err, resp)
}
if parsed.Error != nil {
t.Fatalf("tools/list returned error: %+v", parsed.Error)
}
tools, ok := parsed.Result["tools"].([]interface{})
if !ok {
t.Fatalf("tools not array: %+v", parsed.Result)
}
if len(tools) == 0 {
t.Fatalf("expected at least one tool, got 0")
}
// Every tool must have name, description, and inputSchema with
// `type=object` and a concrete `properties` object — `properties:
// null` trips up strict MCP clients (claude in particular).
for i, tool := range tools {
entry, ok := tool.(map[string]interface{})
if !ok {
t.Fatalf("tool %d not object: %#v", i, tool)
}
if entry["name"] == "" || entry["name"] == nil {
t.Fatalf("tool %d missing name: %#v", i, entry)
}
if entry["description"] == "" || entry["description"] == nil {
t.Fatalf("tool %d missing description: %#v", i, entry)
}
schema, ok := entry["inputSchema"].(map[string]interface{})
if !ok {
t.Fatalf("tool %d inputSchema not object: %#v", i, entry)
}
if schema["type"] != "object" {
t.Fatalf("tool %d schema type != object: %#v", i, schema)
}
props, ok := schema["properties"]
if !ok {
t.Fatalf("tool %s missing properties", entry["name"])
}
if _, ok := props.(map[string]interface{}); !ok {
t.Fatalf("tool %s properties not object (got %T): %#v", entry["name"], props, props)
}
}
}
func TestPingReturnsEmptyObject(t *testing.T) {
s := &Server{}
req := []byte(`{"jsonrpc":"2.0","id":3,"method":"ping"}`)
resp := s.dispatch("", req)
if resp == nil {
t.Fatal("expected response for ping")
}
var parsed struct {
Result map[string]interface{} `json:"result"`
Error *struct{ Code int } `json:"error"`
}
if err := json.Unmarshal(resp, &parsed); err != nil {
t.Fatalf("parse: %v\n%s", err, resp)
}
if parsed.Error != nil {
t.Fatalf("ping returned error: %+v", parsed.Error)
}
if parsed.Result == nil {
t.Fatal("ping result missing")
}
}