馃 distributed transcription service
thistle.dunkirk.sh
1# API Integration Tests
2
3This file (`src/index.test.ts`) contains comprehensive integration tests for all API endpoints in the Thistle application.
4
5## Running the Tests
6
7### Option 1: Manual Server Start (Recommended for Development)
8
91. Start the test server in one terminal:
10 ```bash
11 PORT=3001 bun run src/index.ts
12 ```
13
142. Run the integration tests in another terminal:
15 ```bash
16 bun test src/index.test.ts
17 ```
18
19### Option 2: Run All Tests
20
21To run all tests (both unit and integration):
22```bash
23bun test
24```
25
26**Note**: Integration tests will be skipped if the test server is not running on port 3001.
27
28## Test Coverage
29
30The integration tests cover the following endpoint groups:
31
32### Authentication Endpoints
33- `POST /api/auth/register` - User registration with validation and rate limiting
34- `POST /api/auth/login` - User login with rate limiting
35- `POST /api/auth/logout` - User logout
36- `GET /api/auth/me` - Get current user information
37
38### Session Management
39- `GET /api/sessions` - List user sessions
40- `DELETE /api/sessions` - Delete specific session
41
42### User Management
43- `DELETE /api/user` - Delete user account
44- `PUT /api/user/email` - Update user email
45- `PUT /api/user/password` - Update user password
46- `PUT /api/user/name` - Update user name
47- `PUT /api/user/avatar` - Update user avatar
48
49### Passkey Management
50- `POST /api/passkeys/register/options` - Get passkey registration options
51- `POST /api/passkeys/register/verify` - Verify and create passkey
52- `POST /api/passkeys/authenticate/options` - Get authentication options
53- `POST /api/passkeys/authenticate/verify` - Verify and authenticate with passkey
54- `GET /api/passkeys` - List user passkeys
55- `PUT /api/passkeys/:id` - Update passkey name
56- `DELETE /api/passkeys/:id` - Delete passkey
57
58### Health Endpoint
59- `GET /api/health` - Check service health (database, whisper, storage)
60
61### Transcription Endpoints
62- `GET /api/transcriptions` - List user transcriptions
63- `POST /api/transcriptions` - Upload audio file and start transcription
64- `GET /api/transcriptions/:id` - Get transcription details
65- `GET /api/transcriptions/:id/audio` - Get audio file with range support
66- `GET /api/transcriptions/:id/stream` - SSE stream for transcription updates
67
68### Admin Endpoints
69- `GET /api/admin/users` - List all users
70- `GET /api/admin/users/:id/details` - Get user details
71- `DELETE /api/admin/users/:id` - Delete user
72- `PUT /api/admin/users/:id/role` - Update user role
73- `PUT /api/admin/users/:id/name` - Update user name
74- `PUT /api/admin/users/:id/email` - Update user email
75- `POST /api/admin/users/:id/password-reset` - Send password reset email
76- `GET /api/admin/users/:id/sessions` - List user sessions
77- `DELETE /api/admin/users/:id/sessions` - Delete all user sessions
78- `DELETE /api/admin/users/:id/sessions/:sessionId` - Delete specific session
79- `DELETE /api/admin/users/:id/passkeys/:passkeyId` - Delete user passkey
80- `GET /api/admin/transcriptions` - List all transcriptions
81- `GET /api/admin/transcriptions/:id/details` - Get transcription details
82- `DELETE /api/admin/transcriptions/:id` - Delete transcription
83
84## Test Features
85
86- **Automatic cleanup**: Test data is cleaned up before and after each test
87- **Rate limit testing**: Validates rate limiting on sensitive endpoints
88- **Authorization testing**: Ensures proper authentication and authorization
89- **Validation testing**: Checks input validation and error handling
90- **Security testing**: Tests for common vulnerabilities
91- **File upload testing**: Validates file type and size restrictions
92
93## Test Database
94
95Tests 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.
96
97## Continuous Integration
98
99For CI/CD pipelines, you can use a background server:
100
101```bash
102# Start server in background
103PORT=3001 bun run src/index.ts &
104SERVER_PID=$!
105
106# Wait for server to be ready
107sleep 2
108
109# Run tests
110bun test src/index.test.ts
111
112# Kill server
113kill $SERVER_PID
114```
115
116## Troubleshooting
117
118### Tests are being skipped
119- Make sure the test server is running on port 3001
120- Check that there are no port conflicts
121- Verify the server started successfully (check console output)
122
123### Tests are failing with connection errors
124- Ensure no firewall is blocking localhost connections
125- Try increasing the timeout in the `beforeAll` hook
126- Check that the database is accessible
127
128### Rate limit tests are flaky
129- Rate limits are shared across test runs
130- Clean test data between runs: `rm thistle.db`
131- Or adjust rate limit test expectations