Skip to content

[test-improver] Improve tests for schema normalization package#1406

Merged
lpcox merged 1 commit intomainfrom
test-improver/schema-normalization-coverage-b88c02b62df03d31
Feb 25, 2026
Merged

[test-improver] Improve tests for schema normalization package#1406
lpcox merged 1 commit intomainfrom
test-improver/schema-normalization-coverage-b88c02b62df03d31

Conversation

@github-actions
Copy link
Contributor

File Analyzed

  • Test File: internal/server/schema_normalization_test.go
  • Package: internal/server (testing internal/mcp.NormalizeInputSchema)
  • Lines of Code: 153 → 288 (+135 lines of new tests)

Improvements Made

1. Increased Coverage — All Branches Now Tested

The NormalizeInputSchema function has 6 distinct code paths. The original test file only covered 3 of them. Added TestNormalizeInputSchema to cover all missing branches:

Branch Was Covered Now Covered
nil schema → default empty object
Empty schema (no type, no properties) → empty object
Schema without type but with properties → adds type: object
Non-object type ("string", "array") → returned as-is
Non-string type value (e.g. 42) → returned as-is
Object with properties only → returned as-is
Object with additionalProperties only → returned as-is
Object with both properties and additionalProperties

2. Better Testing Patterns

  • ✅ Added TestNormalizeInputSchema as a comprehensive table-driven test covering all 8 cases
  • ✅ Tests are self-contained — call mcp.NormalizeInputSchema directly without heavyweight server setup
  • ✅ Descriptive test case names clarify intent for each code path

3. Cleaner & More Stable Tests

  • ✅ Added TestNormalizeInputSchema_PreservesOriginalForAllMutatingBranches to explicitly verify the copy-on-write guarantee for the two branches that create new schema copies (no-type-with-properties and object-without-properties). This is a correctness invariant of the function that was previously untested.

Why These Changes?

NormalizeInputSchema is a critical function in the MCP backend tool registration path — it prevents SDK panics by ensuring all registered tools have valid JSON schemas. Despite this importance, only half its branches were covered. The added tests document and verify every code path, making regressions immediately visible and future refactoring safer.


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

Generated by Test Improver

Add comprehensive tests for all branches of mcp.NormalizeInputSchema:
- nil schema returns default empty object schema
- empty schema (no type, no properties) returns empty object schema
- schema without type but with properties adds type:object
- non-object type (string, array) returned as-is
- non-string type value returned as-is
- object with both properties and additionalProperties returned as-is
- object with only additionalProperties returned as-is

Also add TestNormalizeInputSchema_PreservesOriginalForAllMutatingBranches
to explicitly verify immutability for the two code paths that create copies.

Previously only 3 of the 6 branches in NormalizeInputSchema were covered.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review February 25, 2026 18:10
Copilot AI review requested due to automatic review settings February 25, 2026 18:10
@lpcox lpcox merged commit 4770bb2 into main Feb 25, 2026
2 checks passed
@lpcox lpcox deleted the test-improver/schema-normalization-coverage-b88c02b62df03d31 branch February 25, 2026 18:10
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR expands test coverage around schema normalization behavior used in the server tool-registration path (via internal/mcp.NormalizeInputSchema), adding table-driven cases and explicit copy-on-write invariants.

Changes:

  • Added a table-driven TestNormalizeInputSchema covering multiple input schema shapes and expected normalized outputs.
  • Added TestNormalizeInputSchema_PreservesOriginalForAllMutatingBranches to assert the original map is not mutated in branches that create normalized copies.
Comments suppressed due to low confidence (1)

internal/server/schema_normalization_test.go:160

  • These tests directly exercise mcp.NormalizeInputSchema, but that function already has extensive unit coverage in internal/mcp/types_test.go. Duplicating branch-coverage tests in internal/server can increase maintenance cost and risk divergence; consider keeping this file focused on server-level integration behavior and leaving NormalizeInputSchema unit tests in the mcp package (or moving the table test there).
func TestNormalizeInputSchema(t *testing.T) {
	tests := []struct {
		name     string
		schema   map[string]interface{}
		expected map[string]interface{}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +155 to +160
// TestNormalizeInputSchema tests all branches of the NormalizeInputSchema function.
func TestNormalizeInputSchema(t *testing.T) {
tests := []struct {
name string
schema map[string]interface{}
expected map[string]interface{}
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

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

The comment says this test "tests all branches" of NormalizeInputSchema, but the table cases here don’t include the object-schema normalization branch (type: object with neither properties nor additionalProperties), which is a distinct code path in internal/mcp/schema.go. Either add that case to this table or soften the comment to reflect that branch coverage is achieved elsewhere in this file.

This issue also appears on line 156 of the same file.

Copilot uses AI. Check for mistakes.
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.

2 participants