# Slidev End-to-End Test Scripts

This directory contains test scripts for validating the Slidev deck generation and control system with GitHub Pages hosting.

## Files

### `test-utils.ts`
Shared utility functions and types for testing:
- API client wrappers (`generateDeckTest`, `controlCommandTest`)
- GitHub Pages polling (`waitForGitHubPages`)
- Performance metrics calculation (`calculateAverages`)
- Result formatting (`formatResults`, `formatTime`)
- Test data generation (`generateTestData`)

### `performance-test.ts`
Performance testing script that:
- Runs N iterations of deck generation (default: 10)
- Measures build time, GitHub Pages publish time, and total generation time
- Tests control command latency (next, prev, goto)
- Measures page load time from GitHub Pages CDN
- Validates against performance targets:
  - Build time: < 30s
  - GitHub Pages publish: < 60s
  - Total generation: < 90s
  - Control latency: < 500ms
  - Page load time: < 3s
  - Success rate: >= 95%

### `concurrent-test.ts`
Concurrent load testing script that:
- Spawns N simultaneous deck generations (default: 15)
- Tests GitHub Pages concurrent publish handling
- Tracks success/failure rates
- Validates target of 10+ successful concurrent demos

## Usage

### Environment Variables
```bash
export MOTIA_API_URL="https://your-api.railway.app"
export SLIDEV_API_KEY="your-api-key"
```

### Run Performance Test
```bash
# Default: 10 iterations
npm run test:performance

# Custom iterations
npm run test:performance -- --iterations 20

# With environment variables
MOTIA_API_URL=https://api.example.com npm run test:performance
```

### Run Concurrent Test
```bash
# Default: 15 concurrent requests
npm run test:concurrent

# Custom count
npm run test:concurrent -- --count 20
```

### Help
```bash
npm run test:performance -- --help
npm run test:concurrent -- --help
```

## Output

### Performance Test Output
```
═══════════════════════════════════════════════════════════════
                   PERFORMANCE TEST RESULTS
═══════════════════════════════════════════════════════════════

📊 TEST SUMMARY
───────────────────────────────────────────────────────────────
   Total Tests:       10
   Successful:        10
   Failed:            0
   Success Rate:      100.00%

⚡ GENERATION PERFORMANCE
───────────────────────────────────────────────────────────────
   Avg Build Time:           25.00s
   Avg GitHub Pages Publish: 45.00s
   Avg Total Time:           70.00s
   Min Generation Time:      65.00s
   Max Generation Time:      75.00s

🎮 CONTROL PERFORMANCE
───────────────────────────────────────────────────────────────
   Avg Control Latency: 250ms
   Min Latency:         200ms
   Max Latency:         300ms

🎯 TARGET COMPARISON
───────────────────────────────────────────────────────────────
   ✅ Build Time:           25.00s (target: < 30s)
   ✅ GitHub Pages Publish: 45.00s (target: < 60s)
   ✅ Total Generation:     70.00s (target: < 90s)
   ✅ Control Latency:      250ms (target: < 500ms)
```

### Concurrent Test Output
```
═══════════════════════════════════════════════════════════════
                CONCURRENT TEST RESULTS
═══════════════════════════════════════════════════════════════

📊 CONCURRENT LOAD SUMMARY
───────────────────────────────────────────────────────────────
   Total Requests:    15
   Successful:        12
   Failed:            3
   Success Rate:      80.00%

⚡ PERFORMANCE UNDER LOAD
───────────────────────────────────────────────────────────────
   Avg Success Time:  75.00s
   Min Success Time:  65.00s
   Max Success Time:  85.00s

🎯 TARGET VALIDATION
───────────────────────────────────────────────────────────────
   ✅ Successful Requests: 12/15 (target: >= 10)

📦 GITHUB PAGES CONCURRENT PUBLISHING
───────────────────────────────────────────────────────────────
   Published Decks:   12
   Avg Publish Time:  48.00s
   ✅ GitHub Pages handled concurrent publishing
```

## Implementation Notes

### TypeScript Compatibility
The scripts use TypeScript with ts-node for execution. All type definitions are inlined in `test-utils.ts` to avoid import path issues.

### GitHub Pages Timing
- Deck generation API returns immediately after triggering GitHub Actions
- GitHub Pages publish typically takes 30-90 seconds
- Tests use polling (`waitForGitHubPages`) to detect when pages are live
- Poll interval: 5 seconds
- Default timeout: 120 seconds (2 minutes)

### Error Handling
- Network errors trigger retries
- API errors are captured and reported
- Failed tests don't stop the test suite
- Detailed error messages in output

### Test Data
- Uses rotating sample companies and industries
- Unique session IDs with timestamps
- Simple placeholder data for deck generation

## Next Steps

1. **Test with real API**: Run against deployed Slidev API to validate functionality
2. **Add more test scenarios**: Error handling, edge cases, rate limiting
3. **Integration with CI/CD**: Add to GitHub Actions workflow
4. **Add E2E browser tests**: Use Playwright to validate full user experience

## Dependencies

Already installed in `package.json`:
- `ts-node`: TypeScript execution
- `typescript`: TypeScript compiler
- `node-fetch`: HTTP client (built-in in Node 18+)
- `@types/node`: Node.js type definitions

No additional dependencies required.
