8.1 KiB
8.1 KiB
GoChat Test Coverage Report
Generated: Initial template Target: 80%+ coverage across all modules Reference: Chatwoot RSpec coverage patterns mapped to Go testing conventions
Coverage Targets Per Module
| Module | Target Coverage | Priority | Status |
|---|---|---|---|
internal/model |
90% | High | Pending |
internal/repository |
85% | High | Pending |
internal/service |
80% | High | Pending |
internal/auth |
85% | High | Pending |
internal/handler |
75% | Medium | Pending |
internal/middleware |
80% | Medium | Pending |
internal/channel |
75% | Medium | Pending |
internal/channel/provider |
70% | Medium | Pending |
internal/config |
80% | Low | Pending |
internal/app |
60% | Low | Pending |
internal/router |
70% | Low | Pending |
internal/ws |
75% | Medium | Pending |
internal/pubsub |
75% | Medium | Pending |
pkg/crypto |
90% | High | Pending |
pkg/logger |
80% | Medium | Pending |
Module Coverage Details
internal/model (Target: 90%)
Model tests focus on:
- CRUD operations via GORM (Create, Read, Update, Delete)
- Association integrity (foreign keys, belongs-to, has-many)
- Validation rules (required fields, unique constraints, format checks)
- Soft delete behavior (DeletedAt field, recovery)
- Custom types (JSONB permissions, enum fields)
- Edge cases (null fields, empty strings, boundary values)
Key test files:
internal/model/base_test.go— Base model fields, timestamps, soft deleteinternal/model/user_test.go— User CRUD, role validation, email uniquenessinternal/model/account_test.go— Account CRUD, locale/timezone defaultsinternal/model/conversation_test.go— Conversation status transitions, assigneeinternal/model/message_test.go— Message types, content validation, privacyinternal/model/custom_role_test.go— Permission matrix parsing, role CRUDinternal/model/contact_test.go— Contact identification, channel binding
internal/repository (Target: 85%)
Repository tests focus on:
- BaseRepository generic CRUD operations
- Specialized repository query methods (FindByAccount, FindByStatus, etc.)
- Pagination (offset/limit)
- Error handling (record not found, duplicate key)
- Soft-delete scoped queries
- Transaction support
Key test files:
internal/repository/repository_test.go— BaseRepository[T] generic CRUDinternal/repository/user_repo_test.go— FindByEmail, FindByAccountinternal/repository/conversation_repo_test.go— FindByAccount, FindByStatus, FindByAssigneeinternal/repository/message_repo_test.go— FindByConversation, Searchinternal/repository/account_repo_test.go— Account CRUD operations
internal/service (Target: 80%)
Service tests focus on:
- Business logic correctness
- Input validation
- Error handling and propagation
- Cross-service coordination
- Authorization checks (RBAC integration)
- AI/Copilot features (Captain, LLM Provider)
Key test files:
internal/service/auth_service_test.go— Login, Register, Refresh, Logout flowsinternal/service/conversation_service_test.go— CRUD, status transitions, assignmentinternal/service/message_service_test.go— Create, Search, privacy controlsinternal/service/rbac_service_test.go— Role assignment, permission checking, custom rolesinternal/service/captain_service_test.go— Captain Assistant CRUD, Document, Scenariointernal/service/copilot_service_test.go— Thread, Message, AI reply suggestionsinternal/service/llm_provider_test.go— LLM Provider interface compliance
internal/auth (Target: 85%)
Auth tests focus on:
- JWT token generation and validation
- Refresh token lifecycle
- Permission system (administrator, agent, custom_role)
- Policy context construction
- MFA (TOTP) verification
- OAuth provider integration
Key test files:
internal/auth/jwt_test.go— Token pair generation, expiry, validationinternal/auth/permission_test.go— Permission sets for each role levelinternal/auth/policy_test.go— PolicyContext authorization checksinternal/auth/mfa_test.go— TOTP generation and verificationinternal/auth/oauth_test.go— OAuth provider flow
internal/handler (Target: 75%)
Handler tests focus on:
- HTTP request/response correctness
- Route parameter binding
- Authentication middleware integration
- Response format (JSON structure, status codes)
- Error response formatting
Key test files:
internal/handler/auth/auth_handler_test.go— Auth endpointsinternal/handler/api_v1/conversation_handler_test.go— Conversation APIinternal/handler/api_v1/message_handler_test.go— Message APIinternal/handler/webhook/webhook_handler_test.go— Webhook processinginternal/handler/ws/ws_handler_test.go— WebSocket connection
internal/middleware (Target: 80%)
Middleware tests focus on:
- Authentication extraction from headers
- Role-based access control enforcement
- CORS configuration
- Rate limiting behavior
- Request logging
internal/channel (Target: 75%)
Channel tests focus on:
- Channel provider interface compliance
- Webhook processing pipeline
- Message broker routing
- Provider-specific configuration validation
- Incoming/outgoing message transformation
pkg/crypto (Target: 90%)
Crypto tests focus on:
- Password hashing (bcrypt)
- JWT token generation and validation
- Token expiry handling
- Invalid token rejection
pkg/logger (Target: 80%)
Logger tests focus on:
- Log level filtering
- Structured logging output
- Context-aware logging
Test Strategy Matrix
| Test Type | Scope | Tool | Count Target |
|---|---|---|---|
| Unit Tests | Single function/method | go test |
200+ |
| Integration Tests | Service + Repository | go test with SQLite |
50+ |
| E2E Tests | Full HTTP flow | httptest + SQLite | 30+ |
| Benchmark Tests | Performance | go test -bench |
20+ |
| Mock Tests | External dependencies | httptest mock server | 15+ |
Coverage Collection Method
# Run all tests with coverage
go test -coverprofile=coverage.out -covermode=atomic ./...
# View per-function coverage
go tool cover -func=coverage.out
# Generate HTML report
go tool cover -html=coverage.out -o coverage.html
# Run benchmarks
go test -bench=. -benchmem ./internal/service/ ./internal/repository/ ./pkg/crypto/
Coverage Quality Gates
- Critical modules (model, repository, auth, crypto): Must achieve 85%+ coverage
- Business logic (service, middleware): Must achieve 80%+ coverage
- HTTP layer (handler, channel): Must achieve 75%+ coverage
- Infrastructure (app, config, router): Must achieve 60%+ coverage
- Overall project: Must achieve 80%+ average coverage
Chatwoot Test Pattern Mapping
| Chatwoot Pattern | Go Equivalent | Coverage Focus |
|---|---|---|
spec/models/ (RSpec model specs) |
internal/model/*_test.go |
Model CRUD + validations |
spec/services/ (service_object specs) |
internal/service/*_test.go |
Business logic |
spec/controllers/ (controller specs) |
internal/handler/*_test.go |
HTTP API responses |
spec/policies/ (Pundit policy specs) |
internal/auth/*_test.go |
RBAC permission checks |
spec/integration/ (integration specs) |
tests/e2e/*_test.go |
Full flow scenarios |
spec/jobs/ (Sidekiq job specs) |
internal/worker/*_test.go |
Background task processing |
| FactoryBot fixtures | SQLite in-memory GORM seeds | Test data setup |
| Shoulda Matchers | testify/assert + custom validators | Assertion helpers |
Running the Report
# Quick unit test coverage
./scripts/coverage/generate_report.sh --quick
# Full coverage with HTML report
./scripts/coverage/generate_report.sh --html
# Include E2E tests
./scripts/coverage/generate_report.sh --e2e --html
# Include benchmarks
./scripts/coverage/generate_report.sh --bench --html
Continuous Improvement
Coverage targets should be reviewed quarterly. As new features are added:
- Each new module must have tests before merge
- Coverage must not decrease on existing modules
- Critical paths (auth, payments, data integrity) require 90%+ coverage
- Use
go test -raceto catch concurrent access issues