# Story 7.1: Comprehensive Testing Suite

## Metadata
- **Story ID**: 7.1
- **Title**: Comprehensive Testing Suite
- **Epic**: 7 - Testing, Documentation & CI/CD
- **Status**: Pending
- **Priority**: High
- **Estimated Time**: 10-12 hours
- **Dependencies**: All previous epics

## Goal
Achieve comprehensive test coverage with unit tests, integration tests, and contract tests.

## Description
This story implements a complete testing suite with >80% code coverage, integration tests using testcontainers, and contract tests for API validation.

## Deliverables

### 1. Unit Tests
- Achieve >80% code coverage for core modules:
  - Config loader
  - Logger
  - Auth service
  - Permission resolver
  - Module registry
- Use `github.com/stretchr/testify` for assertions
- Use `github.com/golang/mock` or `mockery` for mocks
- Test helpers:
  - `testutil.NewTestDB()` - In-memory SQLite for tests
  - `testutil.NewTestUser()` - Create test user
  - `testutil.NewTestContext()` - Context with user

### 2. Integration Tests
- Install `github.com/testcontainers/testcontainers-go`
- Create integration test suite:
  - Full HTTP request flow
  - Database operations
  - Event bus publishing/consuming
  - Background job execution
- Test scenarios:
  - User registration → login → API access
  - Role assignment → permission check
  - Module loading and initialization
  - Multi-module interaction
- Create `docker-compose.test.yml`:
  - PostgreSQL
  - Redis
  - Kafka (optional)
- Add test tags: `//go:build integration`

### 3. Contract Tests
- Install `github.com/pact-foundation/pact-go` (optional)
- Create API contract tests:
  - Verify API responses match OpenAPI spec
  - Test backward compatibility
- Use OpenAPI validator:
  - Install `github.com/getkin/kin-openapi`
  - Validate request/response against OpenAPI spec
  - Generate OpenAPI spec from code annotations

### 4. Load Testing
- Create `perf/` directory with k6 scripts:
  - `perf/auth-load.js` - Login endpoint load test
  - `perf/api-load.js` - General API load test
- Document performance benchmarks:
  - Request latency (p50, p95, p99)
  - Throughput (requests/second)
  - Resource usage (CPU, memory)

## Acceptance Criteria
- [ ] All tests pass in CI
- [ ] Code coverage >80%
- [ ] Integration tests work with testcontainers
- [ ] Contract tests validate API
- [ ] Load tests are documented

## Files to Create/Modify
- `internal/testutil/` - Test utilities
- `docker-compose.test.yml` - Test containers
- `perf/` - Load test scripts
- All test files across the codebase

