Commit 1e210cc
authored
[test-improver] Improve tests for logger/rpc_formatter (#4332)
## File Analyzed
- **Test File**: `internal/logger/rpc_logger_test.go`
- **Package**: `internal/logger`
- **Lines Added**: +94
## Improvements Made
### 1. Increased Coverage for `formatRPCMessage`
Added 3 new table-driven test cases covering untested branches in
`rpc_formatter.go`:
- ✅ **Empty `ServerID`** — verifies no server/direction prefix is added
when `serverID == ""`; only the payload size and payload content appear
- ✅ **Empty `Payload`** — verifies the payload string is omitted from
output when payload is empty
- ✅ **Error with no payload** — verifies an error-only response (no
payload) formats correctly with `err:` prefix
### 2. Increased Coverage for `formatRPCMessageMarkdown`
Added 3 new table-driven test cases covering untested branches:
- ✅ **Empty `ServerID`** — verifies no bold server prefix
(`**server**→`) is produced
- ✅ **Empty `Payload`** — verifies no `\`\`\`json` code block is emitted
when payload is empty
- ✅ **`tools/call` with non-string `name` in params** — verifies the
tool-name extraction guard (checking `toolName, ok :=
params["name"].(string)`) correctly skips appending a backtick-wrapped
name when the value is not a string
### 3. Increased Coverage for `formatJSONWithoutFields`
Added 2 new table-driven test cases:
- ✅ **Nil `fieldsToRemove` slice** — verifies all fields are preserved
when the removal list is nil
- ✅ **Non-existent field removal is a no-op** — verifies the function
handles attempts to remove fields that aren't in the JSON gracefully
## Test Execution
All tests pass:
```
=== RUN TestFormatRPCMessage
--- PASS: TestFormatRPCMessage (0.00s)
--- PASS: TestFormatRPCMessage/outbound_request
--- PASS: TestFormatRPCMessage/inbound_response_with_error
--- PASS: TestFormatRPCMessage/client_request
--- PASS: TestFormatRPCMessage/empty_server_ID_omits_server_prefix
--- PASS: TestFormatRPCMessage/empty_payload_omits_payload_from_output
--- PASS: TestFormatRPCMessage/response_with_error_and_no_payload
=== RUN TestFormatRPCMessageMarkdown
--- PASS: TestFormatRPCMessageMarkdown (0.00s)
...10 subtests all PASS...
=== RUN TestFormatJSONWithoutFields
--- PASS: TestFormatJSONWithoutFields (0.00s)
...8 subtests all PASS...
ok github.com/github/gh-aw-mcpg/internal/logger 0.006s
```
## Why These Changes?
`rpc_formatter.go` contains the core text/markdown formatting logic for
all RPC message logging. The existing tests covered the "happy path"
scenarios (non-empty server, non-empty payload, valid JSON). However,
several defensive branches in the code — guarding against empty
`ServerID`, empty `Payload`, and non-string type assertions — had zero
test coverage. These new tests exercise those branches directly,
documenting the expected behavior and guarding against regressions.
---
*Generated by Test Improver Workflow*
*Focuses on better patterns, increased coverage, and more stable tests*
> Generated by [Test
Improver](https://github.com/github/gh-aw-mcpg/actions/runs/24776282832/agentic_workflow)
· ● 6.3M ·
[◷](https://github.com/search?q=repo%3Agithub%2Fgh-aw-mcpg+%22gh-aw-workflow-id%3A+test-improver%22&type=pullrequests)
<!-- gh-aw-agentic-workflow: Test Improver, engine: copilot, model:
auto, id: 24776282832, workflow_id: test-improver, run:
https://github.com/github/gh-aw-mcpg/actions/runs/24776282832 -->
<!-- gh-aw-workflow-id: test-improver -->1 file changed
Lines changed: 94 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 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 | + | |
58 | 94 | | |
59 | 95 | | |
60 | 96 | | |
| |||
177 | 213 | | |
178 | 214 | | |
179 | 215 | | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
180 | 256 | | |
181 | 257 | | |
182 | 258 | | |
| |||
258 | 334 | | |
259 | 335 | | |
260 | 336 | | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
261 | 355 | | |
262 | 356 | | |
263 | 357 | | |
| |||
0 commit comments