# How to Test Option A (Phases 1–3)

Step-by-step testing for the Integration API, Connect WhatsApp UI, and “no business” guards.

---

## Prerequisites

1. **Database** – Backend must connect. If you use a remote DB, ensure it’s reachable (see [TEST_WHATSAPP_WEBHOOK.md](./TEST_WHATSAPP_WEBHOOK.md)).
2. **Backend** – From repo root:
   ```bash
   cd backend/unified_server
   npm run dev
   ```
   Expect: server listening (e.g. port 5000), no Prisma/DB errors.
3. **Frontend** – In another terminal:
   ```bash
   cd frontend
   npm run dev
   ```
   Open the app (e.g. `http://localhost:5173` or the port Vite shows).
4. **Test user** – One user **with** a business (for Phase 1 & 2), and optionally one **without** a business (for Phase 3).

---

## Phase 1: Backend Integration API

### 1.1 Get a valid token and IDs

- Log in via the frontend (or use an existing session).
- In the browser:
  - **F12** → **Application** (Chrome) or **Storage** (Firefox) → **Local Storage** → your app origin.
  - Copy:
    - `token` or `access_token` (JWT string)
    - `business_id` (UUID of the business)
    - From the `user` object (also in Local Storage): `user.id` → use as `user_id` (or copy from elsewhere if you store `user_id` separately).
- Or from a user that owns a business: get their UUID and a business UUID from your DB.

**Quick test with script (Phase 1):**  
From `backend/unified_server` run (replace with your values):
```bash
node scripts/test_integration_api.js YOUR_JWT_TOKEN YOUR_USER_UUID YOUR_BUSINESS_UUID
```
Or set env vars and run `npm run test:integration`. See `scripts/test_integration_api.js` for details.

### 1.2 GET WhatsApp status

**curl (replace placeholders):**
```bash
curl -X GET "http://localhost:5000/api/v1/integrations/whatsapp/status?businessId=YOUR_BUSINESS_ID&user_id=YOUR_USER_ID" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"
```

**PowerShell:**
```powershell
$token = "YOUR_JWT_TOKEN"
$businessId = "YOUR_BUSINESS_ID"
$userId = "YOUR_USER_ID"
Invoke-RestMethod -Uri "http://localhost:5000/api/v1/integrations/whatsapp/status?businessId=$businessId&user_id=$userId" -Headers @{ Authorization = "Bearer $token" }
```

**Expected:** `200`, body like:
```json
{
  "STATUS": "success",
  "MESSAGE": "Status retrieved",
  "DATA": { "connected": false, "integration_id": null, "phone_number_id_mask": null }
}
```
If already connected: `connected: true` and a masked phone number.

### 1.3 POST Connect WhatsApp

**curl:**
```bash
curl -X POST "http://localhost:5000/api/v1/integrations/whatsapp/connect" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d "{\"businessId\":\"YOUR_BUSINESS_ID\",\"phone_number_id\":\"123456789012345\",\"access_token\":\"YOUR_META_TOKEN\",\"user_id\":\"YOUR_USER_ID\"}"
```

**PowerShell:**
```powershell
$body = @{
  businessId      = "YOUR_BUSINESS_ID"
  phone_number_id = "123456789012345"
  access_token    = "YOUR_META_ACCESS_TOKEN"
  user_id         = "YOUR_USER_ID"
} | ConvertTo-Json
Invoke-RestMethod -Uri "http://localhost:5000/api/v1/integrations/whatsapp/connect" -Method Post -Body $body -ContentType "application/json" -Headers @{ Authorization = "Bearer $token" }
```

**Expected:** `200`, body like:
```json
{
  "STATUS": "success",
  "MESSAGE": "WhatsApp connected successfully",
  "DATA": { "integration_id": "...", "business_id": "...", "phone_number_id": "...", "connected": true }
}
```

