Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/test/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func NewMcpClient(t *testing.T, mcpHttpServer http.Handler, options ...transport
var err error
ret := &McpClient{ctx: t.Context()}
ret.testServer = httptest.NewServer(mcpHttpServer)
options = append(options, transport.WithContinuousListening())
ret.Client, err = client.NewStreamableHttpClient(ret.testServer.URL+"/mcp", options...)
require.NoError(t, err, "Expected no error creating MCP client")
err = ret.Start(t.Context())
Expand Down
68 changes: 38 additions & 30 deletions pkg/mcp/mcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"net/http"
"os"
"path/filepath"
"runtime"
"testing"
"time"
Expand All @@ -15,38 +14,47 @@ import (
"github.com/stretchr/testify/suite"
)

func TestWatchKubeConfig(t *testing.T) {
type WatchKubeConfigSuite struct {
BaseMcpSuite
}

func (s *WatchKubeConfigSuite) SetupTest() {
s.BaseMcpSuite.SetupTest()
kubeconfig := test.KubeConfigFake()
s.Cfg.KubeConfig = test.KubeconfigFile(s.T(), kubeconfig)
}

func (s *WatchKubeConfigSuite) TestNotifiesToolsChange() {
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
t.Skip("Skipping test on non-Unix-like platforms")
s.T().Skip("Skipping test on non-Unix-like platforms")
}
testCase(t, func(c *mcpContext) {
// Given
withTimeout, cancel := context.WithTimeout(c.ctx, 5*time.Second)
defer cancel()
var notification *mcp.JSONRPCNotification
c.mcpClient.OnNotification(func(n mcp.JSONRPCNotification) {
notification = &n
})
// When
f, _ := os.OpenFile(filepath.Join(c.tempDir, "config"), os.O_APPEND|os.O_WRONLY, 0644)
_, _ = f.WriteString("\n")
for notification == nil {
select {
case <-withTimeout.Done():
default:
time.Sleep(100 * time.Millisecond)
}
}
// Then
t.Run("WatchKubeConfig notifies tools change", func(t *testing.T) {
if notification == nil {
t.Fatalf("WatchKubeConfig did not notify")
}
if notification.Method != "notifications/tools/list_changed" {
t.Fatalf("WatchKubeConfig did not notify tools change, got %s", notification.Method)
}
})
// Given
s.InitMcpClient()
withTimeout, cancel := context.WithTimeout(s.T().Context(), 5*time.Second)
defer cancel()
var notification *mcp.JSONRPCNotification
s.OnNotification(func(n mcp.JSONRPCNotification) {
notification = &n
})
// When
f, _ := os.OpenFile(s.Cfg.KubeConfig, os.O_APPEND|os.O_WRONLY, 0644)
_, _ = f.WriteString("\n")
_ = f.Close()
for notification == nil {
select {
case <-withTimeout.Done():
s.FailNow("timeout waiting for WatchKubeConfig notification")
default:
time.Sleep(100 * time.Millisecond)
}
}
// Then
s.NotNil(notification, "WatchKubeConfig did not notify")
s.Equal("notifications/tools/list_changed", notification.Method, "WatchKubeConfig did not notify tools change")
}

func TestWatchKubeConfig(t *testing.T) {
suite.Run(t, new(WatchKubeConfigSuite))
}

type McpHeadersSuite struct {
Expand Down
Loading