feat(fulfillment): 自动选号并补充精英宝典操作

- 增加直播履约自动选号 worker、重试与任务扫描
- 履约详情复用斗鱼任务能力开通宝典、充值和兑换皮肤
- 补充自动选号与精英宝典操作测试
This commit is contained in:
yml
2026-11-18 10:59:20 +08:00
parent bba3d9dac9
commit 8ba6379687
6 changed files with 501 additions and 6 deletions
+62
View File
@@ -524,6 +524,68 @@ class TestBindingAndCompletion(_Base):
assert detail["task"]["issueCode"] == ""
assert detail["task"]["lastError"] == ""
def test_refresh_binding_classifies_customer_role_cooldown(self):
task = self._allocated()
bind_task = DouyuTask(
batch_id="bind-customer-cooldown",
account_id=task.account_id,
account_assigned_to=self.admin_id,
task_type="confirm_bind",
handbook_scope="elite",
status="failed",
message="待绑定游戏账号(测试角色)未到换绑时间,请重新换账号扫码绑定",
result={
"bind_phase": "confirm_failed",
"role_name": "测试角色",
"bind_ready_for_confirm": True,
"bind_confirmed": False,
},
created_by=self.admin_id,
)
self.session.add(bind_task)
self.session.flush()
task.bind_task_id = bind_task.id
task.binding_status = "role_ready"
task.execution_status = "role_ready"
self.session.commit()
detail = service.refresh_binding(self.session, task_id=task.id)
assert detail["task"]["issueCode"] == "customer_role_cooldown"
assert detail["task"]["bindingStatus"] == "failed"
assert detail["task"]["executionStatus"] == "manual_required"
assert "截图" in detail["task"]["lastError"]
assert "联系客服" in detail["task"]["lastError"]
def test_refresh_binding_classifies_account_role_cooldown(self):
task = self._allocated()
bind_task = DouyuTask(
batch_id="bind-account-cooldown",
account_id=task.account_id,
account_assigned_to=self.admin_id,
task_type="get_bind_qr",
handbook_scope="elite",
status="failed",
message="当前角色 暂不能换绑,剩余 3天",
result={
"bind_phase": "change_waiting",
"change_role_wait_time": 259200,
},
created_by=self.admin_id,
)
self.session.add(bind_task)
self.session.flush()
task.bind_task_id = bind_task.id
task.binding_status = "preparing"
task.execution_status = "waiting_bind"
self.session.commit()
detail = service.refresh_binding(self.session, task_id=task.id)
assert detail["task"]["issueCode"] == "account_role_cooldown"
assert detail["task"]["executionStatus"] == "manual_required"
assert "截图" in detail["task"]["lastError"]
def test_list_hides_qr_but_detail_returns_it(self):
task = self._allocated()
bind_task = DouyuTask(
@@ -0,0 +1,56 @@
"""直播账号履约自动选号 worker。"""
from __future__ import annotations
from datetime import UTC, datetime, timedelta
from unittest.mock import patch
from web.backend.models import PlatformFulfillmentTask, User
from web.backend.services.platform_fulfillment import auto_allocator
def test_auto_allocator_only_scans_ready_unassigned_tasks(db):
actor = User(username="auto-alloc-actor", password_hash="h", role="super_admin")
ready = PlatformFulfillmentTask(
source_system="order_site",
source_order_id="1",
source_order_item_id="1",
platform_order_id="AUTO-1",
internal_sku_key="skin",
commodity_id="commodity-1",
execution_status="ready",
binding_status="not_started",
exchange_status="not_started",
updated_at=datetime.now(UTC) - timedelta(minutes=2),
)
mapped_pending = PlatformFulfillmentTask(
source_system="order_site",
source_order_id="2",
source_order_item_id="2",
platform_order_id="AUTO-2",
internal_sku_key="skin",
commodity_id="commodity-1",
execution_status="pending_mapping",
binding_status="not_started",
exchange_status="not_started",
updated_at=datetime.now(UTC) - timedelta(minutes=2),
)
db.add_all([actor, ready, mapped_pending])
db.commit()
calls: list[int] = []
with (
patch.object(auto_allocator.service, "_integration_actor", return_value=actor),
patch.object(
auto_allocator.service,
"allocate_task",
side_effect=lambda _db, *, task_id, current: (
calls.append(task_id)
or {"task": {"account": {"id": 9}}}
),
),
):
result = auto_allocator.run_auto_allocation_once(db, limit=10)
assert calls == [ready.id]
assert result == {"checked": 1, "allocated": 1, "failed": 0}