feat(huya): 统一实时商品目录并增加兑换代理开关
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
"""Huya live goods selection and stale snapshot protection tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from types import SimpleNamespace
|
||||
|
||||
from web.backend.models import HuyaAccount, HuyaGoodsSnapshot, User
|
||||
from web.backend.routers.huya import list_goods, list_live_goods
|
||||
from web.backend.services import huya_points_exchange as exchange
|
||||
from web.backend.services import huya_runner_goods
|
||||
from web.backend.services.huya_runner import HuyaBatchRunner
|
||||
|
||||
|
||||
def _seed_account(db) -> tuple[User, HuyaAccount]:
|
||||
user = User(username="operator", password_hash="hash", role="super_admin")
|
||||
account = HuyaAccount(
|
||||
uid="1199669301102",
|
||||
yyuid="1199669301102",
|
||||
nickname="小银紫资源",
|
||||
cookie="yyuid=1199669301102; udb_uid=1199669301102",
|
||||
)
|
||||
db.add_all([user, account])
|
||||
db.commit()
|
||||
return user, account
|
||||
|
||||
|
||||
def test_live_goods_endpoint_returns_current_spu(db, monkeypatch):
|
||||
user, account = _seed_account(db)
|
||||
monkeypatch.setattr(
|
||||
"web.backend.routers.huya.workbench.fetch_points_center_catalog",
|
||||
lambda *_args, **_kwargs: SimpleNamespace(
|
||||
goods=[
|
||||
{
|
||||
"product_id": "hy-9391246",
|
||||
"name": "小狗蛇点点大礼包",
|
||||
"price": 3960,
|
||||
"remain_text": "2703",
|
||||
}
|
||||
],
|
||||
message="已拉取 1 个礼包",
|
||||
sid=2277,
|
||||
),
|
||||
)
|
||||
|
||||
rows = list_live_goods(account_id=account.id, db=db, current=user)
|
||||
|
||||
assert [row.product_id for row in rows] == ["hy-9391246"]
|
||||
assert rows[0].raw["product_id"] == "hy-9391246"
|
||||
|
||||
|
||||
def test_list_goods_ignores_legacy_numeric_prize_ids(db):
|
||||
user, account = _seed_account(db)
|
||||
now = datetime.now(UTC)
|
||||
db.add_all(
|
||||
[
|
||||
HuyaGoodsSnapshot(
|
||||
account_id=account.id,
|
||||
sid=2277,
|
||||
product_id="13649",
|
||||
name="小狗蛇点点大礼包",
|
||||
updated_at=now,
|
||||
),
|
||||
HuyaGoodsSnapshot(
|
||||
account_id=account.id,
|
||||
sid=2277,
|
||||
product_id="hy-9391246",
|
||||
name="小狗蛇点点大礼包",
|
||||
updated_at=now - timedelta(minutes=1),
|
||||
),
|
||||
]
|
||||
)
|
||||
db.commit()
|
||||
|
||||
rows = list_goods(db=db, current=user)
|
||||
|
||||
assert [row.product_id for row in rows] == ["hy-9391246"]
|
||||
|
||||
|
||||
def test_exchange_resolves_stale_prize_id_to_current_live_spu(monkeypatch):
|
||||
runner = HuyaBatchRunner(
|
||||
db=None,
|
||||
batch_id="live-goods-repair",
|
||||
task_type="exchange_goods",
|
||||
payload={
|
||||
"product_id": "hy-13649",
|
||||
"product_name": "小狗蛇点点大礼包",
|
||||
},
|
||||
)
|
||||
client = SimpleNamespace()
|
||||
marks = []
|
||||
prepared_args = {}
|
||||
|
||||
monkeypatch.setattr(runner, "_http_client", lambda **_kwargs: client)
|
||||
monkeypatch.setattr(
|
||||
huya_runner_goods,
|
||||
"fetch_points_center_catalog",
|
||||
lambda *_args, **_kwargs: SimpleNamespace(
|
||||
goods=[
|
||||
{
|
||||
"product_id": "hy-9391246",
|
||||
"name": "小狗蛇点点大礼包",
|
||||
"price": 3960,
|
||||
"remain_text": "2703",
|
||||
}
|
||||
],
|
||||
message="已拉取 1 个礼包",
|
||||
),
|
||||
)
|
||||
|
||||
def fake_prepare(_client, **kwargs):
|
||||
prepared_args.update(kwargs)
|
||||
return exchange.PointsExchangeOutcome(
|
||||
exchange.EXCHANGE_STATUS_EXCHANGED,
|
||||
"兑换成功",
|
||||
target={"name": "小狗蛇点点大礼包"},
|
||||
order={"order_id": 101},
|
||||
)
|
||||
|
||||
monkeypatch.setattr(huya_runner_goods, "prepare_exchange", fake_prepare)
|
||||
monkeypatch.setattr(
|
||||
runner,
|
||||
"_mark_task",
|
||||
lambda _db, _task, status, message, result=None: marks.append(
|
||||
(status, message, result)
|
||||
),
|
||||
)
|
||||
|
||||
runner._execute_exchange_goods(
|
||||
None,
|
||||
SimpleNamespace(id=1),
|
||||
HuyaAccount(uid="1199669301102", cookie="yyuid=1199669301102"),
|
||||
{
|
||||
"uid": 1199669301102,
|
||||
"cookie": "yyuid=1199669301102",
|
||||
},
|
||||
{"room_pid": "1199650619883", "pay_channel": "Zfb"},
|
||||
)
|
||||
|
||||
assert prepared_args["spu_id"] == "hy-9391246"
|
||||
assert marks[-1][0] == "success"
|
||||
assert marks[-1][2]["submitted_product_id"] == "hy-13649"
|
||||
assert marks[-1][2]["resolved_product_id"] == "hy-9391246"
|
||||
Reference in New Issue
Block a user