Phase 2: Implement Domain & Mailbox persistence, Analytics, and fix pagination issues
This commit is contained in:
@@ -2,90 +2,98 @@
|
||||
|
||||
use App\Events\NewEmailReceived;
|
||||
use App\Jobs\ProcessIncomingEmail;
|
||||
use App\Models\Domain;
|
||||
use App\Models\Email;
|
||||
use App\Models\EmailBody;
|
||||
use App\Models\Mailbox;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
|
||||
it('stores incoming email in MariaDB and MongoDB, and broadcasts event', function () {
|
||||
test('it processes incoming email and tracks analytics', function () {
|
||||
Event::fake();
|
||||
Queue::fake();
|
||||
|
||||
$hash = 'test-hash-'.time();
|
||||
$domain = Domain::factory()->create([
|
||||
'name' => 'test-ingestion.com',
|
||||
]);
|
||||
|
||||
$mailbox = Mailbox::factory()->create([
|
||||
'address' => 'user@test-ingestion.com',
|
||||
'domain_hash' => $domain->domain_hash,
|
||||
]);
|
||||
|
||||
Log::info('Mailbox created in test', [
|
||||
'id' => $mailbox->id,
|
||||
'address' => $mailbox->address,
|
||||
'domain_hash' => $mailbox->domain_hash,
|
||||
]);
|
||||
|
||||
$hash = 'ingestion-hash-'.uniqid();
|
||||
$payload = [
|
||||
'hash' => $hash,
|
||||
'metadata' => [
|
||||
'recipientEmail' => 'test@imail.app',
|
||||
'recipientName' => 'Test User',
|
||||
'senderEmail' => 'sender@example.com',
|
||||
'senderName' => 'Sender Name',
|
||||
'domain' => 'imail.app',
|
||||
'subject' => 'Test Subject',
|
||||
'recipientEmail' => 'user@test-ingestion.com',
|
||||
'senderEmail' => 'sender@other.com',
|
||||
'domain' => 'test-ingestion.com',
|
||||
'subject' => 'Integration Test',
|
||||
'received_at' => now()->toIso8601String(),
|
||||
'attachmentSize' => 1024,
|
||||
'attachments' => [
|
||||
['filename' => 'test.pdf', 'mimeType' => 'application/pdf', 'size' => 1024],
|
||||
],
|
||||
],
|
||||
'bodyText' => 'This is the plain text body format.',
|
||||
'bodyHtml' => '<html><body><p>This is the HTML body format.</p></body></html>',
|
||||
'bodyText' => 'Testing ingestion.',
|
||||
'bodyHtml' => '<p>Testing ingestion.</p>',
|
||||
];
|
||||
|
||||
Log::info('Mailboxes in DB before job', ['count' => Mailbox::count(), 'addresses' => Mailbox::pluck('address')->toArray()]);
|
||||
|
||||
$job = new ProcessIncomingEmail($payload);
|
||||
$job->handle();
|
||||
|
||||
// Verify MariaDB storage
|
||||
// MariaDB Email record
|
||||
$this->assertDatabaseHas('emails', [
|
||||
'unique_id_hash' => $hash,
|
||||
'recipient_email' => 'test@imail.app',
|
||||
'domain' => 'imail.app',
|
||||
'subject' => 'Test Subject',
|
||||
'preview' => 'This is the plain text body format.',
|
||||
'attachment_size' => 1024,
|
||||
'recipient_email' => 'user@test-ingestion.com',
|
||||
]);
|
||||
|
||||
$email = Email::where('unique_id_hash', $hash)->first();
|
||||
expect($email->attachments_json)->toHaveCount(1)
|
||||
->and($email->attachments_json[0]['filename'])->toBe('test.pdf');
|
||||
|
||||
// Verify MongoDB storage
|
||||
$body = EmailBody::where('unique_id_hash', $hash)->first();
|
||||
expect($body)->not->toBeNull()
|
||||
->and($body->body_text)->toBe('This is the plain text body format.')
|
||||
->and($body->body_html)->toBe('<html><body><p>This is the HTML body format.</p></body></html>');
|
||||
|
||||
// Verify Broadcast Event
|
||||
Event::assertDispatched(NewEmailReceived::class, function ($event) use ($hash) {
|
||||
return $event->email->unique_id_hash === $hash;
|
||||
// Analytics Job
|
||||
Queue::assertPushed(\App\Jobs\TrackAnalytics::class, function ($job) use ($mailbox) {
|
||||
return $job->eventType === 'email_received' && $job->mailboxHash === $mailbox->mailbox_hash;
|
||||
});
|
||||
|
||||
// Cleanup MongoDB (MariaDB is handled by RefreshDatabase if used, but let's be safe)
|
||||
// MongoDB Body
|
||||
$body = EmailBody::where('unique_id_hash', $hash)->first();
|
||||
expect($body)->not->toBeNull()
|
||||
->and($body->body_text)->toBe('Testing ingestion.');
|
||||
|
||||
// Event
|
||||
Event::assertDispatched(NewEmailReceived::class);
|
||||
|
||||
$body->delete();
|
||||
});
|
||||
|
||||
it('generates preview from stripped HTML if text body is missing', function () {
|
||||
test('it handles emails for unknown mailboxes without failing', function () {
|
||||
Event::fake();
|
||||
Queue::fake();
|
||||
|
||||
$hash = 'test-hash-html-only-'.time();
|
||||
$hash = 'unknown-hash-'.uniqid();
|
||||
$payload = [
|
||||
'hash' => $hash,
|
||||
'metadata' => [
|
||||
'recipientEmail' => 'test2@imail.app',
|
||||
'senderEmail' => 'sender2@example.com',
|
||||
'domain' => 'imail.app',
|
||||
'recipientEmail' => 'nonexistent@test-ingestion.com',
|
||||
'senderEmail' => 'sender@other.com',
|
||||
'domain' => 'test-ingestion.com',
|
||||
'received_at' => now()->toIso8601String(),
|
||||
],
|
||||
'bodyText' => null,
|
||||
'bodyHtml' => '<html><body><h1>Welcome</h1><p>This is a <strong>strong</strong> test.</p> <br> <p>Footer</p></body></html>',
|
||||
'bodyText' => 'Testing unknown.',
|
||||
];
|
||||
|
||||
$job = new ProcessIncomingEmail($payload);
|
||||
$job->handle();
|
||||
|
||||
// Verify MariaDB storage preview logic
|
||||
$this->assertDatabaseHas('emails', [
|
||||
'unique_id_hash' => $hash,
|
||||
'preview' => 'Welcome This is a strong test. Footer',
|
||||
]);
|
||||
|
||||
// Cleanup MongoDB
|
||||
EmailBody::where('unique_id_hash', $hash)->delete();
|
||||
Queue::assertPushed(\App\Jobs\TrackAnalytics::class, function ($job) {
|
||||
return $job->mailboxHash === 'unknown';
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user