|
| 1 | +# API Test Suite Documentation |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document describes the comprehensive test suite for the Devlog Web API, designed to ensure our API route overhaul maintains reliability and prevents regressions. |
| 6 | + |
| 7 | +## Test Architecture |
| 8 | + |
| 9 | +### 🔬 **Unit Tests** (`tests/api.test.ts`) |
| 10 | + |
| 11 | +- **Isolated testing** using mocks and no external dependencies |
| 12 | +- **Fast execution** - runs in milliseconds |
| 13 | +- **Safe for CI/CD** - no database or network dependencies |
| 14 | +- **Always run** during development and deployment |
| 15 | + |
| 16 | +### 🚀 **Integration Tests** (`tests/api-integration.test.ts`) |
| 17 | + |
| 18 | +- **End-to-end testing** against actual API endpoints |
| 19 | +- **Real HTTP requests** with proper error handling |
| 20 | +- **Environment-specific** - requires running API server |
| 21 | +- **Opt-in execution** - only runs when explicitly enabled |
| 22 | + |
| 23 | +## Running Tests |
| 24 | + |
| 25 | +### Unit Tests (Always Safe) |
| 26 | + |
| 27 | +```bash |
| 28 | +# Run all unit tests |
| 29 | +pnpm --filter @codervisor/devlog-web test |
| 30 | + |
| 31 | +# Run tests in watch mode |
| 32 | +pnpm --filter @codervisor/devlog-web test:watch |
| 33 | + |
| 34 | +# Run with coverage report |
| 35 | +pnpm --filter @codervisor/devlog-web test:coverage |
| 36 | + |
| 37 | +# Run with UI (interactive) |
| 38 | +pnpm --filter @codervisor/devlog-web test:ui |
| 39 | +``` |
| 40 | + |
| 41 | +### Integration Tests (Requires Running Server) |
| 42 | + |
| 43 | +```bash |
| 44 | +# Start development server first |
| 45 | +docker compose -f docker-compose.dev.yml up web-dev -d --wait |
| 46 | + |
| 47 | +# Run integration tests |
| 48 | +pnpm --filter @codervisor/devlog-web test:integration |
| 49 | + |
| 50 | +# Or with environment variables |
| 51 | +RUN_INTEGRATION_TESTS=true TEST_API_URL=http://localhost:3200/api pnpm --filter @codervisor/devlog-web test:integration |
| 52 | +``` |
| 53 | + |
| 54 | +## Test Coverage |
| 55 | + |
| 56 | +### ✅ **Unit Test Coverage** |
| 57 | + |
| 58 | +#### **RouteParams Utilities** |
| 59 | + |
| 60 | +- ✅ Valid numeric ID parsing |
| 61 | +- ✅ Invalid ID rejection (strings, negatives, zero) |
| 62 | +- ✅ Multiple parameter parsing (project + devlog IDs) |
| 63 | +- ✅ Error response generation |
| 64 | + |
| 65 | +#### **ServiceHelper Utilities** |
| 66 | + |
| 67 | +- ✅ Project existence validation |
| 68 | +- ✅ Devlog service initialization |
| 69 | +- ✅ Devlog existence validation |
| 70 | +- ✅ Service error handling |
| 71 | + |
| 72 | +#### **Error Response Patterns** |
| 73 | + |
| 74 | +- ✅ HTTP status codes (400, 404, 500, etc.) |
| 75 | +- ✅ Consistent error message format |
| 76 | +- ✅ Custom error messages |
| 77 | + |
| 78 | +#### **Success Response Patterns** |
| 79 | + |
| 80 | +- ✅ Standard success responses |
| 81 | +- ✅ Created responses (201) |
| 82 | +- ✅ No content responses (204) |
| 83 | + |
| 84 | +#### **Error Handling Wrapper** |
| 85 | + |
| 86 | +- ✅ Successful request passthrough |
| 87 | +- ✅ Exception catching and conversion |
| 88 | +- ✅ Specific error type handling ("not found", "Invalid") |
| 89 | +- ✅ Non-Error object handling |
| 90 | + |
| 91 | +#### **Route Handler Integration** |
| 92 | + |
| 93 | +- ✅ Project route patterns |
| 94 | +- ✅ Devlog route patterns |
| 95 | +- ✅ Batch operation patterns |
| 96 | +- ✅ Parameter validation integration |
| 97 | +- ✅ Service error propagation |
| 98 | + |
| 99 | +### ✅ **Integration Test Coverage** |
| 100 | + |
| 101 | +#### **Health & Infrastructure** |
| 102 | + |
| 103 | +- ✅ Health check endpoint |
| 104 | +- ✅ Server responsiveness |
| 105 | + |
| 106 | +#### **Project Operations** |
| 107 | + |
| 108 | +- ✅ GET project details |
| 109 | +- ✅ Parameter validation (invalid/nonexistent IDs) |
| 110 | +- ✅ Error response formats |
| 111 | + |
| 112 | +#### **Devlog Operations** |
| 113 | + |
| 114 | +- ✅ List devlogs with pagination |
| 115 | +- ✅ Retrieve individual devlogs |
| 116 | +- ✅ Status filtering |
| 117 | +- ✅ Search functionality |
| 118 | +- ✅ Parameter validation |
| 119 | + |
| 120 | +#### **Statistics Operations** |
| 121 | + |
| 122 | +- ✅ Overview statistics |
| 123 | +- ✅ Time series statistics |
| 124 | +- ✅ Query parameter handling |
| 125 | +- ✅ Parameter validation |
| 126 | + |
| 127 | +#### **Batch Operations** |
| 128 | + |
| 129 | +- ✅ Batch update devlogs |
| 130 | +- ✅ Batch add notes |
| 131 | +- ✅ Request body validation |
| 132 | +- ✅ Error handling |
| 133 | + |
| 134 | +#### **Response Consistency** |
| 135 | + |
| 136 | +- ✅ Project object structure |
| 137 | +- ✅ Devlog object structure |
| 138 | +- ✅ Pagination structure |
| 139 | +- ✅ Error format consistency |
| 140 | + |
| 141 | +## Test Configuration |
| 142 | + |
| 143 | +### Environment Variables |
| 144 | + |
| 145 | +```bash |
| 146 | +# Integration test configuration |
| 147 | +RUN_INTEGRATION_TESTS=true # Enable integration tests |
| 148 | +TEST_API_URL=http://localhost:3201/api # API base URL |
| 149 | +TEST_PROJECT_ID=1 # Project ID for testing |
| 150 | + |
| 151 | +# Test database (unit tests) |
| 152 | +DATABASE_URL=sqlite::memory: # In-memory database for unit tests |
| 153 | +NODE_ENV=test # Test environment marker |
| 154 | +``` |
| 155 | + |
| 156 | +### Mock Configuration |
| 157 | + |
| 158 | +```typescript |
| 159 | +// Service mocks |
| 160 | +const mockProjectService = { |
| 161 | + getInstance: vi.fn(), |
| 162 | + get: vi.fn(), |
| 163 | + update: vi.fn(), |
| 164 | + delete: vi.fn(), |
| 165 | +}; |
| 166 | + |
| 167 | +const mockDevlogService = { |
| 168 | + getInstance: vi.fn(), |
| 169 | + get: vi.fn(), |
| 170 | + save: vi.fn(), |
| 171 | + delete: vi.fn(), |
| 172 | + // ... other methods |
| 173 | +}; |
| 174 | +``` |
| 175 | + |
| 176 | +## Test Safety Features |
| 177 | + |
| 178 | +### 🛡️ **Production Protection** |
| 179 | + |
| 180 | +- **Unit tests**: Never touch real data - fully mocked |
| 181 | +- **Integration tests**: Opt-in only with explicit environment variable |
| 182 | +- **Database isolation**: Uses separate test database/memory database |
| 183 | +- **Network isolation**: Configurable API endpoint URLs |
| 184 | + |
| 185 | +### 🔒 **Data Safety** |
| 186 | + |
| 187 | +- **No destructive operations** in integration tests |
| 188 | +- **Read-only operations** where possible |
| 189 | +- **Transactional test data** (if implemented) |
| 190 | +- **Cleanup procedures** for test artifacts |
| 191 | + |
| 192 | +### ⚡ **Performance Safety** |
| 193 | + |
| 194 | +- **Parallel execution** safe with proper test isolation |
| 195 | +- **Fast unit tests** (< 1 second total runtime) |
| 196 | +- **Limited integration test scope** to prevent timeout issues |
| 197 | +- **Configurable timeouts** for different test types |
| 198 | + |
| 199 | +## Test Maintenance |
| 200 | + |
| 201 | +### Adding New Tests |
| 202 | + |
| 203 | +1. **Unit tests**: Add to `tests/api.test.ts` with proper mocking |
| 204 | +2. **Integration tests**: Add to `tests/api-integration.test.ts` with safety guards |
| 205 | +3. **New utilities**: Mock in test setup and add comprehensive unit tests |
| 206 | +4. **New endpoints**: Follow existing patterns for parameter validation |
| 207 | + |
| 208 | +### Test Data Management |
| 209 | + |
| 210 | +- **Use existing test data** where possible |
| 211 | +- **Create minimal test fixtures** for complex scenarios |
| 212 | +- **Clean up test artifacts** after integration tests |
| 213 | +- **Use deterministic test data** to prevent flaky tests |
| 214 | + |
| 215 | +### CI/CD Integration |
| 216 | + |
| 217 | +```yaml |
| 218 | +# Example GitHub Actions configuration |
| 219 | +- name: Run Unit Tests |
| 220 | + run: pnpm --filter @codervisor/devlog-web test |
| 221 | + |
| 222 | +- name: Run Integration Tests |
| 223 | + env: |
| 224 | + RUN_INTEGRATION_TESTS: true |
| 225 | + DATABASE_URL: ${{ secrets.TEST_DATABASE_URL }} |
| 226 | + run: pnpm --filter @codervisor/devlog-web test:integration |
| 227 | +``` |
| 228 | +
|
| 229 | +## Troubleshooting |
| 230 | +
|
| 231 | +### Common Issues |
| 232 | +
|
| 233 | +1. **Integration tests skipped**: Ensure `RUN_INTEGRATION_TESTS=true` |
| 234 | +2. **API connection failures**: Verify development server is running |
| 235 | +3. **Mock import errors**: Check that `@codervisor/devlog-core` is properly mocked |
| 236 | +4. **Type errors**: Ensure `@types/node` and proper TypeScript configuration |
| 237 | + |
| 238 | +### Debugging Tests |
| 239 | + |
| 240 | +```bash |
| 241 | +# Run specific test file |
| 242 | +pnpm --filter @codervisor/devlog-web test tests/api.test.ts |
| 243 | +
|
| 244 | +# Run specific test case |
| 245 | +pnpm --filter @codervisor/devlog-web test -t "should parse valid numeric project ID" |
| 246 | +
|
| 247 | +# Run with verbose output |
| 248 | +pnpm --filter @codervisor/devlog-web test --reporter=verbose |
| 249 | +
|
| 250 | +# Run with debug information |
| 251 | +DEBUG=* pnpm --filter @codervisor/devlog-web test |
| 252 | +``` |
| 253 | + |
| 254 | +## Test Metrics |
| 255 | + |
| 256 | +### Success Criteria |
| 257 | + |
| 258 | +- **Unit tests**: 100% pass rate required |
| 259 | +- **Coverage**: >80% line coverage on API utilities |
| 260 | +- **Performance**: Unit tests complete in <5 seconds |
| 261 | +- **Integration**: All critical paths verified |
| 262 | + |
| 263 | +### Quality Gates |
| 264 | + |
| 265 | +- ✅ All parameter validation scenarios covered |
| 266 | +- ✅ All error response types tested |
| 267 | +- ✅ All service integration patterns verified |
| 268 | +- ✅ Response format consistency validated |
| 269 | +- ✅ No production data dependencies |
| 270 | + |
| 271 | +--- |
| 272 | + |
| 273 | +**Status**: ✅ **COMPLETE - Comprehensive test suite implemented with safety isolation** |
| 274 | + |
| 275 | +The test suite provides robust coverage of the API route overhaul while maintaining complete isolation from production systems through extensive mocking and environment-specific configurations. |
0 commit comments