Skip to content

Conversation

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented Feb 5, 2026

Test Improvements: internal/auth/header_test.go

File Analyzed

  • Test File: internal/auth/header_test.go
  • Package: internal/auth
  • Lines of Code: 346 → 445 (+29%)
  • Test Functions: 5 → 6 (+20%)

Improvements Made

1. Better Testing Patterns ✅

Added Bound Asserters (All 6 Test Functions)

  • ✅ Converted all test functions to use bound asserters
  • ✅ Changed from assert.Equal(t, want, got) to assert := assert.New(t); assert.Equal(want, got)
  • ✅ More concise code - eliminates repetitive t parameter
  • ✅ Better readability and consistency
  • Result: 100% bound asserter usage (6/6 functions)

Affected Functions:

  • TestTruncateSecret
  • TestParseAuthHeader (also uses require := require.New(t))
  • TestValidateAPIKey
  • TestExtractAgentID
  • TestExtractSessionID
  • TestTruncateSessionID (new)

2. Increased Coverage ✅

New Test Function: TestTruncateSessionID

  • Added complete test coverage for TruncateSessionID function
  • ✅ This function (lines 169-180 in header.go) had ZERO test coverage before
  • ✅ Added 12 comprehensive test cases:
    • Empty session ID returns "(none)"
    • Single character (unchanged)
    • Short session IDs (≤8 chars, unchanged)
    • Exactly 8 characters (boundary - not truncated)
    • Exactly 9 characters (boundary - truncated)
    • Long session IDs (>8 chars, truncated with "...")
    • Very long session IDs
    • Special characters
    • Unicode characters (émojis, 🔑)
    • UUID format
    • Whitespace handling (under and over 8 chars)

Coverage Improvement: TruncateSessionID: 0% → 100%

Enhanced ExtractSessionID Tests

  • ✅ Added 3 new edge case tests (+50% increase)
  • Agent format with multiple spaces (trimming behavior)
  • Bearer with tab character (non-standard whitespace)
  • Additional whitespace scenarios

Before: 6 test cases
After: 9 test cases (+50%)

3. Cleaner & More Stable Tests ✅

  • ✅ Consistent testify assertion patterns across all tests
  • ✅ Proper use of bound asserters for cleaner code
  • ✅ Comprehensive edge case coverage
  • ✅ Well-organized table-driven tests
  • ✅ Clear test names and descriptions

Test Statistics

Test Function Before After Change
TestTruncateSecret 9 cases 9 cases -
TestParseAuthHeader 11 cases 11 cases -
TestValidateAPIKey 8 cases 8 cases -
TestExtractAgentID 6 cases 6 cases -
TestExtractSessionID 6 cases 9 cases +3 (+50%)
TestTruncateSessionID 0 (not tested) 12 cases +12 (NEW)
TOTAL 45 cases 55 cases +10 (+22%)

Code Quality Metrics

Metric Before After Improvement
Lines of Code 346 445 +99 (+29%)
Test Functions 5 6 +1 (+20%)
Bound Asserters 0/5 (0%) 6/6 (100%) +100%
Untested Functions 1 0 Coverage complete
Total Test Cases 45 55 +10 (+22%)

Why These Changes?

Selection Rationale

The internal/auth package was selected because:

  1. Critical functionality - Authentication is security-critical
  2. Coverage gap - TruncateSessionID function had no tests
  3. Good foundation - Tests already used testify but lacked bound asserters
  4. Manageable size - 5 test functions allowed comprehensive improvements

Impact

Eliminated untested code - TruncateSessionID now has 100% test coverage
Improved code quality - All tests use consistent bound asserter pattern
Enhanced edge case testing - More comprehensive whitespace and boundary tests
Better maintainability - Cleaner, more readable test code

Testing Approach

  • Used table-driven tests throughout
  • Added comprehensive edge cases (boundaries, special chars, unicode)
  • Maintained backward compatibility with existing tests
  • Followed project conventions and testify best practices

Generated by Test Improver Workflow
Focuses on better testify patterns, increased coverage, and more stable tests

