feat(huya): 统一实时商品目录并增加兑换代理开关
This commit is contained in:
@@ -42,6 +42,7 @@ from web.backend.schemas import (
|
||||
HuyaExternalOrderUpdate,
|
||||
)
|
||||
from web.backend.services import huya_collab_service as collab
|
||||
from web.backend.services import huya_points_center as points_center
|
||||
from web.backend.services.huya_collab_events import collab_events
|
||||
|
||||
|
||||
@@ -840,32 +841,6 @@ class FakeBindResp:
|
||||
}
|
||||
|
||||
|
||||
class FakeGiftResp:
|
||||
"""合成一份 ``getActPrizeList`` 响应(礼包列表)。"""
|
||||
|
||||
def __init__(self, *, status: int = 200, msg: str = "", goods: list | None = None):
|
||||
self.status = status
|
||||
self.msg = msg
|
||||
self._goods = (
|
||||
goods
|
||||
if goods is not None
|
||||
else [
|
||||
{
|
||||
"product_id": "hy-1001",
|
||||
"name": "不朽伊人大礼包",
|
||||
"price": 200,
|
||||
"remain_text": "39%",
|
||||
"category_name": "礼包",
|
||||
},
|
||||
{"product_id": "", "name": "缺 product_id 的脏数据"},
|
||||
{"product_id": "hy-1002", "name": ""},
|
||||
]
|
||||
)
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {"status": self.status, "msg": self.msg, "goods": self._goods}
|
||||
|
||||
|
||||
def points_center_blob(
|
||||
*,
|
||||
name: str = "不朽伊人大礼包",
|
||||
@@ -968,18 +943,15 @@ class FakeProbeClient:
|
||||
def __init__(
|
||||
self,
|
||||
*outcomes,
|
||||
gifts: list | None = None,
|
||||
points_gifts: list | None = None,
|
||||
profile_nick: str | None = None,
|
||||
profile_outcome: object | None = None,
|
||||
):
|
||||
self.outcomes = list(outcomes)
|
||||
self.gift_outcomes = list(gifts or [])
|
||||
self.point_outcomes = list(points_gifts or [])
|
||||
self.profile_nick = profile_nick
|
||||
self.profile_outcome = profile_outcome
|
||||
self.calls: list[dict] = []
|
||||
self.gift_calls: list[dict] = []
|
||||
self.points_calls: list[dict] = []
|
||||
self.profile_calls: list[dict] = []
|
||||
self._current_bind_outcome: object | None = None
|
||||
@@ -1001,13 +973,6 @@ class FakeProbeClient:
|
||||
raise outcome
|
||||
return outcome
|
||||
|
||||
def get_act_prize_list(self, **kwargs):
|
||||
self.gift_calls.append(kwargs)
|
||||
outcome = self.gift_outcomes.pop(0) if self.gift_outcomes else FakeGiftResp()
|
||||
if isinstance(outcome, Exception):
|
||||
raise outcome
|
||||
return outcome
|
||||
|
||||
def play_mall_query_allocation(self, **kwargs):
|
||||
self.points_calls.append(kwargs)
|
||||
outcome = (
|
||||
@@ -1492,7 +1457,6 @@ class TestCollabCkPreview(TestCollabBase):
|
||||
assert "已拉取 1 个礼包(积分中心)" in preview["gifts_message"]
|
||||
assert client.points_calls[0]["uid"] == 1199647239697
|
||||
assert client.points_calls[0]["deep"] is True
|
||||
assert client.gift_calls == []
|
||||
|
||||
def test_preview_of_invalid_ck_does_not_fetch_gifts(self):
|
||||
client = FakeProbeClient(None)
|
||||
@@ -1531,50 +1495,14 @@ class TestCollabCkPreview(TestCollabBase):
|
||||
|
||||
|
||||
class TestCollabGifts(TestCollabBase):
|
||||
def test_gift_fetch_skips_dirty_rows(self):
|
||||
gifts, message = collab.fetch_activity_gifts(
|
||||
1199647239697, fake_ck("1199647239697"), client=FakeProbeClient()
|
||||
)
|
||||
assert [gift["name"] for gift in gifts] == ["不朽伊人大礼包"]
|
||||
assert message == "已拉取 1 个礼包"
|
||||
|
||||
def test_gift_fetch_reports_failure_without_raising(self):
|
||||
gifts, message = collab.fetch_activity_gifts(
|
||||
1199647239697,
|
||||
fake_ck("1199647239697"),
|
||||
client=FakeProbeClient(gifts=[FakeGiftResp(status=500, msg="活动已结束")]),
|
||||
)
|
||||
assert gifts == []
|
||||
assert "拉取礼包失败" in message and "500" in message
|
||||
|
||||
def test_gift_fetch_survives_exception(self):
|
||||
gifts, message = collab.fetch_activity_gifts(
|
||||
1199647239697,
|
||||
fake_ck("1199647239697"),
|
||||
client=FakeProbeClient(gifts=[RuntimeError("boom")]),
|
||||
)
|
||||
assert gifts == []
|
||||
assert "RuntimeError" in message
|
||||
|
||||
def test_gift_fetch_empty_list_hints_stale_sid(self):
|
||||
"""200 但 0 个商品 = SID 不是当前活动(真机 2026-10-16:2203 → 0,2277 → 30)。"""
|
||||
gifts, message = collab.fetch_activity_gifts(
|
||||
1199647239697,
|
||||
fake_ck("1199647239697"),
|
||||
sid=2203,
|
||||
client=FakeProbeClient(gifts=[FakeGiftResp(goods=[])]),
|
||||
)
|
||||
assert gifts == []
|
||||
assert "SID=2203" in message and "工作台" in message
|
||||
|
||||
def test_points_center_gift_fetch_parses_fields(self):
|
||||
gifts, message = collab.fetch_points_center_gifts(
|
||||
catalog = points_center.fetch_points_center_catalog(
|
||||
1199647239697,
|
||||
fake_ck("1199647239697"),
|
||||
client=FakeProbeClient(),
|
||||
)
|
||||
|
||||
assert gifts == [
|
||||
assert catalog.goods == [
|
||||
{
|
||||
"product_id": "hy-1001",
|
||||
"name": "不朽伊人大礼包",
|
||||
@@ -1587,7 +1515,7 @@ class TestCollabGifts(TestCollabBase):
|
||||
"icon": "",
|
||||
}
|
||||
]
|
||||
assert message == "已拉取 1 个礼包(积分中心)"
|
||||
assert catalog.message == "已拉取 1 个礼包(积分中心)"
|
||||
|
||||
def test_points_center_gift_fetch_loads_all_discovered_tabs(self):
|
||||
"""基础响应只给第一个页签;其余页签按 pageId + tabId 自动补拉。"""
|
||||
@@ -1626,7 +1554,7 @@ class TestCollabGifts(TestCollabBase):
|
||||
]
|
||||
)
|
||||
|
||||
gifts, message = collab.fetch_points_center_gifts(
|
||||
catalog = points_center.fetch_points_center_catalog(
|
||||
1199647239697,
|
||||
fake_ck("1199647239697"),
|
||||
client=client,
|
||||
@@ -1635,14 +1563,17 @@ class TestCollabGifts(TestCollabBase):
|
||||
scene=8,
|
||||
)
|
||||
|
||||
assert [gift["name"] for gift in gifts] == ["基础礼包", "套装-甜橙豚豚"]
|
||||
assert [gift["name"] for gift in catalog.goods] == [
|
||||
"基础礼包",
|
||||
"套装-甜橙豚豚",
|
||||
]
|
||||
assert client.points_calls[0]["page_code"] == "custom_page"
|
||||
assert client.points_calls[0]["source_id"] == "custom_source"
|
||||
assert client.points_calls[0]["scene"] == 8
|
||||
assert "waterfall_tab_id" not in client.points_calls[0]
|
||||
assert client.points_calls[1]["page_id"] == 343
|
||||
assert client.points_calls[1]["waterfall_tab_id"] == 433
|
||||
assert "2 个页签" in message
|
||||
assert "2 个页签" in catalog.message
|
||||
|
||||
def test_points_center_tab_override_uses_configured_ids(self):
|
||||
base_payload = {
|
||||
@@ -1670,7 +1601,7 @@ class TestCollabGifts(TestCollabBase):
|
||||
]
|
||||
)
|
||||
|
||||
gifts, message = collab.fetch_points_center_gifts(
|
||||
catalog = points_center.fetch_points_center_catalog(
|
||||
1199647239697,
|
||||
fake_ck("1199647239697"),
|
||||
client=client,
|
||||
@@ -1678,39 +1609,42 @@ class TestCollabGifts(TestCollabBase):
|
||||
tab_ids=[999],
|
||||
)
|
||||
|
||||
assert [gift["name"] for gift in gifts] == ["基础礼包", "手工页签礼包"]
|
||||
assert [gift["name"] for gift in catalog.goods] == [
|
||||
"基础礼包",
|
||||
"手工页签礼包",
|
||||
]
|
||||
assert client.points_calls[1]["page_id"] == 343
|
||||
assert client.points_calls[1]["waterfall_tab_id"] == 999
|
||||
assert "2 个页签" in message
|
||||
assert "2 个页签" in catalog.message
|
||||
|
||||
def test_points_center_gift_fetch_reports_failure(self):
|
||||
gifts, message = collab.fetch_points_center_gifts(
|
||||
catalog = points_center.fetch_points_center_catalog(
|
||||
1199647239697,
|
||||
fake_ck("1199647239697"),
|
||||
client=FakeProbeClient(
|
||||
points_gifts=[FakePointsCenterResp(code=500, message="接口炸了")]
|
||||
),
|
||||
)
|
||||
assert gifts == []
|
||||
assert "拉取礼包失败" in message and "500" in message
|
||||
assert catalog.goods == []
|
||||
assert "拉取商品失败" in catalog.message and "500" in catalog.message
|
||||
|
||||
def test_points_center_gift_fetch_survives_exception(self):
|
||||
gifts, message = collab.fetch_points_center_gifts(
|
||||
catalog = points_center.fetch_points_center_catalog(
|
||||
1199647239697,
|
||||
fake_ck("1199647239697"),
|
||||
client=FakeProbeClient(points_gifts=[RuntimeError("boom")]),
|
||||
)
|
||||
assert gifts == []
|
||||
assert "RuntimeError" in message
|
||||
assert catalog.goods == []
|
||||
assert "RuntimeError" in catalog.message
|
||||
|
||||
def test_points_center_gift_fetch_empty_list(self):
|
||||
gifts, message = collab.fetch_points_center_gifts(
|
||||
catalog = points_center.fetch_points_center_catalog(
|
||||
1199647239697,
|
||||
fake_ck("1199647239697"),
|
||||
client=FakeProbeClient(points_gifts=[FakePointsCenterResp(goods=[])]),
|
||||
)
|
||||
assert gifts == []
|
||||
assert "积分中心" in message and "空的" in message
|
||||
assert catalog.goods == []
|
||||
assert "积分中心" in catalog.message and "空的" in catalog.message
|
||||
|
||||
def test_points_center_parser_reads_tag75_points(self):
|
||||
from core.huya.shop_structs import parse_points_center_gifts
|
||||
@@ -1771,7 +1705,7 @@ class TestCollabGifts(TestCollabBase):
|
||||
)
|
||||
self.db.commit()
|
||||
|
||||
assert collab.resolve_points_center_config(self.db) == {
|
||||
assert points_center.resolve_points_center_config(self.db) == {
|
||||
"page_code": "custom_page",
|
||||
"source_id": "custom_source",
|
||||
"scene": 8,
|
||||
@@ -1793,6 +1727,7 @@ class TestCollabGifts(TestCollabBase):
|
||||
assert dumped["points_scene"] == "8"
|
||||
assert dumped["points_page_id"] == "343"
|
||||
assert dumped["points_tab_ids"] == "431,433"
|
||||
assert dumped["exchange_use_proxy"] == "0"
|
||||
|
||||
def test_preview_ignores_legacy_sid_and_uses_points_center(self):
|
||||
self.db.add(HuyaConfig(sid="2277"))
|
||||
|
||||
Reference in New Issue
Block a user