100 lines
2.7 KiB
Python
100 lines
2.7 KiB
Python
"""虎牙活动查询统一 SID 发现与接口分流测试。"""
|
|
|
|
from types import SimpleNamespace
|
|
|
|
from web.backend.services.huya_activity_query import query_huya_activity
|
|
|
|
|
|
class _Client:
|
|
def __init__(self, *, sid: int = 0):
|
|
self.sid = sid
|
|
self.score_calls: list[dict] = []
|
|
self.record_calls: list[dict] = []
|
|
|
|
def play_mall_query_allocation(self, **_kwargs):
|
|
if not self.sid:
|
|
raise RuntimeError("catalog unavailable")
|
|
return SimpleNamespace(
|
|
code=200,
|
|
payload={
|
|
"tag2": [
|
|
{
|
|
"tag2_2": {
|
|
"tag0": {
|
|
"tag7": self.sid,
|
|
}
|
|
}
|
|
}
|
|
]
|
|
},
|
|
)
|
|
|
|
def query_user_score(self, **kwargs):
|
|
self.score_calls.append(kwargs)
|
|
return SimpleNamespace(score=100)
|
|
|
|
def get_user_prize_records(self, **kwargs):
|
|
self.record_calls.append(kwargs)
|
|
return SimpleNamespace(records=[])
|
|
|
|
|
|
def test_activity_query_prefers_live_sid_for_score_and_records():
|
|
client = _Client(sid=2277)
|
|
|
|
result = query_huya_activity(
|
|
client,
|
|
uid=100,
|
|
cookie="yyuid=100",
|
|
configured_sid=2203,
|
|
want_score=True,
|
|
want_records=True,
|
|
)
|
|
|
|
assert result.sid == 2277
|
|
assert result.sid_source == "queryAllocation"
|
|
assert result.configured_sid == 2203
|
|
assert client.score_calls[0]["sid"] == 2277
|
|
assert client.record_calls[0]["sid"] == 2277
|
|
|
|
|
|
def test_activity_query_falls_back_to_configured_sid_when_catalog_fails():
|
|
client = _Client()
|
|
|
|
result = query_huya_activity(
|
|
client,
|
|
uid=100,
|
|
cookie="yyuid=100",
|
|
configured_sid=2203,
|
|
want_score=True,
|
|
)
|
|
|
|
assert result.sid == 2203
|
|
assert result.sid_source == "config"
|
|
assert "catalog unavailable" in result.sid_error
|
|
assert client.score_calls[0]["sid"] == 2203
|
|
|
|
|
|
def test_activity_query_reuses_provided_sid_without_catalog_call(monkeypatch):
|
|
client = _Client(sid=2277)
|
|
monkeypatch.setattr(
|
|
client,
|
|
"play_mall_query_allocation",
|
|
lambda **_kwargs: (_ for _ in ()).throw(
|
|
AssertionError("provided SID must not trigger another catalog request")
|
|
),
|
|
)
|
|
|
|
result = query_huya_activity(
|
|
client,
|
|
uid=100,
|
|
cookie="yyuid=100",
|
|
configured_sid=2203,
|
|
current_sid=2277,
|
|
current_sid_source="queryAllocation",
|
|
want_score=True,
|
|
)
|
|
|
|
assert result.sid == 2277
|
|
assert result.sid_source == "queryAllocation"
|
|
assert client.score_calls[0]["sid"] == 2277
|