# Railway Internal Networking Test Services

This directory contains two minimal Express services for validating Railway's internal networking capabilities. These services are temporary and will be used to test service-to-service communication before deploying the bot-spawner service.

## Services

### Service A (Client)
- **Port**: 3000 (configurable via `PORT` env var)
- **Purpose**: Makes HTTP calls to Service B to test internal networking
- **Endpoints**:
  - `GET /health` - Health check endpoint
  - `GET /test-call` - Makes an HTTP call to Service B and returns latency metrics

### Service B (Server)
- **Port**: 3001 (configurable via `PORT` env var)
- **Purpose**: Responds to health checks from Service A
- **Endpoints**:
  - `GET /health` - Returns health status and system information

## Local Testing

### Test Service B
```bash
cd service-b
npm install
npm start
# Service will start on port 3001

# In another terminal:
curl http://localhost:3001/health
```

Expected response:
```json
{
  "status": "healthy",
  "service": "test-service-b",
  "timestamp": "2025-10-20T19:48:25.204Z",
  "uptime": 6.132765333,
  "environment": {
    "nodeVersion": "v24.2.0",
    "platform": "darwin",
    "arch": "arm64"
  }
}
```

### Test Service A
```bash
cd service-a
npm install
SERVICE_B_URL=http://localhost:3001 npm start
# Service will start on port 3000

# In another terminal:
curl http://localhost:3000/health
curl http://localhost:3000/test-call
```

Expected `/test-call` response:
```json
{
  "status": 200,
  "latency": 32,
  "timestamp": "2025-10-20T19:48:53.480Z",
  "serviceB": {
    "status": "healthy",
    "service": "test-service-b",
    "timestamp": "2025-10-20T19:48:53.474Z",
    "uptime": 34.402784,
    "environment": {
      "nodeVersion": "v24.2.0",
      "platform": "darwin",
      "arch": "arm64"
    }
  }
}
```

## Railway Deployment

### Prerequisites
1. Railway CLI installed and authenticated
2. Access to the `buzztrail-voice-agent` project

### Deployment Steps

#### Step 1: Link to Railway Project
```bash
# From the epic-bot-spawner-extraction worktree root
cd /Users/lars/mycode/buzztrail/epic-bot-spawner-extraction

# Check Railway status
railway status

# If not linked, link to the project
# Note: Project ID is 655e15d5-19b0-40a0-b6a4-d556806f3746
```

#### Step 2: Deploy Service B (Server)
Service B must be deployed first since Service A depends on it.

```bash
cd .claude/epics/bot-spawner-extraction/test-services/service-b

# Deploy to Railway
railway up --service test-service-b

# Generate a public domain (optional, for testing)
railway domain

# The service will be available at:
# - Public: https://test-service-b.railway.app (or similar)
# - Internal: http://test-service-b.railway.internal:3001
```

#### Step 3: Deploy Service A (Client)
```bash
cd ../service-a

# Deploy to Railway
railway up --service test-service-a

# Set the SERVICE_B_URL environment variable
railway variables --service test-service-a set SERVICE_B_URL=http://test-service-b.railway.internal:3001

# Generate a public domain
railway domain
```

#### Step 4: Verify Deployment
```bash
# Test Service B health endpoint via public URL
curl https://test-service-b.railway.app/health

# Test Service A health endpoint
curl https://test-service-a.railway.app/health

# Test internal networking (Service A -> Service B)
curl https://test-service-a.railway.app/test-call
```

Expected `/test-call` response should show:
- `status: 200` (successful call to Service B)
- `latency` < 50ms (internal networking should be fast)
- `serviceB` object with Service B's health data

### Environment Variables

#### Service A
- `PORT`: Port to listen on (default: 3000, Railway sets automatically)
- `SERVICE_B_URL`: URL to call Service B (should be `http://test-service-b.railway.internal:3001`)

#### Service B
- `PORT`: Port to listen on (default: 3001, Railway sets automatically)

## Internal Networking URL Format

Railway's internal networking uses the format:
```
http://<service-name>.railway.internal:<port>
```

For these test services:
- **Service B**: `http://test-service-b.railway.internal:3001`

## Validation Checklist

- [ ] Service B deployed and accessible via public URL
- [ ] Service A deployed and accessible via public URL
- [ ] Service A can call Service B via internal networking
- [ ] Latency is < 50ms for internal calls
- [ ] Both services respond to health checks
- [ ] No errors in Railway logs

## Troubleshooting

### Service A can't reach Service B
1. Verify Service B is running: `railway logs --service test-service-b`
2. Check SERVICE_B_URL environment variable in Service A
3. Ensure internal networking is enabled in Railway project settings
4. Try using the public URL as a fallback to verify Service A logic works

### High Latency
If internal networking shows latency > 50ms:
1. Check Railway service regions (should be in the same region)
2. Check Railway platform status
3. Compare with public URL latency

### Deployment Fails
1. Check Railway build logs: `railway logs --service <service-name>`
2. Verify `package.json` scripts are correct
3. Ensure Node.js version is compatible (>=20.0.0)

## Cleanup

After validation is complete, remove the test services:
```bash
# Delete services from Railway dashboard or CLI
railway service delete test-service-a
railway service delete test-service-b
```

## Automated Testing

### Test Runner Script

Use `test-runner.sh` to run 100 consecutive requests and validate networking reliability:

```bash
# Test using Railway internal networking
./test-runner.sh http://test-service-a.railway.internal:3000/test-call

# Test using public URL
./test-runner.sh https://test-service-a.railway.app/test-call
```

### Metrics Analysis

After running tests, analyze results with `analyze-results.ts`:

```bash
# Analyze test results
npx tsx analyze-results.ts

# Get JSON output
npx tsx analyze-results.ts --json
```

The analyzer validates:
- Success rate >= 95%
- P95 latency < 50ms
- DNS resolution working
- Connection reliability

See the test scripts documentation for more details.

## Next Steps

Once internal networking is validated:
1. Run automated test suite with `test-runner.sh`
2. Verify GO recommendation from `analyze-results.ts`
3. Document the successful configuration
4. Use the same pattern for bot-spawner service
5. Update the bot-spawner deployment plan with confirmed settings
6. Remove these test services from Railway
