[test] Add tests for auth.GenerateRandomAgentID by github-actions[bot] · Pull Request #7691 · github/gh-aw-mcpg · GitHub
Skip to content

[test] Add tests for auth.GenerateRandomAgentID#7691

Merged
lpcox merged 1 commit into
mainfrom
test/add-generate-random-agent-id-error-coverage-b89c47b9bf2697af
Jun 17, 2026
Merged

[test] Add tests for auth.GenerateRandomAgentID#7691
lpcox merged 1 commit into
mainfrom
test/add-generate-random-agent-id-error-coverage-b89c47b9bf2697af

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

Function Analyzed

  • Package: internal/auth
  • Function: GenerateRandomAgentID
  • Previous Coverage: 71.4%
  • New Coverage: 100.0%
  • Complexity: Low–Medium (error-wrapping path previously unreachable without mocking crypto/rand.Reader)

Why This Function?

GenerateRandomAgentID (defined in internal/auth/header.go) had the lowest non-trivial, non-dead-code coverage in the codebase at 71.4%. The uncovered lines (211–212) are the error branch where strutil.RandomHex(32) fails and the function must log and return a wrapped error. All existing tests lived in the external apikey_test.go file and only exercised the happy path, leaving the error-handling path completely dark.

Tests Added

Two new tests in internal/auth/header_test.go (package auth, internal access):

  • TestGenerateRandomAgentID_RandomFailure — Replaces crypto/rand.Reader with a stub errorReader that always returns a synthetic error. Verifies:
    • Returned key is empty
    • A non-nil error is returned
    • errors.Is unwraps to the underlying source error (proper %w wrapping)
    • Error message contains "failed to generate random agent ID"
  • TestGenerateRandomAgentID_RecoveryAfterFailure — Confirms that after restoring the original rand.Reader, a subsequent call to GenerateRandomAgentID succeeds and returns the expected 64-char hex key, ruling out any state leakage.

Both tests intentionally omit t.Parallel() because they mutate the package-level crypto/rand.Reader.

Coverage Report

Before: GenerateRandomAgentID  71.4%   (internal/auth total: 94.7%)
After:  GenerateRandomAgentID 100.0%   (internal/auth total: 98.2%)
Improvement: +28.6% on target function, +3.5% on package

Test Execution

=== RUN   TestGenerateRandomAgentID_RandomFailure
--- PASS: TestGenerateRandomAgentID_RandomFailure (0.00s)
=== RUN   TestGenerateRandomAgentID_RecoveryAfterFailure
--- PASS: TestGenerateRandomAgentID_RecoveryAfterFailure (0.00s)
ok      github.com/github/gh-aw-mcpg/internal/auth      coverage: 98.2% of statements

Generated by Test Coverage Improver
Next run will target the next most complex under-tested function

Warning

Firewall blocked 8 domains

The following domains were blocked by the firewall during workflow execution:

  • go.opentelemetry.io
  • go.yaml.in
  • golang.org
  • google.golang.org
  • gopkg.in
  • proxy.golang.org
  • releaseassets.githubusercontent.com
  • sum.golang.org

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "go.opentelemetry.io"
    - "go.yaml.in"
    - "golang.org"
    - "google.golang.org"
    - "gopkg.in"
    - "proxy.golang.org"
    - "releaseassets.githubusercontent.com"
    - "sum.golang.org"

See Network Configuration for more information.

Generated by Test Coverage Improver · 3.8K AIC · ⊞ 28.8K ·

Cover the previously untested error branch (lines 211–212 of header.go)
where strutil.RandomHex fails and GenerateRandomAgentID must return a
wrapped error. Two tests are added to internal/auth/header_test.go:

- TestGenerateRandomAgentID_RandomFailure: replaces crypto/rand.Reader
  with a stub that always errors, then verifies the returned error is
  non-nil, wraps the underlying error (errors.Is), and contains the
  expected context string.

- TestGenerateRandomAgentID_RecoveryAfterFailure: confirms that
  GenerateRandomAgentID behaves correctly again after the original
  rand.Reader is restored, ensuring no state is leaked between calls.

Coverage for GenerateRandomAgentID: 71.4% → 100.0%
Overall internal/auth coverage: 94.7% → 98.2%

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review June 17, 2026 19:04
Copilot AI review requested due to automatic review settings June 17, 2026 19:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Not ready to approve

One of the new tests mutates crypto/rand.Reader without guaranteed cleanup on early assertion failure, which can leak global state and destabilize other tests.

Pull request overview

Adds targeted unit tests in internal/auth to cover the previously-unreached error path in GenerateRandomAgentID by stubbing crypto/rand.Reader, improving function-level coverage to 100%.

Changes:

  • Add a test helper errorReader to force failures from the random source.
  • Add failure-path and recovery tests for GenerateRandomAgentID, verifying %w wrapping and successful behavior after restoring rand.Reader.
File summaries
File Description
internal/auth/header_test.go Adds two tests and a helper reader to exercise GenerateRandomAgentID error handling and recovery behavior.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

Note

Your feedback helps us improve the quality of this feature.
Please use 👍 or 👎 to tell us whether this assessment is correct.


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

Comment on lines +634 to +645
@lpcox lpcox merged commit 6170280 into main Jun 17, 2026
24 checks passed
@lpcox lpcox deleted the test/add-generate-random-agent-id-error-coverage-b89c47b9bf2697af branch June 17, 2026 23:38
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