fix(trading): cart count status=101 识别为空车,区分空车与获取失败
This commit is contained in:
@@ -1138,4 +1138,80 @@ async def test_dump_debug_snapshot_swallows_its_own_errors(tmp_path):
|
||||
assert list((tmp_path / "t1").glob("debug-*")) == []
|
||||
|
||||
|
||||
# ---- _query_cart_count:空车(status=101)与获取失败(其他 status)必须区分 ----
|
||||
#
|
||||
# 响应 body 均为 2026-08-16 真账号实测原样(scripts/probe_cart_count.py +
|
||||
# .probe/cart_count/):空车是合法响应,当失败抛错会让 clear_cart 末尾校验拿 -1、
|
||||
# 开单前清理被 runner 误判为「未清空」。
|
||||
|
||||
|
||||
class _FakeCartCountRequest:
|
||||
"""APIRequestContext 替身:回固定 body,记录入参"""
|
||||
|
||||
def __init__(self, body: str):
|
||||
self._body = body
|
||||
self.calls: list[tuple[str, dict[str, str]]] = []
|
||||
|
||||
async def get(self, url: str, headers: dict[str, str] | None = None) -> Any:
|
||||
self.calls.append((url, headers or {}))
|
||||
|
||||
class _Resp:
|
||||
def __init__(self, text: str):
|
||||
self._text = text
|
||||
|
||||
async def text(self) -> str:
|
||||
return self._text
|
||||
|
||||
return _Resp(self._body)
|
||||
|
||||
|
||||
class _FakeCartCountContext:
|
||||
def __init__(self, body: str):
|
||||
self.request = _FakeCartCountRequest(body)
|
||||
|
||||
|
||||
def _build_cart_count_site(body: str) -> SiteInteractor:
|
||||
site = SiteInteractor(auth_session=None, settings=None) # type: ignore[arg-type]
|
||||
site._context = _FakeCartCountContext(body) # type: ignore[assignment]
|
||||
return site
|
||||
|
||||
|
||||
async def test_query_cart_count_non_empty_returns_count():
|
||||
site = _build_cart_count_site(
|
||||
'callBack({"status":"100","message":"success.","count":"3"})'
|
||||
)
|
||||
|
||||
raw_status, count = await site._query_cart_count()
|
||||
|
||||
assert (raw_status, count) == ("100", 3)
|
||||
|
||||
|
||||
async def test_query_cart_count_empty_cart_is_not_a_failure():
|
||||
"""status=101 + count="" 是空车(合法响应),返回 ("101", 0) 而不是抛错"""
|
||||
site = _build_cart_count_site(
|
||||
'callBack({"status":"101","message":"value not found.","count":""})'
|
||||
)
|
||||
|
||||
raw_status, count = await site._query_cart_count()
|
||||
|
||||
assert (raw_status, count) == ("101", 0)
|
||||
|
||||
|
||||
async def test_query_cart_count_rejected_request_raises():
|
||||
"""status=300(referer/sid 被站点拒绝)才是「获取失败」"""
|
||||
site = _build_cart_count_site(
|
||||
'callBack({"status":"300","message":"referer is empty value.","count":""})'
|
||||
)
|
||||
|
||||
with pytest.raises(CartOperationError):
|
||||
await site._query_cart_count()
|
||||
|
||||
|
||||
async def test_query_cart_count_unparseable_body_raises():
|
||||
site = _build_cart_count_site("<html>not jsonp</html>")
|
||||
|
||||
with pytest.raises(CartOperationError):
|
||||
await site._query_cart_count()
|
||||
|
||||
|
||||
# ---- helper ----
|
||||
|
||||
Reference in New Issue
Block a user