managarten/docs/AUTOMATED_TESTING_SYSTEM.md
Wuesteon 304897261d test: implement comprehensive automated testing system with daily CI/CD
Implement rock-solid automated testing infrastructure for mana-core-auth
with daily execution, notifications, and comprehensive monitoring.

Test Suite Improvements:
- Fix all 36 failing BetterAuthService tests (missing service mocks)
- Add 21 JwtAuthGuard tests achieving 100% statement coverage
- Create silentError helper to suppress intentional error logs
- Fix Todo backend TaskService test structure
- Add jose mock for JWT testing
- Configure jest collectCoverageFrom for mana-core-auth

GitHub Actions Workflow:
- Daily automated test execution (2 AM UTC + manual trigger)
- Matrix parallelization across 6 backend services
- PostgreSQL and Redis service containers
- Coverage enforcement (80% threshold)
- Multi-channel notifications (Discord, Slack, GitHub Issues)
- Support for success notifications (opt-in)

Test Infrastructure:
- Coverage aggregation across multiple services
- Flaky test detection with 30-run history tracking
- Performance metrics tracking with regression detection
- Test data seeding and cleanup scripts
- Comprehensive test reporting with formatted metrics

Documentation:
- TESTING_GUIDE.md (4000+ words) - Complete testing documentation
- AUTOMATED_TESTING_SYSTEM.md - System architecture and workflows
- DISCORD_NOTIFICATIONS_SETUP.md - Discord webhook setup guide
- TESTING_DEPLOYMENT_CHECKLIST.md - Pre-deployment verification
- TESTING_QUICK_REFERENCE.md - Quick command reference

Final Result:
- 180/180 tests passing (100% pass rate)
- Zero console errors in test output
- Automated daily testing with rich notifications
- Production-ready test infrastructure
2025-12-25 19:12:27 +01:00

583 lines
16 KiB
Markdown

