mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-05 23:47:41 +08:00
fix(agent): propagate task cancellation (#6284)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
import json
|
||||
import threading
|
||||
from datetime import datetime, timedelta
|
||||
@@ -591,6 +592,35 @@ async def test_agent_manager_runs_contextless_task_in_broadcast_mode(
|
||||
post_message.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_agent_manager_records_cancelled_scheduled_task_as_failed() -> None:
|
||||
"""被用户停止的定时 Agent 任务不得记录为成功完成。"""
|
||||
user_id = f"cancel-{uuid4().hex}"
|
||||
task = AgentTaskOper().add(
|
||||
name="取消中的后台检查",
|
||||
content="检查资源并报告",
|
||||
trigger_type="cron",
|
||||
cron_expression="0 * * * *",
|
||||
run_at=None,
|
||||
user_id=user_id,
|
||||
username="admin",
|
||||
session_id=f"session-{user_id}",
|
||||
channel=None,
|
||||
source="api",
|
||||
original_chat_id=None,
|
||||
)
|
||||
manager = AgentManager()
|
||||
manager.process_message = AsyncMock(side_effect=asyncio.CancelledError)
|
||||
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await manager.execute_scheduled_task(task.id)
|
||||
|
||||
completed = AgentTaskOper().get(task.id)
|
||||
assert completed.last_status == "failed"
|
||||
assert completed.last_result == "Agent 定时任务已取消"
|
||||
assert completed.run_count == 1
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_cached_agent_clears_channel_for_background_task() -> None:
|
||||
"""复用会话 Agent 时,后台任务必须覆盖上一轮保留的渠道信息。"""
|
||||
|
||||
Reference in New Issue
Block a user