-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_scheduled.py
More file actions
388 lines (305 loc) · 11.8 KB
/
test_scheduled.py
File metadata and controls
388 lines (305 loc) · 11.8 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
"""
Tests for scheduler_service.py and bot_session.py
Run: python test_scheduled.py
"""
import asyncio
import json
import tempfile
import time
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock, patch
# ── Test scheduler_service ──────────────────────────────────────
async def test_scheduler_add_remove():
"""添加/删除/查询任务"""
from scheduler_service import SchedulerService
from scheduled_task import ScheduledTask
queue = asyncio.Queue()
svc = SchedulerService(queue, storage_path=Path(tempfile.mktemp(suffix=".json")))
task = ScheduledTask.new(
cron_expr="0 8 * * *",
description="测试任务",
prompt="执行测试",
source_session_id="sess-1",
source_chat_id="chat-1",
source_platform="web",
)
# add
tid = svc.add(task)
assert tid == task.task_id
assert svc.get(tid) is not None
assert svc.get(tid).description == "测试任务"
# list
assert len(svc.list_all()) == 1
assert len(svc.list_by_session("sess-1")) == 1
assert len(svc.list_by_session("sess-none")) == 0
# remove
assert svc.remove(tid) is True
assert svc.get(tid) is None
assert len(svc.list_all()) == 0
assert svc.remove("nonexistent") is False
# cleanup
svc._path.unlink(missing_ok=True)
print("[PASS] test_scheduler_add_remove")
async def test_scheduler_persistence():
"""重启后任务恢复"""
from scheduler_service import SchedulerService
from scheduled_task import ScheduledTask
path = Path(tempfile.mktemp(suffix=".json"))
queue = asyncio.Queue()
# 创建并添加任务
svc1 = SchedulerService(queue, storage_path=path)
task = ScheduledTask.new(
cron_expr="*/5 * * * *",
description="持久化测试",
prompt="test",
source_session_id="sess-2",
source_chat_id="chat-2",
source_platform="telegram",
)
svc1.add(task)
# 模拟重启 — 新建实例并 load
svc2 = SchedulerService(asyncio.Queue(), storage_path=path)
svc2._load()
assert len(svc2.list_all()) == 1
restored = svc2.get(task.task_id)
assert restored is not None
assert restored.description == "持久化测试"
assert restored.cron_expr == "*/5 * * * *"
assert restored.source_platform == "telegram"
path.unlink()
print("[PASS] test_scheduler_persistence")
async def test_scheduler_cron_trigger():
"""cron 匹配触发逻辑(用过去时间确保立即触发)"""
from scheduler_service import SchedulerService
from scheduled_task import ScheduledTask
queue = asyncio.Queue()
path = Path(tempfile.mktemp(suffix=".json"))
svc = SchedulerService(queue, storage_path=path)
# 用每分钟的 cron,last_run_at=None,应该触发
task = ScheduledTask.new(
cron_expr="* * * * *",
description="每分钟触发",
prompt="test",
source_session_id="sess-3",
source_chat_id="chat-3",
source_platform="web",
)
svc.add(task)
# 手动调用 _trigger 验证队列收到 TaskTrigger
await svc._trigger(task)
assert not queue.empty()
from scheduled_task import TaskTrigger
trigger = queue.get_nowait()
assert isinstance(trigger, TaskTrigger)
assert trigger.task_id == task.task_id
assert trigger.description == "每分钟触发"
assert trigger.prompt == "test"
assert trigger.source_session_id == "sess-3"
# last_run_at 应已更新
assert task.last_run_at is not None
path.unlink()
print("[PASS] test_scheduler_cron_trigger")
async def test_scheduler_disabled_task():
"""禁用的任务不触发"""
from scheduler_service import SchedulerService
from scheduled_task import ScheduledTask
queue = asyncio.Queue()
path = Path(tempfile.mktemp(suffix=".json"))
svc = SchedulerService(queue, storage_path=path)
task = ScheduledTask.new(
cron_expr="* * * * *",
description="禁用任务",
prompt="test",
source_session_id="sess-4",
source_chat_id="chat-4",
source_platform="web",
)
task.enabled = False
svc.add(task)
# _tick 会跳过 disabled 任务 — 这里直接验证 _trigger 不会被调用
# 简化:队列应为空
assert queue.empty()
path.unlink()
print("[PASS] test_scheduler_disabled_task")
async def test_scheduler_multiple_sessions():
"""按 session 列出任务"""
from scheduler_service import SchedulerService
from scheduled_task import ScheduledTask
queue = asyncio.Queue()
path = Path(tempfile.mktemp(suffix=".json"))
svc = SchedulerService(queue, storage_path=path)
t1 = ScheduledTask.new("0 8 * * *", "Task A", "A", "sess-a", "chat-a", "web")
t2 = ScheduledTask.new("0 9 * * *", "Task B", "B", "sess-a", "chat-a", "web")
t3 = ScheduledTask.new("0 10 * * *", "Task C", "C", "sess-b", "chat-b", "telegram")
svc.add(t1)
svc.add(t2)
svc.add(t3)
assert len(svc.list_by_session("sess-a")) == 2
assert len(svc.list_by_session("sess-b")) == 1
assert len(svc.list_by_session("sess-none")) == 0
assert len(svc.list_all()) == 3
path.unlink()
print("[PASS] test_scheduler_multiple_sessions")
# ── Test bot_session ───────────────────────────────────────────
async def test_bot_session_instantiation():
"""BotSession 基本实例化"""
from bot_session import BotSession, BOT_SESSION_ID
queue = asyncio.Queue()
mock_pm = MagicMock()
called = []
async def on_complete(result, sid, cid, plat, debug):
called.append(result)
bot = BotSession(queue, mock_pm, on_complete)
assert bot.session_id == BOT_SESSION_ID
assert bot.is_running is False
print("[PASS] test_bot_session_instantiation")
async def test_bot_session_execute_task():
"""模拟任务执行 — mock run_agent_loop 验证结果捕获"""
from bot_session import BotSession, BOT_SESSION_ID
from scheduled_task import TaskTrigger, BotTaskResult
queue = asyncio.Queue()
mock_pm = MagicMock()
results = []
async def on_complete(result, sid, cid, plat, debug):
results.append((result, sid, cid, plat, debug))
bot = BotSession(queue, mock_pm, on_complete)
trigger = TaskTrigger(
task_id="test-task-1",
description="测试任务",
prompt="请执行测试",
source_session_id="sess-src",
source_chat_id="chat-src",
source_platform="web",
debug=False,
)
# Mock run_agent_loop — simulate finish_task call
async def mock_run_agent_loop(**kwargs):
# 模拟 BotSession 传入的 tool_overrides
finish_handler = kwargs.get("tool_overrides", {}).get("finish_task")
assert finish_handler is not None, "tool_overrides should contain finish_task"
from tool_registry import ToolExecutionResult
result = await finish_handler({
"report": "任务已完成,生成了报告",
"files": ["report.pdf", "data.csv"],
})
assert result.finished is True
assert result.output == "Task completed."
with patch("agent.run_agent_loop", mock_run_agent_loop):
result = await bot._execute_task(trigger)
assert isinstance(result, BotTaskResult)
assert result.task_id == "test-task-1"
assert result.status == "success"
assert "报告" in result.summary
assert result.files == ["report.pdf", "data.csv"]
print("[PASS] test_bot_session_execute_task")
async def test_bot_session_execute_failure():
"""任务失败时状态为 failed"""
from bot_session import BotSession
from scheduled_task import TaskTrigger, BotTaskResult
queue = asyncio.Queue()
mock_pm = MagicMock()
bot = BotSession(queue, mock_pm, lambda *a: None)
trigger = TaskTrigger(
task_id="test-fail-1",
description="失败任务",
prompt="test",
source_session_id="sess-src",
source_chat_id="chat-src",
source_platform="web",
)
async def mock_run_agent_loop(**kwargs):
finish_handler = kwargs.get("tool_overrides", {}).get("finish_task")
await finish_handler({
"report": "Unable to complete: site is down, error connecting",
"files": [],
})
with patch("agent.run_agent_loop", mock_run_agent_loop):
result = await bot._execute_task(trigger)
assert result.status == "failed"
print("[PASS] test_bot_session_execute_failure")
async def test_bot_session_execute_exception():
"""run_agent_loop 崩溃时捕获异常并返回 error 状态"""
from bot_session import BotSession
from scheduled_task import TaskTrigger
queue = asyncio.Queue()
mock_pm = MagicMock()
bot = BotSession(queue, mock_pm, lambda *a: None)
trigger = TaskTrigger(
task_id="test-crash-1",
description="崩溃任务",
prompt="test",
source_session_id="sess-src",
source_chat_id="chat-src",
source_platform="web",
)
async def mock_crashing_loop(**kwargs):
raise RuntimeError("Browser crashed")
with patch("agent.run_agent_loop", mock_crashing_loop):
result = await bot._execute_task(trigger)
assert result.status == "error"
assert "Browser crashed" in result.error
print("[PASS] test_bot_session_execute_exception")
async def test_bot_session_callback_routing():
"""验证 on_complete 回调收到正确的路由参数"""
from bot_session import BotSession
from scheduled_task import TaskTrigger
queue = asyncio.Queue()
mock_pm = MagicMock()
callbacks = []
async def on_complete(result, sid, cid, plat, debug):
callbacks.append((result.status, sid, cid, plat, debug))
bot = BotSession(queue, mock_pm, on_complete)
trigger = TaskTrigger(
task_id="test-route-1",
description="路由测试",
prompt="test",
source_session_id="sess-telegram",
source_chat_id="chat-12345",
source_platform="telegram",
debug=True,
)
async def mock_run_agent_loop(**kwargs):
finish_handler = kwargs.get("tool_overrides", {}).get("finish_task")
await finish_handler({"report": "done", "files": []})
with patch("agent.run_agent_loop", mock_run_agent_loop):
result = await bot._execute_task(trigger)
await bot._on_complete(
result,
trigger.source_session_id,
trigger.source_chat_id,
trigger.source_platform,
trigger.debug,
)
assert len(callbacks) == 1
status, sid, cid, plat, debug = callbacks[0]
assert status == "success"
assert sid == "sess-telegram"
assert cid == "chat-12345"
assert plat == "telegram"
assert debug is True
print("[PASS] test_bot_session_callback_routing")
# ── Main ───────────────────────────────────────────────────────
async def main():
print("=" * 60)
print("Testing scheduler_service.py")
print("=" * 60)
await test_scheduler_add_remove()
await test_scheduler_persistence()
await test_scheduler_cron_trigger()
await test_scheduler_disabled_task()
await test_scheduler_multiple_sessions()
print()
print("=" * 60)
print("Testing bot_session.py")
print("=" * 60)
await test_bot_session_instantiation()
await test_bot_session_execute_task()
await test_bot_session_execute_failure()
await test_bot_session_execute_exception()
await test_bot_session_callback_routing()
print()
print("=" * 60)
print("All tests passed!")
print("=" * 60)
if __name__ == "__main__":
asyncio.run(main())