diff --git a/tests/test_platform_fulfillment.py b/tests/test_platform_fulfillment.py index c5ef22d..6e11a91 100644 --- a/tests/test_platform_fulfillment.py +++ b/tests/test_platform_fulfillment.py @@ -2,6 +2,7 @@ from __future__ import annotations +import asyncio from datetime import UTC, datetime, timedelta from unittest.mock import patch @@ -425,6 +426,44 @@ class TestBindingAndCompletion(_Base): self.session.refresh(task) return task + def test_start_binding_clears_stale_binding_failure(self, monkeypatch): + task = self._allocated() + failed_bind_task = DouyuTask( + batch_id="bind-failed", + account_id=task.account_id, + account_assigned_to=self.admin_id, + task_type="get_bind_qr", + handbook_scope="elite", + status="failed", + message="绑定任务失败", + result={}, + created_by=self.admin_id, + ) + self.session.add(failed_bind_task) + self.session.flush() + task.bind_task_id = failed_bind_task.id + task.binding_status = "failed" + task.execution_status = "manual_required" + task.issue_code = "binding_failed" + task.last_error = "绑定失败,请刷新二维码后重试" + self.session.commit() + monkeypatch.setattr(service, "start_douyu_batch", lambda **_kwargs: None) + + asyncio.run( + service.start_binding( + self.session, + task_id=task.id, + current=self.admin, + force_refresh=True, + ) + ) + + self.session.refresh(task) + assert task.binding_status == "preparing" + assert task.execution_status == "waiting_bind" + assert task.issue_code == "" + assert task.last_error == "" + def test_refresh_binding_maps_role_ready(self): task = self._allocated() bind_task = DouyuTask( @@ -449,6 +488,34 @@ class TestBindingAndCompletion(_Base): assert detail["task"]["bindingStatus"] == "role_ready" assert detail["task"]["bindTask"]["roleName"] == "测试角色" + def test_refresh_binding_clears_stale_failure_when_qr_is_pending(self): + task = self._allocated() + bind_task = DouyuTask( + batch_id="bind-qr-after-failure", + account_id=task.account_id, + account_assigned_to=self.admin_id, + task_type="get_bind_qr", + handbook_scope="elite", + status="running", + result={"url": "https://example.com/new-qr"}, + created_by=self.admin_id, + ) + self.session.add(bind_task) + self.session.flush() + task.bind_task_id = bind_task.id + task.binding_status = "failed" + task.execution_status = "manual_required" + task.issue_code = "binding_failed" + task.last_error = "绑定失败,请刷新二维码后重试" + self.session.commit() + + detail = service.refresh_binding(self.session, task_id=task.id) + + assert detail["task"]["bindingStatus"] == "qr_pending" + assert detail["task"]["executionStatus"] == "waiting_bind" + assert detail["task"]["issueCode"] == "" + assert detail["task"]["lastError"] == "" + def test_list_hides_qr_but_detail_returns_it(self): task = self._allocated() bind_task = DouyuTask( diff --git a/web/backend/services/platform_fulfillment/service.py b/web/backend/services/platform_fulfillment/service.py index d8a8739..cf43300 100644 --- a/web/backend/services/platform_fulfillment/service.py +++ b/web/backend/services/platform_fulfillment/service.py @@ -970,6 +970,8 @@ async def start_binding( task.bind_task_id = bind_task.id task.binding_status = "preparing" task.execution_status = "waiting_bind" + task.issue_code = "" + task.last_error = "" task.operator_id = current.id task.updated_at = _now() renew_lease( @@ -1021,6 +1023,8 @@ def refresh_binding(db: Session, *, task_id: int) -> dict[str, Any]: elif result.get("url"): task.binding_status = "qr_pending" task.execution_status = "waiting_bind" + task.issue_code = "" + task.last_error = "" task.updated_at = _now() db.commit() return get_task_detail(db, task.id)