···11+# API Integration Tests
22+33+This file (`src/index.test.ts`) contains comprehensive integration tests for all API endpoints in the Thistle application.
44+55+## Running the Tests
66+77+### Option 1: Manual Server Start (Recommended for Development)
88+99+1. Start the test server in one terminal:
1010+ ```bash
1111+ PORT=3001 bun run src/index.ts
1212+ ```
1313+1414+2. Run the integration tests in another terminal:
1515+ ```bash
1616+ bun test src/index.test.ts
1717+ ```
1818+1919+### Option 2: Run All Tests
2020+2121+To run all tests (both unit and integration):
2222+```bash
2323+bun test
2424+```
2525+2626+**Note**: Integration tests will be skipped if the test server is not running on port 3001.
2727+2828+## Test Coverage
2929+3030+The integration tests cover the following endpoint groups:
3131+3232+### Authentication Endpoints
3333+- `POST /api/auth/register` - User registration with validation and rate limiting
3434+- `POST /api/auth/login` - User login with rate limiting
3535+- `POST /api/auth/logout` - User logout
3636+- `GET /api/auth/me` - Get current user information
3737+3838+### Session Management
3939+- `GET /api/sessions` - List user sessions
4040+- `DELETE /api/sessions` - Delete specific session
4141+4242+### User Management
4343+- `DELETE /api/user` - Delete user account
4444+- `PUT /api/user/email` - Update user email
4545+- `PUT /api/user/password` - Update user password
4646+- `PUT /api/user/name` - Update user name
4747+- `PUT /api/user/avatar` - Update user avatar
4848+4949+### Passkey Management
5050+- `POST /api/passkeys/register/options` - Get passkey registration options
5151+- `POST /api/passkeys/register/verify` - Verify and create passkey
5252+- `POST /api/passkeys/authenticate/options` - Get authentication options
5353+- `POST /api/passkeys/authenticate/verify` - Verify and authenticate with passkey
5454+- `GET /api/passkeys` - List user passkeys
5555+- `PUT /api/passkeys/:id` - Update passkey name
5656+- `DELETE /api/passkeys/:id` - Delete passkey
5757+5858+### Transcription Endpoints
5959+- `GET /api/transcriptions/health` - Check transcription service health
6060+- `GET /api/transcriptions` - List user transcriptions
6161+- `POST /api/transcriptions` - Upload audio file and start transcription
6262+- `GET /api/transcriptions/:id` - Get transcription details
6363+- `GET /api/transcriptions/:id/audio` - Get audio file with range support
6464+- `GET /api/transcriptions/:id/stream` - SSE stream for transcription updates
6565+6666+### Admin Endpoints
6767+- `GET /api/admin/users` - List all users
6868+- `GET /api/admin/users/:id/details` - Get user details
6969+- `DELETE /api/admin/users/:id` - Delete user
7070+- `PUT /api/admin/users/:id/role` - Update user role
7171+- `PUT /api/admin/users/:id/name` - Update user name
7272+- `PUT /api/admin/users/:id/email` - Update user email
7373+- `PUT /api/admin/users/:id/password` - Update user password
7474+- `GET /api/admin/users/:id/sessions` - List user sessions
7575+- `DELETE /api/admin/users/:id/sessions` - Delete all user sessions
7676+- `DELETE /api/admin/users/:id/sessions/:sessionId` - Delete specific session
7777+- `DELETE /api/admin/users/:id/passkeys/:passkeyId` - Delete user passkey
7878+- `GET /api/admin/transcriptions` - List all transcriptions
7979+- `GET /api/admin/transcriptions/:id/details` - Get transcription details
8080+- `DELETE /api/admin/transcriptions/:id` - Delete transcription
8181+8282+## Test Features
8383+8484+- **Automatic cleanup**: Test data is cleaned up before and after each test
8585+- **Rate limit testing**: Validates rate limiting on sensitive endpoints
8686+- **Authorization testing**: Ensures proper authentication and authorization
8787+- **Validation testing**: Checks input validation and error handling
8888+- **Security testing**: Tests for common vulnerabilities
8989+- **File upload testing**: Validates file type and size restrictions
9090+9191+## Test Database
9292+9393+Tests use the same database as development. Test users and data are identified by email patterns (`test%`, `admin@%`) and are automatically cleaned up after tests run.
9494+9595+## Continuous Integration
9696+9797+For CI/CD pipelines, you can use a background server:
9898+9999+```bash
100100+# Start server in background
101101+PORT=3001 bun run src/index.ts &
102102+SERVER_PID=$!
103103+104104+# Wait for server to be ready
105105+sleep 2
106106+107107+# Run tests
108108+bun test src/index.test.ts
109109+110110+# Kill server
111111+kill $SERVER_PID
112112+```
113113+114114+## Troubleshooting
115115+116116+### Tests are being skipped
117117+- Make sure the test server is running on port 3001
118118+- Check that there are no port conflicts
119119+- Verify the server started successfully (check console output)
120120+121121+### Tests are failing with connection errors
122122+- Ensure no firewall is blocking localhost connections
123123+- Try increasing the timeout in the `beforeAll` hook
124124+- Check that the database is accessible
125125+126126+### Rate limit tests are flaky
127127+- Rate limits are shared across test runs
128128+- Clean test data between runs: `rm thistle.db`
129129+- Or adjust rate limit test expectations