Skip to content

Commit 5223489

Browse files
committed
fix test flake
1 parent efc0cdb commit 5223489

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

mcp/tools/tools_coder_test.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import (
2525
// Running them in parallel is prone to racy behavior.
2626
// nolint:tparallel,paralleltest
2727
func TestCoderTools(t *testing.T) {
28-
t.Parallel()
29-
3028
ctx := testutil.Context(t, testutil.WaitLong)
3129
// Given: a coder server, workspace, and agent.
3230
client, store := coderdtest.NewWithDatabase(t, nil)
@@ -39,12 +37,20 @@ func TestCoderTools(t *testing.T) {
3937
OwnerID: member.ID,
4038
}).WithAgent().Do()
4139

42-
agt := agenttest.New(t, client.URL, r.AgentToken)
43-
t.Cleanup(func() {
44-
_ = agt.Close()
45-
})
46-
// Given: the workspace agent is connected
47-
_ = coderdtest.NewWorkspaceAgentWaiter(t, client, r.Workspace.ID).Wait()
40+
// Note: we want to test the list_workspaces tool before starting the
41+
// workspace agent. Starting the workspace agent will modify the workspace
42+
// state, which will affect the results of the list_workspaces tool.
43+
listWorkspacesDone := make(chan struct{})
44+
agentStarted := make(chan struct{})
45+
go func() {
46+
defer close(agentStarted)
47+
<-listWorkspacesDone
48+
agt := agenttest.New(t, client.URL, r.AgentToken)
49+
t.Cleanup(func() {
50+
_ = agt.Close()
51+
})
52+
_ = coderdtest.NewWorkspaceAgentWaiter(t, client, r.Workspace.ID).Wait()
53+
}()
4854

4955
// Given: a MCP server listening on a pty.
5056
pty := ptytest.New(t)
@@ -118,6 +124,7 @@ func TestCoderTools(t *testing.T) {
118124
})
119125

120126
t.Run("coder_list_workspaces", func(t *testing.T) {
127+
defer close(listWorkspacesDone)
121128
// When: the coder_list_workspaces tool is called
122129
ctr := makeJSONRPCRequest(t, "tools/call", "coder_list_workspaces", map[string]any{
123130
"coder_url": client.URL.String(),
@@ -158,7 +165,11 @@ func TestCoderTools(t *testing.T) {
158165
testutil.RequireJSONEq(t, expected, actual)
159166
})
160167

168+
// NOTE: this test runs after the list_workspaces tool is called.
161169
t.Run("coder_workspace_exec", func(t *testing.T) {
170+
// Given: the workspace agent is connected
171+
<-agentStarted
172+
162173
// When: the coder_workspace_exec tools is called with a command
163174
randString := testutil.GetRandomName(t)
164175
ctr := makeJSONRPCRequest(t, "tools/call", "coder_workspace_exec", map[string]any{
@@ -198,7 +209,11 @@ func TestCoderTools(t *testing.T) {
198209
testutil.RequireJSONEq(t, expected, actual)
199210
})
200211

212+
// NOTE: this test runs after the list_workspaces tool is called.
201213
t.Run("tool_and_command_restrictions", func(t *testing.T) {
214+
// Given: the workspace agent is connected
215+
<-agentStarted
216+
202217
// Given: a restricted MCP server with only allowed tools and commands
203218
restrictedPty := ptytest.New(t)
204219
allowedTools := []string{"coder_workspace_exec"}

0 commit comments

Comments
 (0)