# Automated Testing System - Implementation Summary
Complete automated daily test execution system with monitoring and reporting for the ManaCore monorepo.
## Overview
This document provides an overview of the automated testing infrastructure implemented for continuous quality assurance.
**Implementation Date**: 2025-12-25
**Status**: Ready for deployment
## Components Delivered
### 1. GitHub Actions Workflow
**File**: `.github/workflows/daily-tests.yml`
**Features**:
- Scheduled daily execution at 2 AM UTC
- Manual trigger with configurable parameters
- Parallel test execution across multiple test suites
- Automatic database setup/teardown per suite
- Coverage enforcement (80% minimum)
- Test result aggregation and reporting
- Flaky test detection
- Performance metrics tracking
- Failure notifications (GitHub issues, Slack)
**Test Matrix**:
- Backend tests (Jest + PostgreSQL + Redis)
- Mobile tests (Jest + React Native)
- Web tests (Vitest + Svelte)
- Integration tests (E2E flows)
### 2. Test Execution Scripts
**Directory**: `/scripts/`
#### `/scripts/run-tests-with-coverage.sh`
Comprehensive test execution script with coverage reporting.
**Usage**:
```bash
# Run all tests
./scripts/run-tests-with-coverage.sh
# Run specific package
./scripts/run-tests-with-coverage.sh mana-core-auth
./scripts/run-tests-with-coverage.sh chat-backend
```
**Features**:
- Automatic Docker verification
- Database setup per package
- Coverage threshold checking
- Colored terminal output
- Detailed summary report
### 3. Test Reporting Scripts
**Directory**: `/scripts/test-reporting/`
#### `aggregate-coverage.js`
Merges coverage reports from multiple test suites.
**Outputs**:
- `total-coverage.json`: Aggregated coverage data
- `summary.md`: Markdown coverage summary
#### `generate-summary.js`
Creates GitHub Actions summary with test results.
**Features**:
- Coverage breakdown by suite
- Pass/fail statistics
- Recommendations for improvement
#### `detect-flaky-tests.js`
Identifies tests that fail intermittently.
**Configuration**:
- Flaky threshold: 10% failure rate
- Minimum runs: 3
- History retention: 30 runs per test
**Outputs**:
- `flaky-tests.json`: List of flaky tests
- `test-history.json`: Historical test data
#### `track-metrics.js`
Records test performance metrics over time.
**Tracks**:
- Total test execution time
- Average test duration
- Slowest tests
- Suite-level metrics
- Performance regressions (>20% increase)
**Outputs**:
- `metrics.json`: Current metrics
- `metrics-report.md`: Formatted report
- `metrics-history.json`: 90-day history
#### `format-metrics.js`
Formats metrics for GitHub Actions summary display.
### 4. Test Data Management
**Directory**: `/scripts/test-data/`
#### `seed-test-data.sh`
Seeds databases with consistent test data.
**Usage**:
```bash
# Seed all services
./scripts/test-data/seed-test-data.sh
# Seed specific service
./scripts/test-data/seed-test-data.sh auth
./scripts/test-data/seed-test-data.sh chat
```
**Provides**:
- Deterministic test user accounts
- Pre-configured AI models (chat)
- Consistent credit balances
**Test Users**:
| Email | Password | ID | Role |
|-------|----------|-----|------|
| test-user-1@example.com | TestPassword123! | 00000000-0000-0000-0000-000000000001 | user |
| test-user-2@example.com | TestPassword123! | 00000000-0000-0000-0000-000000000002 | user |
| admin@example.com | AdminPassword123! | 00000000-0000-0000-0000-000000000003 | admin |
#### `cleanup-test-data.sh`
Removes test data and resets databases.
**Usage**:
```bash
# Clean all databases
./scripts/test-data/cleanup-test-data.sh
# Clean specific database
./scripts/test-data/cleanup-test-data.sh auth
```
### 5. Documentation
#### `docs/TESTING_GUIDE.md`
Comprehensive testing documentation (4000+ words).
**Contents**:
- Test types and strategies
- Local testing instructions
- Automated daily tests overview
- Writing tests best practices
- Test data management
- Coverage requirements
- Troubleshooting guide
- CI/CD integration
#### `docs/TESTING_QUICK_REFERENCE.md`
Quick reference for common testing tasks.
**Contents**:
- Quick commands
- Test patterns and templates
- Coverage viewing
- Test data reference
- Troubleshooting shortcuts
- Best practices summary
#### `scripts/test-reporting/README.md`
Documentation for test reporting scripts.
**Contents**:
- Script overview and usage
- Data format specifications
- Development guide
- Integration examples
- Troubleshooting
### 6. Package.json Updates
**File**: `/package.json`
Added convenience scripts:
```json
{
"test:cov": "./scripts/run-tests-with-coverage.sh",
"test:seed": "./scripts/test-data/seed-test-data.sh",
"test:cleanup": "./scripts/test-data/cleanup-test-data.sh"
}
```
## Architecture
### Workflow Execution Flow
```
┌─────────────────────────────────────────┐
│ Daily Tests Workflow (2 AM UTC) │
└─────────────────────────────────────────┘
┌─────────────────────────────────────────┐
│ 1. Setup Job │
│ - Detect test suites │
│ - Generate test matrices │
└─────────────────────────────────────────┘
┌─────────────┴─────────────┬──────────────┐
▼ ▼ ▼
┌──────────┐ ┌──────────────┐ ┌──────────┐
│ Backend │ │ Mobile │ │ Web │
│ Tests │ │ Tests │ │ Tests │
│(Parallel)│ │ (Parallel) │ │(Parallel)│
└──────────┘ └──────────────┘ └──────────┘
│ │ │
└─────────────┬─────────────┴──────────────┘
┌─────────────────────────────────────────┐
│ Integration Tests │
│ - Full E2E flows │
│ - Auth + Database │
└─────────────────────────────────────────┘
┌─────────────┴─────────────┬──────────────┐
▼ ▼ ▼
┌──────────┐ ┌──────────────┐ ┌──────────┐
│ Report │ │ Detect Flaky │ │ Metrics │
│ Job │ │ Tests │ │ Tracking │
└──────────┘ └──────────────┘ └──────────┘
│ │ │
└─────────────┬─────────────┴──────────────┘
┌─────────────────────────────────────────┐
│ Notify Job (on failure) │
│ - GitHub issue │
│ - Slack notification │
└─────────────────────────────────────────┘
```
### Test Data Flow
```
┌──────────────┐
│ Test Suite │
└──────────────┘
┌──────────────────────┐
│ Setup Database │
│ - Run migrations │
│ - Seed test data │
└──────────────────────┘
┌──────────────────────┐
│ Execute Tests │
│ - Unit tests │
│ - Integration tests │
└──────────────────────┘
┌──────────────────────┐
│ Generate Coverage │
│ - coverage-summary │
│ - HTML report │
└──────────────────────┘
┌──────────────────────┐
│ Cleanup │
│ - Remove test data │
│ - Close connections │
└──────────────────────┘
```
## Usage
### Running Tests Locally
```bash
# Quick commands
pnpm test # Run all tests
pnpm test:cov # Run with coverage
pnpm test:seed # Seed test data
pnpm test:cleanup # Clean test data
# Within a package
cd services/mana-core-auth
pnpm test # Run tests
pnpm test:cov # With coverage
pnpm test:watch # Watch mode
```
### Triggering Daily Tests Manually
1. Navigate to GitHub Actions
2. Select "Daily Tests" workflow
3. Click "Run workflow"
4. (Optional) Configure parameters:
- Coverage threshold (default: 80%)
- Verbose output (default: false)
5. Click "Run workflow" button
### Viewing Test Results
**Coverage Reports**:
- Download from GitHub Actions artifacts
- Retention: 30 days
- Format: HTML + JSON
**Aggregated Coverage**:
- Download "aggregated-coverage-report" artifact
- Retention: 90 days
- Includes: `total-coverage.json`, `summary.md`
**Test Metrics**:
- Download "test-metrics" artifact
- Retention: 365 days
- Includes: `metrics.json`, `metrics-history.json`
**Flaky Test Reports**:
- Download "flaky-test-report" artifact
- Retention: 90 days
- Format: JSON with failure rates
## Configuration
### Coverage Thresholds
**Global** (all packages):
- Lines: 80%
- Statements: 80%
- Functions: 80%
- Branches: 80%
**Critical Paths** (100% required):
- `services/mana-core-auth/src/auth/auth.service.ts`
- `services/mana-core-auth/src/credits/credits.service.ts`
- `services/mana-core-auth/src/common/guards/jwt-auth.guard.ts`
### Flaky Test Detection
- **Threshold**: 10% failure rate
- **Minimum Runs**: 3 runs required
- **History**: Last 30 runs per test
- **Action**: GitHub issue created automatically
### Performance Metrics
- **Regression Threshold**: 20% duration increase
- **Suite Threshold**: 30% duration increase
- **History**: 90 days retained
- **Action**: Workflow fails on regression
## Monitoring and Alerts
### Automated Notifications
**GitHub Issues**:
- Created on test failure
- Created on flaky test detection
- Labels: `testing`, `failure`, `flaky-test`, `automated`
**Slack** (if configured):
- Daily test failure notifications
- Sent to configured webhook
- Includes workflow run link
### Metrics Dashboard
Track trends via artifacts:
1. **Coverage Trends**:
- Download aggregated coverage from multiple runs
- Compare `total-coverage.json` over time
2. **Flaky Tests**:
- Review `flaky-tests.json` artifact
- Track failure rates
3. **Performance**:
- Check `metrics-history.json`
- Monitor execution time trends
## Best Practices
### Writing Tests
**DO**:
- Write tests for all new features
- Use descriptive test names
- Keep tests isolated
- Mock external services
- Maintain 80%+ coverage
**DON'T**:
- Skip tests for "simple" code
- Create order-dependent tests
- Make real API calls
- Hardcode IDs or timestamps
- Commit failing tests
### Test Data
**DO**:
- Use deterministic test data
- Clean up after tests
- Use test factories
- Seed consistent data
**DON'T**:
- Share state between tests
- Use production data
- Leave test data behind
- Use random values without seeds
### Coverage
**DO**:
- Aim for high coverage (80%+)
- Test critical paths thoroughly
- Review coverage reports
- Fix coverage drops quickly
**DON'T**:
- Ignore coverage warnings
- Write tests just for coverage
- Skip edge cases
- Rely solely on coverage metrics
## Troubleshooting
### Common Issues
**Tests fail with database connection error**:
```bash
# Solution: Start Docker
pnpm docker:up
```
**Coverage below threshold**:
```bash
# Solution: View uncovered code
cd services/mana-core-auth
pnpm test:cov
open coverage/lcov-report/index.html
```
**Flaky tests detected**:
```bash
# Solution: Review test isolation
# - Check for timing issues
# - Verify proper async/await
# - Ensure cleanup in afterEach
```
**Performance regression**:
```bash
# Solution: Profile slow tests
# - Check test-results/metrics.json
# - Identify slowest tests
# - Optimize or split large tests
```
## Maintenance
### Regular Tasks
**Weekly**:
- Review flaky test reports
- Address failing tests
- Check coverage trends
**Monthly**:
- Review performance metrics
- Update test data as needed
- Clean up old artifacts
**Quarterly**:
- Audit test coverage
- Update testing documentation
- Review and improve test quality
### Updating Scripts
When modifying reporting scripts:
1. Test locally with mock data
2. Update script README
3. Test in workflow with manual trigger
4. Monitor first automated run
5. Update documentation if needed
## Future Enhancements
### Planned Improvements
1. **E2E Tests with Playwright**:
- Browser-based testing
- Visual regression testing
- Cross-browser validation
2. **Test Parallelization**:
- Optimize parallel execution
- Reduce total workflow time
- Smart test splitting
3. **Coverage Visualization**:
- Interactive coverage dashboard
- Historical trend charts
- Per-developer coverage stats
4. **Advanced Flaky Detection**:
- ML-based prediction
- Auto-retry flaky tests
- Root cause analysis
5. **Performance Baselines**:
- Establish performance budgets
- Block slow test commits
- Automated optimization suggestions
## Support
### Documentation
- **Comprehensive Guide**: `/docs/TESTING_GUIDE.md`
- **Quick Reference**: `/docs/TESTING_QUICK_REFERENCE.md`
- **Script Docs**: `/scripts/test-reporting/README.md`
### Getting Help
- **GitHub Issues**: Label with `testing`
- **Team Chat**: #testing channel
- **Documentation**: Check docs first
## Metrics and Success Criteria
### Key Performance Indicators
| Metric | Target | Current |
|--------|--------|---------|
| Overall Coverage | 80%+ | TBD (after first run) |
| Daily Test Success Rate | 95%+ | TBD |
| Flaky Test Count | <5 | TBD |
| Average Test Duration | <60s per suite | TBD |
| Mean Time to Fix | <24 hours | TBD |
### Success Criteria
**Workflow runs successfully daily**
**All test suites execute in parallel**
**Coverage reports generated and aggregated**
**Flaky tests identified and tracked**
**Performance metrics recorded**
**Failures trigger notifications**
**Documentation complete and accessible**
## Conclusion
The automated testing system provides comprehensive quality assurance for the ManaCore monorepo with:
- **Automated Execution**: Daily scheduled runs at 2 AM UTC
- **Parallel Testing**: Fast execution across multiple suites
- **Coverage Enforcement**: 80% minimum threshold
- **Flaky Detection**: Identify unreliable tests
- **Performance Tracking**: Monitor test execution trends
- **Failure Notifications**: Immediate alerts on issues
- **Comprehensive Documentation**: Complete guides and references
The system is ready for deployment and will ensure continuous quality as the monorepo grows.
---
**Implementation**: Hive Mind Swarm (Tester Agent)
**Date**: 2025-12-25
**Status**: Complete