Skip to content

Commit e3fa5a6

Browse files
committed
gemini review + fix tests
1 parent 13fa914 commit e3fa5a6

File tree

2 files changed

+66
-16
lines changed

2 files changed

+66
-16
lines changed

tests/cloudsqlmysql/cloud_sql_mysql_mcp_test.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,32 +106,48 @@ func TestCloudSQLMySQLMCP(t *testing.T) {
106106
if err != nil || statusCode != http.StatusOK || mcpResp.Result.IsError {
107107
t.Fatalf("native error executing list_tables: %v", err)
108108
}
109-
got := mcpResp.Result.Content[0].Text
110-
if !strings.Contains(got, tableNameParam) || !strings.Contains(got, tableNameAuth) {
111-
t.Errorf("list_tables missing expected tables. Got: %s", got)
109+
var gotTables string
110+
for _, c := range mcpResp.Result.Content {
111+
gotTables += c.Text
112+
}
113+
if !strings.Contains(gotTables, tableNameParam) || !strings.Contains(gotTables, tableNameAuth) {
114+
t.Errorf("list_tables missing expected tables. Got: %s", gotTables)
112115
}
113116

114117
go func() {
115118
_ = pool.PingContext(ctx)
116119
_, _ = pool.ExecContext(ctx, "SELECT sleep(5);")
117120
}()
118-
time.Sleep(1 * time.Second)
119-
120-
statusCode, mcpResp, err = tests.InvokeMCPTool(t, "list_active_queries", map[string]any{"min_duration_secs": 0}, nil)
121-
if err != nil || statusCode != http.StatusOK || mcpResp.Result.IsError {
122-
t.Fatalf("native error executing list_active_queries: %v", err)
121+
var activeQueriesFound bool
122+
for i := 0; i < 5; i++ {
123+
time.Sleep(1 * time.Second)
124+
statusCode, mcpResp, err = tests.InvokeMCPTool(t, "list_active_queries", map[string]any{"min_duration_secs": 0}, nil)
125+
if err == nil && statusCode == http.StatusOK && !mcpResp.Result.IsError {
126+
var gotQueries string
127+
for _, c := range mcpResp.Result.Content {
128+
gotQueries += c.Text
129+
}
130+
if strings.Contains(gotQueries, "SELECT sleep(5)") {
131+
activeQueriesFound = true
132+
break
133+
}
134+
}
123135
}
124-
if !strings.Contains(mcpResp.Result.Content[0].Text, "SELECT sleep(5)") {
125-
t.Errorf("active queries did not contain test sleep query. Got: %s", mcpResp.Result.Content[0].Text)
136+
if !activeQueriesFound {
137+
t.Fatalf("active queries did not contain test sleep query after retries")
126138
}
127139

128140
queryPlanSql := fmt.Sprintf("SELECT * FROM %s", tableNameParam)
129141
statusCode, mcpResp, err = tests.InvokeMCPTool(t, "get_query_plan", map[string]any{"sql_statement": queryPlanSql}, nil)
130142
if err != nil || statusCode != http.StatusOK || mcpResp.Result.IsError {
131143
t.Fatalf("native error executing get_query_plan: %v", err)
132144
}
133-
if !strings.Contains(mcpResp.Result.Content[0].Text, "query_block") {
134-
t.Errorf("query plan did not contain 'query_block'. Got: %s", mcpResp.Result.Content[0].Text)
145+
var gotPlan string
146+
for _, c := range mcpResp.Result.Content {
147+
gotPlan += c.Text
148+
}
149+
if !strings.Contains(gotPlan, "query_block") {
150+
t.Errorf("query plan did not contain 'query_block'. Got: %s", gotPlan)
135151
}
136152
})
137153

tests/mcp_tool.go

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,24 @@ func RunMCPCustomToolCallMethod(t *testing.T, toolName string, arguments map[str
177177
if len(mcpResp.Result.Content) == 0 {
178178
t.Fatalf("%s returned empty content field", toolName)
179179
}
180-
got := mcpResp.Result.Content[0].Text
180+
181+
// Gather all the text blocks
182+
var blocks []string
183+
for _, content := range mcpResp.Result.Content {
184+
if content.Type == "text" {
185+
blocks = append(blocks, strings.TrimSpace(content.Text))
186+
}
187+
}
188+
189+
var got string
190+
// If the test is expecting a JSON array, format the stitched blocks as a JSON array
191+
if want != "" && strings.HasPrefix(strings.TrimSpace(want), "[") {
192+
got = "[" + strings.Join(blocks, ",") + "]"
193+
} else {
194+
// Otherwise, just concatenate them normally (for single responses or errors)
195+
got = strings.Join(blocks, "")
196+
}
197+
181198
if !strings.Contains(got, want) {
182199
t.Fatalf(`expected %q to contain %q`, got, want)
183200
}
@@ -278,7 +295,24 @@ func RunMCPToolInvokeTest(t *testing.T, select1Want string, options ...InvokeTes
278295
if len(mcpResp.Result.Content) == 0 {
279296
t.Fatalf("%s returned empty content field", tc.toolName)
280297
}
281-
got := mcpResp.Result.Content[0].Text
298+
299+
// Gather all the text blocks
300+
var blocks []string
301+
for _, content := range mcpResp.Result.Content {
302+
if content.Type == "text" {
303+
blocks = append(blocks, strings.TrimSpace(content.Text))
304+
}
305+
}
306+
307+
var got string
308+
// If the test is expecting a JSON array, format the stitched blocks as a JSON array
309+
if tc.wantResult != "" && strings.HasPrefix(strings.TrimSpace(tc.wantResult), "[") {
310+
got = "[" + strings.Join(blocks, ",") + "]"
311+
} else {
312+
// Otherwise, just concatenate them normally (for single responses or errors)
313+
got = strings.Join(blocks, "")
314+
}
315+
282316
if !strings.Contains(got, tc.wantResult) {
283317
t.Fatalf(`expected %q to contain %q`, got, tc.wantResult)
284318
}
@@ -370,7 +404,7 @@ func GetExecuteSQLMCPExpectedTools() []MCPToolManifest {
370404
Description: "Tool to execute sql",
371405
InputSchema: map[string]any{
372406
"type": "object",
373-
"properties": map[string]any{"sql": map[string]any{"type": "string", "description": "A valid SQL statement to execute."}},
407+
"properties": map[string]any{"sql": map[string]any{"type": "string", "description": "The sql to execute."}},
374408
"required": []any{"sql"},
375409
},
376410
},
@@ -379,7 +413,7 @@ func GetExecuteSQLMCPExpectedTools() []MCPToolManifest {
379413
Description: "Tool to execute sql",
380414
InputSchema: map[string]any{
381415
"type": "object",
382-
"properties": map[string]any{"sql": map[string]any{"type": "string", "description": "A valid SQL statement to execute."}},
416+
"properties": map[string]any{"sql": map[string]any{"type": "string", "description": "The sql to execute."}},
383417
"required": []any{"sql"},
384418
},
385419
},

0 commit comments

Comments
 (0)