fix(huya): 查询积分先验 CK 并改用 HTTP 快速通道

This commit is contained in:
yml2213
2026-10-17 16:22:54 +08:00
parent 2620023db4
commit ef6663beae
2 changed files with 164 additions and 102 deletions
+91 -38
View File
@@ -9,8 +9,8 @@
"""
import asyncio
from types import SimpleNamespace
from core.huya.activity_structs import GetUserScoreResp
from core.huya.frame_decoder import _decode_wup_body, decode_struct_bytes
from core.huya.http_client import HuyaHttpClient
from core.huya.shop_structs import ShopRawRsp
@@ -80,27 +80,19 @@ class _StubClient:
return self.grow
class _LegacyClient:
"""旧活动接口:真机实测任何 sid 都返回 603「活动已结束」。"""
def query_user_score(self, uid: int, cookie: str, sid: int):
response = GetUserScoreResp()
response.status = 603
response.msg = "活动已结束"
response.score = 0
return response
def _runner(monkeypatch, client, legacy=None) -> tuple[HuyaBatchRunner, list]:
def _runner(
monkeypatch, client, *, ck_valid: bool = True
) -> tuple[HuyaBatchRunner, list]:
runner = HuyaBatchRunner(db=None, batch_id="test-batch", task_type="query_points")
marks: list[tuple[str, str, dict | None]] = []
monkeypatch.setattr(runner, "_http_client", lambda: client, raising=False)
probe_status = SimpleNamespace(status=200, msg="") if ck_valid else None
monkeypatch.setattr(
runner, "_shop_client", lambda uid, cookie: client, raising=False
runner,
"_probe_bind_status",
lambda **_kwargs: (probe_status, {}),
raising=False,
)
if legacy is not None:
monkeypatch.setattr(
runner, "_activity_client", lambda uid, cookie: legacy, raising=False
)
monkeypatch.setattr(
runner,
"_mark_task",
@@ -222,6 +214,52 @@ def test_query_points_reads_points_from_points_center(monkeypatch):
assert result["nickname"] == "软踏日潺湲"
def test_query_points_uses_http_and_prechecks_ck_with_new_probe(monkeypatch):
client = _StubClient()
runner, marks = _runner(monkeypatch, client)
probe_calls: list[dict] = []
monkeypatch.setattr(
runner,
"_probe_bind_status",
lambda **kwargs: (
probe_calls.append(kwargs) or SimpleNamespace(status=200, msg=""),
{},
),
raising=False,
)
monkeypatch.setattr(
runner,
"_shop_client",
lambda *_args, **_kwargs: (_ for _ in ()).throw(
AssertionError("查询积分不应再建 WSS 商城会话")
),
raising=False,
)
account = HuyaAccount(uid=str(UID), cookie=COOKIE)
runner._execute_query_points(
None,
None,
account,
{"uid": UID, "cookie": COOKIE},
{"bind_act_id": "9271", "outer_act_id": "17096"},
)
assert probe_calls == [
{
"uid": UID,
"cookie": COOKIE,
"b_act_id_int": 9271,
"outer_act_id": "17096",
}
]
assert marks[-1][0] == "success"
assert [name for name, _kwargs in client.calls] == [
"get_redeem_need_goods",
"get_grow_level_user_info",
]
def test_query_points_keeps_working_when_grow_level_fails(monkeypatch):
client = _StubClient(grow=False)
runner, marks = _runner(monkeypatch, client)
@@ -265,31 +303,46 @@ def test_query_points_reports_server_error_of_points_center(monkeypatch):
assert result["goods_num"] == 8315982
def test_query_points_fallback_reports_legacy_interface_offline(monkeypatch):
def test_query_points_reports_points_center_offline_without_legacy_fallback(
monkeypatch,
):
client = _StubClient(redeem=False)
runner, marks = _runner(monkeypatch, client, legacy=_LegacyClient())
runner, marks = _runner(monkeypatch, client)
account = HuyaAccount(uid=str(UID), cookie=COOKIE)
runner._execute_query_points(
None, None, account, {"uid": UID, "cookie": COOKIE}, {"sid": "2203"}
None,
None,
account,
{"uid": UID, "cookie": COOKIE},
{"bind_act_id": "9271", "outer_act_id": "17096"},
)
status, message, _result = marks[-1]
assert status == "failed"
# 旧接口的真实返回必须如实暴露,而不是只报「活动已结束」
assert "旧活动接口(sid=2203)返回 603 活动已结束" in message
assert "积分中心接口无响应" in message
def test_query_points_without_sid_reports_points_center_offline(monkeypatch):
client = _StubClient(redeem=False)
runner, marks = _runner(monkeypatch, client, legacy=_LegacyClient())
account = HuyaAccount(uid=str(UID), cookie=COOKIE)
runner._execute_query_points(
None, None, account, {"uid": UID, "cookie": COOKIE}, {}
)
status, message, _result = marks[-1]
status, message, result = marks[-1]
assert status == "error"
assert "积分中心接口无响应" in message
assert result["ck_valid"] is True
# 旧接口已下线,不能再让它多等一个 15 秒超时。
assert [name for name, _kwargs in client.calls] == ["get_redeem_need_goods"]
def test_query_points_marks_ck_expired_before_calling_points_center(monkeypatch):
client = _StubClient()
runner, marks = _runner(monkeypatch, client, ck_valid=False)
account = HuyaAccount(uid=str(UID), cookie=COOKIE, status="imported")
runner._execute_query_points(
None,
None,
account,
{"uid": UID, "cookie": COOKIE},
{"bind_act_id": "9271", "outer_act_id": "17096"},
)
status, message, result = marks[-1]
assert status == "error"
assert "CK 已失效" in message
assert result["ck_expired"] is True
assert result["ck_expired_reason"] == "角色信息接口无响应"
assert account.status == "ck_expired"
assert client.calls == []