fix(fulfillment): 通过状态转换识别最新角色
This commit is contained in:
@@ -6,6 +6,7 @@ from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from core.douyu import DouyuActivityClient
|
||||
from web.backend.models import Account
|
||||
from web.backend.services.account_binding import (
|
||||
BIND_BOUND,
|
||||
BIND_PENDING,
|
||||
@@ -172,6 +173,51 @@ def test_existing_pending_role_is_baseline_not_new_scan_result():
|
||||
baseline_role_name=old_role,
|
||||
baseline_is_bound_act=False,
|
||||
)
|
||||
assert not is_pending_binding_role(
|
||||
candidates[0],
|
||||
baseline_role_name=old_role,
|
||||
baseline_is_bound_act=True,
|
||||
)
|
||||
assert not is_pending_binding_role(
|
||||
candidates[1],
|
||||
baseline_role_name=old_role,
|
||||
baseline_is_bound_act=True,
|
||||
)
|
||||
assert not is_pending_binding_role(
|
||||
candidates[0],
|
||||
baseline_role_name=old_role,
|
||||
baseline_is_bound_act=True,
|
||||
baseline_info=candidates[1],
|
||||
)
|
||||
|
||||
|
||||
def test_same_role_after_activity_bind_transition_is_pending():
|
||||
old_role = "少和我较劲"
|
||||
baseline = _role_candidate(ACT_ALIAS, old_role, bound_act=True)
|
||||
after_scan = _role_candidate(ACT_ALIAS, old_role, bound_act=False)
|
||||
|
||||
assert is_pending_binding_role(
|
||||
after_scan,
|
||||
baseline_role_name=old_role,
|
||||
baseline_is_bound_act=True,
|
||||
baseline_info=baseline,
|
||||
)
|
||||
|
||||
|
||||
def test_confirmed_new_role_replaces_account_current_role():
|
||||
account = Account(
|
||||
username="replace-role",
|
||||
password="p",
|
||||
email="replace-role@example.com",
|
||||
email_password="ep",
|
||||
status="active",
|
||||
game_name="少和我较劲",
|
||||
)
|
||||
new_role = _role_candidate(ACT_ALIAS, "新的绑定角色", bound_act=True)
|
||||
|
||||
_RolePicker()._apply_bind_info_to_account(account, new_role)
|
||||
|
||||
assert account.game_name == "新的绑定角色"
|
||||
|
||||
|
||||
def test_new_role_wins_when_cjm_and_activity_snapshots_differ():
|
||||
|
||||
@@ -825,7 +825,12 @@ class TestBindingAndCompletion(_Base):
|
||||
task_type="get_bind_qr",
|
||||
handbook_scope="elite",
|
||||
status="success",
|
||||
result={"url": "https://example.com/qr", "role_name": "测试角色"},
|
||||
result={
|
||||
"url": "https://example.com/qr",
|
||||
"role_name": "测试角色",
|
||||
"need_bind_act": True,
|
||||
"bind_ready_for_confirm": True,
|
||||
},
|
||||
created_by=self.admin_id,
|
||||
)
|
||||
self.session.add(bind_task)
|
||||
@@ -841,6 +846,84 @@ class TestBindingAndCompletion(_Base):
|
||||
assert detail["task"]["bindTask"]["roleName"] == "测试角色"
|
||||
assert notified == [task.id]
|
||||
|
||||
def test_refresh_binding_ignores_legacy_same_role_pending(self):
|
||||
task = self._allocated()
|
||||
bind_task = DouyuTask(
|
||||
batch_id="bind-stale-same-role",
|
||||
account_id=task.account_id,
|
||||
account_assigned_to=self.admin_id,
|
||||
task_type="get_bind_qr",
|
||||
handbook_scope="elite",
|
||||
status="success",
|
||||
result={
|
||||
"url": "https://example.com/old-qr",
|
||||
"role_name": "少和我较劲",
|
||||
"current_role_name": "少和我较劲",
|
||||
"baseline_role_name": "少和我较劲",
|
||||
"need_bind_act": False,
|
||||
"is_bound_act": False,
|
||||
"bind_ready_for_confirm": True,
|
||||
"bind_confirmed": False,
|
||||
"bind_phase": "role_ready",
|
||||
},
|
||||
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)
|
||||
|
||||
self.session.refresh(task)
|
||||
assert task.bind_task_id is None
|
||||
assert task.binding_status == "not_started"
|
||||
assert task.execution_status == "allocated"
|
||||
assert detail["task"]["bindTask"] is None
|
||||
event = (
|
||||
self.session.query(PlatformFulfillmentEvent)
|
||||
.filter_by(task_id=task.id, event_type="binding_stale_role_ignored")
|
||||
.one()
|
||||
)
|
||||
assert event.detail_json["roleName"] == "少和我较劲"
|
||||
|
||||
def test_refresh_binding_keeps_same_role_after_pending_transition(self):
|
||||
task = self._allocated()
|
||||
bind_task = DouyuTask(
|
||||
batch_id="bind-new-same-role",
|
||||
account_id=task.account_id,
|
||||
account_assigned_to=self.admin_id,
|
||||
task_type="get_bind_qr",
|
||||
handbook_scope="elite",
|
||||
status="success",
|
||||
result={
|
||||
"url": "https://example.com/new-qr",
|
||||
"role_name": "少和我较劲",
|
||||
"current_role_name": "少和我较劲",
|
||||
"baseline_role_name": "少和我较劲",
|
||||
"need_bind_act": True,
|
||||
"is_bound_act": False,
|
||||
"bind_ready_for_confirm": True,
|
||||
"bind_confirmed": False,
|
||||
"bind_phase": "role_ready",
|
||||
},
|
||||
created_by=self.admin_id,
|
||||
)
|
||||
self.session.add(bind_task)
|
||||
self.session.flush()
|
||||
task.bind_task_id = bind_task.id
|
||||
task.binding_status = "waiting_bind"
|
||||
task.execution_status = "waiting_bind"
|
||||
self.session.commit()
|
||||
|
||||
detail = service.refresh_binding(self.session, task_id=task.id)
|
||||
|
||||
assert detail["task"]["bindingStatus"] == "role_ready"
|
||||
assert detail["task"]["bindTask"]["roleName"] == "少和我较劲"
|
||||
assert detail["task"]["bindTask"]["bindReadyForConfirm"] is True
|
||||
|
||||
def test_refresh_binding_clears_stale_failure_when_qr_is_pending(self):
|
||||
task = self._allocated()
|
||||
bind_task = DouyuTask(
|
||||
|
||||
Reference in New Issue
Block a user