package runner import ( "bufio" "context" "encoding/json" "io" "os" "os/exec" "path/filepath" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/testci" ) func TestToolVerbUnderFakeHarness(t *testing.T) { s, b := toolFixture(t) binary := filepath.Join(t.TempDir(), "equipo-runner") build := exec.Command("go", "build", "-o", binary, "../../cmd/equipo-runner") build.Env = append(os.Environ(), "CGO_ENABLED=0") testci.GoCommand(t, build) if out, err := build.CombinedOutput(); err != nil { t.Fatalf("tool binary: %v %s", err, out) } config, err := s.MCPConfig(binary) mustJournal(t, err) cap, err := readToolCapability(s.CapabilityPath) mustJournal(t, err) raw, _ := json.Marshal(config) if strings.Contains(string(raw), cap.Secret) || config.Type != "stdio" || len(config.Args) != 1 || config.Args[0] != "tool" { t.Fatal("unsafe creation config") } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() cmd := exec.CommandContext(ctx, config.Command, config.Args...) cmd.Env = []string{"PATH=/usr/bin:/bin"} for k, v := range config.Env { cmd.Env = append(cmd.Env, k+"="+v) } in, err := cmd.StdinPipe() mustJournal(t, err) out, err := cmd.StdoutPipe() mustJournal(t, err) var logs strings.Builder cmd.Stderr = &logs mustJournal(t, cmd.Start()) t.Cleanup(func() { if cmd.ProcessState == nil { cmd.Process.Kill() cmd.Wait() } }) enc := json.NewEncoder(in) scan := bufio.NewScanner(out) rpc := func(id int, method string, params any) map[string]any { t.Helper() mustJournal(t, enc.Encode(map[string]any{"jsonrpc": "2.0", "id": id, "method": method, "params": params})) if !scan.Scan() { t.Fatal("no MCP reply", scan.Err()) } var reply map[string]any mustJournal(t, json.Unmarshal(scan.Bytes(), &reply)) if reply["error"] != nil { t.Fatal("MCP error", reply) } return reply } rpc(1, "initialize", map[string]any{"protocolVersion": "2024-11-05", "capabilities": map[string]any{}, "clientInfo": map[string]any{"name": "fake", "version": "1"}}) mustJournal(t, enc.Encode(map[string]any{"jsonrpc": "2.0", "method": "notifications/initialized"})) mustJournal(t, enc.Encode(map[string]any{"jsonrpc": "2.0", "method": "notifications/cancelled", "params": map[string]any{"requestId": 99}})) rpc(2, "tools/list", map[string]any{}) 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?"}}) in.Close() mustJournal(t, cmd.Wait()) if strings.Contains(logs.String(), cap.Secret) { t.Fatal("capability logged") } rows, err := b.Journal.ToolSubmissions(context.Background(), 10) mustJournal(t, err) if len(rows) != 1 { t.Fatal("harness submission count", len(rows)) } } func TestToolVerbFrames(t *testing.T) { s, _ := toolFixture(t) for _, raw := range []string{`{"jsonrpc":"2.0","id":1,"id":2,"method":"initialize"}` + "\n", strings.Repeat("x", maxToolFrame+1) + "\n", "{}", "broken\n"} { var out strings.Builder if ServeTools(context.Background(), strings.NewReader(raw), &out, s.SocketPath, s.CapabilityPath) == nil { t.Fatal("malformed stdio accepted") } } cap, err := readToolCapability(s.CapabilityPath) mustJournal(t, err) s.Invalidate() 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?"}`)} if callToolSocket(context.Background(), s.SocketPath, q) == nil { t.Fatal("disconnected socket accepted") } if ServeTools(context.Background(), strings.NewReader(""), io.Discard, s.SocketPath, s.CapabilityPath+"missing") == nil { t.Fatal("missing capability ignored") } }