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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284"""Tests for TimeoutService - Approval timeout monitoring."""
from datetime import UTC, datetime, timedelta
from unittest.mock import AsyncMock
import pytest
from src.services.timeout_service import TimeoutService
@pytest.fixture
def mock_queue_service(mocker):
"""Create mock QueueService."""
service = mocker.AsyncMock()
service.get_expired_timeouts = AsyncMock(return_value=[])
service.remove_from_timeout_tracking = AsyncMock()
service.get_timeout_count = AsyncMock(return_value=0)
return service
@pytest.fixture
def mock_job_service(mocker):
"""Create mock JobService."""
service = mocker.AsyncMock()
service.get_job_status = AsyncMock(return_value=None)
service.update_job_status = AsyncMock()
return service
@pytest.fixture
def mock_cleanup_service(mocker):
"""Create mock CleanupService."""
service = mocker.AsyncMock()
service.cleanup_job_files = AsyncMock(return_value=True)
return service
@pytest.fixture
def mock_metrics_service(mocker):
"""Create mock MetricsService."""
service = mocker.AsyncMock()
service.increment_metric = AsyncMock()
return service
@pytest.fixture
def timeout_service(
mock_queue_service,
mock_job_service,
mock_cleanup_service,
mock_metrics_service
):
"""Create TimeoutService with mocked dependencies."""
return TimeoutService(
queue_service=mock_queue_service,
job_service=mock_job_service,
cleanup_service=mock_cleanup_service,
metrics_service=mock_metrics_service
)
@pytest.fixture
def expired_job_data():
"""Sample job data for expired approval."""
return {
"job_id": "test-job-123",
"status": "awaiting_approval",
"s3_key": "temp/test-job-123.pdf",
"created_at": (datetime.now(UTC) - timedelta(hours=5)).isoformat(),
"pii_findings": '[{"type": "SSN", "confidence": 0.9}]'
}
@pytest.fixture
def completed_job_data():
"""Sample job data for already completed job."""
return {
"job_id": "test-job-456",
"status": "completed",
"s3_key": "temp/test-job-456.pdf",
"created_at": (datetime.now(UTC) - timedelta(hours=1)).isoformat(),
"result_url": "https://s3.example.com/results/test-job-456/output.md"
}
class TestProcessExpiredApprovals:
"""Tests for process_expired_approvals method."""
@pytest.mark.asyncio
async def test_no_expired_approvals(
self,
timeout_service,
mock_queue_service
):
"""Test when no approvals have expired."""
mock_queue_service.get_expired_timeouts.return_value = []
result = await timeout_service.process_expired_approvals()
assert result == 0
mock_queue_service.get_expired_timeouts.assert_called_once()
@pytest.mark.asyncio
async def test_process_single_expired_approval(
self,
timeout_service,
mock_queue_service,
mock_job_service,
mock_cleanup_service,
mock_metrics_service,
expired_job_data
):
"""Test processing a single expired approval."""
mock_queue_service.get_expired_timeouts.return_value = [("test-job-123", 1609459200.0)]
mock_job_service.get_job_status.return_value = expired_job_data
result = await timeout_service.process_expired_approvals()
assert result == 1
mock_job_service.update_job_status.assert_called_once_with(
"test-job-123",
status="failed",
error_message="Approval deadline exceeded - no response received"
)
mock_cleanup_service.cleanup_job_files.assert_called_once_with("temp/test-job-123.pdf")
mock_queue_service.remove_from_timeout_tracking.assert_called_once_with(
"test-job-123"
)
mock_metrics_service.increment_metric.assert_called_with(
"approval_timeouts_processed", 1
)
@pytest.mark.asyncio
async def test_process_multiple_expired_approvals(
self,
timeout_service,
mock_queue_service,
mock_job_service,
expired_job_data
):
"""Test processing multiple expired approvals."""
mock_queue_service.get_expired_timeouts.return_value = [
("job-1", 1609459200.0),
("job-2", 1609459300.0),
("job-3", 1609459400.0)
]
mock_job_service.get_job_status.return_value = expired_job_data
result = await timeout_service.process_expired_approvals()
assert result == 3
assert mock_job_service.update_job_status.call_count == 3
@pytest.mark.asyncio
async def test_skip_already_processed_job(
self,
timeout_service,
mock_queue_service,
mock_job_service,
completed_job_data
):
"""Test skipping jobs that are no longer awaiting_approval."""
mock_queue_service.get_expired_timeouts.return_value = [("test-job-456", 1609459200.0)]
mock_job_service.get_job_status.return_value = completed_job_data
result = await timeout_service.process_expired_approvals()
# Should not process, but should remove from timeout tracking
assert result == 0
mock_job_service.update_job_status.assert_not_called()
mock_queue_service.remove_from_timeout_tracking.assert_called_once_with(
"test-job-456"
)
@pytest.mark.asyncio
async def test_handle_missing_job(
self,
timeout_service,
mock_queue_service,
mock_job_service
):
"""Test handling job that doesn't exist in Redis."""
mock_queue_service.get_expired_timeouts.return_value = [("missing-job", 1609459200.0)]
mock_job_service.get_job_status.return_value = None
result = await timeout_service.process_expired_approvals()
# Should remove from tracking but not process
assert result == 0
mock_queue_service.remove_from_timeout_tracking.assert_called_once_with(
"missing-job"
)
@pytest.mark.asyncio
async def test_cleanup_failure_continues_processing(
self,
timeout_service,
mock_queue_service,
mock_job_service,
mock_cleanup_service,
expired_job_data
):
"""Test that cleanup failure doesn't prevent status update."""
mock_queue_service.get_expired_timeouts.return_value = [("test-job-123", 1609459200.0)]
mock_job_service.get_job_status.return_value = expired_job_data
mock_cleanup_service.cleanup_denied_job.return_value = False
result = await timeout_service.process_expired_approvals()
# Should still count as processed even if cleanup failed
assert result == 1
mock_job_service.update_job_status.assert_called_once()
@pytest.mark.asyncio
async def test_partial_failure_continues_processing(
self,
timeout_service,
mock_queue_service,
mock_job_service,
expired_job_data
):
"""Test that one job failure doesn't stop processing others."""
mock_queue_service.get_expired_timeouts.return_value = [
("job-1", 1609459200.0),
("job-2", 1609459300.0),
("job-3", 1609459400.0)
]
# First job fails, others succeed
def get_job_side_effect(job_id):
if job_id == "job-1":
raise Exception("Redis error")
return expired_job_data
mock_job_service.get_job_status.side_effect = get_job_side_effect
result = await timeout_service.process_expired_approvals()
# Should process 2 out of 3
assert result == 2
class TestGetPendingApprovalCount:
"""Tests for get_pending_approval_count method."""
@pytest.mark.asyncio
async def test_get_count_success(
self,
timeout_service,
mock_queue_service
):
"""Test getting pending approval count."""
mock_queue_service.get_timeout_count.return_value = 5
result = await timeout_service.get_pending_approval_count()
assert result == 5
mock_queue_service.get_timeout_count.assert_called_once()
@pytest.mark.asyncio
async def test_get_count_zero(
self,
timeout_service,
mock_queue_service
):
"""Test getting count when no approvals pending."""
mock_queue_service.get_timeout_count.return_value = 0
result = await timeout_service.get_pending_approval_count()
assert result == 0
@pytest.mark.asyncio
async def test_get_count_error_returns_zero(
self,
timeout_service,
mock_queue_service
):
"""Test that errors return 0 instead of raising."""
mock_queue_service.get_timeout_count.side_effect = Exception("Redis error")
result = await timeout_service.get_pending_approval_count()
assert result == 0