# Chat Widget Testbook

Validates the nimschatwidget integration on the issue detail page.

## Prerequisites

- nimsforestissue running with NATS connected
- Forest pipeline running (river → tree → nim → agentclaudecode → songbird)
- Admin session (logged in at /login)

## 1. Verify deployment

```bash
# Songbird is catching and publishing to river
ssh root@46.225.164.179 "docker logs nimsforestissue 2>&1 | grep 'Chat widget'"
# Expected: Chat widget enabled (publishing to river.chat.widget)
```

## 2. Widget endpoint auth

```bash
# Without auth → 401
curl -s -o /dev/null -w "%{http_code}" https://issues.nimsforest.nimsforest.com/admin/chat/widget
# Expected: 401

# With auth → 200 + JS content
curl -s -H "Cookie: nimsforestissue_session=<admin_token>" https://issues.nimsforest.nimsforest.com/admin/chat/widget | head -c 50
# Expected: (function() {
```

## 3. Nims list endpoint

```bash
curl -s -H "Cookie: nimsforestissue_session=<admin_token>" https://issues.nimsforest.nimsforest.com/admin/chat/nims | python3 -m json.tool | head -10
# Expected: JSON array of {name, role} objects starting with neo
```

## 4. SSE events endpoint

```bash
# Opens SSE stream (will hang waiting for events, Ctrl+C to stop)
curl -s -N -H "Cookie: nimsforestissue_session=<admin_token>" "https://issues.nimsforest.nimsforest.com/admin/chat/events?session=test-1"
# Expected: connection stays open, receives data: lines when nim responds
```

## 5. Send endpoint

```bash
curl -s -X POST -H "Content-Type: application/json" \
  -H "Cookie: nimsforestissue_session=<admin_token>" \
  -d '{"session_id":"test-1","target_nim":"nimble","text":"hello","context":"Test context"}' \
  https://issues.nimsforest.nimsforest.com/admin/chat/send
# Expected: {"status":"ok"}
```

## 6. Browser validation

1. Open https://issues.nimsforest.nimsforest.com/login
2. Log in with admin token
3. Navigate to any issue detail: /admin/issues/{id}
4. **Verify**: green floating chat button visible in bottom-right corner
5. Click the chat button
6. **Verify**: slide panel opens from right side with nim selector dropdown
7. **Verify**: dropdown lists all nims (neo, nimble, napoleon, etc.)
8. Select a nim (e.g. neo)
9. Type a message and press Enter
10. **Verify**: user message appears as green bubble on right
11. **Verify**: typing indicator appears (3 bouncing dots)
12. **Verify**: nim response arrives as white bubble on left with nim name
13. Click the X button to close panel
14. **Verify**: panel slides closed

## 7. Context verification

1. Open an issue detail page with a known description
2. Open browser DevTools → Network tab
3. Open chat widget and send a message
4. Inspect the POST to /admin/chat/send
5. **Verify**: request body contains `context` field with issue title, status, priority, and description
6. **Verify**: the nim's response acknowledges the issue context

## 8. SSE reconnect

1. Open chat widget on an issue page
2. Kill the nimsforestissue container: `docker restart nimsforestissue`
3. **Verify**: SSE reconnects after ~3 seconds (check Network tab)
4. Send a new message
5. **Verify**: message sends and response arrives normally

## 9. Mobile layout

1. Open issue detail on mobile (or resize browser to < 768px)
2. **Verify**: chat button is smaller (48px) and positioned 16px from edges
3. Click to open
4. **Verify**: panel is full-width overlay instead of 400px side panel

## 10. Chat disabled (no NATS)

If NATS is not configured, the chat widget should not appear. Verify by checking logs — if "Wind connected" is absent, no chat button should render on issue detail pages.
