# Mailpit Testing Guide for NOTIF-DECOMP

## Status: Mailpit Running ✅

- **SMTP Server:** localhost:1025
- **Web UI:** http://localhost:8025

## Quick Test (Main Worktree)

Since the notif-decomp worktree requires Docker MySQL, test from main worktree instead:

```bash
cd /Users/charlie/code/showprima

# 1. Temporarily update .env
MAIL_MAILER=smtp
MAIL_HOST=127.0.0.1
MAIL_PORT=1025
MAIL_FROM_ADDRESS=test@showprima.test

# 2. Test basic email
php artisan tinker
> Mail::raw('Test email', fn($m) => $m->to('test@example.com')->subject('Test'));

# 3. Check Mailpit: http://localhost:8025
```

## Test NotificationService Features

```bash
php artisan tinker

# Get a test order
> $order = App\Model\Order::first();

# Test order confirmation (with tracking)
> $service = app(App\Domains\Notifications\Services\NotificationService::class);
> $result = $service->sendOrderConfirmation($order);

# Check EmailLog
> $log = App\Domains\Notifications\Models\EmailLog::latest()->first();
> $log->tracking_id; // Should have unique ID
> $log->sent_at;     // Should be populated

# Check email in Mailpit (http://localhost:8025)
```

## What to Verify in Mailpit

### 1. Tracking Pixel Embedded
Look for in email HTML:
```html
<img src="http://localhost:8000/api/emails/track-open/{tracking_id}" width="1" height="1" alt="" />
```

### 2. Links Rewritten for Click Tracking
Original links should be rewritten:
```html
<!-- Before -->
<a href="https://example.com">Click here</a>

<!-- After -->
<a href="http://localhost:8000/api/emails/track-click/{tracking_id}?url=https%3A%2F%2Fexample.com">Click here</a>
```

### 3. Preserved Links (NOT rewritten)
- `mailto:support@showprima.com`
- `tel:+1234567890`
- `#anchor-links`
- `javascript:void(0)`

### 4. Test Click Tracking
1. Click a rewritten link in Mailpit
2. Should 302 redirect to original URL
3. Check EmailLog updated:
```php
> $log->refresh();
> $log->clicked_at;    // Should be populated
> $log->clicked_link;  // Original URL
```

### 5. Test Open Tracking
1. Load tracking pixel (view email in Mailpit)
2. Check EmailLog:
```php
> $log->refresh();
> $log->opened_at;  // Should be populated (idempotent - only first open)
```

## Test OutboxService

```php
# Get failed emails
> $outbox = app(App\Domains\Notifications\Services\OutboxService::class);
> $outbox->getFailedEmails();
> $outbox->getOutboxStats();

# Retry a failed email
> $failed = App\Domains\Notifications\Models\EmailLog::where('status', 'failed')->first();
> $outbox->retryEmail($failed);
```

## Test TrackingService

```php
> $tracking = app(App\Domains\Notifications\Services\TrackingService::class);
> $tracking->getEmailTracking($log);
// Returns: sent_at, delivered_at, opened_at, clicked_at stats
```

## Cleanup

```bash
# Stop Mailpit when done
pkill mailpit

# Revert .env changes
git checkout .env
```

## Expected Results

✅ Email appears in Mailpit immediately (QUEUE_CONNECTION=sync)
✅ Tracking pixel embedded before `</body>` tag
✅ All http/https links rewritten with tracking URLs
✅ EmailLog.tracking_id populated
✅ Click → 302 redirect → clicked_at updated
✅ Open → opened_at updated (idempotent)

## Troubleshooting

**Email not appearing:**
- Check Mailpit is running: `mailpit readyz`
- Check .env: `MAIL_MAILER=smtp`, `MAIL_HOST=127.0.0.1`, `MAIL_PORT=1025`
- Check logs: `tail -f storage/logs/laravel.log`

**Tracking pixel missing:**
- Check EmailService: `MAIL_TRACK_CLICKS=true` in .env
- Check EmailLog has tracking_id
- Check email is HTML (not plain text)

**Links not rewritten:**
- Check config: `config('mail.track_clicks')` should be true
- Check LinkRewriter is called in EmailService
- Only http/https links are rewritten

