Centralize Douyu client creation, CK session probing, identity refresh, role alias resolution, capability persistence, cache and batch throttling across collaboration, fulfillment, tasks and cookie management. Add the account-name copy action in the collaboration table.
226 lines
6.0 KiB
Python
226 lines
6.0 KiB
Python
"""斗鱼统一账号探针:能力分离、alias 选择和写回保护。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime
|
|
|
|
from web.backend.models import Account, AccountEvent
|
|
from web.backend.services.douyu_account_probe import (
|
|
ROLE_BOUND,
|
|
ROLE_ERROR,
|
|
ROLE_PENDING,
|
|
ROLE_UNBOUND,
|
|
ROLE_UNKNOWN,
|
|
persist_probe_result,
|
|
probe_account,
|
|
resolve_elite_role,
|
|
)
|
|
|
|
CONFIG = {
|
|
"manual_id": "manual-id",
|
|
"rid": "room-id",
|
|
"bind_act_alias": "current-alias",
|
|
"confirm_act_alias": "current-alias",
|
|
"legacy_act_alias": "cjm",
|
|
}
|
|
|
|
|
|
class RoleClient:
|
|
def __init__(self, responses, errors=None):
|
|
self.responses = responses
|
|
self.errors = errors or {}
|
|
self.calls: list[str] = []
|
|
|
|
def bind_info(self, act_alias: str, *, v2: bool = False):
|
|
self.calls.append(act_alias)
|
|
if act_alias in self.errors:
|
|
raise RuntimeError(self.errors[act_alias])
|
|
return self.responses.get(act_alias, {})
|
|
|
|
|
|
def _bind(
|
|
role: str = "",
|
|
*,
|
|
bound_act: bool = False,
|
|
bound_role: bool = False,
|
|
need_act: bool = True,
|
|
need_role: bool = True,
|
|
):
|
|
return {
|
|
"is_bound_act": bound_act,
|
|
"is_bound_role": bound_role,
|
|
"need_bind_act": need_act,
|
|
"need_bind_role": need_role,
|
|
"role_name": role,
|
|
"area_name": "微信" if role else "",
|
|
"plat_name": "安卓" if role else "",
|
|
}
|
|
|
|
|
|
def test_role_resolver_prefers_current_bound_and_reports_pending_legacy():
|
|
client = RoleClient(
|
|
{
|
|
"current-alias": _bind("当前角色", bound_act=True, bound_role=True),
|
|
"cjm": _bind("新角色", bound_role=True),
|
|
}
|
|
)
|
|
|
|
result = resolve_elite_role(client, CONFIG)
|
|
|
|
assert result["status"] == ROLE_BOUND
|
|
assert result["role_name"] == "当前角色"
|
|
assert result["source_alias"] == "current-alias"
|
|
assert result["pending_role_name"] == "新角色"
|
|
assert client.calls == ["current-alias", "cjm"]
|
|
|
|
|
|
def test_role_resolver_uses_legacy_pending_role_when_current_is_empty():
|
|
client = RoleClient(
|
|
{
|
|
"current-alias": _bind(""),
|
|
"cjm": _bind("待确认角色", bound_role=True, need_act=False),
|
|
}
|
|
)
|
|
|
|
result = resolve_elite_role(client, CONFIG)
|
|
|
|
assert result["status"] == ROLE_PENDING
|
|
assert result["role_name"] == "待确认角色"
|
|
assert result["source_alias"] == "cjm"
|
|
|
|
|
|
def test_role_resolver_partial_failure_does_not_claim_unbound():
|
|
client = RoleClient(
|
|
{
|
|
"current-alias": _bind(""),
|
|
"cjm": _bind(""),
|
|
},
|
|
errors={"current-alias": "登录已失效"},
|
|
)
|
|
|
|
result = resolve_elite_role(client, CONFIG)
|
|
|
|
assert result["status"] == ROLE_UNKNOWN
|
|
assert result["errors"]
|
|
|
|
|
|
def test_role_resolver_official_empty_is_explicitly_unbound():
|
|
client = RoleClient(
|
|
{
|
|
"current-alias": _bind(""),
|
|
"cjm": _bind(""),
|
|
}
|
|
)
|
|
|
|
result = resolve_elite_role(client, CONFIG)
|
|
|
|
assert result["status"] == ROLE_UNBOUND
|
|
assert result["role_name"] == ""
|
|
|
|
|
|
class ProbeClient:
|
|
def __init__(self):
|
|
self.calls = 0
|
|
|
|
def elite_user_info(self, **_kwargs):
|
|
self.calls += 1
|
|
return {
|
|
"manual_type": 1,
|
|
"manual_score": 100,
|
|
"expire_time": 0,
|
|
"user_info": {"nickname": "精英昵称"},
|
|
}
|
|
|
|
def bind_info(self, act_alias: str, *, v2: bool = False):
|
|
self.calls += 1
|
|
return _bind("角色A", bound_act=True, bound_role=True)
|
|
|
|
|
|
def test_probe_account_reads_identity_and_does_not_call_activity_for_invalid_ck():
|
|
account = Account(
|
|
id=1,
|
|
username="douyu-user",
|
|
password="p",
|
|
email="u@example.com",
|
|
email_password="ep",
|
|
cookie="acf_uid=100; acf_nickname=cookie-name",
|
|
)
|
|
valid_client = ProbeClient()
|
|
valid = probe_account(
|
|
account,
|
|
CONFIG,
|
|
client=valid_client,
|
|
session_checker=lambda _cookie: {
|
|
"valid": True,
|
|
"message": "有效",
|
|
"nickname": "会话昵称",
|
|
"checked_at": datetime.now(UTC),
|
|
},
|
|
)
|
|
|
|
assert valid["session"]["status"] == "valid"
|
|
assert valid["identity"]["nickname"] == "精英昵称"
|
|
assert valid["identity"]["nickname_source"] == "elite"
|
|
assert valid["roles"]["elite"]["status"] == ROLE_BOUND
|
|
|
|
invalid_client = ProbeClient()
|
|
invalid = probe_account(
|
|
account,
|
|
CONFIG,
|
|
client=invalid_client,
|
|
session_checker=lambda _cookie: {
|
|
"valid": False,
|
|
"message": "未登录",
|
|
"checked_at": datetime.now(UTC),
|
|
},
|
|
)
|
|
assert invalid["session"]["status"] == "invalid"
|
|
assert invalid_client.calls == 0
|
|
|
|
|
|
def test_persist_probe_error_preserves_last_known_role(db):
|
|
account = Account(
|
|
username="preserve-role",
|
|
password="p",
|
|
email="preserve@example.com",
|
|
email_password="ep",
|
|
status="active",
|
|
cookie="acf_uid=100",
|
|
game_name="保留角色",
|
|
game_channel="微信 · 安卓",
|
|
role_bind_status=ROLE_BOUND,
|
|
)
|
|
db.add(account)
|
|
db.commit()
|
|
|
|
persist_probe_result(
|
|
db,
|
|
account,
|
|
{
|
|
"probe_id": "probe-error",
|
|
"identity": {"uid": "100", "username": account.username},
|
|
"session": {},
|
|
"elite": {},
|
|
"eligibility": {},
|
|
"roles": {
|
|
"elite": {
|
|
"status": ROLE_ERROR,
|
|
"errors": ["接口超时"],
|
|
}
|
|
},
|
|
"gold": {},
|
|
"goods": {},
|
|
"errors": {"elite_role": "接口超时"},
|
|
},
|
|
source="test.role_error",
|
|
record_event=True,
|
|
commit=True,
|
|
)
|
|
|
|
db.refresh(account)
|
|
assert account.game_name == "保留角色"
|
|
assert account.role_bind_status == ROLE_BOUND
|
|
event = db.query(AccountEvent).filter_by(event_type="account_probe").one()
|
|
assert event.detail["probe_id"] == "probe-error"
|
|
assert event.detail["role"] == ROLE_ERROR
|