# Local Development & Testing Guide

## Overview

This guide explains how to test the full demo session flow locally before deploying to production.

## Prerequisites

- Supabase CLI installed and project linked
- Motia dev server running on port 3005
- Environment variables configured in `apps/slide-system/.env.motia`

## Local Development Workflow

### 1. Start Motia Dev Server

```bash
cd apps/slide-system

# Start with environment variables
DAILY_API_KEY=xxx \
SUPABASE_URL=xxx \
SUPABASE_SERVICE_KEY=xxx \
  pnpx motia dev --port 3005
```

Or use the script with env file:
```bash
source .env.motia 2>/dev/null && \
DAILY_API_KEY=$DAILY_API_KEY \
SUPABASE_URL=$SUPABASE_URL \
SUPABASE_SERVICE_KEY=$SUPABASE_SERVICE_KEY \
  pnpx motia dev --port 3005
```

### 2. Test Motia Step Directly

Test the Motia Step without going through the Edge Function:

```bash
curl -X POST http://localhost:3005/demo-sessions \
  -H 'Content-Type: application/json' \
  -d '{"session_type":"live_demo","environment":"dev"}'
```

**Expected Response:**
```json
{
  "session_id": "uuid",
  "daily_room_url": "https://buzztrail.daily.co/demo-xxx",
  "daily_room_name": "demo-xxx",
  "status": "waiting_for_customer",
  "created_at": "2025-10-08T06:56:23.266027+00:00"
}
```

### 3. Test Edge Function with Tunnel (Optional)

For end-to-end testing with the Supabase Edge Function, you need to expose your local Motia server to the internet.

#### Option A: Cloudflare Tunnel (Recommended)

**Unauthenticated (Quick Test):**
```bash
cloudflared tunnel --url http://localhost:3005
# Note the URL provided (e.g., https://xxx.trycloudflare.com)
```

**Authenticated (Stable):**
```bash
# Login once
cloudflared tunnel login

# Create named tunnel
cloudflared tunnel create motia-dev

# Run tunnel
cloudflared tunnel run motia-dev --url http://localhost:3005
```

**Update Edge Function Secret:**
```bash
cd .claude/supabase
supabase secrets set \
  MOTIA_API_URL="https://your-tunnel-url.trycloudflare.com/demo-sessions" \
  --project-ref dwdytxxqcvlrmyemonxz
```

#### Option B: ngrok

```bash
ngrok http 3005
# Use the HTTPS URL provided
```

### 4. Test Edge Function

Once tunnel is set up:

```bash
curl -X POST https://dwdytxxqcvlrmyemonxz.supabase.co/functions/v1/create-demo-session \
  -H "Authorization: Bearer $SUPABASE_ANON_KEY" \
  -H "Content-Type: application/json" \
  -d '{"session_type":"live_demo"}'
```

## Switching to Cloud Deployment

When ready to test with cloud Motia:

### 1. Deploy Motia with Environment Variables

```bash
cd apps/slide-system

pnpx motia cloud deploy \
  -k $MOTIA_API_KEY \
  -v "v1.0.x" \
  -s "c58ebdb4-aeab-472a-b399-808b1a11e4d7" \
  --env-file .env.motia
```

**Important:** Wait 1-2 minutes between deployments to avoid rate limiting (429 errors).

### 2. Update Edge Function Secret

```bash
cd .claude/supabase

supabase secrets set \
  MOTIA_API_URL="https://7vvifh-m1ppeh.arsell-0maz8f95q3.motia.cloud/demo-sessions" \
  --project-ref dwdytxxqcvlrmyemonxz
```

### 3. Verify Deployment

```bash
# Test Edge Function
curl -X POST https://dwdytxxqcvlrmyemonxz.supabase.co/functions/v1/create-demo-session \
  -H "Authorization: Bearer $SUPABASE_ANON_KEY" \
  -H "Content-Type: application/json" \
  -d '{"session_type":"live_demo"}'
```

## Troubleshooting

### Motia Step Not Responding

1. **Check if Motia dev server is running:**
   ```bash
   curl http://localhost:3005/demo-sessions
   ```

2. **Check environment variables:**
   ```bash
   echo $DAILY_API_KEY
   echo $SUPABASE_URL
   ```

3. **View Motia logs:**
   - Check terminal running `pnpx motia dev`

### Edge Function Errors

1. **View Edge Function logs:**
   - Go to Supabase Dashboard → Edge Functions → create-demo-session → Logs

2. **Check secrets are set:**
   ```bash
   supabase secrets list --project-ref dwdytxxqcvlrmyemonxz
   ```

3. **Verify CORS:**
   ```bash
   curl -X OPTIONS https://dwdytxxqcvlrmyemonxz.supabase.co/functions/v1/create-demo-session \
     -H "Origin: https://example.com" -i
   ```

### Tunnel Issues

**Cloudflare Tunnel connection timeout:**
- Check your firewall settings
- Try authenticated tunnel instead of quick tunnel
- Verify local Motia server is running: `curl http://localhost:3005/demo-sessions`

**ngrok connection refused:**
- Ensure ngrok is authenticated: `ngrok config check`
- Check port 3005 is accessible locally

### Daily.co API Errors

**401 Authentication Error:**
- Verify `DAILY_API_KEY` in `.env.motia` is correct
- Check key hasn't expired in Daily.co dashboard

**Room Creation Failures:**
- Check Daily.co account limits
- Verify room name doesn't conflict with existing rooms

### Supabase Errors

**400 Bad Request (constraint violation):**
- Check `session_type` is one of: `live_demo`, `test`, `replay`, `preview`
- Verify database schema has proper constraints

**403 Forbidden:**
- Check RLS policies on `conversation_sessions` table
- Verify service role key is correct

## Development Best Practices

1. **Always test locally first** before deploying to cloud
2. **Use environment files** (`.env.motia`) for secrets, never commit them
3. **Wait between Motia deployments** to avoid rate limiting
4. **Check logs** when debugging issues
5. **Test with different session types** to verify validation

## Architecture

```
Client Request
    ↓
Supabase Edge Function (HTTPS)
    ↓
[Local Dev: Tunnel → localhost:3005]
[Production: Direct HTTPS to Motia Cloud]
    ↓
Motia Step (demo-session-orchestration)
    ↓
Daily.co (create room) + Supabase (save session)
    ↓
Response to Client
```

## Quick Reference

**Endpoints:**
- Local Motia: `http://localhost:3005/demo-sessions`
- Cloud Motia: `https://7vvifh-m1ppeh.arsell-0maz8f95q3.motia.cloud/demo-sessions`
- Edge Function: `https://dwdytxxqcvlrmyemonxz.supabase.co/functions/v1/create-demo-session`

**Valid Session Types:**
- `live_demo` - Live demonstration session
- `test` - Testing session
- `replay` - Replay session
- `preview` - Preview session

**Environment Variables:**
- `DAILY_API_KEY` - Daily.co API authentication
- `SUPABASE_URL` - Supabase project URL
- `SUPABASE_SERVICE_KEY` - Supabase service role key
- `MOTIA_API_URL` - Motia Step endpoint (Edge Function only)
- `ENVIRONMENT` - Environment name (Edge Function only)
