Files
coder/coderd/x/chatd/mcpclient/inprocess_test.go
T
Ethan c95f3a58f9 chore: remove the remaining export_test.go shims (#32434)
Closes CODAGT-1607

Follow-up to #32162, which left 5 `export_test.go` shims behind:
`coderd`, `aibridged`, `chatd`, `chatstate` and `mcpclient`. This PR
removes them. It's stacked on #32433, which deals with the chat
workspace functions in `coderd`.

Same approach as before. Tests of package internals are now
`*_internal_test.go` files, with no changes beyond what the move forces,
and tests that only needed a constant just use the value. `chatstate`
needed one small accessor, so `AllowedExecutionTransitionOutputs` is
just exported for real, and its tests stay where they were.
Unfortunately the `mcpclient` connect budget test needs an unexported
timeout, so it moved to an internal file and builds its own server
there.

I've removed `TestConnectAll_LateServerReaped`,
`TestAllExecutionStates_Enumeration` and a few assertions instead of
moving them, as I couldn't find a breakage that only they would catch.

While here, I've dropped the test-only `reaperDone` hook from
`mcpclient`. The wait for a connect to finish within its budget is now a
small function of its own, with a test that a session which connects too
late still gets closed. Nothing checked that before.

With no shims left, the `testpackage` path exclusion is gone, so a new
`export_test.go` now fails lint.
2026-12-01 00:17:20 +11:00

118 lines
3.9 KiB
Go

package mcpclient_test
import (
"bytes"
"context"
"slices"
"strings"
"testing"
"charm.land/fantasy"
"github.com/google/uuid"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/stretchr/testify/require"
"cdr.dev/slog/v3/sloggers/slogtest"
"github.com/coder/coder/v2/coderd/x/chatd/mcpclient"
"github.com/coder/coder/v2/testutil"
)
func TestConnectInProcess(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitLong)
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
srv := mcp.NewServer(&mcp.Implementation{Name: "test-server", Version: "1.0.0"}, nil)
echo := echoTool()
srv.AddTool(echo.tool, echo.handler)
cfg := mcpclient.InProcessServer{ID: uuid.New(), Slug: "bot", Server: srv}
tools, summaries, cleanup := mcpclient.ConnectInProcess(ctx, logger, []mcpclient.InProcessServer{cfg})
require.Len(t, summaries, 1)
require.Equal(t, mcpclient.ConnectOutcomeConnected, summaries[0].Outcome)
require.Equal(t, cfg.ID, summaries[0].ConfigID)
require.Equal(t, []string{"bot__echo"}, toolNames(tools))
require.Len(t, slices.Collect(srv.Sessions()), 1)
resp, err := tools[0].Run(ctx, fantasy.ToolCall{ID: "call-1", Name: "bot__echo", Input: `{"input":"hi"}`})
require.NoError(t, err)
require.Equal(t, "echo: hi", resp.Content)
cleanup()
require.Eventually(t, func() bool {
return len(slices.Collect(srv.Sessions())) == 0
}, testutil.WaitShort, testutil.IntervalFast, "closing the client must end the server session")
}
func TestConnectInProcess_RejectsTooManyTools(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitLong)
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
srv := mcp.NewServer(&mcp.Implementation{Name: "test-server", Version: "1.0.0"}, nil)
for _, tool := range manyTools(65) {
srv.AddTool(tool.tool, tool.handler)
}
cfg := mcpclient.InProcessServer{ID: uuid.New(), Slug: "bot", Server: srv}
tools, summaries, cleanup := mcpclient.ConnectInProcess(ctx, logger, []mcpclient.InProcessServer{cfg})
t.Cleanup(cleanup)
require.Empty(t, tools)
require.Len(t, summaries, 1)
require.Equal(t, mcpclient.ConnectOutcomeError, summaries[0].Outcome)
require.Contains(t, summaries[0].Error, "maximum is 64")
}
func TestConnectInProcess_ToolResultCap(t *testing.T) {
t.Parallel()
const maxBytes = 256 << 10
for _, tc := range []struct {
name string
result *mcp.CallToolResult
wantError bool
}{
{name: "MediaOverCap", result: &mcp.CallToolResult{Content: []mcp.Content{
&mcp.TextContent{Text: "report.pdf"},
&mcp.EmbeddedResource{Resource: &mcp.ResourceContents{
URI: "file:///report.pdf",
MIMEType: "application/pdf",
Blob: bytes.Repeat([]byte("a"), maxBytes),
}},
}}},
{name: "TextOverCap", result: textToolResult(strings.Repeat("a", maxBytes+1)), wantError: true},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitLong)
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
srv := mcp.NewServer(&mcp.Implementation{Name: "test-server", Version: "1.0.0"}, nil)
srv.AddTool(
&mcp.Tool{Name: "get_file", InputSchema: map[string]any{"type": "object"}},
func(context.Context, *mcp.CallToolRequest) (*mcp.CallToolResult, error) {
return tc.result, nil
},
)
cfg := mcpclient.InProcessServer{ID: uuid.New(), Slug: "bot", Server: srv}
tools, _, cleanup := mcpclient.ConnectInProcess(ctx, logger, []mcpclient.InProcessServer{cfg})
t.Cleanup(cleanup)
require.Len(t, tools, 1)
resp, err := tools[0].Run(ctx, fantasy.ToolCall{ID: "call-1", Input: "{}"})
require.NoError(t, err)
require.Equal(t, tc.wantError, resp.IsError)
if tc.wantError {
require.Contains(t, resp.Content, "exceeded maximum size")
return
}
require.Equal(t, "report.pdf", resp.Content)
require.Equal(t, "application/pdf", resp.MediaType)
require.Len(t, resp.Data, maxBytes)
})
}
}