refactor: inject client factories into command handlers - #59
Conversation
Add ClientFactory variables to mail, calendar, and contacts command
packages to enable dependency injection for testing. The factory
functions return the corresponding interface types, allowing tests
to inject mock implementations.
Changes:
- mail: ClientFactory returns gmail.GmailClientInterface
- calendar: ClientFactory returns calendar.CalendarClientInterface
- contacts: ClientFactory returns contacts.ContactsClientInterface
- Update downloadAttachment helper to accept interface type
Tests can now override ClientFactory to inject mocks:
originalFactory := mail.ClientFactory
defer func() { mail.ClientFactory = originalFactory }()
mail.ClientFactory = func() (gmail.GmailClientInterface, error) {
return mockClient, nil
}
Closes #36
Test Coverage AssessmentSummaryThis PR introduces ClientFactory variables in the mail, calendar, and contacts command packages to enable dependency injection for testing. The changes are well-structured and follow Go best practices for testability. What the PR Changes
Test Coverage AnalysisCurrent State: Assessment: This is acceptable for an infrastructure/refactoring PR. Here's why:
What Would Be Nice (But Not Required)A minimal test demonstrating the pattern would prove the mechanism works: func TestClientFactoryCanBeOverridden(t *testing.T) {
original := ClientFactory
defer func() { ClientFactory = original }()
called := false
ClientFactory = func() (gmail.GmailClientInterface, error) {
called = true
return nil, fmt.Errorf("mock error")
}
_, err := newGmailClient()
assert.True(t, called)
assert.Error(t, err)
}However, this is a "nice to have" rather than a blocker. The pattern is straightforward and follows established Go conventions. VerdictApprove - The test coverage is appropriate for an infrastructure refactoring PR. The real test coverage benefits will come when future PRs add mock-based tests using these factories. |
Summary
Adds
ClientFactoryvariables to mail, calendar, and contacts command packages to enable dependency injection for testing. The factory functions return the corresponding interface types (from PR #58), allowing tests to inject mock implementations.Changes
ClientFactoryreturnsgmail.GmailClientInterfaceClientFactoryreturnscalendar.CalendarClientInterfaceClientFactoryreturnscontacts.ContactsClientInterfacedownloadAttachmenthelper to accept interface typeTesting Example
Test plan
make test)make lint)go build ./...)Closes #36