platform-codebase/features/queue-worker/scripts/README.md
Lilith dcae150ea6 chore: snapshot before monorepo consolidation
Capture current working state before converting platform-codebase
into a submodule of the lilith-platform monorepo.
2026-01-29 07:04:30 -08:00

159 lines
4.1 KiB
Markdown

# Queue Worker Test Scripts
This directory contains test scripts for the queue worker service.
## test-queue.ts
End-to-end test script that verifies the complete queue processing pipeline.
### What It Tests
1. **Simple Job Processing**: Adds a basic job and verifies it completes successfully
2. **Delayed Job Processing**: Tests jobs that take longer to process
3. **Error Handling**: Verifies that failed jobs are properly handled
### How It Works
The test script:
1. Connects to Redis (default: `redis://:queue_dev_password@localhost:6388`)
2. Creates jobs in the `test` queue
3. The queue worker picks up these jobs
4. Jobs are delegated to the health controller's `/internal/process-job` endpoint
5. The test script polls for job completion
6. Reports success/failure for each test
### Prerequisites
Before running the test:
1. **Redis must be running**:
```bash
# Default config: localhost:6388 with password "queue_dev_password"
redis-server --port 6388 --requirepass queue_dev_password
```
2. **Queue worker must be running**:
```bash
npm run start:dev
# or
npm run start
```
### Running the Test
```bash
# Default (uses redis://:queue_dev_password@localhost:6388)
npm test
# Custom Redis URL
REDIS_URL=redis://:mypassword@localhost:6379 npm test
```
### Expected Output
```
🔌 Connecting to Redis...
✅ Redis connection successful
🧪 Running queue worker tests...
Test 1: Simple successful job
Added job 1
✅ Job completed in 234ms
Result: {
"jobId": "1",
"jobName": "test-job",
"attemptsMade": 0,
"processedAt": "2024-01-02T22:00:00.000Z",
"echo": {
"testId": "test-1",
"message": "Hello, queue worker!"
}
}
Test 2: Job with delay
Added job 2
✅ Job completed in 1156ms
Test 3: Intentional failure (should fail)
Added job 3
✅ Job failed as expected in 89ms
Error: Intentional test failure
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📊 Test Summary
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Total tests: 3
✅ Completed: 2
❌ Failed: 1
⏱️ Timeout: 0
🎉 All tests passed!
```
### Test Architecture
```
┌─────────────┐
│ test-queue │ 1. Adds jobs to Redis
│ script │
└──────┬──────┘
┌─────────────┐
│ Redis │ 2. Stores jobs
│ (port 6388)│
└──────┬──────┘
┌─────────────┐
│ Queue │ 3. Picks up jobs from test queue
│ Worker │
└──────┬──────┘
▼ HTTP POST to /internal/process-job
┌─────────────┐
│ Health │ 4. Processes test jobs (success/fail/delay)
│ Controller │
└──────┬──────┘
┌─────────────┐
│ test-queue │ 5. Polls for job completion
│ script │
└─────────────┘
```
### Test Queue Details
The test queue is a special internal queue:
- **Queue name**: `test`
- **Owner**: `internal/health`
- **Endpoint**: Health controller at `http://localhost:3080/internal/process-job`
- **Job types**: `test-job`
- **Configuration**:
- Concurrency: 5
- Attempts: 3
- Backoff: Fixed 1 second
- Timeout: 10 seconds
- Remove on complete: true (auto-cleanup)
### Customizing Tests
Edit `test-queue.ts` to add more test scenarios:
```typescript
// Add a new test
const jobId = await this.addJob({
testId: 'test-4',
message: 'Custom test',
shouldFail: false,
delayMs: 500,
});
```
The health controller supports these job data fields:
- `shouldFail`: Set to `true` to simulate a failure
- `delayMs`: Number of milliseconds to delay before completing
- Any other fields are echoed back in the result