Commit a6e023e
authored
[test] Add tests for middleware.wrapToolHandler (#7736)
## 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]
> <details>
> <summary>Firewall blocked 8 domains</summary>
>
> 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:
>
> ```yaml
> 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](https://github.github.com/gh-aw/reference/network/) for
more information.
>
> </details>
> Generated by [Test Coverage
Improver](https://github.com/github/gh-aw-mcpg/actions/runs/27777421616)
· 3.5K AIC · ⊞ 28.8K ·
[◷](https://github.com/search?q=repo%3Agithub%2Fgh-aw-mcpg+%22gh-aw-workflow-id%3A+test-coverage-improver%22&type=pullrequests)
<!-- gh-aw-agentic-workflow: Test Coverage Improver, engine: copilot,
version: 1.0.60, model: claude-sonnet-4.6, id: 27777421616, workflow_id:
test-coverage-improver, run:
https://github.com/github/gh-aw-mcpg/actions/runs/27777421616 -->
<!-- gh-aw-workflow-id: test-coverage-improver -->
<!-- gh-aw-workflow-call-id: github/gh-aw-mcpg/test-coverage-improver
-->1 file changed
Lines changed: 225 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
0 commit comments