Skip to content

[test-improver] Improve tests for mcp package connection utilities#1590

Closed
github-actions[bot] wants to merge 1 commit intomainfrom
test-improver/mcp-connection-tests-f9c4c6fb834a64f1
Closed

[test-improver] Improve tests for mcp package connection utilities#1590
github-actions[bot] wants to merge 1 commit intomainfrom
test-improver/mcp-connection-tests-f9c4c6fb834a64f1

Conversation

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented Mar 4, 2026

File Analyzed

  • Test File: internal/mcp/connection_stderr_test.go
  • Package: internal/mcp
  • Lines of Code: 38 → 261 (substantial improvement)

Improvements Made

1. Replaced Documentation-Only Test With Real Assertions

The original file contained a single TestConnection_MultipleServersStderrLogging test that consisted entirely of comment-style documentation and three t.Log() calls — no assertions whatsoever. It was essentially a code comment dressed as a test. This PR replaces it with meaningful unit tests that actually verify behavior.

2. New Coverage: NormalizeInputSchema (Previously Untested)

internal/mcp/schema.go exports NormalizeInputSchema but had zero test coverage. Added comprehensive table-driven tests:

  • TestNormalizeInputSchema — 5 cases covering nil schema, object schema with/without properties, schema with properties but no type, empty schema
  • TestNormalizeInputSchema_NonObjectTypes — string, array, and integer schemas are returned as-is (no properties added)
  • TestNormalizeInputSchema_AdditionalProperties — schemas with additionalProperties skip empty-properties injection
  • TestNormalizeInputSchema_DoesNotMutateOriginal — verifies the function never mutates the caller's schema map
  • TestNormalizeInputSchema_NilSchemaReturnsIndependentMaps — each nil call returns an independent map (mutation isolation)

3. New Coverage: connection.go Helper Functions

Three unexported helpers in connection.go were also untested:

  • TestMarshalToResponse — 3 sub-tests: map input, nil input, struct input; verifies JSONRPC=2.0 and correct JSON round-trip
  • TestRequireSession — verifies that a nil session returns a clear "SDK session not available" error
  • TestCallSDKMethod_UnsupportedMethod — verifies the default switch case returns an "unsupported method: ..." error
  • TestCallSDKMethod_SessionRequiredMethods — all 6 MCP methods (tools/list, tools/call, resources/list, resources/read, prompts/list, prompts/get) return a session error when session is nil

4. Preserved Documentation Value

The serverID attribution behavior (the original reason for the file) is now documented via TestConnection_ServerIDField, which actually asserts that the serverID field is stored correctly on the Connection struct, while the doc comment preserves the historical context about before/after log line attribution.

Why These Changes?

connection_stderr_test.go was the weakest test file in the repository — it had no assertions and provided zero coverage. Meanwhile, schema.go's NormalizeInputSchema function (which has 5 distinct code paths) was completely untested despite being called on every tool registration. This PR addresses both gaps in a single file by adding real tests for the mcp package's utility functions.


Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests

Generated by Test Improver

…st.go

- Replace documentation-only test with real unit tests using testify assertions
- Add TestConnection_ServerIDField: verifies serverID field attribution behavior
- Add TestNormalizeInputSchema: comprehensive table-driven tests for
  NormalizeInputSchema (previously untested in schema.go)
- Add TestNormalizeInputSchema_NonObjectTypes, _AdditionalProperties,
  _DoesNotMutateOriginal, _NilSchemaReturnsIndependentMaps
- Add TestMarshalToResponse: tests the marshalToResponse helper
- Add TestRequireSession: verifies error on nil session
- Add TestCallSDKMethod_UnsupportedMethod: verifies unsupported method error
- Add TestCallSDKMethod_SessionRequiredMethods: all 6 SDK methods return
  session error when session is nil

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant