package mcp_test import ( "bytes" "encoding/json" "net/http" "net/http/httptest" "os" "path/filepath" "testing" "github.com/mathiasbq/hyperguild/ingestion/internal/mcp" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func toolCall(t *testing.T, srv http.Handler, name string, args map[string]any) map[string]any { t.Helper() bodyBytes, err := json.Marshal(map[string]any{ "jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": map[string]any{"name": name, "arguments": args}, }) require.NoError(t, err) req := httptest.NewRequest(http.MethodPost, "/mcp", bytes.NewReader(bodyBytes)) rr := httptest.NewRecorder() srv.ServeHTTP(rr, req) require.Equal(t, http.StatusOK, rr.Code) var resp map[string]any require.NoError(t, json.Unmarshal(rr.Body.Bytes(), &resp)) return resp } func TestBrainQueryReturnsResults(t *testing.T) { brainDir := t.TempDir() knowledge := filepath.Join(brainDir, "knowledge") require.NoError(t, os.MkdirAll(knowledge, 0o755)) require.NoError(t, os.WriteFile( filepath.Join(knowledge, "tdd.md"), []byte("# TDD\n\nTest-driven development is iterative.\n"), 0o644, )) srv := mcp.NewServer(brainDir, nil, nil) resp := toolCall(t, srv, "brain_query", map[string]any{"query": "tdd"}) require.Nil(t, resp["error"]) result := resp["result"].(map[string]any) content := result["content"].([]any) require.NotEmpty(t, content) text := content[0].(map[string]any)["text"].(string) assert.Contains(t, text, "tdd.md") }