| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "net" |
| 8 | "slices" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 13 | ) |
| 14 | |
| 15 | func mcpRoundTrip(t *testing.T, call ToolFunc, requests ...string) []map[string]any { |
| 16 | t.Helper() |
| 17 | a, b := net.Pipe() |
| 18 | defer a.Close() |
| 19 | go serveMCP(context.Background(), b, converseSet(call != nil), textOnly(call)) |
| 20 | r := bufio.NewReader(a) |
| 21 | var out []map[string]any |
| 22 | for _, q := range requests { |
| 23 | if _, err := a.Write([]byte(q + "\n")); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | line, err := r.ReadBytes('\n') |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | var v map[string]any |
| 31 | if err := json.Unmarshal(line, &v); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | out = append(out, v) |
| 35 | } |
| 36 | return out |
| 37 | } |
| 38 | |
| 39 | func TestMCPScopedTools(t *testing.T) { |
| 40 | var calls []string |
| 41 | call := func(_ context.Context, name, useID string, args json.RawMessage) (string, bool) { |
| 42 | calls = append(calls, name+"|"+useID+"|"+string(args)) |
| 43 | return "ok", false |
| 44 | } |
| 45 | out := mcpRoundTrip(t, call, |
| 46 | `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18"}}`, |
| 47 | `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`, |
| 48 | `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"permission","arguments":{"tool_name":"mcp__equipo__add_project","input":{"repo":"oficina/x"}}}}`, |
| 49 | `{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"permission","arguments":{"tool_name":"Bash","input":{"command":"ls"}}}}`, |
| 50 | `{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"add_project","arguments":{"repo":"oficina/x"},"_meta":{"claudecode/toolUseId":"toolu_1"}}}`, |
| 51 | `{"jsonrpc":"2.0","id":6,"method":"tools/call","params":{"name":"submit_candidate","arguments":{}}}`, |
| 52 | ) |
| 53 | var names []string |
| 54 | for _, tool := range out[1]["result"].(map[string]any)["tools"].([]any) { |
| 55 | names = append(names, tool.(map[string]any)["name"].(string)) |
| 56 | // Every schema's required is an array, never null. |
| 57 | if _, ok := tool.(map[string]any)["inputSchema"].(map[string]any)["required"].([]any); !ok { |
| 58 | t.Fatalf("%v: required is not an array", tool) |
| 59 | } |
| 60 | } |
| 61 | if want := append([]string{"permission"}, provider.ConverseTools()...); !slices.Equal(names, want) { |
| 62 | t.Fatalf("tools %v, want %v", names, want) |
| 63 | } |
| 64 | text := func(i int) string { |
| 65 | return out[i]["result"].(map[string]any)["content"].([]any)[0].(map[string]any)["text"].(string) |
| 66 | } |
| 67 | if !strings.Contains(text(2), `"behavior":"allow"`) || !strings.Contains(text(3), `"behavior":"deny"`) { |
| 68 | t.Fatalf("permission: %s / %s", text(2), text(3)) |
| 69 | } |
| 70 | if text(4) != "ok" || !slices.Equal(calls, []string{`add_project|toolu_1|{"repo":"oficina/x"}`}) { |
| 71 | t.Fatalf("call %v %v", text(4), calls) |
| 72 | } |
| 73 | if out[5]["error"] == nil { |
| 74 | t.Fatalf("unlisted tool ran: %v", out[5]) |
| 75 | } |
| 76 | // Without a tool caller, only the permission callback is listed and |
| 77 | // it denies everything. |
| 78 | out = mcpRoundTrip(t, nil, |
| 79 | `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}`, |
| 80 | `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`, |
| 81 | `{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"permission","arguments":{"tool_name":"mcp__equipo__status","input":{}}}}`, |
| 82 | ) |
| 83 | if n := len(out[1]["result"].(map[string]any)["tools"].([]any)); n != 1 { |
| 84 | t.Fatalf("%d tools without a caller", n) |
| 85 | } |
| 86 | if !strings.Contains(out[2]["result"].(map[string]any)["content"].([]any)[0].(map[string]any)["text"].(string), "deny") { |
| 87 | t.Fatal("permission allowed without a caller") |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func TestStreamActivityReadsNamesOnly(t *testing.T) { |
| 92 | for raw, want := range map[string]string{ |
| 93 | `{"type":"stream_event","event":{"type":"content_block_start","content_block":{"type":"thinking","thinking":"secret plan"}}}`: "thinking/", |
| 94 | `{"type":"stream_event","event":{"type":"content_block_start","content_block":{"type":"tool_use","name":"mcp__equipo__find","input":{"query":"x"}}}}`: "tool/find", |
| 95 | `{"type":"stream_event","event":{"type":"content_block_start","content_block":{"type":"tool_use","name":"WebFetch","input":{"url":"https://example.com/?t=1"}}}}`: "tool/other", |
| 96 | `{"type":"stream_event","event":{"type":"content_block_delta","delta":{"type":"text_delta","text":"hi"}}}`: "", |
| 97 | } { |
| 98 | kind, tool, ok := streamActivity([]byte(raw), converseSet(true).names) |
| 99 | got := "" |
| 100 | if ok { |
| 101 | got = kind + "/" + tool |
| 102 | } |
| 103 | if got != want { |
| 104 | t.Fatalf("%s: %q, want %q", raw, got, want) |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestCleanPath(t *testing.T) { |
| 110 | for in, ok := range map[string]bool{"README.md": true, "a/b.go": true, "": true, "/a": true, "../x": false, "a/../../x": false, "a//b": false, "-n": false, "HEAD:x": false} { |
| 111 | if _, err := cleanPath(in); (err == nil) != ok { |
| 112 | t.Fatalf("%q: %v", in, err) |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | func TestCatalogText(t *testing.T) { |
| 118 | md := "# Woodstar\n\n[](https://x) Woodstar reads [books](https://y) with panes.\n\n## Configuration\n\nSet it.\n" |
| 119 | if got := summary(md); got != "Woodstar reads books with panes." { |
| 120 | t.Fatalf("summary %q", got) |
| 121 | } |
| 122 | if got := headings(md); len(got) != 1 || got[0] != "Woodstar" { |
| 123 | t.Fatalf("headings %q", got) |
| 124 | } |
| 125 | } |
| 126 | |