# Testing Guide This directory contains all tests for the Telegram Marketing Agent System. ## Test Structure ``` tests/ ├── unit/ # Unit tests for individual components │ ├── services/ # Service-specific unit tests │ ├── utils/ # Utility function tests │ └── middleware/ # Middleware tests ├── integration/ # Integration tests │ ├── api/ # API endpoint tests │ ├── services/ # Service integration tests │ └── workflows/ # End-to-end workflow tests ├── e2e/ # End-to-end tests │ ├── campaigns/ # Campaign workflow tests │ └── users/ # User management tests ├── fixtures/ # Test data and mocks ├── helpers/ # Test utilities └── config/ # Test configuration ``` ## Running Tests ### All Tests ```bash npm test ``` ### Unit Tests Only ```bash npm run test:unit ``` ### Integration Tests Only ```bash npm run test:integration ``` ### End-to-End Tests ```bash npm run test:e2e ``` ### Watch Mode ```bash npm run test:watch ``` ### Coverage Report ```bash npm run test:coverage ``` ## Test Framework We use the following testing stack: - **Jest**: Main testing framework - **Supertest**: API endpoint testing - **MongoDB Memory Server**: In-memory MongoDB for tests - **Redis Mock**: Redis mocking for tests - **Sinon**: Mocking and stubbing - **Faker**: Test data generation ## Writing Tests ### Unit Test Example ```javascript describe('CampaignService', () => { describe('createCampaign', () => { it('should create a new campaign', async () => { const campaignData = { name: 'Test Campaign', type: 'message' }; const result = await campaignService.createCampaign(campaignData); expect(result).toHaveProperty('id'); expect(result.name).toBe(campaignData.name); }); }); }); ``` ### Integration Test Example ```javascript describe('POST /api/v1/campaigns', () => { it('should create a campaign', async () => { const response = await request(app) .post('/api/v1/campaigns') .set('Authorization', `Bearer ${token}`) .send({ name: 'Test Campaign', type: 'message' }); expect(response.status).toBe(201); expect(response.body.success).toBe(true); }); }); ``` ## Test Coverage Goals - **Unit Tests**: >80% coverage - **Integration Tests**: All critical paths - **E2E Tests**: Main user workflows ## CI/CD Integration Tests are automatically run on: - Pull requests - Commits to main branch - Before deployment ## Best Practices 1. **Isolation**: Each test should be independent 2. **Cleanup**: Always clean up test data 3. **Mocking**: Mock external dependencies 4. **Descriptive**: Use clear test descriptions 5. **Fast**: Keep tests fast and focused 6. **Deterministic**: Tests should always produce same results