# Quick Start: LiveKit Bot Local Testing

**Last Updated**: 2025-10-18
**Status**: Ready for testing

## What We've Built

I've created a complete local testing environment for the screenshare bot with LiveKit room connection:

### 1. Token Generator (`generate_livekit_token.py`)
- Generates LiveKit access tokens for bot authentication
- Loads credentials from `.env.local`
- Usage: `.venv/bin/python generate_livekit_token.py [room_name] [identity]`

### 2. LiveKit Connection Test (`test_livekit_connection.py`)
- Full end-to-end bot test with LiveKit room
- Streams slideshow to LiveKit room for configurable duration
- Logs all activity to `./test_logs/livekit-test-TIMESTAMP.log`
- Usage: `.venv/bin/python test_livekit_connection.py [room_name] [duration_seconds]`

### 3. Screenshot Test (`test_local_capture.py`)
- Tests screenshot capture without LiveKit (already working)
- Captures 30 frames at 10 FPS
- Usage: `.venv/bin/python test_local_capture.py`

## Setup Instructions

### Step 1: Get LiveKit Credentials

You need 3 pieces of information from LiveKit Cloud:

1. **LIVEKIT_URL**: `wss://buzztrail-fifa4bj6.livekit.cloud`
   - Found at: https://cloud.livekit.io/projects/<your-project>/settings
   - This is already confirmed (from voice-demo-ui/.env.local)

2. **LIVEKIT_API_KEY**: Get from Railway or LiveKit Cloud
   - Railway: `railway variables` (from slide-system service)
   - LiveKit Cloud: https://cloud.livekit.io/projects/<your-project>/settings → API Keys

3. **LIVEKIT_API_SECRET**: Get from Railway or LiveKit Cloud
   - Same location as API_KEY

### Step 2: Get Supabase Credentials

These are optional for basic testing, but recommended:

1. **SUPABASE_URL**: `https://dwdytxxqcvlrmyemonxz.supabase.co`
   - Found in environment-variables.md

2. **SUPABASE_SERVICE_KEY**: Get from Railway
   - Railway: `railway variables` (from slide-system service)
   - Or Supabase dashboard: Settings → API → service_role key

### Step 3: Create .env.local

```bash
# Copy the template
cp .env.local.example .env.local

# Edit with your credentials
nano .env.local
```

Fill in the values:

```bash
# Transport type
TRANSPORT_TYPE=livekit

# LiveKit Configuration
LIVEKIT_URL=wss://buzztrail-fifa4bj6.livekit.cloud
LIVEKIT_API_KEY=<your_api_key_from_railway>
LIVEKIT_API_SECRET=<your_api_secret_from_railway>
LIVEKIT_ROOM_NAME=test-room-local

# Supabase Configuration (optional for basic test)
SUPABASE_URL=https://dwdytxxqcvlrmyemonxz.supabase.co
SUPABASE_SERVICE_KEY=<your_service_key_from_railway>

# Python Configuration
PYTHONUNBUFFERED=1
```

## Running the Tests

### Test 1: Screenshot Capture Only (No LiveKit)

This already works - it captures screenshots without LiveKit:

```bash
.venv/bin/python test_local_capture.py
```

**Expected output:**
- 30 frames captured at 100% success rate
- ~200ms average capture time
- Frames saved to `./test_screenshots/session_TIMESTAMP/`

### Test 2: Generate LiveKit Access Token

Once you have credentials in `.env.local`:

```bash
.venv/bin/python generate_livekit_token.py test-room-local screenshare-bot
```

**Expected output:**
```
✅ Loaded environment variables from .env.local
📝 Generating token for:
   Room: test-room-local
   Identity: screenshare-bot
   API Key: <first 10 chars>...
✅ Token generated successfully

======================================================================
LIVEKIT ACCESS TOKEN
======================================================================
<long JWT token string>
======================================================================
```

### Test 3: Full Bot with LiveKit Room Connection

