重构(斗鱼绑定): 统一角色候选判定与精英宝典查询
This commit is contained in:
@@ -57,8 +57,8 @@ def _bind(
|
||||
}
|
||||
|
||||
|
||||
def test_role_resolver_does_not_report_legacy_history_as_pending():
|
||||
"""cjm 可能返回同斗鱼账号历史绑过的另一个游戏角色,不能盖过当前活动角色。"""
|
||||
def test_role_resolver_keeps_legacy_candidate_alongside_current_bound():
|
||||
"""查询必须同时保留当前绑定和 cjm 最新候选,不能把候选直接丢掉。"""
|
||||
client = RoleClient(
|
||||
{
|
||||
"current-alias": _bind("当前角色", bound_act=True, bound_role=True),
|
||||
@@ -71,7 +71,8 @@ def test_role_resolver_does_not_report_legacy_history_as_pending():
|
||||
assert result["status"] == ROLE_BOUND
|
||||
assert result["role_name"] == "当前角色"
|
||||
assert result["source_alias"] == "current-alias"
|
||||
assert result["pending_role_name"] == ""
|
||||
assert result["pending_role_name"] == "历史其他账号角色"
|
||||
assert result["pending_source_alias"] == "cjm"
|
||||
assert client.calls == ["current-alias", "cjm"]
|
||||
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import pytest
|
||||
|
||||
from core.douyu import DouyuActivityClient, DouyuActivityError
|
||||
from web.backend.models import Account, DouyuTask
|
||||
from web.backend.services import douyu_runner_bind
|
||||
from web.backend.services.account_binding import (
|
||||
BIND_BOUND,
|
||||
BIND_PENDING,
|
||||
@@ -22,6 +23,7 @@ from web.backend.services.account_binding import (
|
||||
is_platform_account_bind_cooldown,
|
||||
is_role_change_cooling,
|
||||
pending_binding_role_from_result,
|
||||
resolve_binding_candidates,
|
||||
role_change_available_at,
|
||||
)
|
||||
from web.backend.services.douyu_binding import (
|
||||
@@ -731,6 +733,178 @@ def test_fulfillment_pending_waits_for_customer_target():
|
||||
assert pending_binding_role_from_result(result) == ""
|
||||
|
||||
|
||||
def test_binding_candidate_resolver_uses_context_not_caller_rules():
|
||||
candidates = [
|
||||
_role_candidate(ACT_ALIAS, "孤斟", bound_act=True),
|
||||
_role_candidate(LEGACY_ALIAS, "星果"),
|
||||
]
|
||||
|
||||
query = resolve_binding_candidates(
|
||||
candidates,
|
||||
baseline_role_name="孤斟",
|
||||
baseline_is_bound_act=True,
|
||||
baseline_info=candidates[0],
|
||||
actionable_aliases=[ACT_ALIAS],
|
||||
prefer_aliases=[ACT_ALIAS, LEGACY_ALIAS],
|
||||
allow_legacy_pending=True,
|
||||
)
|
||||
fulfillment_without_target = resolve_binding_candidates(
|
||||
candidates,
|
||||
baseline_role_name="孤斟",
|
||||
baseline_is_bound_act=True,
|
||||
baseline_info=candidates[0],
|
||||
actionable_aliases=[ACT_ALIAS],
|
||||
prefer_aliases=[ACT_ALIAS, LEGACY_ALIAS],
|
||||
require_expected_role=True,
|
||||
)
|
||||
fulfillment_with_target = resolve_binding_candidates(
|
||||
candidates,
|
||||
baseline_role_name="孤斟",
|
||||
baseline_is_bound_act=True,
|
||||
baseline_info=candidates[0],
|
||||
actionable_aliases=[ACT_ALIAS],
|
||||
expected_role_name="星果",
|
||||
require_expected_role=True,
|
||||
prefer_aliases=[ACT_ALIAS, LEGACY_ALIAS],
|
||||
)
|
||||
|
||||
assert query["selected"]["role_name"] == "星果"
|
||||
assert fulfillment_without_target["selected"] is None
|
||||
assert (
|
||||
fulfillment_without_target["decisions"][1]["reason"]
|
||||
== "expected_role_required"
|
||||
)
|
||||
assert fulfillment_with_target["selected"]["role_name"] == "星果"
|
||||
|
||||
|
||||
def test_query_result_can_surface_legacy_candidate_without_overwriting_bound_role():
|
||||
result = {
|
||||
"role_name": "星果",
|
||||
"current_role_name": "孤斟",
|
||||
"act_alias": LEGACY_ALIAS,
|
||||
"action_act_aliases": [ACT_ALIAS],
|
||||
"baseline_role_name": "孤斟",
|
||||
"baseline_is_bound_act": True,
|
||||
"before_bind_info": {
|
||||
"act_alias": ACT_ALIAS,
|
||||
"role_name": "孤斟",
|
||||
"is_bound_act": True,
|
||||
},
|
||||
"allow_legacy_pending": True,
|
||||
"bind_ready_for_confirm": True,
|
||||
"bind_confirmed": False,
|
||||
"bind_candidates": [
|
||||
{
|
||||
"act_alias": ACT_ALIAS,
|
||||
"role_name": "孤斟",
|
||||
"is_bound_act": True,
|
||||
},
|
||||
{
|
||||
"act_alias": LEGACY_ALIAS,
|
||||
"role_name": "星果",
|
||||
"is_bound_act": False,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
assert pending_binding_role_from_result(result) == "星果"
|
||||
result.pop("allow_legacy_pending")
|
||||
assert pending_binding_role_from_result(result) == ""
|
||||
|
||||
|
||||
def test_query_game_surfaces_latest_candidate_while_keeping_current_bound(monkeypatch):
|
||||
bound_info = {
|
||||
"act_alias": ACT_ALIAS,
|
||||
"role_name": "孤斟",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "安卓(android)",
|
||||
"is_bound_act": True,
|
||||
"is_bound_role": True,
|
||||
}
|
||||
pending_info = {
|
||||
"act_alias": LEGACY_ALIAS,
|
||||
"role_name": "星果",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "安卓(android)",
|
||||
"is_bound_act": False,
|
||||
"is_bound_role": True,
|
||||
}
|
||||
role_result = {
|
||||
"status": BIND_BOUND,
|
||||
"role_name": "孤斟",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "安卓(android)",
|
||||
"source_alias": ACT_ALIAS,
|
||||
"pending_role_name": "星果",
|
||||
"pending_area_name": "手Q",
|
||||
"pending_plat_name": "安卓(android)",
|
||||
"pending_source_alias": LEGACY_ALIAS,
|
||||
"pending_bind_info": pending_info,
|
||||
"bind_info": bound_info,
|
||||
"snapshot": binding_snapshot(bound_info),
|
||||
"bind_infos": [bound_info, pending_info],
|
||||
"candidates": [
|
||||
_role_candidate(ACT_ALIAS, "孤斟", bound_act=True),
|
||||
_role_candidate(LEGACY_ALIAS, "星果"),
|
||||
],
|
||||
"query_act_aliases": [ACT_ALIAS, LEGACY_ALIAS],
|
||||
"errors": [],
|
||||
}
|
||||
captured: dict[str, Any] = {}
|
||||
|
||||
class _QueryRunner(BindMixin):
|
||||
def _client(self, _cookie):
|
||||
return object()
|
||||
|
||||
def _push_log(self, *_args, **_kwargs):
|
||||
return None
|
||||
|
||||
def _push_binding_decision_log(self, **_kwargs):
|
||||
return None
|
||||
|
||||
def _mark_task(self, _db, _task, status, message, result):
|
||||
captured.update(status=status, message=message, result=result)
|
||||
|
||||
monkeypatch.setattr(
|
||||
douyu_runner_bind,
|
||||
"resolve_elite_role",
|
||||
lambda *_args, **_kwargs: role_result,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
douyu_runner_bind,
|
||||
"persist_probe_result",
|
||||
lambda *_args, **_kwargs: None,
|
||||
)
|
||||
task = DouyuTask(
|
||||
id=1,
|
||||
batch_id="query-result",
|
||||
account_id=7,
|
||||
task_type="query_game_name",
|
||||
handbook_scope="elite",
|
||||
created_by=1,
|
||||
result={},
|
||||
)
|
||||
|
||||
_QueryRunner()._execute_query_game_name(
|
||||
None,
|
||||
task,
|
||||
_binding_account(),
|
||||
"",
|
||||
ROLE_CONFIG,
|
||||
)
|
||||
|
||||
result = captured["result"]
|
||||
assert captured["status"] == "success"
|
||||
assert result["role_name"] == "星果"
|
||||
assert result["current_role_name"] == "孤斟"
|
||||
assert result["bind_ready_for_confirm"] is True
|
||||
assert result["bind_confirmed"] is False
|
||||
assert result["allow_legacy_pending"] is True
|
||||
assert "当前已绑定: 孤斟" in captured["message"]
|
||||
assert "待确认角色: 星果" in captured["message"]
|
||||
assert pending_binding_role_from_result(result) == "星果"
|
||||
|
||||
|
||||
def test_result_rejects_legacy_role_when_action_alias_still_has_role():
|
||||
result = {
|
||||
"role_name": "TuT小玉",
|
||||
|
||||
Reference in New Issue
Block a user