馃 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### Transcription Endpoints
59- `GET /api/transcriptions/health` - Check transcription service health
60- `GET /api/transcriptions` - List user transcriptions
61- `POST /api/transcriptions` - Upload audio file and start transcription
62- `GET /api/transcriptions/:id` - Get transcription details
63- `GET /api/transcriptions/:id/audio` - Get audio file with range support
64- `GET /api/transcriptions/:id/stream` - SSE stream for transcription updates
65
66### Admin Endpoints
67- `GET /api/admin/users` - List all users
68- `GET /api/admin/users/:id/details` - Get user details
69- `DELETE /api/admin/users/:id` - Delete user
70- `PUT /api/admin/users/:id/role` - Update user role
71- `PUT /api/admin/users/:id/name` - Update user name
72- `PUT /api/admin/users/:id/email` - Update user email
73- `PUT /api/admin/users/:id/password` - Update user password
74- `GET /api/admin/users/:id/sessions` - List user sessions
75- `DELETE /api/admin/users/:id/sessions` - Delete all user sessions
76- `DELETE /api/admin/users/:id/sessions/:sessionId` - Delete specific session
77- `DELETE /api/admin/users/:id/passkeys/:passkeyId` - Delete user passkey
78- `GET /api/admin/transcriptions` - List all transcriptions
79- `GET /api/admin/transcriptions/:id/details` - Get transcription details
80- `DELETE /api/admin/transcriptions/:id` - Delete transcription
81
82## Test Features
83
84- **Automatic cleanup**: Test data is cleaned up before and after each test
85- **Rate limit testing**: Validates rate limiting on sensitive endpoints
86- **Authorization testing**: Ensures proper authentication and authorization
87- **Validation testing**: Checks input validation and error handling
88- **Security testing**: Tests for common vulnerabilities
89- **File upload testing**: Validates file type and size restrictions
90
91## Test Database
92
93Tests 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.
94
95## Continuous Integration
96
97For CI/CD pipelines, you can use a background server:
98
99```bash
100# Start server in background
101PORT=3001 bun run src/index.ts &
102SERVER_PID=$!
103
104# Wait for server to be ready
105sleep 2
106
107# Run tests
108bun test src/index.test.ts
109
110# Kill server
111kill $SERVER_PID
112```
113
114## Troubleshooting
115
116### Tests are being skipped
117- Make sure the test server is running on port 3001
118- Check that there are no port conflicts
119- Verify the server started successfully (check console output)
120
121### Tests are failing with connection errors
122- Ensure no firewall is blocking localhost connections
123- Try increasing the timeout in the `beforeAll` hook
124- Check that the database is accessible
125
126### Rate limit tests are flaky
127- Rate limits are shared across test runs
128- Clean test data between runs: `rm thistle.db`
129- Or adjust rate limit test expectations