| 1 | package fakeclaude |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "reflect" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/rastrillo/rastrillo/budget" |
| 15 | ) |
| 16 | |
| 17 | // TestMain doubles as the Equipo MCP server the fake connects to: with |
| 18 | // FAKE_MCP_LOG set, the test binary answers MCP on stdio and records |
| 19 | // every tools/call there. |
| 20 | func TestMain(m *testing.M) { |
| 21 | if log := os.Getenv("FAKE_MCP_LOG"); log != "" { |
| 22 | os.Exit(serveMCP(log)) |
| 23 | } |
| 24 | os.Exit(budget.Main(m)) |
| 25 | } |
| 26 | |
| 27 | func serveMCP(log string) int { |
| 28 | in := bufio.NewScanner(os.Stdin) |
| 29 | in.Buffer(make([]byte, 0, 64<<10), 1<<20) |
| 30 | kept := 0 |
| 31 | for in.Scan() { |
| 32 | var req struct { |
| 33 | ID *int `json:"id"` |
| 34 | Method string `json:"method"` |
| 35 | Params json.RawMessage `json:"params"` |
| 36 | } |
| 37 | if json.Unmarshal(in.Bytes(), &req) != nil || req.ID == nil { |
| 38 | continue |
| 39 | } |
| 40 | var result any |
| 41 | switch req.Method { |
| 42 | case "initialize": |
| 43 | result = map[string]any{"protocolVersion": "2025-06-18", "capabilities": map[string]any{"tools": map[string]any{}}, "serverInfo": map[string]string{"name": "equipo", "version": "test"}} |
| 44 | case "tools/list": |
| 45 | result = map[string]any{"tools": []any{map[string]string{"name": "submit_findings"}, map[string]string{"name": "keep_image"}, map[string]string{"name": "permission"}}} |
| 46 | case "tools/call": |
| 47 | var call struct { |
| 48 | Name string `json:"name"` |
| 49 | Arguments json.RawMessage `json:"arguments"` |
| 50 | } |
| 51 | json.Unmarshal(req.Params, &call) |
| 52 | f, _ := os.OpenFile(log, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) |
| 53 | line, _ := json.Marshal(call) |
| 54 | f.Write(append(line, '\n')) |
| 55 | f.Close() |
| 56 | text, isError := "Findings recorded.", false |
| 57 | switch call.Name { |
| 58 | case "keep_image": |
| 59 | kept++ |
| 60 | text = "img-" + string(rune('0'+kept)) |
| 61 | case "submit_findings": |
| 62 | default: |
| 63 | text, isError = "Refused: unknown tool.", true |
| 64 | } |
| 65 | result = map[string]any{"content": []any{map[string]string{"type": "text", "text": text}}, "isError": isError} |
| 66 | } |
| 67 | raw, _ := json.Marshal(map[string]any{"jsonrpc": "2.0", "id": *req.ID, "result": result}) |
| 68 | os.Stdout.Write(append(raw, '\n')) |
| 69 | } |
| 70 | return 0 |
| 71 | } |
| 72 | |
| 73 | type researchRun struct { |
| 74 | tools []string |
| 75 | calls []struct { |
| 76 | Name string |
| 77 | Arguments map[string]any |
| 78 | } |
| 79 | reply string |
| 80 | } |
| 81 | |
| 82 | func runResearch(t *testing.T, native, prompt string) researchRun { |
| 83 | t.Helper() |
| 84 | home := t.TempDir() |
| 85 | if err := WriteCredentials(home, time.Now().Add(time.Hour)); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | t.Setenv("CLAUDE_CONFIG_DIR", home) |
| 89 | t.Setenv(tokenEnv, "") |
| 90 | os.Unsetenv(tokenEnv) |
| 91 | log := filepath.Join(t.TempDir(), "calls.jsonl") |
| 92 | t.Setenv("FAKE_MCP_LOG", log) |
| 93 | self, err := os.Executable() |
| 94 | if err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | cfg := filepath.Join(t.TempDir(), "mcp.json") |
| 98 | raw, _ := json.Marshal(map[string]any{"mcpServers": map[string]any{"equipo": map[string]any{"type": "stdio", "command": self, "args": []string{}}}}) |
| 99 | if err = os.WriteFile(cfg, raw, 0600); err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | msg, _ := json.Marshal(map[string]any{"type": "user", "session_id": "", "message": map[string]any{"role": "user", "content": prompt}}) |
| 103 | var out, errOut bytes.Buffer |
| 104 | args := []string{"-p", "--verbose", "--input-format", "stream-json", "--output-format", "stream-json", "--model", "claude-test", "--session-id", "00000000-0000-4000-8000-000000000001", "--mcp-config", cfg, "--tools", native} |
| 105 | if code := Main(args, bytes.NewReader(append(msg, '\n')), &out, &errOut); code != 0 { |
| 106 | t.Fatalf("exit %d: %s", code, errOut.String()) |
| 107 | } |
| 108 | var run researchRun |
| 109 | for _, line := range strings.Split(strings.TrimSpace(out.String()), "\n") { |
| 110 | var frame struct { |
| 111 | Type, Subtype string |
| 112 | Tools []string |
| 113 | Result string |
| 114 | } |
| 115 | if err := json.Unmarshal([]byte(line), &frame); err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | if frame.Type == "system" && frame.Subtype == "init" { |
| 119 | run.tools = frame.Tools |
| 120 | } |
| 121 | if frame.Type == "result" { |
| 122 | run.reply = frame.Result |
| 123 | } |
| 124 | } |
| 125 | calls, _ := os.ReadFile(log) |
| 126 | for _, line := range strings.Split(strings.TrimSpace(string(calls)), "\n") { |
| 127 | if line == "" { |
| 128 | continue |
| 129 | } |
| 130 | var c struct { |
| 131 | Name string |
| 132 | Arguments map[string]any |
| 133 | } |
| 134 | if err := json.Unmarshal([]byte(line), &c); err != nil { |
| 135 | t.Fatal(err) |
| 136 | } |
| 137 | run.calls = append(run.calls, c) |
| 138 | } |
| 139 | return run |
| 140 | } |
| 141 | |
| 142 | // Research mode: keep:<source>|<role>|<description> calls keep_image; |
| 143 | // summary:, fact:<label>|<value>|<source>,<source>, gap: and image: |
| 144 | // ("$kept" being the last kept image's id) make one submit_findings. |
| 145 | // The init frame reports exactly the native tools of --tools. |
| 146 | func TestFakeResearchMode(t *testing.T) { |
| 147 | prompt := "[Equipo: research]\nQuestion: what does the shop look like?\n" + |
| 148 | "keep:s1|logo|The shop's sign\n" + |
| 149 | "summary:A small shop in green.\n" + |
| 150 | "fact:Colour|Dark green|s1,s2\n" + |
| 151 | "fact:Font|Serif|s2\n" + |
| 152 | "gap:No opening hours\n" + |
| 153 | "image:$kept\n" |
| 154 | run := runResearch(t, "", prompt) |
| 155 | if !reflect.DeepEqual(run.tools, []string{"mcp__equipo__submit_findings", "mcp__equipo__keep_image"}) { |
| 156 | t.Fatalf("init tools %v", run.tools) |
| 157 | } |
| 158 | if len(run.calls) != 2 || run.calls[0].Name != "keep_image" || run.calls[1].Name != "submit_findings" { |
| 159 | t.Fatalf("calls %+v", run.calls) |
| 160 | } |
| 161 | if want := map[string]any{"source_id": "s1", "role": "logo", "description": "The shop's sign"}; !reflect.DeepEqual(run.calls[0].Arguments, want) { |
| 162 | t.Fatalf("keep_image %v", run.calls[0].Arguments) |
| 163 | } |
| 164 | want := map[string]any{ |
| 165 | "summary": "A small shop in green.", |
| 166 | "facts": []any{ |
| 167 | map[string]any{"label": "Colour", "value": "Dark green", "sources": []any{"s1", "s2"}}, |
| 168 | map[string]any{"label": "Font", "value": "Serif", "sources": []any{"s2"}}, |
| 169 | }, |
| 170 | "images": []any{"img-1"}, |
| 171 | "gaps": []any{"No opening hours"}, |
| 172 | } |
| 173 | if !reflect.DeepEqual(run.calls[1].Arguments, want) { |
| 174 | t.Fatalf("submit_findings %v", run.calls[1].Arguments) |
| 175 | } |
| 176 | if !strings.Contains(run.reply, "kept img-1") || !strings.Contains(run.reply, "Findings recorded.") { |
| 177 | t.Fatalf("reply %q", run.reply) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // findings:silent submits nothing; findings:raw:<json> submits that JSON |
| 182 | // verbatim; findings:twice submits twice; a native tool on --tools is |
| 183 | // reported in init as the CLI would. |
| 184 | func TestFakeResearchWords(t *testing.T) { |
| 185 | run := runResearch(t, "", "[Equipo: research]\nfindings:silent\n") |
| 186 | if len(run.calls) != 0 { |
| 187 | t.Fatalf("silent research called %+v", run.calls) |
| 188 | } |
| 189 | run = runResearch(t, "", "[Equipo: research]\nfindings:raw:{\"summary\":\"x\",\"extra\":true}\n") |
| 190 | if len(run.calls) != 1 || !reflect.DeepEqual(run.calls[0].Arguments, map[string]any{"summary": "x", "extra": true}) { |
| 191 | t.Fatalf("raw findings %+v", run.calls) |
| 192 | } |
| 193 | run = runResearch(t, "", "[Equipo: research]\nsummary:once\nfindings:twice\n") |
| 194 | if len(run.calls) != 2 || run.calls[0].Name != "submit_findings" || run.calls[1].Name != "submit_findings" { |
| 195 | t.Fatalf("twice %+v", run.calls) |
| 196 | } |
| 197 | run = runResearch(t, "Read", "[Equipo: research]\nfindings:silent\n") |
| 198 | if !reflect.DeepEqual(run.tools, []string{"Read", "mcp__equipo__submit_findings", "mcp__equipo__keep_image"}) { |
| 199 | t.Fatalf("init with --tools Read: %v", run.tools) |
| 200 | } |
| 201 | } |
| 202 | |