【积分商店】onmyoji_gacha 新增积分商店与兑换抽卡命令
This commit is contained in:
@@ -371,3 +371,98 @@ async def test_daily_sign_in_repeat_does_not_award_points():
|
||||
|
||||
assert (success, balance, status) == (False, 0, "signed_already_or_failed")
|
||||
assert calls == [("sign-in", "10001", 20)]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_shop_products_and_exchange_call_xapi_with_auth(fake_aiohttp):
|
||||
responses, calls = fake_aiohttp
|
||||
responses.extend(
|
||||
[
|
||||
success_data(
|
||||
{
|
||||
"user_id": "10001",
|
||||
"date": "2026-08-15",
|
||||
"balance": 120,
|
||||
"extra_draws_today": 0,
|
||||
"daily_limit": 3,
|
||||
"draws_left": 2,
|
||||
"products": [
|
||||
{
|
||||
"product_id": "extra_draw",
|
||||
"name": "抽卡机会",
|
||||
"description": "消耗 99 Bot 积分兑换 1 次抽卡机会,当日有效,次日清零",
|
||||
"price_points": 99,
|
||||
"effect": "extra_draw",
|
||||
"valid_scope": "today",
|
||||
}
|
||||
],
|
||||
}
|
||||
),
|
||||
success_data(
|
||||
{
|
||||
"success": True,
|
||||
"product_id": "extra_draw",
|
||||
"cost_points": 99,
|
||||
"balance": 21,
|
||||
"date": "2026-08-15",
|
||||
"extra_draws_today": 1,
|
||||
"daily_limit": 3,
|
||||
"draws_left": 3,
|
||||
}
|
||||
),
|
||||
]
|
||||
)
|
||||
manager = DataManager()
|
||||
|
||||
products = await manager.get_shop_products("10001")
|
||||
exchanged = await manager.exchange_shop_product("10001", "extra_draw")
|
||||
|
||||
assert products["products"][0]["price_points"] == 99
|
||||
assert products["balance"] == 120
|
||||
assert products["draws_left"] == 2
|
||||
assert exchanged["success"] is True
|
||||
assert exchanged["balance"] == 21
|
||||
assert exchanged["draws_left"] == 3
|
||||
assert calls[0]["method"] == "GET"
|
||||
assert calls[0]["url"] == "http://xapi.test/bot/gacha/shop/products"
|
||||
assert calls[0]["params"] == {"user": "robot", "token": "secret", "user_id": "10001"}
|
||||
assert calls[1]["method"] == "POST"
|
||||
assert calls[1]["json"] == {
|
||||
"user": "robot",
|
||||
"token": "secret",
|
||||
"user_id": "10001",
|
||||
"product_id": "extra_draw",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_shop_exchange_rejection_keeps_message_from_xapi(fake_aiohttp):
|
||||
"""业务拒绝时插件拿到 data.success=false 与 message,不做本地判断。"""
|
||||
|
||||
responses, calls = fake_aiohttp
|
||||
responses.append(
|
||||
success_data({"success": False, "message": "Bot积分不足,还需49积分", "balance": 50})
|
||||
)
|
||||
manager = DataManager()
|
||||
|
||||
exchanged = await manager.exchange_shop_product("10001", "extra_draw")
|
||||
|
||||
assert exchanged == {"success": False, "message": "Bot积分不足,还需49积分", "balance": 50}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_shop_methods_return_none_on_network_error(monkeypatch):
|
||||
"""网络异常时商店方法返回 None,与插件其他 API 方法一致。"""
|
||||
|
||||
class FailingSession:
|
||||
async def __aenter__(self):
|
||||
raise aiohttp.ClientError("offline")
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb):
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(data_manager_module.aiohttp, "ClientSession", lambda: FailingSession())
|
||||
manager = DataManager()
|
||||
|
||||
assert await manager.get_shop_products("10001") is None
|
||||
assert await manager.exchange_shop_product("10001", "extra_draw") is None
|
||||
|
||||
Reference in New Issue
Block a user