AI generated by Test Improver

- Add bound asserters to all 6 test functions for cleaner testify usage
- Add new TestTruncateSessionID function with 12 comprehensive test cases
- Enhance ExtractSessionID tests with 3 additional edge cases
- Increase test coverage: TruncateSessionID 0% -> 100%
- Increase total test cases from 45 to 55 (+22%)
- All tests now use consistent testify patterns
@lpcox lpcox marked this pull request as ready for review February 7, 2026 01:26
Copilot AI review requested due to automatic review settings February 7, 2026 01:26
@lpcox lpcox merged commit 9f04b76 into main Feb 7, 2026
2 checks passed
@lpcox lpcox deleted the test-improver/auth-header-tests-20260205-7d74b8f3a177660f branch February 7, 2026 01:26
Copy link
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

Improves internal/auth package test quality and coverage by refactoring to testify bound asserters and adding new/expanded table-driven cases (notably adding coverage for TruncateSessionID and more ExtractSessionID edge cases).

Changes:

  • Refactor existing tests to use bound testify asserters (assert.New(t), require.New(t)).
  • Add TestTruncateSessionID with comprehensive truncation/boundary cases.
  • Expand TestExtractSessionID with additional whitespace/tab scenarios.
Comments suppressed due to low confidence (5)

internal/auth/header_test.go:78

  • assert/require are bound to the parent test's t, but used inside t.Run subtests. This can cause failures to be reported on the wrong test name and makes subtest output less actionable. Instantiate assert := assert.New(t) / require := require.New(t) inside the t.Run closure (or pass t to the unbound assertion functions).
func TestParseAuthHeader(t *testing.T) {
	assert := assert.New(t)
	require := require.New(t)

internal/auth/header_test.go:183

  • The bound asserter is created with the parent test's t, but assertions run inside t.Run subtests. For correct subtest-scoped failure reporting, create assert := assert.New(t) inside the subtest closure (or use assert.Equal(t, ...)).
func TestValidateAPIKey(t *testing.T) {
	assert := assert.New(t)

internal/auth/header_test.go:250

  • The bound asserter is created with the parent test's t, but used from t.Run subtests. This can make failures point at the parent test instead of the specific subtest. Create the asserter with the subtest t inside t.Run.
func TestExtractAgentID(t *testing.T) {
	assert := assert.New(t)

internal/auth/header_test.go:298

  • The bound asserter is created with the parent test's t, but assertions execute inside t.Run subtests. Use a subtest-scoped asserter (create it inside t.Run) so failures are attributed to the right subtest.
func TestExtractSessionID(t *testing.T) {
	assert := assert.New(t)

internal/auth/header_test.go:371

  • The bound asserter is created with the parent test's t, but the assertions are executed inside t.Run subtests. To ensure failures are reported against the correct subtest, create assert := assert.New(t) inside each t.Run closure (or use assert.Equal(t, ...)).
func TestTruncateSessionID(t *testing.T) {
	assert := assert.New(t)


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 12 to +14
func TestTruncateSecret(t *testing.T) {
assert := assert.New(t)

Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

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

The bound asserter is created with the parent test's t, but assertions are executed inside t.Run subtests. This can misattribute failures to the parent test and breaks subtest-scoped reporting. Create the asserter inside each t.Run (using the subtest t) or switch back to the unbound assert.Equal(t, ...) form for table-driven subtests.

This issue also appears in the following locations of the same file:

  • line 75
  • line 181
  • line 248
  • line 296
  • line 369

Copilot uses AI. Check for mistakes.
want: " ",
},
{
name: "Agent format with multiple spaces (trimmed)",
Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

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

Test case name says "(trimmed)", but ExtractSessionID does not trim whitespace for the Agent format—it only removes the prefix. Either update the test name to match the actual behavior or (if trimming is desired) adjust the production code and expected value accordingly.

Suggested change
name: "Agent format with multiple spaces (trimmed)",
name: "Agent format with multiple spaces (prefix only removed)",

Copilot uses AI. Check for mistakes.
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