This is the main test - it will:
1. Generate a LiveKit access token
2. Start the bot
3. Connect to LiveKit room
4. Stream slideshow for 30 seconds (or specified duration)
5. Log everything to `./test_logs/`

```bash
# Run for 30 seconds (default)
.venv/bin/python test_livekit_connection.py test-room-local 30

# Run for 60 seconds
.venv/bin/python test_livekit_connection.py my-custom-room 60
```

**Expected output:**
```
[LiveKitTest] LIVEKIT ROOM CONNECTION TEST
[LiveKitTest] Room name: test-room-local
[LiveKitTest] 🔑 Generating LiveKit access token...
[LiveKitTest] ✅ Token generated for identity: bot-XXXXXX
[LiveKitTest] 🤖 Creating bot instance...
[LiveKitTest] 🚀 Starting bot...
======================================================================
BOT.PY STARTING - Python process initiated
======================================================================
...
[LiveKitTest] ✅ Bot ran for 30.2 seconds
[LiveKitTest] ✅ Frames streamed: 302
[LiveKitTest] ✅ Average FPS: 10.03
```

### Test 4: Docker Container Test (99% Railway Parity)

If you want to run in Docker (same environment as Railway):

```bash
# Screenshot test in Docker
./test-docker.sh test

# Full bot with LiveKit in Docker
./test-docker.sh bot

# Open bash shell in Docker container
./test-docker.sh bash
```

## Troubleshooting

### Error: ".env.local not found"

**Solution**: Create .env.local from the template above

### Error: "Missing LIVEKIT_API_KEY or LIVEKIT_API_SECRET"

**Solution**: Get credentials from Railway:

```bash
# From the slide-system directory (not screenshare-bot)
cd /Users/lars/mycode/buzztrail/extension/apps/slide-system
railway link  # If not already linked
railway variables | grep LIVEKIT
```

Or visit Railway dashboard:
- https://railway.app/dashboard
- Select `slide-system` service
- Navigate to **Variables** tab
- Copy `LIVEKIT_API_KEY` and `LIVEKIT_API_SECRET`

### Error: "Failed to join LiveKit room"

**Possible causes**:
1. Wrong API key/secret
2. Room name has special characters
3. LiveKit Cloud is down
4. Token generation failed

**Solution**: Check logs in `./test_logs/livekit-test-TIMESTAMP.log`

### Error: "ImportError: No module named 'livekit.api'"

**Solution**: Install livekit-api:
```bash
.venv/bin/pip install livekit-api
```

## Next Steps After Successful Local Test

Once the bot works locally:

1. **Compare Local vs Railway Logs**
   - Local logs: `./test_logs/livekit-test-*.log`
   - Railway logs: `railway logs --service slide-system`
   - Look for differences in:
     - Bot startup sequence
     - LiveKit connection
     - Frame streaming rate
     - Error messages

2. **Test with Same Room as Railway**
   - Create a voice demo session on Railway
   - Get the room name from Railway logs
   - Run local bot with that room name
   - See if local bot can join the same room

3. **Debug Railway-Specific Issues**
   - Check environment variables on Railway
   - Verify Python version matches (3.14.0)
   - Check for Docker-specific issues
   - Look at file permissions

## Files Created

- `generate_livekit_token.py` - Token generator
- `test_livekit_connection.py` - Full LiveKit test
- `test_local_capture.py` - Screenshot-only test (already exists)
- `test-docker.sh` - Docker test helper
- `docker-compose.local.yml` - Docker config
- `.env.local.example` - Credentials template
- `README-LOCAL-TESTING.md` - Full testing guide

## Success Criteria

The bot is working locally when you see:

1. ✅ Bot starts without errors
2. ✅ Connects to LiveKit room
3. ✅ Streams frames at ~10 FPS
4. ✅ Screenshot capture success rate >95%
5. ✅ Logs show "Room joined" and "Screen share track published"
6. ✅ No crashes or connection drops during test

Once you see all of these locally, we can compare with Railway to find the issue!
