Some checks failed
Deploy / deploy (push) Has been cancelled
Full-stack web application for Telegram management - Frontend: Vue 3 + Vben Admin - Backend: NestJS - Features: User management, group broadcast, statistics 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
266 lines
5.7 KiB
Markdown
266 lines
5.7 KiB
Markdown
# Telegram Marketing Agent System - Test Suite
|
|
|
|
Comprehensive testing framework for the Telegram Marketing Agent System.
|
|
|
|
## Overview
|
|
|
|
This test suite provides integration and unit tests for all microservices in the marketing agent system.
|
|
|
|
## Structure
|
|
|
|
```
|
|
test/
|
|
├── integration/ # Integration tests
|
|
│ ├── setup.js # Test environment setup
|
|
│ ├── helpers.js # Test utilities and helpers
|
|
│ ├── api-gateway.test.js
|
|
│ ├── orchestration.test.js
|
|
│ └── compliance.test.js
|
|
├── unit/ # Unit tests (to be added)
|
|
├── jest.config.js # Jest configuration
|
|
├── setup.test.js # Global test setup
|
|
└── package.json # Test dependencies
|
|
```
|
|
|
|
## Getting Started
|
|
|
|
### Installation
|
|
|
|
```bash
|
|
cd test
|
|
npm install
|
|
```
|
|
|
|
### Environment Setup
|
|
|
|
Create a `.env.test` file:
|
|
|
|
```env
|
|
# Test Database Configuration
|
|
TEST_POSTGRES_HOST=localhost
|
|
TEST_POSTGRES_PORT=5432
|
|
TEST_POSTGRES_USER=test_user
|
|
TEST_POSTGRES_PASSWORD=test_pass
|
|
TEST_POSTGRES_DB=marketing_test
|
|
|
|
# Test RabbitMQ (optional)
|
|
TEST_RABBITMQ_URL=amqp://localhost:5672
|
|
|
|
# API Keys (use test keys)
|
|
ANTHROPIC_API_KEY=test-anthropic-key
|
|
OPENAI_API_KEY=test-openai-key
|
|
```
|
|
|
|
### Running Tests
|
|
|
|
```bash
|
|
# Run all tests
|
|
npm test
|
|
|
|
# Run integration tests only
|
|
npm run test:integration
|
|
|
|
# Run with coverage
|
|
npm run test:coverage
|
|
|
|
# Watch mode for development
|
|
npm run test:watch
|
|
|
|
# Debug tests
|
|
npm run test:debug
|
|
```
|
|
|
|
## Test Categories
|
|
|
|
### Integration Tests
|
|
|
|
#### API Gateway Tests
|
|
- Authentication (JWT, API Key)
|
|
- Rate limiting
|
|
- Service routing
|
|
- CORS handling
|
|
- Error handling
|
|
- Health checks
|
|
|
|
#### Orchestration Tests
|
|
- Campaign creation and execution
|
|
- Task management
|
|
- Claude AI integration
|
|
- Message queue processing
|
|
- End-to-end workflows
|
|
|
|
#### Compliance Tests
|
|
- Data encryption/decryption
|
|
- Privacy rights (export, deletion)
|
|
- Consent management
|
|
- Audit logging
|
|
- Regulatory compliance (GDPR, CCPA)
|
|
|
|
### Writing Tests
|
|
|
|
#### Integration Test Example
|
|
|
|
```javascript
|
|
const TestEnvironment = require('./setup');
|
|
const TestHelpers = require('./helpers');
|
|
|
|
describe('My Service Integration', () => {
|
|
let testEnv;
|
|
let helpers;
|
|
let apiClient;
|
|
|
|
beforeAll(async () => {
|
|
testEnv = new TestEnvironment();
|
|
helpers = new TestHelpers(testEnv);
|
|
|
|
await testEnv.setup();
|
|
helpers.setupMocks();
|
|
|
|
// Start services
|
|
await testEnv.startService('my-service', myApp, 3000);
|
|
|
|
// Create test user
|
|
const user = await testEnv.createTestUser();
|
|
apiClient = await helpers.createAuthenticatedClient('http://localhost:3000', user);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
helpers.cleanupMocks();
|
|
await testEnv.cleanup();
|
|
});
|
|
|
|
test('should do something', async () => {
|
|
const response = await apiClient.get('/api/endpoint');
|
|
const data = helpers.expectApiSuccess(response);
|
|
|
|
expect(data).toBeDefined();
|
|
});
|
|
});
|
|
```
|
|
|
|
#### Test Helpers
|
|
|
|
The test framework provides numerous helpers:
|
|
|
|
```javascript
|
|
// Create test data
|
|
const campaign = await helpers.createTestCampaign();
|
|
const task = await helpers.createTestTask();
|
|
|
|
// Generate test data
|
|
const messages = helpers.generateTestData('messages', 10);
|
|
|
|
// Wait for conditions
|
|
await helpers.waitForCondition(
|
|
async () => {
|
|
const task = await getTask(taskId);
|
|
return task.status === 'completed';
|
|
},
|
|
5000 // timeout
|
|
);
|
|
|
|
// Verify API responses
|
|
helpers.expectApiSuccess(response, 200);
|
|
helpers.expectApiError(response, 400, 'Validation error');
|
|
|
|
// Verify database state
|
|
const state = await helpers.verifyDatabaseState({
|
|
mongodb: {
|
|
Campaign: { status: 'active' }
|
|
},
|
|
postgres: {
|
|
users: 'email LIKE \'%@test.com\''
|
|
}
|
|
});
|
|
```
|
|
|
|
## Best Practices
|
|
|
|
1. **Test Isolation**: Each test should be independent
|
|
2. **Mock External Services**: Use nock for HTTP mocks
|
|
3. **Use Test Databases**: MongoDB Memory Server for MongoDB
|
|
4. **Clean Up**: Always clean up test data
|
|
5. **Meaningful Assertions**: Test behavior, not implementation
|
|
|
|
## Continuous Integration
|
|
|
|
The test suite is designed to run in CI environments:
|
|
|
|
```yaml
|
|
# Example GitHub Actions configuration
|
|
name: Tests
|
|
on: [push, pull_request]
|
|
|
|
jobs:
|
|
test:
|
|
runs-on: ubuntu-latest
|
|
|
|
services:
|
|
postgres:
|
|
image: postgres:14
|
|
env:
|
|
POSTGRES_PASSWORD: test_pass
|
|
options: >-
|
|
--health-cmd pg_isready
|
|
--health-interval 10s
|
|
--health-timeout 5s
|
|
--health-retries 5
|
|
|
|
steps:
|
|
- uses: actions/checkout@v3
|
|
- uses: actions/setup-node@v3
|
|
with:
|
|
node-version: '18'
|
|
|
|
- name: Install dependencies
|
|
run: |
|
|
cd marketing-agent
|
|
npm run install:all
|
|
cd test
|
|
npm install
|
|
|
|
- name: Run tests
|
|
run: |
|
|
cd marketing-agent/test
|
|
npm run test:ci
|
|
env:
|
|
TEST_POSTGRES_HOST: localhost
|
|
TEST_POSTGRES_PASSWORD: test_pass
|
|
```
|
|
|
|
## Troubleshooting
|
|
|
|
### Common Issues
|
|
|
|
1. **Port conflicts**: Ensure test ports (13000-13010) are available
|
|
2. **Database connection**: Check PostgreSQL is running for integration tests
|
|
3. **Memory issues**: Increase Node.js memory: `NODE_OPTIONS=--max-old-space-size=4096`
|
|
4. **Timeout errors**: Increase test timeout in jest.config.js
|
|
|
|
### Debug Mode
|
|
|
|
```bash
|
|
# Run specific test in debug mode
|
|
node --inspect-brk node_modules/.bin/jest --runInBand api-gateway.test.js
|
|
|
|
# Then attach debugger in Chrome: chrome://inspect
|
|
```
|
|
|
|
## Coverage Reports
|
|
|
|
After running tests with coverage:
|
|
|
|
```bash
|
|
npm run test:coverage
|
|
|
|
# View HTML report
|
|
open coverage/lcov-report/index.html
|
|
```
|
|
|
|
## Contributing
|
|
|
|
1. Write tests for new features
|
|
2. Ensure all tests pass before PR
|
|
3. Maintain test coverage above 70%
|
|
4. Follow existing test patterns
|
|
5. Add integration tests for API changes |