Commit b0670d0
authored
[test-improver] Improve tests for mcp/tool_result package (#4011)
## File Analyzed
- **Test File**: `internal/mcp/tool_result_test.go`
- **Package**: `internal/mcp`
- **Lines Added**: +60
## Improvements Made
### 1. New Coverage: `NewErrorCallToolResult`
The `NewErrorCallToolResult` function had **0% coverage** — it was
entirely untested. Added `TestNewErrorCallToolResult` with three
subtests:
- ✅ Non-nil error returns `IsError=true` result with the error message
as `TextContent`
- ✅ Nil error input substitutes `"unknown error"` message (covers the
nil-guard branch)
- ✅ Error messages with special characters (e.g. embedded JSON) are
preserved verbatim
### 2. New Coverage: `ConvertToCallToolResult` marshal error path
Added `TestConvertToCallToolResult_MarshalError` to cover the
early-return error path when the data argument cannot be JSON-marshaled
(e.g., passing a Go channel). This path was previously unreachable by
any test.
### 3. Coverage Improvement
| Function | Before | After |
|---|---|---|
| `NewErrorCallToolResult` | **0%** | **100%** |
| `ConvertToCallToolResult` | 89.7% | 93.1% |
| `internal/mcp` package total | 81.0% | 81.6% |
## Test Execution
All new and existing tests pass:
```
=== RUN TestNewErrorCallToolResult
=== RUN TestNewErrorCallToolResult/non-nil_error_produces_IsError_result_with_error_message_as_text
=== RUN TestNewErrorCallToolResult/nil_error_substitutes_unknown_error_message
=== RUN TestNewErrorCallToolResult/error_message_with_special_characters_is_preserved
--- PASS: TestNewErrorCallToolResult (0.00s)
=== RUN TestConvertToCallToolResult_MarshalError
--- PASS: TestConvertToCallToolResult_MarshalError (0.00s)
PASS
ok github.com/github/gh-aw-mcpg/internal/mcp 0.011s
```
## Why These Changes?
`NewErrorCallToolResult` is a utility used when a tool call fails and
the gateway needs to produce a structured error response back to the MCP
client. Despite being a public, used function, it had zero test
coverage. The nil-error guard and the function's three return values
(result, nil, err) are now all exercised.
The `ConvertToCallToolResult` marshal error path guards against
programming errors where an un-serializable type is accidentally passed
in; this defensive path was untested.
---
*Generated by Test Improver Workflow*
*Focuses on better patterns, increased coverage, and more stable tests*
> [!WARNING]
> <details>
> <summary><strong>1 file changed
+60
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
315 | 316 | | |
316 | 317 | | |
317 | 318 | | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
0 commit comments