fix(douyu): 修复多 alias 待确认绑定并补充协议文档
This commit is contained in:
@@ -89,6 +89,23 @@ def test_role_resolver_uses_legacy_pending_role_when_current_is_empty():
|
||||
assert result["source_alias"] == "cjm"
|
||||
|
||||
|
||||
def test_role_resolver_prefers_existing_pending_on_action_alias_same_role():
|
||||
role = "忧郁情潇藏心中"
|
||||
client = RoleClient(
|
||||
{
|
||||
"current-alias": _bind(role, bound_role=True, need_act=True),
|
||||
"cjm": _bind(role, bound_role=True, need_act=False),
|
||||
}
|
||||
)
|
||||
|
||||
result = resolve_elite_role(client, CONFIG)
|
||||
|
||||
assert result["status"] == ROLE_PENDING
|
||||
assert result["role_name"] == role
|
||||
assert result["source_alias"] == "current-alias"
|
||||
assert result["decision"]["reason"] == "existing_actionable_pending"
|
||||
|
||||
|
||||
def test_role_resolver_partial_failure_does_not_claim_unbound():
|
||||
client = RoleClient(
|
||||
{
|
||||
|
||||
@@ -11,6 +11,7 @@ from web.backend.services.account_binding import (
|
||||
BIND_BOUND,
|
||||
BIND_PENDING,
|
||||
BIND_UNBOUND,
|
||||
binding_pending_decision,
|
||||
binding_role_from_result,
|
||||
binding_snapshot,
|
||||
derive_from_bind_info,
|
||||
@@ -39,6 +40,7 @@ def _role_candidate(
|
||||
role_name: str,
|
||||
*,
|
||||
bound_act: bool = False,
|
||||
need_bind_act: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
return {
|
||||
"act_alias": alias,
|
||||
@@ -48,7 +50,7 @@ def _role_candidate(
|
||||
"is_bound_account": True,
|
||||
"is_bound_role": True,
|
||||
"is_bound_act": bound_act,
|
||||
"need_bind_act": True,
|
||||
"need_bind_act": need_bind_act,
|
||||
"need_bind_role": True,
|
||||
}
|
||||
|
||||
@@ -193,6 +195,36 @@ def test_existing_pending_role_is_baseline_not_new_scan_result():
|
||||
)
|
||||
|
||||
|
||||
def test_existing_action_alias_pending_is_confirmable_even_same_role():
|
||||
role = "忧郁情潇藏心中"
|
||||
baseline = _role_candidate(ACT_ALIAS, role, need_bind_act=True)
|
||||
legacy = _role_candidate(LEGACY_ALIAS, role, need_bind_act=False)
|
||||
activity = _role_candidate(ACT_ALIAS, role, need_bind_act=True)
|
||||
picker = _RolePicker()
|
||||
|
||||
decision = binding_pending_decision(
|
||||
activity,
|
||||
baseline_role_name=role,
|
||||
baseline_is_bound_act=False,
|
||||
baseline_info=baseline,
|
||||
actionable_aliases=[ACT_ALIAS],
|
||||
)
|
||||
selected = picker._pick_bind_info(
|
||||
[legacy, activity],
|
||||
baseline_role_name=role,
|
||||
baseline_is_bound_act=False,
|
||||
baseline_info=baseline,
|
||||
prefer_pending=True,
|
||||
prefer_aliases=[LEGACY_ALIAS, ACT_ALIAS],
|
||||
actionable_aliases=[ACT_ALIAS],
|
||||
)
|
||||
|
||||
assert decision["confirmable"] is True
|
||||
assert decision["reason"] == "existing_actionable_pending"
|
||||
assert selected is not None
|
||||
assert selected["act_alias"] == ACT_ALIAS
|
||||
|
||||
|
||||
def test_same_role_after_activity_bind_transition_is_pending():
|
||||
old_role = "少和我较劲"
|
||||
baseline = _role_candidate(ACT_ALIAS, old_role, bound_act=True)
|
||||
|
||||
@@ -398,6 +398,148 @@ def test_binding_poll_once_finishes_when_role_is_found(db, monkeypatch):
|
||||
assert task.result["bind_ready_for_confirm"] is True
|
||||
|
||||
|
||||
def test_qr_preflight_accepts_existing_action_alias_pending_same_role(
|
||||
db, monkeypatch
|
||||
):
|
||||
_, account, task = _fixtures(db, status="running")
|
||||
role = "忧郁情潇藏心中"
|
||||
legacy = {
|
||||
"act_alias": "cjm",
|
||||
"role_name": role,
|
||||
"area_name": "微信",
|
||||
"plat_name": "安卓(android)",
|
||||
"is_bound_account": True,
|
||||
"is_bound_role": True,
|
||||
"is_bound_act": False,
|
||||
"need_bind_act": False,
|
||||
"need_bind_role": True,
|
||||
"change_role_wait_time": 0,
|
||||
"can_change_time": 0,
|
||||
"can_change_role": True,
|
||||
}
|
||||
activity = {
|
||||
**legacy,
|
||||
"act_alias": "20260120QYOOB",
|
||||
"need_bind_act": True,
|
||||
"change_role_wait_time": 2_592_000,
|
||||
}
|
||||
client = type(
|
||||
"Client",
|
||||
(),
|
||||
{"get_bind_qr": lambda self, _alias: {"url": "https://example.test/qr"}},
|
||||
)()
|
||||
runner = DouyuBatchRunner(
|
||||
db,
|
||||
task.batch_id,
|
||||
"get_bind_qr",
|
||||
close_db=False,
|
||||
)
|
||||
monkeypatch.setattr(runner, "_client", lambda _cookie: client)
|
||||
monkeypatch.setattr(
|
||||
runner,
|
||||
"_fetch_bind_info_candidates",
|
||||
lambda *_args: [legacy, activity],
|
||||
)
|
||||
|
||||
runner._execute_get_bind_qr(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
"PHPSESSID=x",
|
||||
{
|
||||
"bind_act_alias": "20260120QYOOB",
|
||||
"confirm_act_alias": "20260120QYOOB",
|
||||
"legacy_act_alias": "cjm",
|
||||
},
|
||||
)
|
||||
|
||||
db.refresh(task)
|
||||
assert task.status == "success"
|
||||
assert task.result["url"] == "https://example.test/qr"
|
||||
assert task.result["pending_role_name"] == role
|
||||
assert task.result["bind_ready_for_confirm"] is True
|
||||
assert task.result["bind_decision"]["alias"] == "20260120QYOOB"
|
||||
assert task.result["bind_decision"]["reason"] == "existing_actionable_pending"
|
||||
|
||||
|
||||
def test_binding_poll_accepts_existing_action_alias_pending_same_role(
|
||||
db, monkeypatch
|
||||
):
|
||||
"""活动 alias 的同名待确认角色不能被 legacy(cjm) 的旧快照压掉。"""
|
||||
_, account, task = _fixtures(db, status="running")
|
||||
role = "忧郁情潇藏心中"
|
||||
legacy = {
|
||||
"act_alias": "cjm",
|
||||
"role_name": role,
|
||||
"area_name": "微信",
|
||||
"plat_name": "安卓(android)",
|
||||
"is_bound_account": True,
|
||||
"is_bound_role": True,
|
||||
"is_bound_act": False,
|
||||
"need_bind_act": False,
|
||||
"need_bind_role": True,
|
||||
"change_role_wait_time": 0,
|
||||
"can_change_time": 0,
|
||||
"can_change_role": True,
|
||||
}
|
||||
activity = {
|
||||
**legacy,
|
||||
"act_alias": "20260120QYOOB",
|
||||
"need_bind_act": True,
|
||||
"change_role_wait_time": 2_592_000,
|
||||
}
|
||||
task.queue_lane = "binding_poll"
|
||||
task.next_run_at = datetime.now(UTC)
|
||||
task.run_deadline_at = datetime.now(UTC) + timedelta(minutes=10)
|
||||
task.result = {
|
||||
"url": "https://example.test/qr",
|
||||
"query_act_aliases": ["cjm", "20260120QYOOB"],
|
||||
"baseline_role_name": role,
|
||||
"baseline_is_bound_act": False,
|
||||
"before_bind_info": activity,
|
||||
"bind_polling": True,
|
||||
}
|
||||
db.commit()
|
||||
runner = DouyuBatchRunner(
|
||||
db,
|
||||
task.batch_id,
|
||||
"get_bind_qr",
|
||||
close_db=False,
|
||||
)
|
||||
client = type("Client", (), {})()
|
||||
monkeypatch.setattr(runner, "_client", lambda _cookie: client)
|
||||
monkeypatch.setattr(
|
||||
runner,
|
||||
"_fetch_bind_info_candidates",
|
||||
lambda *_args: [legacy, activity],
|
||||
)
|
||||
|
||||
runner.poll_bind_role_once(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
{
|
||||
"bind_act_alias": "20260120QYOOB",
|
||||
"confirm_act_alias": "20260120QYOOB",
|
||||
"legacy_act_alias": "cjm",
|
||||
},
|
||||
)
|
||||
|
||||
db.refresh(task)
|
||||
assert task.status == "success"
|
||||
assert task.result["role_name"] == role
|
||||
assert task.result["bind_ready_for_confirm"] is True
|
||||
assert task.result["bind_decision"]["alias"] == "20260120QYOOB"
|
||||
assert task.result["bind_decision"]["reason"] == "existing_actionable_pending"
|
||||
assert {
|
||||
row["alias"]: row["reason"]
|
||||
for row in task.result["bind_candidate_decisions"]
|
||||
} == {
|
||||
"cjm": "same_role_stale",
|
||||
"20260120QYOOB": "existing_actionable_pending",
|
||||
}
|
||||
|
||||
|
||||
def test_active_task_budget_rejects_new_batch(db, monkeypatch):
|
||||
user = User(username="budget-user", password_hash="hash", role="operation")
|
||||
db.add(user)
|
||||
|
||||
Reference in New Issue
Block a user