| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "io" |
| 8 | "os" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "amadan.net/oficina/equipo/internal/testci" |
| 16 | ) |
| 17 | |
| 18 | func TestToolVerbUnderFakeHarness(t *testing.T) { |
| 19 | s, b := toolFixture(t) |
| 20 | binary := filepath.Join(t.TempDir(), "equipo-runner") |
| 21 | build := exec.Command("go", "build", "-o", binary, "../../cmd/equipo-runner") |
| 22 | build.Env = append(os.Environ(), "CGO_ENABLED=0") |
| 23 | testci.GoCommand(t, build) |
| 24 | if out, err := build.CombinedOutput(); err != nil { |
| 25 | t.Fatalf("tool binary: %v %s", err, out) |
| 26 | } |
| 27 | config, err := s.MCPConfig(binary) |
| 28 | mustJournal(t, err) |
| 29 | cap, err := readToolCapability(s.CapabilityPath) |
| 30 | mustJournal(t, err) |
| 31 | raw, _ := json.Marshal(config) |
| 32 | if strings.Contains(string(raw), cap.Secret) || config.Type != "stdio" || len(config.Args) != 1 || config.Args[0] != "tool" { |
| 33 | t.Fatal("unsafe creation config") |
| 34 | } |
| 35 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 36 | defer cancel() |
| 37 | cmd := exec.CommandContext(ctx, config.Command, config.Args...) |
| 38 | cmd.Env = []string{"PATH=/usr/bin:/bin"} |
| 39 | for k, v := range config.Env { |
| 40 | cmd.Env = append(cmd.Env, k+"="+v) |
| 41 | } |
| 42 | in, err := cmd.StdinPipe() |
| 43 | mustJournal(t, err) |
| 44 | out, err := cmd.StdoutPipe() |
| 45 | mustJournal(t, err) |
| 46 | var logs strings.Builder |
| 47 | cmd.Stderr = &logs |
| 48 | mustJournal(t, cmd.Start()) |
| 49 | t.Cleanup(func() { |
| 50 | if cmd.ProcessState == nil { |
| 51 | cmd.Process.Kill() |
| 52 | cmd.Wait() |
| 53 | } |
| 54 | }) |
| 55 | enc := json.NewEncoder(in) |
| 56 | scan := bufio.NewScanner(out) |
| 57 | rpc := func(id int, method string, params any) map[string]any { |
| 58 | t.Helper() |
| 59 | mustJournal(t, enc.Encode(map[string]any{"jsonrpc": "2.0", "id": id, "method": method, "params": params})) |
| 60 | if !scan.Scan() { |
| 61 | t.Fatal("no MCP reply", scan.Err()) |
| 62 | } |
| 63 | var reply map[string]any |
| 64 | mustJournal(t, json.Unmarshal(scan.Bytes(), &reply)) |
| 65 | if reply["error"] != nil { |
| 66 | t.Fatal("MCP error", reply) |
| 67 | } |
| 68 | return reply |
| 69 | } |
| 70 | rpc(1, "initialize", map[string]any{"protocolVersion": "2024-11-05", "capabilities": map[string]any{}, "clientInfo": map[string]any{"name": "fake", "version": "1"}}) |
| 71 | mustJournal(t, enc.Encode(map[string]any{"jsonrpc": "2.0", "method": "notifications/initialized"})) |
| 72 | mustJournal(t, enc.Encode(map[string]any{"jsonrpc": "2.0", "method": "notifications/cancelled", "params": map[string]any{"requestId": 99}})) |
| 73 | rpc(2, "tools/list", map[string]any{}) |
| 74 | rpc(3, "tools/call", map[string]any{"name": "ask_person", "_meta": map[string]any{"progressToken": "progress"}, "arguments": map[string]any{"request_id": "harness-1", "question": "Which colour?"}}) |
| 75 | in.Close() |
| 76 | mustJournal(t, cmd.Wait()) |
| 77 | if strings.Contains(logs.String(), cap.Secret) { |
| 78 | t.Fatal("capability logged") |
| 79 | } |
| 80 | rows, err := b.Journal.ToolSubmissions(context.Background(), 10) |
| 81 | mustJournal(t, err) |
| 82 | if len(rows) != 1 { |
| 83 | t.Fatal("harness submission count", len(rows)) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func TestToolVerbFrames(t *testing.T) { |
| 88 | s, _ := toolFixture(t) |
| 89 | for _, raw := range []string{`{"jsonrpc":"2.0","id":1,"id":2,"method":"initialize"}` + "\n", strings.Repeat("x", maxToolFrame+1) + "\n", "{}", "broken\n"} { |
| 90 | var out strings.Builder |
| 91 | if ServeTools(context.Background(), strings.NewReader(raw), &out, s.SocketPath, s.CapabilityPath) == nil { |
| 92 | t.Fatal("malformed stdio accepted") |
| 93 | } |
| 94 | } |
| 95 | cap, err := readToolCapability(s.CapabilityPath) |
| 96 | mustJournal(t, err) |
| 97 | s.Invalidate() |
| 98 | q := toolRequest{Capability: cap.Secret, InstanceID: cap.InstanceID, TaskID: cap.TaskID, Attempt: cap.Attempt, Fence: cap.Fence, RequestID: "r", Name: "ask_person", Arguments: json.RawMessage(`{"question":"Hi?"}`)} |
| 99 | if callToolSocket(context.Background(), s.SocketPath, q) == nil { |
| 100 | t.Fatal("disconnected socket accepted") |
| 101 | } |
| 102 | if ServeTools(context.Background(), strings.NewReader(""), io.Discard, s.SocketPath, s.CapabilityPath+"missing") == nil { |
| 103 | t.Fatal("missing capability ignored") |
| 104 | } |
| 105 | } |
| 106 | |