Skip to content

test(mcp): cover workbench detail handler success paths - #1311

Open
ricky9408 wants to merge 2 commits into
GoogleCloudPlatform:mainfrom
ricky9408:test/mcp-detail-handler-success
Open

ricky9408 wants to merge 2 commits into
GoogleCloudPlatform:mainfrom
ricky9408:test/mcp-detail-handler-success

Conversation

@ricky9408

Copy link
Copy Markdown

Closes #1126.

Adds AI-assisted integration tests for five MCP detail tools, including pagination and offset continuation.

Targeted tests and race checks passed. The full local suite had the same failures on unchanged main.

Protect the response contract for detail tools so clients can rely on successful results when continuing pages or offsets.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces integration tests for the MCP workbench detail tools in pkg/server/mcp/workbench_detail_integration_test.go, along with corresponding golden markdown files under pkg/server/mcp/testdata/ to verify paging, bodies, and error handling. The review feedback suggests two improvements to the test helper functions: copying the args map in readDetailBodyPages to avoid mutating shared test case definitions, and trimming trailing whitespace in detailFooter to make the markdown parsing more robust against trailing newlines.

Comment on lines +275 to +303
func readDetailBodyPages(t *testing.T, session *mcpsdk.ClientSession, tool string, args map[string]any, language string) string {
t.Helper()
var body strings.Builder
for page := range 4 {
markdown := callDetailTool(t, session, tool, args)
body.WriteString(detailBody(t, markdown, language))
tail := detailFooter(markdown, "byteOffset")
if tail == "" {
if page == 0 {
t.Fatalf("%s did not truncate the first page", tool)
}
return body.String()
}
offset, err := strconv.Atoi(tail)
if err != nil || offset <= 0 {
t.Fatalf("%s returned invalid byteOffset %q", tool, tail)
}
// The Markdown fence removes a trailing newline from each chunk.
if body.Len()+1 == offset {
body.WriteByte('\n')
}
if body.Len() != offset {
t.Fatalf("%s returned byteOffset %d after %d bytes", tool, offset, body.Len())
}
args["byteOffset"] = offset
}
t.Fatalf("%s body did not finish within four responses", tool)
return ""
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The args map is passed as a parameter and mutated directly inside readDetailBodyPages (by setting args["byteOffset"] = offset). Since maps in Go are reference types, this mutates the map owned by the caller (e.g., tc.args in the test cases). While the current tests run sequentially, mutating shared test case definitions can lead to unexpected side effects, flaky tests, or race conditions if the tests are ever refactored to run in parallel or run multiple times.

It is safer and more idiomatic to copy the map inside the helper function before modifying it.

func readDetailBodyPages(t *testing.T, session *mcpsdk.ClientSession, tool string, args map[string]any, language string) string {
	t.Helper()
	toolArgs := make(map[string]any, len(args)+1)
	for k, v := range args {
		toolArgs[k] = v
	}
	var body strings.Builder
	for page := range 4 {
		markdown := callDetailTool(t, session, tool, toolArgs)
		body.WriteString(detailBody(t, markdown, language))
		tail := detailFooter(markdown, "byteOffset")
		if tail == "" {
			if page == 0 {
				t.Fatalf("%s did not truncate the first page", tool)
			}
			return body.String()
		}
		offset, err := strconv.Atoi(tail)
		if err != nil || offset <= 0 {
			t.Fatalf("%s returned invalid byteOffset %q", tool, tail)
		}
		// The Markdown fence removes a trailing newline from each chunk.
		if body.Len()+1 == offset {
			body.WriteByte('\n')
		}
		if body.Len() != offset {
			t.Fatalf("%s returned byteOffset %d after %d bytes", tool, offset, body.Len())
		}
		toolArgs["byteOffset"] = offset
	}
	t.Fatalf("%s body did not finish within four responses", tool)
	return ""
}
Comment on lines +316 to +323
func detailFooter(markdown, key string) string {
lastLine := markdown[strings.LastIndexByte(markdown, '\n')+1:]
value, found := strings.CutPrefix(lastLine, key+": ")
if !found {
return ""
}
return value
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation of detailFooter is fragile if the markdown string contains any trailing newlines or whitespace (which is common in template outputs or files read from disk). If a trailing newline is present, strings.LastIndexByte(markdown, '\n') will point to the end of the string, making lastLine empty and causing the helper to fail to extract the footer value.

Trimming trailing whitespace first makes this helper much more robust.

Suggested change
func detailFooter(markdown, key string) string {
lastLine := markdown[strings.LastIndexByte(markdown, '\n')+1:]
value, found := strings.CutPrefix(lastLine, key+": ")
if !found {
return ""
}
return value
}
func detailFooter(markdown, key string) string {
trimmed := strings.TrimSpace(markdown)
lastLine := trimmed[strings.LastIndexByte(trimmed, '\n')+1:]
value, found := strings.CutPrefix(lastLine, key+": ")
if !found {
return ""
}
return value
}
@kyasbal

kyasbal commented Nov 30, 2026

Copy link
Copy Markdown
Member

/gcbrun


gotBody := readDetailBodyPages(t, session, tc.tool, tc.args, tc.language)

if gotBody != wantBody {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use cmp.Diff for this assertion, then the test error would be easier to see.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The continuation test now shows cmp.Diff on mismatch.


func assertDetailGolden(t *testing.T, name, got string) {
t.Helper()
want := readGolden(t, filepath.Join("testdata", name+".golden.md"))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we changed the logic for generating the tool result, we need to change the golden.
How do you think about updating the golden if a environment variable is passed. That would be far easier to update golden.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forget if we have a similar logic still, but I vaguely remember I implemented a similar logic in somewhere.
If you can use it, that would be nicer. But if not, you can implement by yourself since it's a simple logic

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added KHI_UPDATE_DETAIL_GOLDENS=1 in b972d2e. I verified it regenerates the fixtures and that normal runs still fail on a mismatch without rewriting them.

@kyasbal

kyasbal commented Nov 30, 2026

Copy link
Copy Markdown
Member

/gcbrun

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants