[test] Add tests for middleware.wrapToolHandler#7736
Merged
Conversation
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>
Contributor
There was a problem hiding this comment.
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 nilpath in schema generation when a jq filter turns structured data intonull. - 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
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Test Coverage Improvement:
wrapToolHandlerFunction Analyzed
internal/middlewarewrapToolHandlerWhy This Function?
wrapToolHandleris 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.goTestWrapToolHandler_FastPath_BypassedWithUnknownEnvKeyonlyKnownKeys = falsewhen env map has a key that's not"content"or"isError". Uses amarshalCallRecorderhelper to provejson.Marshal(data)was called (fast path skipped).TestWrapToolHandler_FastPath_BypassedWithTwoKeysOneUnknownlen(env) == 2(max that enters the loop) — one known key, one unknown.TestWrapToolHandler_NilDataAfterFilter"null"jq filter on structured data with emptyresult.ContentcausestryApplyToolResponseFilterto returnfilteredData = nil. Withthreshold=0, the code enters schema generation and hitscase nil, producing aPayloadMetadataresponse withpayloadSchema: null.TestWrapToolHandler_SchemaGenerationError_CanceledContextiter.Next()to return a context-cancellation error beforewalk_schemaruns.schemaErr != niltriggers the graceful fallback that returns the original result instead of metadata.Coverage Report
Test Execution
All tests pass:
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.iogo.yaml.ingolang.orggoogle.golang.orggopkg.ingoproxy.ioproxy.golang.orgreleaseassets.githubusercontent.comSee Network Configuration for more information.