Files
Cerbero-mcp/old/tests/alpaca/test_leverage_cap.py
T
Adriano bc75d3980a feat(V2): unified /mcp interface; retire Bybit/Alpaca to old/
Add a common cross-exchange interface (/mcp) over the integrated venues
(deribit, hyperliquid):

- get_instruments: uniform schema where each row carries its own
  `exchange`, `fees` (maker/taker, live from Deribit, null where the
  venue has no per-instrument schedule) and `history_start` (listing
  date, live from Deribit creation_timestamp), plus type/tick_size and a
  lossless `native` blob. Optional `exchange` filter; fan-out otherwise.
- get_historical: generalized to {exchange, instrument, interval,
  start_date, end_date}, returning a single chosen venue's candles.
  Consensus merge stays available on /mcp-cross.

New: routers/unified.py, exchanges/cross/instruments.py (normalizers),
UnifiedClient in cross/client.py, schemas in cross/tools.py. Deribit
get_instruments now also surfaces maker/taker_commission and
creation_timestamp (additive).

Retire Bybit and Alpaca from the API surface: move clients, routers,
settings classes and their tests under old/ (history preserved via
git mv); drop them from the builder, /mcp-cross dispatch and symbol_map.
Bybit remains a public funding/OI data source in sentiment (not the
trading client). IBKR is intentionally excluded from /mcp for now.

Docs: rewrite API_REFERENCE.md (remove Bybit/Alpaca, document /mcp,
clarify that data_timestamp is injected globally by middleware).

Tests: add unified-interface coverage; update cross/settings/builder/boot
tests for the reduced venue set. Fix a pre-existing flaky assertion in
the Hyperliquid signing test (r/s use eth_utils.to_hex like the official
SDK, so a leading zero byte yields <66 chars ~1/256 of the time).

323 passed, ruff clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-05-29 09:09:22 +00:00

47 lines
1.5 KiB
Python

from __future__ import annotations
import pytest
from cerbero_mcp.exchanges.alpaca.leverage_cap import enforce_leverage, get_max_leverage
from fastapi import HTTPException
def test_get_max_leverage_returns_creds_value():
creds = {"max_leverage": 4}
assert get_max_leverage(creds) == 4
def test_get_max_leverage_default_when_missing():
"""Default 1 (cash) se il secret non ha max_leverage."""
assert get_max_leverage({}) == 1
def test_enforce_leverage_pass_at_cap_one():
"""Alpaca cash account: cap 1, leverage 1 OK."""
creds = {"max_leverage": 1}
enforce_leverage(1, creds=creds, exchange="alpaca") # no raise
def test_enforce_leverage_reject_over_cap_one():
creds = {"max_leverage": 1}
with pytest.raises(HTTPException) as exc:
enforce_leverage(2, creds=creds, exchange="alpaca")
assert exc.value.status_code == 403
assert exc.value.detail["error"] == "LEVERAGE_CAP_EXCEEDED"
assert exc.value.detail["exchange"] == "alpaca"
assert exc.value.detail["requested"] == 2
assert exc.value.detail["max"] == 1
def test_enforce_leverage_reject_when_below_one():
creds = {"max_leverage": 1}
with pytest.raises(HTTPException) as exc:
enforce_leverage(0, creds=creds, exchange="alpaca")
assert exc.value.status_code == 403
def test_enforce_leverage_default_when_none():
"""Se requested è None, applica il cap come default."""
creds = {"max_leverage": 1}
result = enforce_leverage(None, creds=creds, exchange="alpaca")
assert result == 1