Skip to content

[test-improver] Improve tests for logger/rpc_formatter#4332

Merged
lpcox merged 1 commit intomainfrom
test-improver/rpc-logger-coverage-cd753916d799014d
Apr 22, 2026
Merged

[test-improver] Improve tests for logger/rpc_formatter#4332
lpcox merged 1 commit intomainfrom
test-improver/rpc-logger-coverage-cd753916d799014d

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

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 · ● 6.3M ·

Add missing test cases to TestFormatRPCMessage, TestFormatRPCMessageMarkdown,
and TestFormatJSONWithoutFields that cover untested branches in rpc_formatter.go:

- formatRPCMessage: empty ServerID (no server prefix), empty Payload, and
  response with error but no payload
- formatRPCMessageMarkdown: empty ServerID, empty Payload (no code block),
  and tools/call with a non-string 'name' field in params (no tool name appended)
- formatJSONWithoutFields: nil fieldsToRemove slice and removing non-existent fields

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review April 22, 2026 14:34
Copilot AI review requested due to automatic review settings April 22, 2026 14:34
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Expands unit test coverage for the RPC logging formatter helpers in internal/logger, targeting previously untested defensive branches around empty server IDs/payloads and tool-name extraction.

Changes:

  • Adds new table-driven cases for formatRPCMessage covering empty ServerID, empty Payload, and error-only responses.
  • Adds new table-driven cases for formatRPCMessageMarkdown covering empty ServerID, empty Payload, and non-string params.name for tools/call.
  • Adds new table-driven cases for formatJSONWithoutFields covering nil removal lists and no-op deletions.
Show a summary per file
File Description
internal/logger/rpc_logger_test.go Adds additional table-driven test cases to improve branch coverage for RPC text/markdown formatting and JSON field stripping.

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

Comment on lines +58 to +69
{
name: "empty server ID omits server prefix",
info: &RPCMessageInfo{
Direction: RPCDirectionOutbound,
MessageType: RPCMessageRequest,
ServerID: "",
Method: "tools/list",
PayloadSize: 30,
Payload: `{"method":"tools/list"}`,
},
want: []string{"30b", `{"method":"tools/list"}`},
},
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test case name says the server prefix is omitted, but it only asserts positive substrings ("30b" and the payload). If the formatter ever starts emitting a direction/method prefix even when ServerID is empty (e.g. "→tools/list"), this test would still pass. Add a negative assertion (e.g., ensure the result does not contain "→"/"←", or assert it starts with the size token) so the test actually guards the intended behavior.

Copilot uses AI. Check for mistakes.
@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented Apr 22, 2026

@copilot address this review feedback #4332 (review)

@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented Apr 22, 2026

@copilot address the review feedback #4332 (review)

@lpcox lpcox merged commit 1e210cc into main Apr 22, 2026
26 checks passed
@lpcox lpcox deleted the test-improver/rpc-logger-coverage-cd753916d799014d branch April 22, 2026 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants