# Complete Testing Guide

## Overview

This guide covers testing for both backend APIs and frontend E2E tests.

## Backend API Testing

### Setup

1. Install test dependencies:
```bash
cd backend
pip install -r requirements-test.txt
```

2. Ensure backend server is running:
```bash
python server.py
# Server should be at http://localhost:5000
```

### Running Backend Tests

```bash
# Run all tests
pytest

# Run specific test file
pytest tests/test_auth.py
pytest tests/test_memos.py
pytest tests/test_comments.py

# Run with verbose output
pytest -v

# Run with coverage
pytest --cov=memos_core --cov=api tests/
```

### Test Files

- `tests/test_auth.py` - Authentication (sign in, sign up, get current user)
- `tests/test_memos.py` - Memo CRUD operations
- `tests/test_comments.py` - Comment creation and listing

## Frontend E2E Testing (Playwright)

### Setup

1. Install dependencies:
```bash
cd frontend
npm install
# or
pnpm install
```

2. Install Playwright browsers:
```bash
npx playwright install
```

3. Ensure frontend dev server is running:
```bash
npm run dev
# Server should be at http://localhost:3001
```

### Running Frontend Tests

```bash
# Run all tests
npm run test:e2e

# Run in UI mode (interactive)
npm run test:e2e:ui

# Run in headed mode (see browser)
npm run test:e2e:headed

# Run in debug mode
npm run test:e2e:debug

# Run specific test file
npx playwright test auth.spec.ts
npx playwright test comments.spec.ts
```

### Test Files

- `tests/e2e/auth.spec.ts` - Authentication flows
- `tests/e2e/comments.spec.ts` - Comment functionality
- `tests/e2e/memos.spec.ts` - Memo CRUD operations
- `tests/e2e/filters.spec.ts` - Filter functionality

## Test Data

Both backend and frontend tests use:
- Email: `test@example.com`
- Password: `testpassword123`

Make sure this user exists or tests will create it.

## Viewing Results

### Backend
- Test output in terminal
- Coverage report: `pytest --cov --cov-report=html`

### Frontend
- HTML report: `npx playwright show-report`
- Screenshots: `test-results/` (on failure)
- Videos: `test-results/` (on failure)

## CI/CD Integration

### Backend
```yaml
- name: Run backend tests
  run: |
    cd backend
    pip install -r requirements-test.txt
    pytest --cov --cov-report=xml
```

### Frontend
```yaml
- name: Install Playwright
  run: npx playwright install --with-deps

- name: Run E2E tests
  run: npm run test:e2e
```

## Troubleshooting

### Backend Tests
- **Import errors**: Make sure you're in the backend directory
- **Connection errors**: Ensure server is running
- **Auth errors**: Check test user credentials

### Frontend Tests
- **Timeout errors**: Increase timeout in `playwright.config.ts`
- **Selector errors**: Update selectors if UI changed
- **Server not found**: Ensure dev server is running on port 3001

