# Real reply check: does a CRM reply land in the same Gmail thread?

Replying from the CRM (HUB-166, `POST /api/v1/inbox/threads/:id/reply`) sends the reply with
the Gmail thread id and with `In-Reply-To` / `References` set from the thread's last message, so
Gmail keeps it in the conversation. The tests use a fake Gmail, so this has to be checked once
with a real mailbox. Run it on staging, or locally with the Google app's settings. It needs the
same setup as [the real send check](gmail-real-send-check.md) (API and worker running, a
connected Gmail mailbox, a second address, database access).

## Steps

1. **Start a conversation.** From your second address, send an email with a small PDF attached
   to the connected mailbox, subject `CRM reply check <today's date>`. Wait for the incremental
   sync (at most a few minutes) until `GET /api/v1/inbox/threads?folder=inbox` shows it. Note
   the thread `id`.

2. **Reply.** `POST /api/v1/inbox/threads/<thread id>/reply`:

   ```json
   {
     "mode": "reply",
     "bodyHtml": "<p>Real reply check (HUB-166).</p>",
     "idempotencyKey": "real-reply-check-0001"
   }
   ```

   The answer is `202`; note the `id`. Within a minute `GET /api/v1/inbox/threads/<thread id>`
   shows it as the last message with `sendStatus: "sent"`.

3. **Check Gmail.** In the connected mailbox, the conversation shows the original email and the
   reply **as one conversation** (not two). In the second address's mailbox the reply also
   arrives in the same conversation. Open the reply → ⋮ → **Show original**: `In-Reply-To:` is
   the original email's Message-ID, and `References:` ends with it. **Take a screenshot.**

4. **Reply all.** Send yourself (second address) a new email to the mailbox with a third
   address in Cc. After the sync, reply with `"mode": "reply_all"` and a new idempotency key.
   The reply goes to the second and third addresses and not to the connected mailbox itself.

5. **Forward.** On the thread from step 1, `"mode": "forward"` with
   `"to": ["<a third address>"]` and a new idempotency key. The forward arrives with the PDF
   attached, and it opens correctly.

6. **One copy in the CRM.** After the next sync:

   ```sql
   SELECT id, send_status, provider_message_id, thread_id
   FROM email_messages
   WHERE thread_id = '<thread id>'
   ORDER BY created_at;
   ```

   Each email shows once (the reply and the forward rows now have a `provider_message_id`), all
   in the same `thread_id`, and the thread is still in the inbox folder.

7. **Double send.** Send the request from step 2 again, same `idempotencyKey`: `200` with the
   same `id`, and Gmail shows the reply once.

## Result

Fill this in and attach the screenshot to the ticket (HUB-166).

|                                                        |                     |
| ------------------------------------------------------ | ------------------- |
| Date, environment                                      |                     |
| Who ran it                                             |                     |
| Reply in the same Gmail conversation (step 3)?         |                     |
| In-Reply-To / References as expected (step 3)?         |                     |
| Reply all left out the mailbox's own address (step 4)? |                     |
| Forward arrived with the attachment (step 5)?          |                     |
| One row per email, same CRM thread (step 6)?           |                     |
| Repeat with the same key sent a second copy? (step 7)  |                     |
| Screenshot                                             | attached to HUB-166 |
