Skip to content

[test] Add tests for middleware.wrapToolHandler#7736

Merged
lpcox merged 2 commits into
mainfrom
test-coverage-wrap-tool-handler-c71e96e13a4d44f3
Jun 19, 2026
Merged

[test] Add tests for middleware.wrapToolHandler#7736
lpcox merged 2 commits into
mainfrom
test-coverage-wrap-tool-handler-c71e96e13a4d44f3

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

Test Coverage Improvement: wrapToolHandler

Function Analyzed

  • Package: internal/middleware
  • Function: wrapToolHandler
  • Previous Coverage: 88.6%
  • New Coverage: 94.3%
  • Complexity: High (~260 lines, multiple branches, jq integration, fast-path optimization)

Why This Function?

wrapToolHandler is the core middleware that intercepts tool responses, applies jq filters, checks payload size thresholds, saves large payloads to disk, and generates jq schema metadata. It had several uncovered branches despite being a critical code path, making it the highest-priority candidate by combined complexity × (1 - coverage) score.

Tests Added

New file: internal/middleware/jqschema_wrap_coverage_test.go

Test Lines Covered Description
TestWrapToolHandler_FastPath_BypassedWithUnknownEnvKey 629–631 Fast-path guard: onlyKnownKeys = false when env map has a key that's not "content" or "isError". Uses a marshalCallRecorder helper to prove json.Marshal(data) was called (fast path skipped).
TestWrapToolHandler_FastPath_BypassedWithTwoKeysOneUnknown 629–631 Same guard with len(env) == 2 (max that enters the loop) — one known key, one unknown.
TestWrapToolHandler_NilDataAfterFilter 706–708 A "null" jq filter on structured data with empty result.Content causes tryApplyToolResponseFilter to return filteredData = nil. With threshold=0, the code enters schema generation and hits case nil, producing a PayloadMetadata response with payloadSchema: null.
TestWrapToolHandler_SchemaGenerationError_CanceledContext 724–726, 729–734 A pre-cancelled context causes gojq's iter.Next() to return a context-cancellation error before walk_schema runs. schemaErr != nil triggers the graceful fallback that returns the original result instead of metadata.

Coverage Report

Before: wrapToolHandler  88.6%   internal/middleware 92.1%
After:  wrapToolHandler  94.3%   internal/middleware 94.1%
Improvement: +5.7pp on function, +2.0pp on package

Test Execution

All tests pass:

--- PASS: TestWrapToolHandler_FastPath_BypassedWithUnknownEnvKey (0.00s)
--- PASS: TestWrapToolHandler_FastPath_BypassedWithTwoKeysOneUnknown (0.00s)
--- PASS: TestWrapToolHandler_NilDataAfterFilter (0.00s)
--- PASS: TestWrapToolHandler_SchemaGenerationError_CanceledContext (0.00s)
PASS
ok  github.com/github/gh-aw-mcpg/internal/middleware 0.022s coverage: 94.1%

Generated by Test Coverage Improver
Next run will target the next most complex under-tested function

Warning

Firewall blocked 8 domains

The following domains were blocked by the firewall during workflow execution:

  • go.opentelemetry.io
  • go.yaml.in
  • golang.org
  • google.golang.org
  • gopkg.in
  • goproxy.io
  • proxy.golang.org
  • releaseassets.githubusercontent.com

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

network:
  allowed:
    - defaults
    - "go.opentelemetry.io"
    - "go.yaml.in"
    - "golang.org"
    - "google.golang.org"
    - "gopkg.in"
    - "goproxy.io"
    - "proxy.golang.org"
    - "releaseassets.githubusercontent.com"

See Network Configuration for more information.

Generated by Test Coverage Improver · 3.5K AIC · ⊞ 28.8K ·

Cover three previously-uncovered code paths in wrapToolHandler:

- Lines 629-631 (onlyKnownKeys = false): two tests verify that the fast-path
  optimisation is bypassed when the env map contains a key that is neither
  'content' nor 'isError'. A marshalCallRecorder helper type confirms that
  json.Marshal(data) is actually invoked on the normal path.

- Lines 706-708 (case nil in schema type switch): a 'null' jq filter applied
  to structured data with empty result.Content makes tryApplyToolResponseFilter
  return nil data. With threshold=0 the code reaches the schema type switch,
  hits case nil, and produces a PayloadMetadata response with payloadSchema=nil.

- Lines 724-726, 729-734 (applyJqSchema error fallback): passing a
  pre-cancelled context to the wrapped handler causes gojq to return a
  context-cancellation error from iter.Next() before the walk_schema function
  runs. schemaErr != nil triggers the graceful fallback that returns the
  original result instead of a metadata response.

wrapToolHandler coverage: 88.6% → 94.3%
internal/middleware overall: 92.1% → 94.1%

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review June 19, 2026 01:28
Copilot AI review requested due to automatic review settings June 19, 2026 01:28

Copilot AI 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.

Pull request overview

Adds targeted unit tests in internal/middleware to exercise previously uncovered branches in wrapToolHandler (large-payload jqschema middleware), primarily around the fast-path guard conditions and schema-generation behavior.

Changes:

  • Added new tests to ensure the fast-path optimization is bypassed when the backing payload map contains unknown keys.
  • Added a test to cover the case nil path in schema generation when a jq filter turns structured data into null.
  • Added a test intended to cover the schema-generation error fallback behavior.
Show a summary per file
File Description
internal/middleware/jqschema_wrap_coverage_test.go New tests covering additional wrapToolHandler branches (fast-path guard, nil schema path, schema error fallback).

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: 1

Comment thread internal/middleware/jqschema_wrap_coverage_test.go Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
GitHub Advanced Security started work on behalf of lpcox June 19, 2026 01:40 View session
GitHub Advanced Security finished work on behalf of lpcox June 19, 2026 01:41
@lpcox lpcox merged commit a6e023e into main Jun 19, 2026
27 checks passed
@lpcox lpcox deleted the test-coverage-wrap-tool-handler-c71e96e13a4d44f3 branch June 19, 2026 02:28
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