//go:build darwin || testbuild package main import ( "bytes" "context" "encoding/json" "os" "os/exec" "path/filepath" "strings" "testing" "time" ) // runToolProxy runs equipo-runner tool-proxy with args as the CLI starts // it, in a child process (toolProxyChild): the endpoint lets go of its // inherited descriptors and environment, which must not happen to the // test process. extra are descriptors from 3 on, env the child's // environment besides what starts it. func runToolProxy(t *testing.T, args []string, input string, extra []*os.File, env ...string) (code int, out, stderr string, report toolProxyReport) { t.Helper() rawArgs, err := json.Marshal(args) if err != nil { t.Fatal(err) } reportPath := filepath.Join(t.TempDir(), "report.json") ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) defer cancel() cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^$") cmd.Env = append([]string{childArgs + "=" + string(rawArgs), childReport + "=" + reportPath}, env...) cmd.ExtraFiles = extra cmd.Stdin = strings.NewReader(input) var o, e bytes.Buffer cmd.Stdout, cmd.Stderr = &o, &e err = cmd.Run() if exit, ok := err.(*exec.ExitError); ok { code = exit.ExitCode() } else if err != nil { t.Fatalf("%q: %v", args, err) } raw, err := os.ReadFile(reportPath) if err != nil || json.Unmarshal(raw, &report) != nil { t.Fatalf("%q: no report (exit %d, stderr %q): %v", args, code, e.String(), err) } return code, o.String(), e.String(), report } // equipo-runner tool-proxy --socket is a Mac's MCP tool endpoint. // Like the Linux tool proxy it takes exactly that, reads no environment // and no configuration, says nothing on stderr, and exits 2 for any other // arguments and 1 when it cannot serve. func TestToolProxyCommand(t *testing.T) { path := echoSocket(t) dir := filepath.Dir(path) ping := `{"jsonrpc":"2.0","id":1,"method":"ping"}` + "\n" for _, tc := range []struct { args []string code int out string }{ {[]string{"tool-proxy", "--socket", path}, 0, `{"jsonrpc":"2.0","id":1,"result":{}}` + "\n"}, {[]string{"tool-proxy"}, 2, ""}, {[]string{"tool-proxy", "--socket"}, 2, ""}, {[]string{"tool-proxy", "--sock", path}, 2, ""}, {[]string{"tool-proxy", "--socket", path, "extra"}, 2, ""}, {[]string{"tool-proxy", "--env-file", "/nonexistent", "--socket", path}, 2, ""}, {[]string{"tool-proxy", "--socket", "s"}, 1, ""}, {[]string{"tool-proxy", "--socket", filepath.Join(dir, "missing")}, 1, ""}, } { code, out, stderr, report := runToolProxy(t, tc.args, ping, nil, "EQUIPO_ORIGIN=https://equipo.example.com") if code != tc.code || out != tc.out || stderr != "" || len(report.Getenv) != 0 { t.Fatalf("%q: exit %d, out %q, stderr %q, read %q", tc.args, code, out, stderr, report.Getenv) } } }