-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathGmailIngestServiceTest.php
More file actions
140 lines (119 loc) · 4.95 KB
/
GmailIngestServiceTest.php
File metadata and controls
140 lines (119 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace Tests\Unit\Support;
use App\Models\Support\SupportGmailCursor;
use App\Services\Support\CaseIntakeService;
use App\Services\Support\Gmail\GmailConnector;
use App\Services\Support\Gmail\GmailIngestService;
use App\Services\Support\Gmail\GmailMessage;
use App\Services\Support\SupportActionLogger;
use Illuminate\Support\Facades\Cache;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
final class GmailIngestServiceTest extends TestCase
{
use RefreshDatabase;
public function test_poll_and_ingest_creates_cases_and_dedupes(): void
{
config()->set('support_gmail.enabled', true);
config()->set('support_gmail.lock.name', 'test:support:gmail:poll');
config()->set('support_gmail.lock.ttl_seconds', 30);
config()->set('support_gmail.user', 'me');
config()->set('support_gmail.label', 'Support-AI');
config()->set('support_gmail.query', 'newer_than:7d');
$fakeConnector = new class implements GmailConnector {
public function fetchNewMessages(
string $mailbox,
?string $label,
string $query,
?string $sinceHistoryId,
int $max = 25,
): array {
return [
'messages' => [
new GmailMessage('m1', 't1', 'Subj 1', 'sender@example.com', "Hello 1"),
new GmailMessage('m1', 't1', 'Subj 1', 'sender@example.com', "Hello 1 DUP"),
new GmailMessage('m2', 't2', 'Subj 2', 'sender2@example.com', "Hello 2"),
],
'next_history_id' => '123',
'warnings' => [],
];
}
};
$svc = new GmailIngestService(
connector: $fakeConnector,
intake: app(CaseIntakeService::class),
logger: app(SupportActionLogger::class),
);
$res = $svc->pollAndIngest(25);
$this->assertSame(2, $res['ingested']);
$this->assertSame(1, $res['duplicates']);
$this->assertSame([], $res['warnings']);
$cursor = SupportGmailCursor::query()->where('mailbox', 'me')->where('label', 'Support-AI')->first();
$this->assertNotNull($cursor);
$this->assertSame('123', $cursor->last_history_id);
$this->assertNotNull($cursor->last_polled_at);
}
public function test_poll_skips_when_lock_is_held(): void
{
config()->set('support_gmail.enabled', true);
config()->set('support_gmail.lock.name', 'test:support:gmail:poll:held');
config()->set('support_gmail.lock.ttl_seconds', 30);
$lock = Cache::lock('test:support:gmail:poll:held', 30);
$this->assertTrue($lock->get());
$connector = new class implements GmailConnector {
public function fetchNewMessages(
string $mailbox,
?string $label,
string $query,
?string $sinceHistoryId,
int $max = 25,
): array {
throw new \RuntimeException('connector_should_not_be_called');
}
};
$svc = new GmailIngestService(
connector: $connector,
intake: app(CaseIntakeService::class),
logger: app(SupportActionLogger::class),
);
$res = $svc->pollAndIngest(25);
$this->assertSame(['ingested' => 0, 'duplicates' => 0, 'cursor_updated' => false, 'warnings' => []], $res);
$lock->release();
}
public function test_poll_passes_through_connector_warnings(): void
{
config()->set('support_gmail.enabled', true);
config()->set('support_gmail.lock.name', 'test:support:gmail:poll:warnings');
config()->set('support_gmail.lock.ttl_seconds', 30);
config()->set('support_gmail.user', 'me');
config()->set('support_gmail.label', 'Missing-Label');
config()->set('support_gmail.query', 'newer_than:7d');
$fakeConnector = new class implements GmailConnector {
public function fetchNewMessages(
string $mailbox,
?string $label,
string $query,
?string $sinceHistoryId,
int $max = 25,
): array {
return [
'messages' => [],
'next_history_id' => null,
'warnings' => [
'Configured Gmail label "Missing-Label" was not found; polling without label filter.',
],
];
}
};
$svc = new GmailIngestService(
connector: $fakeConnector,
intake: app(CaseIntakeService::class),
logger: app(SupportActionLogger::class),
);
$res = $svc->pollAndIngest(25);
$this->assertSame(
['Configured Gmail label "Missing-Label" was not found; polling without label filter.'],
$res['warnings'],
);
}
}