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
|
import asyncio
import os
from unittest.mock import AsyncMock, MagicMock, patch
from app.storage import TranscriptStore
from app.transcriber import (
IPBlockedError,
TranscriptDisabledError,
)
from app.worker import process_next
def _to_thread_passthrough(func, *args, **kwargs):
"""Replacement for asyncio.to_thread that runs the function synchronously."""
async def _coro():
return func(*args, **kwargs)
return _coro()
def _patch_to_thread():
return patch("app.worker.asyncio.to_thread", new=_to_thread_passthrough)
class TestProcessNext:
def test_process_next_returns_false_when_queue_empty(self, tmp_path) -> None:
async def _run() -> None:
db_path = os.path.join(str(tmp_path), "test.db")
store = TranscriptStore(db_path=db_path)
await store.initialize()
with patch("app.worker.asyncio.sleep", new_callable=AsyncMock) as mock_sleep:
api = MagicMock()
result = await process_next(store, api)
assert result is False
mock_sleep.assert_not_called()
await store.close()
asyncio.run(_run())
def test_process_next_success(self, tmp_path) -> None:
async def _run() -> None:
db_path = os.path.join(str(tmp_path), "test.db")
store = TranscriptStore(db_path=db_path)
await store.initialize()
entry = await store.enqueue("vid_001")
with patch("app.worker.asyncio.sleep", new_callable=AsyncMock) as mock_sleep, \
_patch_to_thread(), \
patch("app.worker.fetch_transcript_by_id", return_value=[{"text": "hello", "start": 0.0, "duration": 1.0}]):
api = MagicMock()
result = await process_next(store, api)
assert result is True
mock_sleep.assert_called_once()
slept_for = mock_sleep.call_args.args[0]
assert 30.0 <= slept_for <= 60.0
assert abs(slept_for - entry["assigned_delay"]) < 0.001
transcript = await store.get_transcript("vid_001")
assert transcript is not None
assert transcript["full_text"] == "hello"
assert await store.get_queue_entry("vid_001") is None
await store.close()
asyncio.run(_run())
def test_process_next_ip_blocked(self, tmp_path) -> None:
async def _run() -> None:
db_path = os.path.join(str(tmp_path), "test.db")
store = TranscriptStore(db_path=db_path)
await store.initialize()
await store.enqueue("vid_001")
with patch("app.worker.asyncio.sleep", new_callable=AsyncMock), \
_patch_to_thread(), \
patch("app.worker.fetch_transcript_by_id", side_effect=IPBlockedError("blocked")):
api = MagicMock()
result = await process_next(store, api)
assert result is True
entry = await store.get_queue_entry("vid_001")
assert entry is not None
assert entry["status"] == "failed"
assert entry["error_type"] == "ip_blocked"
assert await store.get_transcript("vid_001") is None
await store.close()
asyncio.run(_run())
def test_process_next_transcript_disabled(self, tmp_path) -> None:
async def _run() -> None:
db_path = os.path.join(str(tmp_path), "test.db")
store = TranscriptStore(db_path=db_path)
await store.initialize()
await store.enqueue("vid_001")
with patch("app.worker.asyncio.sleep", new_callable=AsyncMock), \
_patch_to_thread(), \
patch("app.worker.fetch_transcript_by_id", side_effect=TranscriptDisabledError("disabled")):
api = MagicMock()
result = await process_next(store, api)
assert result is True
entry = await store.get_queue_entry("vid_001")
assert entry is not None
assert entry["status"] == "failed"
assert entry["error_type"] == "transcript_disabled"
await store.close()
asyncio.run(_run())
def test_process_next_downloads_before_sleeping(self, tmp_path) -> None:
async def _run() -> None:
db_path = os.path.join(str(tmp_path), "test.db")
store = TranscriptStore(db_path=db_path)
await store.initialize()
await store.enqueue("vid_001")
call_order = []
async def mock_sleep(seconds):
call_order.append("sleep")
def mock_fetch(video_id, api):
call_order.append("fetch")
return [{"text": "hello", "start": 0.0, "duration": 1.0}]
with patch("app.worker.asyncio.sleep", side_effect=mock_sleep), \
_patch_to_thread(), \
patch("app.worker.fetch_transcript_by_id", side_effect=mock_fetch):
api = MagicMock()
result = await process_next(store, api)
assert result is True
assert call_order == ["fetch", "sleep"]
await store.close()
asyncio.run(_run())
def test_process_next_sleeps_after_error(self, tmp_path) -> None:
async def _run() -> None:
db_path = os.path.join(str(tmp_path), "test.db")
store = TranscriptStore(db_path=db_path)
await store.initialize()
entry = await store.enqueue("vid_001")
with patch("app.worker.asyncio.sleep", new_callable=AsyncMock) as mock_sleep, \
_patch_to_thread(), \
patch("app.worker.fetch_transcript_by_id", side_effect=IPBlockedError("blocked")):
api = MagicMock()
result = await process_next(store, api)
assert result is True
mock_sleep.assert_called_once()
slept_for = mock_sleep.call_args.args[0]
assert abs(slept_for - entry["assigned_delay"]) < 0.001
entry = await store.get_queue_entry("vid_001")
assert entry is not None
assert entry["status"] == "failed"
await store.close()
asyncio.run(_run())
def test_process_next_sleeps_after_error_before_next_download(self, tmp_path) -> None:
async def _run() -> None:
db_path = os.path.join(str(tmp_path), "test.db")
store = TranscriptStore(db_path=db_path)
await store.initialize()
await store.enqueue("vid_001")
call_order = []
async def mock_sleep(seconds):
call_order.append("sleep")
def mock_fetch(video_id, api):
call_order.append("fetch")
raise IPBlockedError("blocked")
with patch("app.worker.asyncio.sleep", side_effect=mock_sleep), \
_patch_to_thread(), \
patch("app.worker.fetch_transcript_by_id", side_effect=mock_fetch):
api = MagicMock()
await process_next(store, api)
assert call_order == ["fetch", "sleep"]
await store.close()
asyncio.run(_run())
def test_process_next_no_sleep_before_first_download_after_empty_queue(self, tmp_path) -> None:
async def _run() -> None:
db_path = os.path.join(str(tmp_path), "test.db")
store = TranscriptStore(db_path=db_path)
await store.initialize()
call_order = []
async def mock_sleep(seconds):
call_order.append("sleep")
def mock_fetch(video_id, api):
call_order.append("fetch")
return [{"text": "hello", "start": 0.0, "duration": 1.0}]
with patch("app.worker.asyncio.sleep", side_effect=mock_sleep), \
_patch_to_thread(), \
patch("app.worker.fetch_transcript_by_id", side_effect=mock_fetch):
api = MagicMock()
# Queue is empty — no sleep, no fetch
result = await process_next(store, api)
assert result is False
assert call_order == []
# Video is added while queue was idle
await store.enqueue("vid_001")
# Next call downloads immediately, then sleeps after
result = await process_next(store, api)
assert result is True
assert call_order == ["fetch", "sleep"]
await store.close()
asyncio.run(_run())
def test_process_next_processes_fifo_order(self, tmp_path) -> None:
async def _run() -> None:
db_path = os.path.join(str(tmp_path), "test.db")
store = TranscriptStore(db_path=db_path)
await store.initialize()
await store.enqueue("vid_001")
await store.enqueue("vid_002")
with patch("app.worker.asyncio.sleep", new_callable=AsyncMock), \
_patch_to_thread(), \
patch("app.worker.fetch_transcript_by_id", return_value=[{"text": "first", "start": 0.0, "duration": 1.0}]):
api = MagicMock()
await process_next(store, api)
assert (await store.get_transcript("vid_001")) is not None
assert (await store.get_transcript("vid_002")) is None
assert (await store.get_queue_entry("vid_002")) is not None
await store.close()
asyncio.run(_run())
|