Commit cebccbe
authored
[test-improver] Improve tests for mcp tool_result package (#4254)
# Test Improvements: `tool_result_test.go`
## File Analyzed
- **Test File**: `internal/mcp/tool_result_test.go`
- **Package**: `internal/mcp`
- **Lines Added**: +38
## Improvements Made
### 1. Increased Coverage
- ✅ Added test for `ConvertToCallToolResult` when the `content` field is
a non-array value (e.g. a JSON string) — triggers the `json.Unmarshal`
error fallback that wraps raw bytes as a text content item
- ✅ Added test documenting `ParseToolArguments` behavior with `null`
JSON input — shows it returns a `nil` map (not empty map) without error
- **`ConvertToCallToolResult` coverage**: 93.1% → **100%**
- **`ParseToolArguments` coverage**: maintained at 100%
- **`NewErrorCallToolResult` coverage**: maintained at 100%
### 2. Cleaner & More Stable Tests
- ✅ New tests follow the existing `t.Run()` subtest style in the file
(not table-driven, consistent with surrounding code)
- ✅ Use `require`/`assert` from testify for precise, idiomatic
assertions
## Test Execution
All `internal/mcp` tests pass:
```
ok github.com/github/gh-aw-mcpg/internal/mcp 0.744s coverage: 82.0% of statements
github.com/github/gh-aw-mcpg/internal/mcp/tool_result.go:15: ConvertToCallToolResult 100.0%
github.com/github/gh-aw-mcpg/internal/mcp/tool_result.go:131: ParseToolArguments 100.0%
github.com/github/gh-aw-mcpg/internal/mcp/tool_result.go:148: NewErrorCallToolResult 100.0%
```
*Note: `TestFetchAndFixSchema_NetworkError` in `internal/config` is a
pre-existing failure unrelated to this PR (network test making a real
HTTP request that returns 403 in CI).*
## Why These Changes?
`ConvertToCallToolResult` had an untested fallback path: when the
response JSON has a `content` field that is not an array (e.g.
`{"content": "some string"}`), the typed struct unmarshal fails and the
function falls back to wrapping the raw bytes as a text content item.
This path was completely untested.
The `ParseToolArguments` null-JSON test documents a subtle but important
behavior: passing `null` JSON yields a `nil` map (not an empty map),
which callers should be aware of.
---
*Generated by Test Improver Workflow*
*Focuses on better patterns, increased coverage, and more stable tests*
> [!WARNING]
> <details>
> <summary><strong>1 file changed
Lines changed: 38 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
221 | 221 | | |
222 | 222 | | |
223 | 223 | | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
224 | 247 | | |
225 | 248 | | |
226 | 249 | | |
| |||
317 | 340 | | |
318 | 341 | | |
319 | 342 | | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
320 | 358 | | |
321 | 359 | | |
322 | 360 | | |
| |||
0 commit comments