# RateRight Growth Engine - Test Suite

This directory contains automated tests for the RateRight Growth CRM.

## Test Files

| File | Description |
|------|-------------|
| `api-smoke.spec.js` | API endpoint health checks - verifies all endpoints exist and return proper status codes |
| `critical-flows.spec.js` | UI/UX tests - page loads, responsive design, accessibility basics |
| `database-integrity.spec.js` | Code pattern verification - checks adherence to patterns documented in LESSONS.md |
| `bug-fixes.spec.js` | Bug fix verification - ensures previous bugs stay fixed |

## Running Tests

```bash
# Run all tests
npx playwright test

# Run with browser visible (recommended for debugging)
npx playwright test --headed

# Run a specific test file
npx playwright test tests/api-smoke.spec.js

# Run tests matching a pattern
npx playwright test -g "health"

# Run with specific browser
npx playwright test --project="Desktop Chrome"
npx playwright test --project="Mobile iPhone"
```

## Test Categories

### API Smoke Tests (`api-smoke.spec.js`)
- Verifies all API endpoints exist (return 401, not 404)
- Checks health endpoint
- Tests webhook endpoints
- Runs fast, no browser needed

### Critical Flow Tests (`critical-flows.spec.js`)
- Login page renders
- Protected routes redirect
- Mobile responsive design
- CSS/JS assets load
- Basic accessibility checks
- Performance timing

### Database Integrity Tests (`database-integrity.spec.js`)
- Import path patterns (../config/database)
- Soft delete query patterns
- Migration file patterns
- Rate limiting on sensitive endpoints
- Security pattern checks

### Bug Fix Tests (`bug-fixes.spec.js`)
- Dark mode contrast
- Mobile viewport rendering
- Touch scroll detection
- Voice assistant timeout
- Phone format handling

## Screenshots

Screenshots are saved to `tests/screenshots/` when tests run. Useful for debugging and documentation.

## CI/CD Integration

These tests can be run in CI before deployment:

```yaml
# Example GitHub Actions
- name: Run Playwright tests
  run: npx playwright test
  env:
    CI: true
```

## Writing New Tests

```javascript
const { test, expect } = require('@playwright/test');

test('My new test', async ({ page }) => {
  await page.goto('/login');
  await page.waitForLoadState('networkidle');

  // Take screenshot for debugging
  await page.screenshot({ path: 'tests/screenshots/my-test.png' });

  // Make assertions
  expect(await page.title()).toBeTruthy();
});
```

## Best Practices

1. **Screenshot everything** - Screenshots help debug failed tests
2. **Wait for network idle** - Ensure page is fully loaded before assertions
3. **Test both desktop and mobile** - Use viewport sizes
4. **Check for 401, not 404** - Protected endpoints should require auth
5. **Don't hardcode credentials** - Use environment variables for auth

## Troubleshooting

### Tests timing out
- Check if the production server is running
- Increase timeout in playwright.config.js

### Screenshots show blank page
- App may be redirecting to login
- Check for JavaScript errors in console

### API tests returning 500
- Check Railway logs for server errors
- Verify environment variables are set

## Maintenance

When adding new features:
1. Add API endpoint tests to `api-smoke.spec.js`
2. Add UI tests to `critical-flows.spec.js`
3. Add pattern checks to `database-integrity.spec.js`
4. Run tests locally before pushing
