fix(fulfillment): reconcile stale pending roles from newer observations
This commit is contained in:
@@ -25,11 +25,15 @@ from web.backend.services.account_binding import (
|
||||
role_change_available_at,
|
||||
)
|
||||
from web.backend.services.douyu_binding import (
|
||||
BINDING_RECONCILE_CONFIRMED,
|
||||
BINDING_RECONCILE_STALE_PENDING,
|
||||
BINDING_VIEW_CONFIRMED,
|
||||
BINDING_VIEW_FAILED,
|
||||
BINDING_VIEW_NONE,
|
||||
BINDING_VIEW_PENDING,
|
||||
reconcile_douyu_binding,
|
||||
resolve_douyu_binding,
|
||||
resolve_fulfillment_binding,
|
||||
)
|
||||
from web.backend.services.douyu_runner_bind import BindMixin
|
||||
|
||||
@@ -369,13 +373,19 @@ def test_task_result_helpers_accept_existing_action_alias_pending():
|
||||
assert binding_role_from_result(result) == "只为爱杰宝"
|
||||
|
||||
|
||||
def _binding_task(*, status: str, result: dict[str, Any]) -> DouyuTask:
|
||||
def _binding_task(
|
||||
*,
|
||||
status: str,
|
||||
result: dict[str, Any],
|
||||
task_id: int = 101,
|
||||
task_type: str = "get_bind_qr",
|
||||
) -> DouyuTask:
|
||||
return DouyuTask(
|
||||
id=101,
|
||||
id=task_id,
|
||||
batch_id="binding-view",
|
||||
account_id=1,
|
||||
created_by=1,
|
||||
task_type="get_bind_qr",
|
||||
task_type=task_type,
|
||||
handbook_scope="elite",
|
||||
status=status,
|
||||
result=result,
|
||||
@@ -487,6 +497,129 @@ def test_account_binding_without_task_does_not_confirm_fulfillment_order():
|
||||
assert binding.bind_confirmed is False
|
||||
|
||||
|
||||
def test_newer_observation_reconciles_pending_role_by_identity():
|
||||
account = _binding_account()
|
||||
account.game_name = "续梦乖"
|
||||
pending = resolve_douyu_binding(
|
||||
_binding_task(
|
||||
task_id=100,
|
||||
status="success",
|
||||
result={
|
||||
"role_name": "续梦乖",
|
||||
"baseline_role_name": "布丁贩卖机",
|
||||
"before_bind_info": {
|
||||
"act_alias": ACT_ALIAS,
|
||||
"role_name": "布丁贩卖机",
|
||||
"is_bound_act": True,
|
||||
"need_bind_act": True,
|
||||
},
|
||||
"act_alias": ACT_ALIAS,
|
||||
"need_bind_act": True,
|
||||
"bind_ready_for_confirm": True,
|
||||
"bind_confirmed": False,
|
||||
"bind_phase": "role_ready",
|
||||
},
|
||||
),
|
||||
account=account,
|
||||
)
|
||||
observation = resolve_douyu_binding(
|
||||
_binding_task(
|
||||
task_id=101,
|
||||
task_type="query_game_name",
|
||||
status="success",
|
||||
result={
|
||||
"role_name": "续梦乖",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "安卓(android)",
|
||||
"is_bound_act": True,
|
||||
"bind_confirmed": True,
|
||||
"bind_phase": "confirmed",
|
||||
},
|
||||
),
|
||||
account=account,
|
||||
)
|
||||
|
||||
reconciliation = reconcile_douyu_binding(pending, observation)
|
||||
fulfillment_view = resolve_fulfillment_binding(
|
||||
_binding_task(
|
||||
task_id=101,
|
||||
task_type="query_game_name",
|
||||
status="success",
|
||||
result={
|
||||
"role_name": "续梦乖",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "安卓(android)",
|
||||
"is_bound_act": True,
|
||||
"bind_confirmed": True,
|
||||
"bind_phase": "confirmed",
|
||||
},
|
||||
),
|
||||
account=account,
|
||||
binding_status="bound",
|
||||
)
|
||||
|
||||
assert reconciliation.action == BINDING_RECONCILE_CONFIRMED
|
||||
assert fulfillment_view.status == BINDING_VIEW_CONFIRMED
|
||||
assert fulfillment_view.effective_role_name == "续梦乖"
|
||||
|
||||
|
||||
def test_newer_observation_invalidates_different_pending_role():
|
||||
account = _binding_account()
|
||||
account.game_name = "续梦乖"
|
||||
pending = resolve_douyu_binding(
|
||||
_binding_task(
|
||||
task_id=100,
|
||||
status="success",
|
||||
result={
|
||||
"role_name": "布丁贩卖机",
|
||||
"baseline_role_name": "续梦乖",
|
||||
"before_bind_info": {
|
||||
"act_alias": ACT_ALIAS,
|
||||
"role_name": "续梦乖",
|
||||
"is_bound_act": True,
|
||||
"need_bind_act": True,
|
||||
},
|
||||
"act_alias": ACT_ALIAS,
|
||||
"need_bind_act": True,
|
||||
"bind_ready_for_confirm": True,
|
||||
"bind_confirmed": False,
|
||||
"bind_phase": "role_ready",
|
||||
},
|
||||
),
|
||||
account=account,
|
||||
)
|
||||
observation_task = _binding_task(
|
||||
task_id=101,
|
||||
task_type="query_game_name",
|
||||
status="success",
|
||||
result={
|
||||
"role_name": "续梦乖",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "安卓(android)",
|
||||
"is_bound_act": True,
|
||||
"bind_confirmed": True,
|
||||
"bind_phase": "confirmed",
|
||||
},
|
||||
)
|
||||
observation = resolve_douyu_binding(observation_task, account=account)
|
||||
|
||||
reconciliation = reconcile_douyu_binding(pending, observation)
|
||||
fulfillment_view = resolve_fulfillment_binding(
|
||||
observation_task,
|
||||
account=account,
|
||||
binding_status="waiting_bind",
|
||||
)
|
||||
|
||||
assert reconciliation.action == BINDING_RECONCILE_STALE_PENDING
|
||||
assert reconciliation.pending_role_name == "布丁贩卖机"
|
||||
assert reconciliation.observed_role_name == "续梦乖"
|
||||
assert fulfillment_view.status == BINDING_VIEW_NONE
|
||||
assert fulfillment_view.pending_role_name == ""
|
||||
assert fulfillment_view.effective_role_name == "续梦乖"
|
||||
assert fulfillment_view.bind_ready_for_confirm is False
|
||||
assert fulfillment_view.bind_confirmed is False
|
||||
|
||||
|
||||
def test_confirmed_new_role_replaces_account_current_role():
|
||||
account = Account(
|
||||
username="replace-role",
|
||||
|
||||
Reference in New Issue
Block a user