242 lines
7.9 KiB
Python
242 lines
7.9 KiB
Python
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_client_deribit_returns_correct_url(monkeypatch):
|
|
from tests.unit.test_settings import _minimal_env
|
|
|
|
for k, v in _minimal_env().items():
|
|
monkeypatch.setenv(k, v)
|
|
|
|
from cerbero_mcp.exchanges import build_client
|
|
from cerbero_mcp.settings import Settings
|
|
|
|
s = Settings()
|
|
c_test = await build_client(s, "deribit", "testnet")
|
|
c_live = await build_client(s, "deribit", "mainnet")
|
|
|
|
# DeribitClient espone base_url come property derivata da self.testnet
|
|
assert "test" in c_test.base_url.lower()
|
|
assert "test" not in c_live.base_url.lower()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_client_bybit_returns_correct_env(monkeypatch):
|
|
from tests.unit.test_settings import _minimal_env
|
|
|
|
for k, v in _minimal_env().items():
|
|
monkeypatch.setenv(k, v)
|
|
|
|
# BybitClient costruisce internamente httpx.AsyncClient: nessuna
|
|
# connessione reale finché non si invoca un metodo di rete.
|
|
from cerbero_mcp.exchanges import build_client
|
|
from cerbero_mcp.settings import Settings
|
|
|
|
s = Settings()
|
|
c_test = await build_client(s, "bybit", "testnet")
|
|
c_live = await build_client(s, "bybit", "mainnet")
|
|
|
|
assert c_test is not c_live
|
|
assert c_test.testnet is True
|
|
assert c_live.testnet is False
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_client_hyperliquid_returns_correct_env(monkeypatch):
|
|
from tests.unit.test_settings import _minimal_env
|
|
|
|
for k, v in _minimal_env().items():
|
|
monkeypatch.setenv(k, v)
|
|
|
|
from cerbero_mcp.exchanges import build_client
|
|
from cerbero_mcp.settings import Settings
|
|
|
|
s = Settings()
|
|
c_test = await build_client(s, "hyperliquid", "testnet")
|
|
c_live = await build_client(s, "hyperliquid", "mainnet")
|
|
|
|
assert c_test is not c_live
|
|
assert c_test.testnet is True
|
|
assert c_live.testnet is False
|
|
assert "test" in c_test.base_url.lower()
|
|
assert "test" not in c_live.base_url.lower()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_client_alpaca_returns_correct_env(monkeypatch):
|
|
from tests.unit.test_settings import _minimal_env
|
|
|
|
for k, v in _minimal_env().items():
|
|
monkeypatch.setenv(k, v)
|
|
|
|
# AlpacaClient (V2) usa httpx puro: il costruttore non apre connessioni
|
|
# reali (httpx.AsyncClient è lazy fino alla prima request), quindi nessuno
|
|
# stub SDK è necessario.
|
|
from cerbero_mcp.exchanges import build_client
|
|
from cerbero_mcp.settings import Settings
|
|
|
|
s = Settings()
|
|
c_test = await build_client(s, "alpaca", "testnet")
|
|
c_live = await build_client(s, "alpaca", "mainnet")
|
|
try:
|
|
assert c_test is not c_live
|
|
assert c_test.paper is True
|
|
assert c_live.paper is False
|
|
finally:
|
|
await c_test.aclose()
|
|
await c_live.aclose()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_client_macro_no_env_distinction(monkeypatch):
|
|
from tests.unit.test_settings import _minimal_env
|
|
|
|
for k, v in _minimal_env().items():
|
|
monkeypatch.setenv(k, v)
|
|
|
|
from cerbero_mcp.exchanges import build_client
|
|
from cerbero_mcp.exchanges.macro.client import MacroClient
|
|
from cerbero_mcp.settings import Settings
|
|
|
|
s = Settings()
|
|
c_test = await build_client(s, "macro", "testnet")
|
|
c_live = await build_client(s, "macro", "mainnet")
|
|
|
|
# entrambi sono MacroClient validi (env ignorato)
|
|
assert isinstance(c_test, MacroClient)
|
|
assert isinstance(c_live, MacroClient)
|
|
# Stesse credenziali (env ignorato)
|
|
assert c_test.fred_api_key == c_live.fred_api_key
|
|
assert c_test.finnhub_api_key == c_live.finnhub_api_key
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_client_sentiment_no_env_distinction(monkeypatch):
|
|
from tests.unit.test_settings import _minimal_env
|
|
|
|
for k, v in _minimal_env().items():
|
|
monkeypatch.setenv(k, v)
|
|
|
|
from cerbero_mcp.exchanges import build_client
|
|
from cerbero_mcp.exchanges.sentiment.client import SentimentClient
|
|
from cerbero_mcp.settings import Settings
|
|
|
|
s = Settings()
|
|
c_test = await build_client(s, "sentiment", "testnet")
|
|
c_live = await build_client(s, "sentiment", "mainnet")
|
|
|
|
# entrambi sono SentimentClient validi (env ignorato)
|
|
assert isinstance(c_test, SentimentClient)
|
|
assert isinstance(c_live, SentimentClient)
|
|
# Stesse credenziali (env ignorato)
|
|
assert c_test.cryptopanic_key == c_live.cryptopanic_key
|
|
assert c_test.lunarcrush_key == c_live.lunarcrush_key
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_deribit_url_from_env_overrides_default(monkeypatch):
|
|
"""DERIBIT_URL_TESTNET custom nel .env → builder lo passa al client."""
|
|
from tests.unit.test_settings import _minimal_env
|
|
|
|
env = _minimal_env(DERIBIT_URL_TESTNET="https://custom-test.example.com/api/v2")
|
|
for k, v in env.items():
|
|
monkeypatch.setenv(k, v)
|
|
|
|
from cerbero_mcp.exchanges import build_client
|
|
from cerbero_mcp.settings import Settings
|
|
|
|
s = Settings()
|
|
c = await build_client(s, "deribit", "testnet")
|
|
assert c.base_url == "https://custom-test.example.com/api/v2"
|
|
assert "custom-test.example.com" in c.base_url
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_deribit_url_live_from_env_overrides_default(monkeypatch):
|
|
from tests.unit.test_settings import _minimal_env
|
|
|
|
env = _minimal_env(DERIBIT_URL_LIVE="https://custom-live.example.com/api/v2")
|
|
for k, v in env.items():
|
|
monkeypatch.setenv(k, v)
|
|
|
|
from cerbero_mcp.exchanges import build_client
|
|
from cerbero_mcp.settings import Settings
|
|
|
|
s = Settings()
|
|
c = await build_client(s, "deribit", "mainnet")
|
|
assert c.base_url == "https://custom-live.example.com/api/v2"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_hyperliquid_url_from_env_overrides_default(monkeypatch):
|
|
from tests.unit.test_settings import _minimal_env
|
|
|
|
env = _minimal_env(HYPERLIQUID_URL_TESTNET="https://hl-custom.example.com")
|
|
for k, v in env.items():
|
|
monkeypatch.setenv(k, v)
|
|
|
|
from cerbero_mcp.exchanges import build_client
|
|
from cerbero_mcp.settings import Settings
|
|
|
|
s = Settings()
|
|
c = await build_client(s, "hyperliquid", "testnet")
|
|
assert c.base_url == "https://hl-custom.example.com"
|
|
# SDK override deve essere disponibile per init lazy
|
|
assert c._base_url_override == "https://hl-custom.example.com"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_bybit_url_from_env_overrides_default(monkeypatch):
|
|
"""Bybit (httpx): override BYBIT_URL_TESTNET applica direttamente a
|
|
`self.base_url`, usato come base di ogni richiesta REST V5."""
|
|
from tests.unit.test_settings import _minimal_env
|
|
|
|
env = _minimal_env(BYBIT_URL_TESTNET="https://bybit-custom.example.com")
|
|
for k, v in env.items():
|
|
monkeypatch.setenv(k, v)
|
|
|
|
from cerbero_mcp.exchanges import build_client
|
|
from cerbero_mcp.settings import Settings
|
|
|
|
s = Settings()
|
|
c = await build_client(s, "bybit", "testnet")
|
|
assert c.base_url == "https://bybit-custom.example.com"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_alpaca_url_from_env_overrides_default(monkeypatch):
|
|
"""Alpaca V2 (httpx): `base_url` override applica al solo trading
|
|
endpoint; data endpoints (data.alpaca.markets) restano hardcoded."""
|
|
from tests.unit.test_settings import _minimal_env
|
|
|
|
env = _minimal_env(ALPACA_URL_TESTNET="https://alpaca-custom.example.com")
|
|
for k, v in env.items():
|
|
monkeypatch.setenv(k, v)
|
|
|
|
from cerbero_mcp.exchanges import build_client
|
|
from cerbero_mcp.settings import Settings
|
|
|
|
s = Settings()
|
|
c = await build_client(s, "alpaca", "testnet")
|
|
try:
|
|
assert c.base_url == "https://alpaca-custom.example.com"
|
|
finally:
|
|
await c.aclose()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_client_unknown_exchange_raises(monkeypatch):
|
|
from tests.unit.test_settings import _minimal_env
|
|
|
|
for k, v in _minimal_env().items():
|
|
monkeypatch.setenv(k, v)
|
|
|
|
from cerbero_mcp.exchanges import build_client
|
|
from cerbero_mcp.settings import Settings
|
|
|
|
s = Settings()
|
|
with pytest.raises(ValueError):
|
|
await build_client(s, "ftx", "testnet")
|