fix(ai): DeepSeek 余额按官方 balance_infos 解析
/user/balance 返回的是 {is_available, balance_infos[]},没有顶层 data,
旧实现永远解析出 None,连接测试会显示「余额 None CNY」。同时补全
TotHost 预设平台,新环境初始化后即可直接配 whmcs-panel。
This commit is contained in:
+32
-2
@@ -212,17 +212,47 @@ def test_registry_get_adapter_routing():
|
||||
|
||||
# --------------------------- AI 平台适配器 --------------------------- #
|
||||
def test_deepseek_balance_normalization():
|
||||
"""官方 /user/balance 返回 balance_infos,不是顶层 data"""
|
||||
adapter = DeepSeekAdapter({"api_key": "x"})
|
||||
mock = {"data": {"total_balance": "88.50", "granted_balance": "10.00"}}
|
||||
mock = {
|
||||
"is_available": True,
|
||||
"balance_infos": [{
|
||||
"currency": "CNY",
|
||||
"total_balance": "88.50",
|
||||
"granted_balance": "10.00",
|
||||
"topped_up_balance": "78.50",
|
||||
}],
|
||||
}
|
||||
with patch.object(DeepSeekAdapter, "_get", return_value=mock):
|
||||
acc = adapter.get_account()
|
||||
assert acc.balance == 88.5 # 字符串 -> float
|
||||
assert acc.currency == "CNY"
|
||||
|
||||
|
||||
def test_deepseek_balance_prefers_cny_when_multiple():
|
||||
adapter = DeepSeekAdapter({"api_key": "x"})
|
||||
mock = {"balance_infos": [
|
||||
{"currency": "USD", "total_balance": "1.00"},
|
||||
{"currency": "CNY", "total_balance": "7.20"},
|
||||
]}
|
||||
with patch.object(DeepSeekAdapter, "_get", return_value=mock):
|
||||
acc = adapter.get_account()
|
||||
assert acc.balance == 7.2
|
||||
assert acc.currency == "CNY"
|
||||
|
||||
|
||||
def test_deepseek_balance_supports_legacy_shape():
|
||||
"""兼容旧响应 data.total_balance,避免上线后旧 mock/代理失效"""
|
||||
adapter = DeepSeekAdapter({"api_key": "x"})
|
||||
with patch.object(DeepSeekAdapter, "_get", return_value={"data": {"total_balance": "5.00"}}):
|
||||
acc = adapter.get_account()
|
||||
assert acc.balance == 5.0
|
||||
|
||||
|
||||
def test_deepseek_test_connection_includes_balance():
|
||||
adapter = DeepSeekAdapter({"api_key": "x"})
|
||||
with patch.object(DeepSeekAdapter, "_get", return_value={"data": {"total_balance": "88.50"}}):
|
||||
mock = {"balance_infos": [{"currency": "CNY", "total_balance": "88.50"}]}
|
||||
with patch.object(DeepSeekAdapter, "_get", return_value=mock):
|
||||
result = adapter.test_connection()
|
||||
assert result["ok"] is True
|
||||
assert "88.5" in result["message"]
|
||||
|
||||
Reference in New Issue
Block a user