[test-improver] Improve tests for logger/rpc_logger package - #1789
Conversation
…tions - Replace all manual strings.Contains checks with assert.Contains / assert.NotContains - Replace assert.True/False + strings.Contains with direct assert.Contains/NotContains - Replace t.Fatalf logger init with require.NoError for consistent failure semantics - Add tests for LogRPCRequestWithAgentSnapshot and LogRPCResponseWithAgentSnapshot (previously untested public API; verifies agent DIFC tags appear in JSONL output) - Add test for empty agent tags (nil secrecy/integrity slices omitted from JSON) - Add test for LogRPCMessage (previously untested) - Add TestLogRPCResponse_NoError that verifies JSONL output directly Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR improves the internal/logger RPC logger test suite by making assertions more idiomatic (testify) and adding new coverage for previously untested RPC logging entrypoints, including agent DIFC snapshot logging to JSONL.
Changes:
- Replaced manual
strings.Containsloops and boolean wrappers withassert.Contains/assert.NotContainsandrequire.NoErroracross existing tests. - Added new tests for
LogRPCRequestWithAgentSnapshot,LogRPCResponseWithAgentSnapshot,LogRPCMessage, and a “no error” JSONL response case. - Expanded JSONL verification logic in tests by parsing log output.
Comments suppressed due to low confidence (1)
internal/logger/rpc_logger_test.go:528
TestLogRPCRequestWithAgentSnapshot_EmptyTagsclaims to verifyomitemptybehavior, but unmarshalling intoJSONLRPCMessageand asserting the slices arenilcan’t distinguish between fields being omitted vs being present asnull(which would happen ifomitemptywere removed). To actually test omission, parse the raw JSON line into amap[string]any(orjson.RawMessage) and assert theagent_secrecy/agent_integritykeys are not present.
// nil slices should not appear as fields in JSON
assert.Nil(t, entry.AgentSecrecy, "AgentSecrecy should be nil when empty tags passed")
assert.Nil(t, entry.AgentIntegrity, "AgentIntegrity should be nil when empty tags passed")
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| scanner := bufio.NewScanner(strings.NewReader(string(jsonlContent))) | ||
| for scanner.Scan() { | ||
| jsonlLines = append(jsonlLines, scanner.Text()) | ||
| } |
There was a problem hiding this comment.
After scanning the JSONL content, the test doesn’t check scanner.Err(). If the scanner hits an error (e.g., token too long), this loop will silently stop and the assertions may be misleading. Add an assertion on scanner.Err() after the loop (or avoid bufio.Scanner and split/read lines in a way that surfaces errors).
| } | |
| } | |
| require.NoError(t, scanner.Err(), "scanner encountered an error while reading JSONL content") |
| var entry JSONLRPCMessage | ||
| require.NoError(t, json.Unmarshal([]byte(jsonlLines[0]), &entry)) | ||
| assert.Empty(t, entry.Error, "Error field should be empty when no error") |
There was a problem hiding this comment.
This test says it verifies the JSONL entry has “no error field”, but unmarshalling into JSONLRPCMessage and asserting entry.Error is empty can’t detect whether the error key was omitted vs present as "error":"" (if omitempty were removed). If the intent is to enforce omission, unmarshal the JSON into a map[string]any and assert the error key is absent.
This issue also appears on line 525 of the same file.
| var entry JSONLRPCMessage | |
| require.NoError(t, json.Unmarshal([]byte(jsonlLines[0]), &entry)) | |
| assert.Empty(t, entry.Error, "Error field should be empty when no error") | |
| // First, verify that the JSON object does not contain an "error" field at all. | |
| var raw map[string]any | |
| require.NoError(t, json.Unmarshal([]byte(jsonlLines[0]), &raw)) | |
| _, hasErrorField := raw["error"] | |
| assert.False(t, hasErrorField, "JSONL entry should not contain an 'error' field when no error is passed") | |
| // Then, verify other structured fields as before. | |
| var entry JSONLRPCMessage | |
| require.NoError(t, json.Unmarshal([]byte(jsonlLines[0]), &entry)) |
Test Improvements:
internal/logger/rpc_logger_test.goFile Analyzed
internal/logger/rpc_logger_test.gointernal/loggerImprovements Made
1. Better Idiomatic Testify Usage
Several tests used manual
strings.Containschecks instead of testify's purpose-built assertions. This made failures harder to read and inconsistent with the rest of the codebase.Before — manual loop with
t.Errorf:After — idiomatic testify:
Specific patterns replaced across 4 test functions (
TestFormatRPCMessage,TestFormatRPCMessageMarkdown,TestFormatJSONWithoutFields,TestLogRPCRequestPayloadTruncation):if !strings.Contains(x, y) { t.Errorf(...) }→assert.Contains(t, x, y, ...)if strings.Contains(x, y) { t.Errorf(...) }→assert.NotContains(t, x, y, ...)assert.True(t, strings.Contains(x, y), ...)→assert.Contains(t, x, y, ...)assert.False(t, strings.Contains(x, y), ...)→assert.NotContains(t, x, y, ...)if err := Init...; err != nil { t.Fatalf(...) }→require.NoError(t, Init..., "...")2. Increased Coverage — 5 New Tests
Four public API functions had zero test coverage:
LogRPCRequestWithAgentSnapshotLogRPCResponseWithAgentSnapshotLogRPCMessageNew tests added:
TestLogRPCRequestWithAgentSnapshot— verifiesAgentSecrecy/AgentIntegrityDIFC tags are written to JSONL outputTestLogRPCResponseWithAgentSnapshot— same for response path, with multiple integrity tagsTestLogRPCRequestWithAgentSnapshot_EmptyTags— verifies nil tag slices are correctly omitted from JSON (usingomitempty)TestLogRPCMessage— covers the genericLogRPCMessagepath (text + markdown)TestLogRPCResponse_NoError— directly verifies the JSONL output of a successful response (no error field)3. Cleaner & More Stable Tests
require.NoErrorstops tests immediately on logger init failure (previouslyt.Fatalf— same semantics but more idiomatic)errvariable toreadErrinTestLogRPCResponseto eliminate variable reuse across different error sourcesassert.ElementsMatchfor order-independent tag comparisonWhy These Changes?
rpc_logger_test.gowas testing important security-sensitive logging paths (secrets redaction, truncation) and the new agent DIFC snapshot functions — but used inconsistent assertion styles: a mix of manualstrings.Containsloops,assert.True(t, strings.Contains(...))wrappers, and proper testify calls. The manual patterns produce worse failure messages and bypass testify's consistent output format. The four untested public functions (LogRPCRequest/ResponseWithAgentSnapshot,LogRPCMessage) are important for DIFC audit trails and deserved direct coverage.Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests