# H5P Service Tests

## Overview

Dieser Ordner enthält Integration Tests für den BreakPilot H5P Service.

## Test-Struktur

```
tests/
├── README.md           # Diese Datei
├── setup.js            # Jest Test Setup
└── server.test.js      # Integration Tests für Server Endpoints
```

## Test-Coverage

Die Tests decken folgende Bereiche ab:

### 1. Health & Info Endpoints
- `GET /` - Service Info Page
- `GET /health` - Health Check

### 2. Editor Selection Page
- `GET /h5p/editor/new` - Hauptseite mit allen 8 Content-Typen

### 3. Content Type Editors (8 Typen)
- Quiz Editor
- Interactive Video Editor
- Course Presentation Editor
- Flashcards Editor
- Timeline Editor
- Drag and Drop Editor
- Fill in the Blanks Editor
- Memory Game Editor

### 4. Content Type Players (8 Typen)
- Quiz Player
- Interactive Video Player
- Course Presentation Player
- Flashcards Player (coming soon)
- Timeline Player
- Drag and Drop Player
- Fill in the Blanks Player
- Memory Game Player

### 5. Static File Serving
- `/h5p/core/*` - H5P Core Files
- `/h5p/editors/*` - Editor HTML Files
- `/h5p/players/*` - Player HTML Files

### 6. Error Handling
- 404 für nicht existierende Routes
- Invalid Editor/Player Routes

## Tests ausführen

### Lokale Entwicklung

```bash
# Alle Tests ausführen
npm test

# Tests mit Watch-Mode
npm run test:watch

# Tests für CI/CD
npm run test:ci
```

### Docker Container Tests

```bash
# Service starten
docker compose -f docker-compose.content.yml up -d h5p-service

# Tests im Container ausführen
docker compose -f docker-compose.content.yml exec h5p-service npm test
```

## Test-Konfiguration

### Environment Variables

| Variable | Default | Beschreibung |
|----------|---------|--------------|
| `H5P_TEST_URL` | `http://localhost:8080` | Base URL für Tests |

### Jest Konfiguration

Siehe `jest.config.js` für Details:
- Test Timeout: 10000ms
- Coverage Reports: text, lcov, html
- Test Match: `**/tests/**/*.test.js`

## Coverage

Coverage Reports werden generiert in:
- `coverage/lcov-report/index.html` (HTML Report)
- `coverage/lcov.info` (LCOV Format)
- Terminal Output

Ziel: >80% Coverage

## Neue Tests hinzufügen

### Test-Template

```javascript
describe('Feature Name', () => {
  test('should do something', async () => {
    const response = await request(BASE_URL).get('/endpoint');

    expect(response.status).toBe(200);
    expect(response.text).toContain('Expected Content');
  });
});
```

## Troubleshooting

### Service nicht erreichbar

```bash
# Service Status prüfen
docker compose -f docker-compose.content.yml ps

# Logs ansehen
docker compose -f docker-compose.content.yml logs h5p-service

# Service neu starten
docker compose -f docker-compose.content.yml restart h5p-service
```

### Tests schlagen fehl

1. Prüfe, ob Service läuft: `curl http://localhost:8003/health`
2. Prüfe Logs: `docker compose -f docker-compose.content.yml logs h5p-service`
3. Rebuilde Container: `docker compose -f docker-compose.content.yml up -d --build h5p-service`

## Best Practices

1. **Isolierte Tests**: Jeder Test sollte unabhängig laufen
2. **Cleanup**: Tests sollten keine persistenten Änderungen hinterlassen
3. **Assertions**: Klare und aussagekräftige Expectations
4. **Beschreibungen**: Aussagekräftige test/describe Namen
5. **Speed**: Integration Tests sollten <10s dauern

## CI/CD Integration

Die Tests werden automatisch ausgeführt bei:
- Pull Requests
- Commits auf `main` branch
- Release Builds

GitHub Actions Workflow:
```yaml
- name: Run H5P Service Tests
  run: |
    docker compose -f docker-compose.content.yml up -d h5p-service
    docker compose -f docker-compose.content.yml exec h5p-service npm run test:ci
```

## Zukünftige Erweiterungen

- [ ] E2E Tests mit Playwright
- [ ] Performance Tests
- [ ] Content Validation Tests
- [ ] Security Tests (XSS, CSRF)
- [ ] Load Tests
