Phase 5: Add comprehensive test utilities and mock servers
Completed Phase 5 of the refactoring plan, creating a robust testing
infrastructure to reduce code duplication and improve test quality.
New Package: internal/testutil/
- Created dedicated package for shared test utilities
- Follows Go best practices for test helpers
fixtures.go - Test Data Generators:
- NewTestDPoPKey() - Generate test ECDSA keys (eliminates 27+ duplicates)
- NewTestSession() - Create sessions with sensible defaults
- TestSessionOption pattern for customization (WithDID, WithPDS, etc.)
- WithExpiredAccessToken/RefreshToken for expiration testing
- NewTestAuthServerMetadata() - OAuth metadata fixtures
- NewTestClientConfig() - Client configuration fixtures
- RandomString() - URL-safe random string generator
mock_server.go - Mock Servers:
- MockOAuthServer - Full OAuth authorization server
* Metadata endpoint (/.well-known/oauth-authorization-server)
* JWKS endpoint with RSA key generation
* Authorization endpoint with code generation
* Token endpoint (authorization_code and refresh_token grants)
* PAR endpoint for pushed authorization requests
- MockPDSServer - AT Protocol PDS server
* com.atproto.repo.createRecord with DPoP validation
* com.atproto.repo.deleteRecord with DPoP validation
* com.atproto.server.describeServer
- MockHandleServer - Handle resolution server
* com.atproto.identity.resolveHandle
* AddHandle() for custom handle mappings
helpers.go - Assertion and Utilities:
- Assertion helpers: AssertNoError, AssertEqual, AssertNotNil, etc.
- AssertContains/AssertNotContains for string matching
- AssertPanics for panic testing
- NewTestContext() with automatic cleanup
- MockHTTPClient for testing HTTP interactions
- MockRoundTripper for http.RoundTripper testing
- WaitForCondition() for async testing
Testing:
- All testutil functions have their own tests
- 13 passing tests demonstrating usage
- All package tests pass with race detection
- 100% backward compatibility maintained
Benefits:
- Reduces test code duplication (27+ key generations โ 1 function)
- Faster test writing with reusable fixtures
- More consistent test data across the codebase
- Better test isolation with proper mock servers
- Easier maintenance - update fixtures once, affects all tests
- Foundation for incrementally improving existing tests
Next Steps:
- Existing tests can be incrementally updated to use testutil
- No breaking changes required
- Immediate value for new tests being written
๐ค Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>