- Use a **real** Phone Number ID and Access Token from [Meta Developer Dashboard](https://developers.facebook.com/) → Your App → WhatsApp → API Setup if you want to test real messaging later.
- For a quick backend-only check, any non-empty `phone_number_id` and `access_token` are enough; the backend will store them.

### 1.4 Verify in DB (optional)

- Check `integrations`: one row with `integration_type = 'whatsapp'`, `business_id = YOUR_BUSINESS_ID`.
- Check `api_keys`: one active row linked to that integration with `phone_number_id` and `access_token` set.

---

## Phase 2: Frontend – Connect WhatsApp (Settings → Channels)

1. **Log in** with a user that **has** a business.
2. Open **Settings** (sidebar or profile).
3. Open the **Channels** tab.
4. **Status** – You should see either:
   - “Not connected”, or  
   - “Connected (xxxx…yyyy)” if you already connected via API/earlier test.
5. **Connect:**
   - Enter **Phone Number ID** (from Meta → WhatsApp → API Setup).
   - Enter **Access Token** (same place; paste carefully).
   - Click **“Connect WhatsApp”**.
6. **Expected:** Success toast and status changes to “Connected” (with mask). Form fields clear.
7. **Reconnect:** Change token or same credentials and click **“Reconnect WhatsApp”** – should succeed and form clear again.
8. **Error case:** Use invalid token or leave a field empty – you should see an error message and no “Connected” state.

---

## Phase 3: No-business guards

Use a user that has **no** business (no business created, or use an account that never had one).

### 3.1 Inbox

1. Log in with the “no business” user.
2. Go to **Inbox**.
3. **Expected:** You see a card: “Create a business to use the Inbox” with:
   - “Go to Settings → Workspace” and “Or create a new business →”.
   - No conversation list or chat UI.

### 3.2 Contacts

1. Same user, go to **Contacts**.
2. **Expected:** Same style card: “Create a business to manage Contacts” and the same two links.

### 3.3 Dashboard banner

1. Same user, go to **Dashboard**.
2. **Expected:** A banner at the top: “Create a business to use Inbox, Contacts, and connect WhatsApp” with a **“Create business”** button.
3. Click **“Create business”** – should go to `/business-info`.

### 3.4 After creating a business

1. Create a business (via **Settings → Workspace** or **/business-info**).
2. **Expected:**
   - **Inbox** – Normal inbox (conversation list + chat).
   - **Contacts** – Normal contacts list.
   - **Dashboard** – No “Create a business” banner.

---

## Optional: End-to-end (real WhatsApp)

After Phase 1 & 2 are verified:

1. **Connect** your real WhatsApp number in Settings → Channels (real Phone Number ID + token from Meta).
2. **Webhook** – In Meta → WhatsApp → Configuration, set:
   - Callback URL: `https://your-backend-domain/api/v1/webhooks/whatsapp`
   - Verify token: same as in your backend `.env` (`WEBHOOK_VERIFY_TOKEN` / `META_WEBHOOK_VERIFY_TOKEN`).
3. Send a message **to** your WhatsApp number from another phone; check:
   - Backend logs for webhook POST.
   - Inbox for the new conversation/message.
4. From **Inbox**, reply to that conversation; the contact should receive the message on WhatsApp.

For **local** webhook testing (no public URL), see [TEST_WHATSAPP_WEBHOOK.md](./TEST_WHATSAPP_WEBHOOK.md) (script or ngrok).

---

## Quick checklist

| Step | What to do | Pass? |
|------|------------|--------|
| P1 | Backend runs, GET status returns 200 with `connected` | ☐ |
| P1 | POST connect with valid body returns 200, DB has integration + api_key | ☐ |
| P2 | Settings → Channels shows status and Connect form | ☐ |
| P2 | Connect WhatsApp succeeds and shows “Connected” | ☐ |
| P3 | No-business user: Inbox shows “Create a business” guard | ☐ |
| P3 | No-business user: Contacts shows guard | ☐ |
| P3 | No-business user: Dashboard shows banner + Create business button | ☐ |
| P3 | After creating business, guards/banner gone, Inbox/Contacts work | ☐ |
