# StreamA2026 Test Runner

Comprehensive test system for the StreamA2026 Docker bundle that automatically checks all critical components and fixes errors when possible.

## Features

- **Docker Checks**: Verifies Docker installation, container status, network, and volumes
- **Database Checks**: Validates database schema against SQLAlchemy models
- **API Tests**: Tests all API endpoints with automatic test user creation
- **Health Checks**: Validates system health endpoints
- **Frontend Checks**: Verifies frontend accessibility
- **Auto-Recovery**: Automatically fixes common issues (container restart, schema updates, etc.)
- **Detailed Output**: Colored terminal output with progress tracking

## Installation

1. Install test dependencies:
```bash
pip install -r tests/requirements.txt
```

2. Ensure Docker and Docker Compose are installed and running

3. Ensure `.env` file exists in project root (copy from `.env.example` if needed)

## Usage

### Run all tests with auto-fix (default):
```bash
python tests/test_runner.py
```

### Run tests without auto-fix:
```bash
python tests/test_runner.py --no-fix
```

### Skip frontend checks:
```bash
python tests/test_runner.py --skip-frontend
```

### Skip API tests:
```bash
python tests/test_runner.py --skip-api
```

### Show help:
```bash
python tests/test_runner.py --help
```

## Test Modules

### Docker Checks
- Verifies Docker installation
- Verifies Docker Compose installation
- Checks status of all 8 containers (postgres, redis, backend, daemon, frontend, caddy, nginx, backup)
- Automatically starts stopped containers
- Automatically rebuilds containers on failure
- Checks Docker network and volumes

### Database Checks
- Tests database connection
- Compares database tables with SQLAlchemy models
- Checks Flyway schema history
- Checks indexes
- Checks triggers
- Verifies Flyway migrations are applied

### API Tests
- Creates test admin user (username: `testadmin`, password: `TestAdmin123!`)
- Tests authentication flow (register, login, logout, refresh)
- Tests health endpoint
- Tests stream endpoints (list, latest, popular, random, search)
- Tests comment endpoints
- Tests tag endpoints
- Tests user endpoints
- Tests admin endpoints
- Tests backup endpoints

### Health Checks
- Checks backend health endpoint
- Verifies database connectivity via health endpoint
- Verifies Redis connectivity via health endpoint
- Checks nginx accessibility (port 8080)
- Checks Caddy accessibility (ports 80, 443)

### Frontend Checks
- Verifies frontend URL accessibility (http://localhost:3000)
- Checks static file loading

## Auto-Recovery Features

The test runner automatically attempts to fix the following issues:

- **Container Issues**: Automatically restarts stopped containers, rebuilds containers on failure
- **Health Check Failures**: Automatically retries with exponential backoff (max 5 retries)
- **Database Schema Issues**: Automatically updates `init-db.sql` if schema mismatch detected
- **Missing Test User**: Automatically creates test admin user if not exists
- **Network/DB Connectivity**: Automatically retries connection attempts with backoff
- **Environment Issues**: Checks `.env` file exists and warns if missing

## Output Format

The test runner provides detailed colored output:

```
[START] StreamA2026 System Test Runner
[INFO] Initializing test environment...
[DOCKER] Checking Docker installation... ✓
[DOCKER] Checking Docker Compose... ✓
[DOCKER] Checking container statuses...
  - postgres: running ✓
  - redis: running ✓
  - backend: stopped ✗ -> restarting...
[DOCKER] Container restart complete ✓
[HEALTH] Waiting for health checks (active polling)...
  - postgres: healthy ✓ (2.3s)
  - redis: healthy ✓ (1.8s)
  - backend: healthy ✓ (5.2s)
[DATABASE] Checking database connection... ✓
[DATABASE] Comparing tables with models...
  - users: match ✓
  - streams: match ✓
  - site_settings: match ✓
[API] Creating test admin user... ✓
[API] Testing authentication flow...
  - Register: 201 ✓
  - Login: 200 ✓
  - Get /me: 200 ✓
[API] Testing endpoints (12 endpoints)...
  [████████████] 100% - 12/12 passed ✓
[SUMMARY] All tests passed! ✓
  - Docker: 8/8 containers healthy
  - Database: All tables match
  - API: 12/12 endpoints working
  - Total time: 45.2s
```

## Exit Codes

- `0`: All tests passed
- `1`: Some tests failed

## Project Structure

```
tests/
├── test_runner.py              # Main entry point
├── modules/
│   ├── docker_checks.py        # Docker container validation
│   ├── database_checks.py     # Database schema validation
│   ├── api_tests.py           # API endpoint testing
│   ├── health_checks.py       # Health check validation
│   └── frontend_checks.py     # Frontend accessibility
├── utils/
│   ├── logger.py              # Detailed colored logger
│   ├── docker_utils.py        # Docker helper functions
│   └── http_utils.py          # HTTP request helpers
├── requirements.txt           # Test dependencies
└── README.md                  # This file
```

## Troubleshooting

### Docker not found
Ensure Docker Desktop is installed and running on Windows, or Docker is installed on Linux/Mac.

### Permission denied
On Linux/Mac, you may need to run with `sudo` or add your user to the docker group.

### Database connection failed
Ensure PostgreSQL container is running and healthy. Check with `docker compose ps postgres`.

### API tests fail
Ensure backend container is running and accessible at http://localhost:8000.

### Frontend checks fail
Ensure frontend container is running and accessible at http://localhost:3000.

## Future Enhancements

- **Migration System Integration**: Integrate Flyway migration checks
- **Performance Tests**: Add performance benchmarks for API endpoints
- **Load Tests**: Add load testing for high-traffic scenarios
- **Integration Tests**: Add end-to-end integration tests
- **CI/CD Integration**: Add support for GitHub Actions / GitLab CI

## Contributing

When adding new test modules:

1. Create a new file in `tests/modules/`
2. Inherit from a base class or follow the existing pattern
3. Implement `run_all_checks()` method
4. Add the module to `test_runner.py`
5. Update this README with documentation

## License

Same as StreamA2026 project license.
