| 1 | //go:build darwin || testbuild |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "context" |
| 8 | "encoding/json" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | "time" |
| 15 | ) |
| 16 | |
| 17 | // runToolProxy runs equipo-runner tool-proxy with args as the CLI starts |
| 18 | // it, in a child process (toolProxyChild): the endpoint lets go of its |
| 19 | // inherited descriptors and environment, which must not happen to the |
| 20 | // test process. extra are descriptors from 3 on, env the child's |
| 21 | // environment besides what starts it. |
| 22 | func runToolProxy(t *testing.T, args []string, input string, extra []*os.File, env ...string) (code int, out, stderr string, report toolProxyReport) { |
| 23 | t.Helper() |
| 24 | rawArgs, err := json.Marshal(args) |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | reportPath := filepath.Join(t.TempDir(), "report.json") |
| 29 | ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) |
| 30 | defer cancel() |
| 31 | cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^$") |
| 32 | cmd.Env = append([]string{childArgs + "=" + string(rawArgs), childReport + "=" + reportPath}, env...) |
| 33 | cmd.ExtraFiles = extra |
| 34 | cmd.Stdin = strings.NewReader(input) |
| 35 | var o, e bytes.Buffer |
| 36 | cmd.Stdout, cmd.Stderr = &o, &e |
| 37 | err = cmd.Run() |
| 38 | if exit, ok := err.(*exec.ExitError); ok { |
| 39 | code = exit.ExitCode() |
| 40 | } else if err != nil { |
| 41 | t.Fatalf("%q: %v", args, err) |
| 42 | } |
| 43 | raw, err := os.ReadFile(reportPath) |
| 44 | if err != nil || json.Unmarshal(raw, &report) != nil { |
| 45 | t.Fatalf("%q: no report (exit %d, stderr %q): %v", args, code, e.String(), err) |
| 46 | } |
| 47 | return code, o.String(), e.String(), report |
| 48 | } |
| 49 | |
| 50 | // equipo-runner tool-proxy --socket <path> is a Mac's MCP tool endpoint. |
| 51 | // Like the Linux tool proxy it takes exactly that, reads no environment |
| 52 | // and no configuration, says nothing on stderr, and exits 2 for any other |
| 53 | // arguments and 1 when it cannot serve. |
| 54 | func TestToolProxyCommand(t *testing.T) { |
| 55 | path := echoSocket(t) |
| 56 | dir := filepath.Dir(path) |
| 57 | ping := `{"jsonrpc":"2.0","id":1,"method":"ping"}` + "\n" |
| 58 | for _, tc := range []struct { |
| 59 | args []string |
| 60 | code int |
| 61 | out string |
| 62 | }{ |
| 63 | {[]string{"tool-proxy", "--socket", path}, 0, `{"jsonrpc":"2.0","id":1,"result":{}}` + "\n"}, |
| 64 | {[]string{"tool-proxy"}, 2, ""}, |
| 65 | {[]string{"tool-proxy", "--socket"}, 2, ""}, |
| 66 | {[]string{"tool-proxy", "--sock", path}, 2, ""}, |
| 67 | {[]string{"tool-proxy", "--socket", path, "extra"}, 2, ""}, |
| 68 | {[]string{"tool-proxy", "--env-file", "/nonexistent", "--socket", path}, 2, ""}, |
| 69 | {[]string{"tool-proxy", "--socket", "s"}, 1, ""}, |
| 70 | {[]string{"tool-proxy", "--socket", filepath.Join(dir, "missing")}, 1, ""}, |
| 71 | } { |
| 72 | code, out, stderr, report := runToolProxy(t, tc.args, ping, nil, "EQUIPO_ORIGIN=https://equipo.example.com") |
| 73 | if code != tc.code || out != tc.out || stderr != "" || len(report.Getenv) != 0 { |
| 74 | t.Fatalf("%q: exit %d, out %q, stderr %q, read %q", tc.args, code, out, stderr, report.Getenv) |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |