Skip to content

"Vibe-Trading: Your Personal Trading Agent"

49.7 KB
Raw Blame
"""Tests for the direct-SDK trading connectors (Tiger, Longbridge).

Layer A is read-only; these tests exercise the parts that do not require the
optional broker SDKs or live credentials: profile registration, the paper/live
identity guard, config resolution, read/write classification, secret redaction,
and the service dispatch degrading cleanly when nothing is configured.
"""

from __future__ import annotations

import json
from decimal import Decimal
from types import SimpleNamespace

import pytest

from backtest.loaders import longbridge as longbridge_loader
from src.live.classification import ToolClass
from src.trading.connectors.longbridge import credentials as lb_credentials
from src.trading import profiles, service
from src.trading.connectors.alpaca import sdk as al
from src.trading.connectors.alpaca.classification import ALPACA_TOOL_CLASS
from src.trading.connectors.binance import sdk as bn
from src.trading.connectors.binance.classification import BINANCE_TOOL_CLASS
from src.trading.connectors.mt5 import sdk as mt5
from src.trading.connectors.dhan import sdk as dh
from src.trading.connectors.dhan.classification import DHAN_TOOL_CLASS
from src.trading.connectors.futu import sdk as ft
from src.trading.connectors.futu.classification import FUTU_TOOL_CLASS
from src.trading.connectors.longbridge import sdk as lb
from src.trading.connectors.longbridge.classification import LONGBRIDGE_TOOL_CLASS
from src.trading.connectors.okx import sdk as ox
from src.trading.connectors.okx.classification import OKX_TOOL_CLASS
from src.trading.connectors.shoonya import sdk as sh
from src.trading.connectors.shoonya.classification import SHOONYA_TOOL_CLASS
from src.trading.connectors.etoro import client as etoro_client
from src.trading.connectors.tiger import sdk as tg
from src.trading.connectors.tiger.classification import TIGER_TOOL_CLASS

pytestmark = pytest.mark.unit


# --------------------------------------------------------------------------- #
# Profile registration
# --------------------------------------------------------------------------- #


def test_sdk_profiles_registered() -> None:
    """All broker connectors register paper and read-only live profiles."""
    ids = {p.id for p in profiles.list_profiles()}
    assert {
        "tiger-paper-sdk", "tiger-live-sdk-readonly",
        "longbridge-paper-sdk", "longbridge-live-sdk-readonly",
        "alpaca-paper-sdk", "alpaca-live-sdk-readonly",
        "okx-paper-sdk", "okx-live-sdk-readonly",
        "binance-paper-sdk", "binance-live-sdk-readonly",
        "futu-paper-sdk", "futu-live-sdk-readonly",
        "dhan-paper-sdk", "dhan-live-sdk-readonly",
        "shoonya-paper-sdk", "shoonya-live-sdk-readonly",
        "etoro-paper-sdk", "etoro-paper-trade",
        "etoro-live-sdk-readonly", "etoro-live-trade",
        "kis-paper-sdk", "kis-paper-trade", "kis-live-sdk-readonly",
        "upbit-paper-sdk", "upbit-paper-trade", "upbit-live-sdk-readonly",
        "toss-live-sdk-readonly",
    } <= ids


def test_no_discriminator_brokers_expose_no_live_trade_profile() -> None:
    """Brokers without a runtime paper/live discriminator (Longbridge, Dhan,
    Shoonya) must NOT register any live order-placing profile — the Longbridge
    precedent. A ``*-live-trade`` profile here would be a red-line regression."""
    ids = {p.id for p in profiles.list_profiles()}
    for broker in ("longbridge", "dhan", "shoonya", "upbit"):
        assert f"{broker}-live-trade" not in ids
        # No live profile for these brokers may advertise an order capability.
        for p in profiles.list_profiles():
            if p.connector == broker and p.environment == "live":
                assert not any(".place" in cap or "requires_mandate" in cap for cap in p.capabilities)


@pytest.mark.parametrize(
    "profile_id, connector, environment",
    [
        ("tiger-paper-sdk", "tiger", "paper"),
        ("tiger-live-sdk-readonly", "tiger", "live"),
        ("longbridge-paper-sdk", "longbridge", "paper"),
        ("longbridge-live-sdk-readonly", "longbridge", "live"),
        ("alpaca-paper-sdk", "alpaca", "paper"),
        ("alpaca-live-sdk-readonly", "alpaca", "live"),
        ("okx-paper-sdk", "okx", "paper"),
        ("okx-live-sdk-readonly", "okx", "live"),
        ("binance-paper-sdk", "binance", "paper"),
        ("binance-live-sdk-readonly", "binance", "live"),
        ("futu-paper-sdk", "futu", "paper"),
        ("futu-live-sdk-readonly", "futu", "live"),
        ("dhan-paper-sdk", "dhan", "paper"),
        ("dhan-live-sdk-readonly", "dhan", "live"),
        ("shoonya-paper-sdk", "shoonya", "paper"),
        ("shoonya-live-sdk-readonly", "shoonya", "live"),
        ("etoro-paper-sdk", "etoro", "paper"),
        ("etoro-live-sdk-readonly", "etoro", "live"),
    ],
)
def test_sdk_profiles_are_readonly_broker_sdk(profile_id, connector, environment) -> None:
    """Layer A profiles are broker_sdk transport and strictly read-only."""
    profile = profiles.profile_by_id(profile_id)
    assert profile.connector == connector
    assert profile.environment == environment
    assert profile.transport == "broker_sdk"
    assert profile.readonly is True
    # No order-placing / mandate-gated capability is advertised in Layer A.
    assert not any(".place" in cap or "requires_mandate" in cap for cap in profile.capabilities)


# --------------------------------------------------------------------------- #
# Tiger paper/live identity guard (17-digit account rule)
# --------------------------------------------------------------------------- #


@pytest.mark.parametrize(
    "account, is_paper",
    [
        ("20191106192858300", True),   # 17-digit paper
        ("51230321", False),            # prime/standard
        ("U12300123", False),           # global
        ("", False),
        ("2019110619285830", False),    # 16 digits
        ("201911061928583000", False),  # 18 digits
    ],
)
def test_tiger_is_paper_account(account, is_paper) -> None:
    assert tg.is_paper_account(account) is is_paper


def test_tiger_paper_profile_rejects_live_account() -> None:
    """A paper profile pointed at a non-17-digit account fails closed."""
    cfg = tg.TigerConfig(tiger_id="x", private_key_path="x", account="U12300123", profile="paper")
    with pytest.raises(tg.TigerProfileMismatchError):
        tg._assert_profile(cfg)


def test_tiger_live_profile_rejects_paper_account() -> None:
    """A live profile pointed at a 17-digit paper account fails closed."""
    cfg = tg.TigerConfig(tiger_id="x", private_key_path="x", account="20191106192858300", profile="live-readonly")
    with pytest.raises(tg.TigerProfileMismatchError):
        tg._assert_profile(cfg)


def test_tiger_paper_profile_accepts_paper_account() -> None:
    cfg = tg.TigerConfig(tiger_id="x", private_key_path="x", account="20191106192858300", profile="paper")
    tg._assert_profile(cfg)  # must not raise


# --------------------------------------------------------------------------- #
# Config resolution
# --------------------------------------------------------------------------- #


def test_tiger_build_config_merges_profile_then_overrides(monkeypatch, tmp_path) -> None:
    monkeypatch.setattr(tg, "get_runtime_root", lambda: tmp_path)
    cfg = tg.build_config({"profile": "paper"}, {"account": "20191106192858300"})
    assert cfg.profile == "paper"
    assert cfg.account == "20191106192858300"


def test_tiger_invalid_profile_rejected() -> None:
    with pytest.raises(tg.TigerConfigError):
        tg.TigerConfig.from_mapping({"profile": "live-trade-now"})


def test_longbridge_build_config_and_region(monkeypatch, tmp_path) -> None:
    for env_name in (
        "LONGBRIDGE_APP_KEY",
        "LONGBRIDGE_APP_SECRET",
        "LONGBRIDGE_ACCESS_TOKEN",
    ):
        monkeypatch.delenv(env_name, raising=False)
    monkeypatch.setattr(lb, "get_runtime_root", lambda: tmp_path)
    monkeypatch.setattr(lb_credentials, "get_runtime_root", lambda: tmp_path)
    cfg = lb.build_config({"profile": "live-readonly", "region": "cn"}, None)
    assert cfg.profile == "live-readonly"
    assert cfg.region == "cn"


def test_longbridge_invalid_region_rejected() -> None:
    with pytest.raises(lb.LongbridgeConfigError):
        lb.LongbridgeConfig.from_mapping({"region": "moon"})


def test_longbridge_quote_context_uses_from_apikey_on_sdk_4(monkeypatch) -> None:
    """SDK 4.0+ refuses ``Config(...)``; the connector must use the factory."""
    factory_calls: list[tuple[str, ...]] = []
    built = object()

    class Sdk4Config:
        def __new__(cls, *args: object, **kwargs: object) -> "Sdk4Config":
            raise TypeError("cannot create 'builtins.Config' instances")

        @staticmethod
        def from_apikey(*args: str) -> object:
            factory_calls.append(args)
            return built

    fake_openapi = SimpleNamespace(
        Config=Sdk4Config, QuoteContext=lambda config: config
    )
    monkeypatch.setattr(lb, "_require_openapi", lambda: fake_openapi)
    cfg = lb.LongbridgeConfig(
        app_key="k", app_secret="s", access_token="t", _credential_source="environment"
    )

    assert lb._quote_context(cfg) is built
    assert factory_calls == [("k", "s", "t")]


def test_longbridge_with_overrides_preserves_atomic_credentials() -> None:
    cfg = lb.LongbridgeConfig(
        app_key="atomic-key",
        app_secret="atomic-secret",
        access_token="atomic-token",
        _credential_source="environment",
    )

    updated = cfg.with_overrides(
        app_key="ignored-key", profile="live-readonly", region="cn"
    )

    assert (updated.app_key, updated.app_secret, updated.access_token) == (
        "atomic-key",
        "atomic-secret",
        "atomic-token",
    )
    assert updated._credential_source == "environment"
    assert updated.profile == "live-readonly"
    assert updated.region == "cn"


def test_longbridge_public_config_redacts_secrets() -> None:
    """Secret material must never appear in status payloads or config reprs."""
    values = {
        "app_key": "repr-distinctive-app-key-7f31",
        "app_secret": "repr-distinctive-app-secret-8a42",
        "access_token": "repr-distinctive-access-token-9b53",
    }
    cfg = lb.LongbridgeConfig(**values)
    pub = lb._public_config(cfg)
    assert pub["app_secret"] == "***redacted***"
    assert pub["access_token"] == "***redacted***"
    assert pub["app_key"].endswith("***")
    assert all(value not in repr(cfg) for value in values.values())
    assert all(value not in repr(pub) for value in values.values())


def test_longbridge_balance_keeps_financing_and_per_currency_cash() -> None:
    """``total_cash`` nets every currency, so a borrowed HKD leg only shows per currency."""
    balance = SimpleNamespace(
        currency="HKD",
        # USD 1,000 at 7.8 net of the HKD -500 leg.
        total_cash=Decimal("7300.00"),
        max_finance_amount=Decimal("5000.00"),
        remaining_finance_amount=Decimal("4000.00"),
        cash_infos=[
            SimpleNamespace(
                currency="USD",
                available_cash=Decimal("1000.00"),
                withdraw_cash=Decimal("900.00"),
                frozen_cash=Decimal("10.00"),
                settling_cash=Decimal("20.00"),
            ),
            SimpleNamespace(
                currency="HKD",
                available_cash=Decimal("-500.00"),
                withdraw_cash=Decimal("-600.00"),
                frozen_cash=Decimal("500.00"),
                settling_cash=Decimal("-200.00"),
            ),
        ],
    )
    row = lb._balance_to_dict(balance)
    assert row["max_finance_amount"] == 5000.0
    assert row["remaining_finance_amount"] == 4000.0
    assert row["cash_infos"] == [
        {
            "currency": "USD",
            "available_cash": 1000.0,
            "withdraw_cash": 900.0,
            "frozen_cash": 10.0,
            "settling_cash": 20.0,
        },
        {
            "currency": "HKD",
            "available_cash": -500.0,
            "withdraw_cash": -600.0,
            "frozen_cash": 500.0,
            "settling_cash": -200.0,
        },
    ]


def test_longbridge_balance_sparse_reply_reads_as_none() -> None:
    """Fields the SDK omits read as missing, never as zero cash or headroom."""
    row = lb._balance_to_dict(SimpleNamespace(currency="HKD", total_cash=1))
    assert row["cash_infos"] == []
    assert row["max_finance_amount"] is None
    assert row["remaining_finance_amount"] is None

    row = lb._balance_to_dict(
        SimpleNamespace(currency="HKD", cash_infos=[SimpleNamespace(currency="USD")])
    )
    assert row["cash_infos"] == [
        {
            "currency": "USD",
            "available_cash": None,
            "withdraw_cash": None,
            "frozen_cash": None,
            "settling_cash": None,
        }
    ]


def _set_longbridge_environment(monkeypatch, values) -> None:
    for field, env_name in {
        "app_key": "LONGBRIDGE_APP_KEY",
        "app_secret": "LONGBRIDGE_APP_SECRET",
        "access_token": "LONGBRIDGE_ACCESS_TOKEN",
    }.items():
        monkeypatch.setenv(env_name, values[field])


def test_connector_uses_environment_credentials(monkeypatch, tmp_path) -> None:
    values = {
        "app_key": "connector-environment-key",
        "app_secret": "connector-environment-secret",
        "access_token": "connector-environment-token",
    }
    _set_longbridge_environment(monkeypatch, values)
    monkeypatch.setattr(lb, "get_runtime_root", lambda: tmp_path)
    monkeypatch.setattr(lb_credentials, "get_runtime_root", lambda: tmp_path)

    cfg = lb.build_config({"profile": "live-readonly", "region": "cn"}, None)

    assert (cfg.app_key, cfg.app_secret, cfg.access_token) == tuple(values.values())
    assert cfg.profile == "live-readonly"
    assert cfg.region == "cn"
    monkeypatch.setattr(lb, "longbridge_available", lambda: False)
    assert lb.check_status(cfg)["credential_source"] == "environment"


def test_loader_and_connector_resolve_same_source(monkeypatch, tmp_path) -> None:
    values = {
        "app_key": "shared-file-key",
        "app_secret": "shared-file-secret",
        "access_token": "shared-file-token",
    }
    for env_name in (
        "LONGBRIDGE_APP_KEY",
        "LONGBRIDGE_APP_SECRET",
        "LONGBRIDGE_ACCESS_TOKEN",
    ):
        monkeypatch.delenv(env_name, raising=False)
    (tmp_path / "longbridge.json").write_text(json.dumps(values), encoding="utf-8")
    monkeypatch.setattr(lb, "get_runtime_root", lambda: tmp_path)
    monkeypatch.setattr(lb_credentials, "get_runtime_root", lambda: tmp_path)

    connector = lb.build_config()
    loader = longbridge_loader.LongbridgeLoader()

    assert (connector.app_key, connector.app_secret, connector.access_token) == (
        loader._app_key,
        loader._app_secret,
        loader._access_token,
    )
    monkeypatch.setattr(lb, "longbridge_available", lambda: False)
    assert lb.check_status(connector)["credential_source"] == "runtime_file"
    assert loader._credential_source == "runtime_file"


def test_connector_reports_conflict_without_sdk_call(monkeypatch, tmp_path) -> None:
    environment = {
        "app_key": "conflict-environment-key",
        "app_secret": "conflict-environment-secret",
        "access_token": "conflict-environment-token",
    }
    runtime_file = {
        "app_key": "conflict-file-key",
        "app_secret": "conflict-file-secret",
        "access_token": "conflict-file-token",
    }
    _set_longbridge_environment(monkeypatch, environment)
    (tmp_path / "longbridge.json").write_text(json.dumps(runtime_file), encoding="utf-8")
    monkeypatch.setattr(lb, "get_runtime_root", lambda: tmp_path)
    monkeypatch.setattr(lb_credentials, "get_runtime_root", lambda: tmp_path)
    monkeypatch.setattr(
        lb,
        "_trade_context",
        lambda cfg: (_ for _ in ()).throw(AssertionError("SDK must not initialize")),
    )

    report = lb.check_status(lb.build_config())

    assert report["configured"] is False
    assert report["connection_state"] == "error"
    assert report["credential_source"] is None
    assert report["error_code"] == "credentials_conflict"
    assert all(
        field in report["error"]
        for field in ("app_key", "app_secret", "access_token")
    )
    with pytest.raises(lb.LongbridgeConfigError, match="sources conflict"):
        lb._require_resolved_config(lb.build_config())


def test_connector_status_redacts_credentials(monkeypatch, tmp_path) -> None:
    values = {
        "app_key": "status-sensitive-key",
        "app_secret": "status-sensitive-secret",
        "access_token": "status-sensitive-token",
    }
    secret_exception = RuntimeError(
        "authentication failed for " + "/".join(values.values())
    )
    _set_longbridge_environment(monkeypatch, values)
    monkeypatch.setattr(lb, "get_runtime_root", lambda: tmp_path)
    monkeypatch.setattr(lb_credentials, "get_runtime_root", lambda: tmp_path)
    monkeypatch.setattr(lb, "longbridge_available", lambda: True)
    monkeypatch.setattr(
        lb,
        "_trade_context",
        lambda cfg: SimpleNamespace(
            account_balance=lambda: (_ for _ in ()).throw(secret_exception)
        ),
    )

    report = lb.check_status(lb.build_config())
    serialized = str(report)

    assert report["configured"] is True
    assert report["connection_state"] == "error"
    assert report["error_code"] == "authentication_failed"
    assert report["error"] == "Longbridge authentication failed."
    assert all(value not in serialized for value in values.values())
    assert report["error"].__class__ is str
    assert secret_exception not in _exception_chain_from_payload(report)


def _exception_chain_from_payload(payload) -> tuple[BaseException, ...]:
    """Return exceptions publicly reachable from a returned payload."""
    seen: set[int] = set()
    found: list[BaseException] = []
    pending = list(payload.values()) if isinstance(payload, dict) else [payload]
    while pending:
        value = pending.pop()
        if id(value) in seen:
            continue
        seen.add(id(value))
        if isinstance(value, BaseException):
            found.append(value)
            if value.__cause__ is not None:
                pending.append(value.__cause__)
            if value.__context__ is not None:
                pending.append(value.__context__)
        elif isinstance(value, dict):
            pending.extend(value.values())
        elif isinstance(value, (list, tuple, set)):
            pending.extend(value)
    return tuple(found)


# --------------------------------------------------------------------------- #
# Read/write classification (live gate input)
# --------------------------------------------------------------------------- #


def test_tiger_order_ops_classified_write() -> None:
    for name in ("place_order", "cancel_order", "modify_order"):
        assert TIGER_TOOL_CLASS[name] is ToolClass.WRITE
    for name in ("get_assets", "get_positions", "get_bars"):
        assert TIGER_TOOL_CLASS[name] is ToolClass.READ


def test_longbridge_order_ops_classified_write() -> None:
    for name in ("submit_order", "cancel_order", "replace_order"):
        assert LONGBRIDGE_TOOL_CLASS[name] is ToolClass.WRITE
    for name in ("account_balance", "stock_positions", "candlesticks"):
        assert LONGBRIDGE_TOOL_CLASS[name] is ToolClass.READ


# --------------------------------------------------------------------------- #
# Service dispatch degrades cleanly when nothing is configured
# --------------------------------------------------------------------------- #


def test_service_check_connection_unconfigured_tiger(monkeypatch, tmp_path) -> None:
    monkeypatch.setattr(tg, "get_runtime_root", lambda: tmp_path)
    result = service.check_connection("tiger-paper-sdk")
    assert result["status"] == "error"
    assert "not configured" in result["error"]
    assert result["connector"] == "tiger"
    assert result["transport"] == "broker_sdk"


def test_service_check_connection_unconfigured_longbridge(monkeypatch, tmp_path) -> None:
    for env_name in (
        "LONGBRIDGE_APP_KEY",
        "LONGBRIDGE_APP_SECRET",
        "LONGBRIDGE_ACCESS_TOKEN",
    ):
        monkeypatch.delenv(env_name, raising=False)
    monkeypatch.setattr(lb, "get_runtime_root", lambda: tmp_path)
    monkeypatch.setattr(lb_credentials, "get_runtime_root", lambda: tmp_path)
    result = service.check_connection("longbridge-paper-sdk")
    assert result["status"] == "error"
    assert "not configured" in result["error"]
    assert result["connector"] == "longbridge"
    assert result["transport"] == "broker_sdk"


def test_service_check_connection_unconfigured_etoro(monkeypatch, tmp_path) -> None:
    for env_name in ("ETORO_API_KEY", "ETORO_USER_KEY"):
        monkeypatch.delenv(env_name, raising=False)
    monkeypatch.setattr(etoro_client, "get_runtime_root", lambda: tmp_path)
    result = service.check_connection("etoro-paper-sdk")
    assert result["status"] == "error"
    assert "not configured" in result["error"]
    assert result["connector"] == "etoro"
    assert result["transport"] == "broker_sdk"


# --------------------------------------------------------------------------- #
# Alpaca
# --------------------------------------------------------------------------- #


def test_alpaca_paper_live_host_and_flag() -> None:
    assert al.AlpacaConfig(profile="paper").is_paper is True
    assert al.AlpacaConfig(profile="paper").host == al.PAPER_HOST
    assert al.AlpacaConfig(profile="live-readonly").is_paper is False
    assert al.AlpacaConfig(profile="live-readonly").host == al.LIVE_HOST


def test_alpaca_invalid_feed_rejected() -> None:
    with pytest.raises(al.AlpacaConfigError):
        al.AlpacaConfig.from_mapping({"feed": "nasdaq"})


def test_alpaca_redacts_secrets() -> None:
    cfg = al.AlpacaConfig(api_key="AKFOURCHARS", secret_key="topsecret")
    pub = al._public_config(cfg)
    assert pub["secret_key"] == "***redacted***"
    assert "topsecret" not in str(pub)
    assert pub["api_key"].endswith("***")


def test_alpaca_classification() -> None:
    assert ALPACA_TOOL_CLASS["submit_order"] is ToolClass.WRITE
    assert ALPACA_TOOL_CLASS["cancel_order_by_id"] is ToolClass.WRITE
    assert ALPACA_TOOL_CLASS["get_account"] is ToolClass.READ


def test_alpaca_service_unconfigured(monkeypatch, tmp_path) -> None:
    monkeypatch.setattr(al, "get_runtime_root", lambda: tmp_path)
    result = service.check_connection("alpaca-paper-sdk")
    assert result["status"] == "error"
    assert result["connector"] == "alpaca"
    assert result["transport"] == "broker_sdk"


# --------------------------------------------------------------------------- #
# OKX
# --------------------------------------------------------------------------- #


def test_okx_flag_mapping() -> None:
    assert ox.OKXConfig(profile="paper").flag == "1"
    assert ox.OKXConfig(profile="live-readonly").flag == "0"
    assert ox.OKXConfig(profile="live").flag == "0"


def test_okx_redacts_secrets() -> None:
    cfg = ox.OKXConfig(api_key="KEYFOURXX", api_secret="sec", passphrase="pass")
    pub = ox._public_config(cfg)
    assert pub["api_secret"] == "***redacted***"
    assert pub["passphrase"] == "***redacted***"
    assert "sec" not in str(pub) or pub["api_secret"] == "***redacted***"


def test_okx_classification() -> None:
    assert OKX_TOOL_CLASS["place_order"] is ToolClass.WRITE
    assert OKX_TOOL_CLASS["cancel_order"] is ToolClass.WRITE
    assert OKX_TOOL_CLASS["get_account_balance"] is ToolClass.READ


def test_okx_service_unconfigured(monkeypatch, tmp_path) -> None:
    monkeypatch.setattr(ox, "get_runtime_root", lambda: tmp_path)
    result = service.check_connection("okx-paper-sdk")
    assert result["status"] == "error"
    assert result["connector"] == "okx"


# --------------------------------------------------------------------------- #
# Binance
# --------------------------------------------------------------------------- #


def test_binance_testnet_host_mapping() -> None:
    assert bn.BinanceConfig(profile="paper").is_testnet is True
    assert "testnet" in bn.BinanceConfig(profile="paper").host
    assert bn.BinanceConfig(profile="live-readonly").is_testnet is False
    assert bn.BinanceConfig(profile="live-readonly").host == "https://api.binance.com"


def test_binance_classification() -> None:
    assert BINANCE_TOOL_CLASS["create_order"] is ToolClass.WRITE
    assert BINANCE_TOOL_CLASS["cancel_order"] is ToolClass.WRITE
    assert BINANCE_TOOL_CLASS["fetch_balance"] is ToolClass.READ
    assert BINANCE_TOOL_CLASS["load_markets"] is ToolClass.READ


def test_binance_quote_uses_public_client_without_credentials(monkeypatch) -> None:
    captured = {}

    class FakeExchange:
        def fetch_ticker(self, symbol):
            assert symbol == "BTC/USDT"
            return {"last": 75000}

    def build_exchange(config):
        captured["config"] = config
        return FakeExchange()

    monkeypatch.setattr(bn, "_exchange", build_exchange)
    result = bn.get_quote(
        "BTCUSDT",
        config=bn.BinanceConfig(
            api_key="private-key", api_secret="private-secret", profile="live-readonly"
        ),
    )

    assert result["quote"]["last"] == 75000
    assert captured["config"].api_key == ""
    assert captured["config"].api_secret == ""
    assert captured["config"].host == bn.LIVE_HOST


def test_binance_search_instruments_resolves_exact_active_spot_pair(monkeypatch) -> None:
    class FakeExchange:
        def load_markets(self):
            return {
                "ETH/USDT": {
                    "id": "ETHUSDT",
                    "symbol": "ETH/USDT",
                    "spot": True,
                    "active": True,
                },
                "ETH/USDT:USDT": {
                    "id": "ETHUSDT",
                    "symbol": "ETH/USDT:USDT",
                    "spot": False,
                    "active": True,
                },
                "BTC/USDT": {
                    "id": "BTCUSDT",
                    "symbol": "BTC/USDT",
                    "spot": True,
                    "active": True,
                },
            }

    monkeypatch.setattr(bn, "_exchange", lambda _cfg: FakeExchange())

    result = bn.search_instruments(
        "ETHUSDT",
        config=bn.BinanceConfig(profile="paper"),
    )

    assert result == {
        "status": "ok",
        "query": "ETHUSDT",
        "instruments": [
            {
                "symbol": "ETH-USDT",
                "native_symbol": "ETH/USDT",
                "exchange_symbol": "ETHUSDT",
                "base": "ETH",
                "quote": "USDT",
                "market": "crypto",
                "type": "cryptocurrency",
                "exchange": "BINANCE",
                "active": True,
            }
        ],
    }


def test_binance_search_instruments_does_not_guess_prose(monkeypatch) -> None:
    def _unexpected_exchange(_cfg):
        raise AssertionError("prose lookup must not load the Binance market catalog")

    monkeypatch.setattr(bn, "_exchange", _unexpected_exchange)

    result = bn.search_instruments(
        "Ethereum",
        config=bn.BinanceConfig(profile="paper"),
    )

    assert result == {"status": "ok", "query": "Ethereum", "instruments": []}


def test_service_routes_instrument_search_to_selected_binance_profile(monkeypatch) -> None:
    captured = {}

    def _search(query, *, config, limit):
        captured.update(query=query, profile=config.profile, limit=limit)
        return {"status": "ok", "instruments": [{"symbol": "ETH-USDT"}]}

    monkeypatch.setattr(bn, "search_instruments", _search)

    result = service.search_instruments(
        "ETH-USDT",
        "binance-paper-trade",
        limit=3,
    )

    assert captured == {"query": "ETH-USDT", "profile": "paper", "limit": 3}
    assert result["profile_id"] == "binance-paper-trade"
    assert result["connector"] == "binance"


def test_service_routes_instrument_search_to_selected_mt5_profile(monkeypatch) -> None:
    captured = {}

    def _build_config(profile_config, overrides):
        return SimpleNamespace(profile=profile_config["profile"])

    def _search(query, *, config, limit):
        captured.update(query=query, profile=config.profile, limit=limit)
        return {"status": "ok", "instruments": [{"symbol": "XAUUSDm"}]}

    monkeypatch.setattr(mt5, "build_config", _build_config)
    monkeypatch.setattr(mt5, "search_instruments", _search)

    result = service.search_instruments("XAUUSD", "mt5-paper-sdk", limit=3)

    assert captured == {"query": "XAUUSD", "profile": "paper", "limit": 3}
    assert result["profile_id"] == "mt5-paper-sdk"
    assert result["connector"] == "mt5"


def test_binance_service_unconfigured(monkeypatch, tmp_path) -> None:
    monkeypatch.setattr(bn, "get_runtime_root", lambda: tmp_path)
    result = service.check_connection("binance-paper-sdk")
    assert result["status"] == "error"
    assert result["connector"] == "binance"


def test_binance_positions_replace_ld_wrappers_with_simple_earn(monkeypatch) -> None:
    class FakeExchange:
        def fetch_balance(self):
            return {
                "USDT": {"free": 12, "used": 0, "total": 12},
                "LDBTC": {"free": 0, "used": 0.9, "total": 0.9},
            }

        def sapi_get_simple_earn_flexible_position(self, params):
            assert params == {"size": 100}
            return {"rows": [{"asset": "BTC", "totalAmount": "1.0"}]}

    monkeypatch.setattr(bn, "_exchange", lambda _cfg: FakeExchange())
    result = bn.get_positions(
        bn.BinanceConfig(api_key="key", api_secret="secret", profile="live-readonly")
    )

    assert result["positions"] == [
        {"symbol": "USDT", "quantity": 12.0, "free": 12.0, "used": 0.0, "source": "spot"},
        {
            "symbol": "BTC",
            "quantity": 1.0,
            "free": 0.0,
            "used": 1.0,
            "source": "simple_earn_flexible",
        },
    ]


def test_binance_exchange_adjusts_for_server_time(monkeypatch) -> None:
    captured = {}

    class FakeExchange:
        def __init__(self, config):
            captured.update(config)

        def set_sandbox_mode(self, enabled):
            captured["sandbox"] = enabled

    monkeypatch.setattr(bn, "_require_ccxt", lambda: SimpleNamespace(binance=FakeExchange))
    monkeypatch.setattr(bn, "getproxies", lambda: {})

    bn._exchange(bn.BinanceConfig(api_key="key", api_secret="secret", profile="live-readonly"))

    assert captured["options"] == {
        "adjustForTimeDifference": True,
        "recvWindow": 10_000,
        "fetchOpenOrders": {"warnWithoutSymbol": False},
    }
    assert captured["sandbox"] is False


def test_binance_account_read_resyncs_once_when_timestamp_is_ahead(monkeypatch) -> None:
    class FakeExchange:
        def __init__(self):
            self.options = {"timeDifference": 0}
            self.reads = 0
            self.syncs = 0

        def fetch_balance(self):
            self.reads += 1
            if self.reads == 1:
                raise RuntimeError(
                    'binance {"code":-1021,"msg":"Timestamp for this request was '
                    '1000ms ahead of the server\'s time."}'
                )
            return {"USDT": {"free": 12, "used": 0, "total": 12}}

        def load_time_difference(self):
            self.syncs += 1
            self.options["timeDifference"] = 1500

    exchange = FakeExchange()
    monkeypatch.setattr(bn, "_exchange", lambda _cfg: exchange)

    result = bn.get_account_snapshot(bn.BinanceConfig(profile="live-readonly"))

    assert result["status"] == "ok"
    assert exchange.reads == 2
    assert exchange.syncs == 1
    assert exchange.options["timeDifference"] == 2500


def test_binance_clock_retry_does_not_repeat_other_errors() -> None:
    class FakeExchange:
        def load_time_difference(self):
            raise AssertionError("unrelated failures must not resync")

    reads = 0

    def fail():
        nonlocal reads
        reads += 1
        raise RuntimeError('binance {"code":-2015,"msg":"Invalid API-key"}')

    with pytest.raises(RuntimeError, match="Invalid API-key"):
        bn._read_with_clock_retry(FakeExchange(), fail)
    assert reads == 1


def test_binance_clock_retry_stops_after_second_rejection() -> None:
    class FakeExchange:
        def __init__(self):
            self.options = {"timeDifference": 0}
            self.syncs = 0

        def load_time_difference(self):
            self.syncs += 1
            self.options["timeDifference"] = 1500

    exchange = FakeExchange()
    reads = 0

    def fail():
        nonlocal reads
        reads += 1
        raise RuntimeError('binance {"code":-1021,"msg":"Timestamp was 1000ms ahead of the server"}')

    with pytest.raises(RuntimeError, match="ahead of the server"):
        bn._read_with_clock_retry(exchange, fail)
    assert reads == 2
    assert exchange.syncs == 1


def test_binance_positions_resyncs_simple_earn_read(monkeypatch) -> None:
    class FakeExchange:
        def __init__(self):
            self.options = {"timeDifference": 0}
            self.earn_reads = 0

        def fetch_balance(self):
            return {"LDBTC": {"free": 0, "used": 0.9, "total": 0.9}}

        def sapi_get_simple_earn_flexible_position(self, params):
            assert params == {"size": 100}
            self.earn_reads += 1
            if self.earn_reads == 1:
                raise RuntimeError(
                    'binance {"code":-1021,"msg":"Timestamp for this request was '
                    '1000ms ahead of the server\'s time."}'
                )
            return {"rows": [{"asset": "BTC", "totalAmount": "1.0"}]}

        def load_time_difference(self):
            self.options["timeDifference"] = 1600

    exchange = FakeExchange()
    monkeypatch.setattr(bn, "_exchange", lambda _cfg: exchange)

    result = bn.get_positions(bn.BinanceConfig(profile="live-readonly"))

    assert exchange.earn_reads == 2
    assert result["positions"] == [
        {"symbol": "BTC", "quantity": 1.0, "free": 0.0, "used": 1.0, "source": "simple_earn_flexible"}
    ]


# --------------------------------------------------------------------------- #
# Futu (local OpenD gateway)
# --------------------------------------------------------------------------- #


def test_futu_trd_env_mapping() -> None:
    assert ft.FutuConfig(profile="paper").trd_env_name == "SIMULATE"
    assert ft.FutuConfig(profile="live-readonly").trd_env_name == "REAL"


def test_futu_saved_filter_trdmarket_survives_every_profile(monkeypatch, tmp_path) -> None:
    """A market saved in futu.json must not be overwritten by a profile.

    ``build_config`` applies profile config after the saved file. Hard-coding
    ``filter_trdmarket`` to HK on every profile therefore forces the HK trade
    context even for a US-only account. The saved market stays, while the
    profile still selects the paper/live environment.
    """
    from src.trading.connectors.futu.profiles import FUTU_PROFILES

    monkeypatch.setattr(ft, "get_runtime_root", lambda: tmp_path)
    (tmp_path / "futu.json").write_text(
        json.dumps({"filter_trdmarket": "US", "profile": "paper"}),
        encoding="utf-8",
    )

    assert len(FUTU_PROFILES) == 4
    for profile in FUTU_PROFILES:
        cfg = ft.build_config(profile.config, None)
        assert cfg.filter_trdmarket == "US", profile.id
        assert cfg.profile == profile.config["profile"]


def test_futu_filter_trdmarket_defaults_to_hk(monkeypatch, tmp_path) -> None:
    """With no saved market, every Futu profile still opens the HK default."""
    from src.trading.connectors.futu.profiles import FUTU_PROFILES

    monkeypatch.setattr(ft, "get_runtime_root", lambda: tmp_path)
    for profile in FUTU_PROFILES:
        cfg = ft.build_config(profile.config, None)
        assert cfg.filter_trdmarket == "HK", profile.id


def test_futu_filter_trdmarket_override_beats_saved_file(monkeypatch, tmp_path) -> None:
    """An explicit override still wins over the saved market."""
    from src.trading.connectors.futu.profiles import FUTU_PROFILES

    monkeypatch.setattr(ft, "get_runtime_root", lambda: tmp_path)
    (tmp_path / "futu.json").write_text(
        json.dumps({"filter_trdmarket": "US"}),
        encoding="utf-8",
    )
    cfg = ft.build_config(FUTU_PROFILES[0].config, {"filter_trdmarket": "SG"})
    assert cfg.filter_trdmarket == "SG"


def test_futu_classification() -> None:
    assert FUTU_TOOL_CLASS["place_order"] is ToolClass.WRITE
    assert FUTU_TOOL_CLASS["modify_order"] is ToolClass.WRITE
    assert FUTU_TOOL_CLASS["unlock_trade"] is ToolClass.WRITE
    assert FUTU_TOOL_CLASS["position_list_query"] is ToolClass.READ


def test_futu_service_unconfigured_gateway_down(monkeypatch, tmp_path) -> None:
    monkeypatch.setattr(ft, "get_runtime_root", lambda: tmp_path)
    result = service.check_connection("futu-paper-sdk")
    # OpenD gateway is not running in CI → clean error, not a crash.
    assert result["status"] == "error"
    assert result["connector"] == "futu"
    assert result["transport"] == "broker_sdk"


def test_binance_redacts_secrets() -> None:
    cfg = bn.BinanceConfig(api_key="ABCD1234", api_secret="topsecret")
    pub = bn._public_config(cfg)
    assert pub["api_secret"] == "***redacted***"
    assert "topsecret" not in str(pub)
    assert pub["api_key"].endswith("***")


def test_binance_assert_host_consistent_profiles_pass() -> None:
    """Host property is the guard: paper→testnet host, live→api.binance.com.

    The host is derived from the profile (paper→``testnet_host``,
    live→``api.binance.com``), so a paper profile structurally cannot resolve to
    the live host. ``_assert_host`` is defense-in-depth over that derivation and
    must accept both consistent profiles without raising.
    """
    bn._assert_host(bn.BinanceConfig(profile="paper"))
    bn._assert_host(bn.BinanceConfig(profile="live-readonly"))
    assert "testnet" in bn.BinanceConfig(profile="paper").host
    assert bn.BinanceConfig(profile="live-readonly").host == "https://api.binance.com"


def test_okx_invalid_profile_rejected() -> None:
    with pytest.raises(ox.OKXConfigError):
        ox.OKXConfig.from_mapping({"profile": "go-live-now"})


# --------------------------------------------------------------------------- #
# Live gate: order ops are WRITE-pinned through the real classifier + registry
# --------------------------------------------------------------------------- #


@pytest.mark.parametrize(
    "broker, order_op",
    [
        ("tiger", "place_order"),
        ("longbridge", "submit_order"),
        ("alpaca", "submit_order"),
        ("okx", "place_order"),
        ("binance", "create_order"),
        ("futu", "place_order"),
        ("dhan", "place_order"),
        ("shoonya", "place_order"),
    ],
)
def test_order_ops_write_pinned_via_registry(broker, order_op) -> None:
    """Every broker's order op resolves WRITE through the shared classifier."""
    from src.live import registry
    from src.live.classification import classify_tool

    curated = registry._BROKER_CURATED_MAPS[broker]
    assert classify_tool(order_op, None, curated) is ToolClass.WRITE


def test_unknown_op_does_not_classify_read() -> None:
    """An unmapped op resolves to UNKNOWN (never READ); the registry then treats
    UNKNOWN as WRITE (fail-closed) when wrapping the live channel."""
    from src.live import registry
    from src.live.classification import classify_tool

    curated = registry._BROKER_CURATED_MAPS["okx"]
    verdict = classify_tool("some_unmapped_future_tool", None, curated)
    assert verdict is not ToolClass.READ
    assert verdict in (ToolClass.WRITE, ToolClass.UNKNOWN)


# --------------------------------------------------------------------------- #
# Period mapping (generic token → per-SDK token)
# --------------------------------------------------------------------------- #


def test_period_maps_distinguish_minute_from_month() -> None:
    """The 1m (minute) vs 1M (month) tokens must not collide in any map."""
    assert tg._PERIOD_MAP["1m"] == "1min" and tg._PERIOD_MAP["1M"] == "month"
    assert ox._BAR_MAP["1m"] == "1m" and ox._BAR_MAP["1M"] == "1M"
    assert ft._KLTYPE_MAP["1m"] == "K_1M" and ft._KLTYPE_MAP["1M"] == "K_MON"


# --------------------------------------------------------------------------- #
# Read-path mapping with stubbed SDK clients (no broker SDK installed)
# --------------------------------------------------------------------------- #


class _FakeLbTrade:
    def today_orders(self):
        return [
            {"order_id": "1", "symbol": "700.HK", "status": "NewStatus", "quantity": 100},
            {"order_id": "2", "symbol": "700.HK", "status": "FilledStatus", "quantity": 100},
            {"order_id": "3", "symbol": "AAPL.US", "status": "CanceledStatus", "quantity": 5},
        ]


def test_longbridge_open_orders_filters_terminal(monkeypatch) -> None:
    monkeypatch.setattr(lb, "_trade_context", lambda cfg: _FakeLbTrade())
    out = lb.get_open_orders(lb.LongbridgeConfig(app_key="k", app_secret="s", access_token="t"))
    ids = [o["order_id"] for o in out["open_orders"]]
    assert ids == ["1"]  # filled + cancelled dropped


def test_longbridge_status_normalization_variants() -> None:
    """Terminal-status filtering must work across SDK string forms."""
    for terminal in ("Filled", "FilledStatus", "OrderStatus.Filled", "CANCELED", "Rejected"):
        assert not lb._is_open_order({"status": terminal})
    for live in ("NewStatus", "PartialFilledStatus", "PartialFilled", "WaitToNew"):
        assert lb._is_open_order({"status": live})


class _FakeTigerQuote:
    def __init__(self) -> None:
        self.calls: list[dict] = []

    def get_bars(self, symbols, period=None, limit=None):
        self.calls.append({"period": period, "limit": limit})
        return []


def test_tiger_history_month_does_not_collapse_to_minute(monkeypatch) -> None:
    """Regression: ``1M`` (month) must map to ``month``, not ``1min``."""
    fake = _FakeTigerQuote()
    monkeypatch.setattr(tg, "_quote_client", lambda cfg: fake)
    monkeypatch.setattr(tg, "_assert_profile", lambda cfg: None)
    cfg = tg.TigerConfig(tiger_id="x", private_key_path="x", account="20191106192858300", profile="paper")
    tg.get_historical_bars("AAPL", config=cfg, period="1M", limit=12)
    assert fake.calls[-1]["period"] == "month"
    assert fake.calls[-1]["limit"] == 12


def test_trading_history_tool_exposes_period_and_limit() -> None:
    from src.tools.trading_connector_tool import TradingHistoryTool

    props = TradingHistoryTool.parameters["properties"]
    assert "period" in props and "limit" in props


class _FakeOkxMarket:
    def get_candlesticks(self, instId=None, bar=None, limit=None):
        return {"code": "0", "data": [["1700000000000", "100", "110", "90", "105", "12", "1200", "1200", "1"]]}


def test_okx_history_maps_candles_and_period(monkeypatch) -> None:
    monkeypatch.setattr(ox, "_market_client", lambda cfg: _FakeOkxMarket())
    out = ox.get_historical_bars("BTC-USDT", config=ox.OKXConfig(api_key="k", api_secret="s", passphrase="p"), period="1h")
    assert out["period"] == "1h" and out["bar"] == "1H"
    assert len(out["bars"]) == 1
    bar = out["bars"][0]
    assert bar["open"] == "100" and bar["close"] == "105" and bar["confirm"] == "1"


# --------------------------------------------------------------------------- #
# Dhan + Shoonya: structural paper-only cap (no runtime discriminator)
#
# Like Longbridge, these brokers expose no sandbox / no runtime paper/live
# discriminator (same token/login reaches the same real account). The order
# path is therefore structurally capped at paper: any non-paper config is
# refused at the first line, so a flipped ``profile`` override can never reach a
# live order. Paper orders are simulated locally (neither broker has a sandbox).
# --------------------------------------------------------------------------- #


@pytest.mark.parametrize("mod, Config", [(dh, dh.DhanConfig), (sh, sh.ShoonyaConfig)])
@pytest.mark.parametrize("profile", ["live", "live-readonly"])
def test_in_broker_place_order_refuses_non_paper(mod, Config, profile) -> None:
    """A non-paper config is refused before any SDK call (fail-closed)."""
    result = mod.place_order(Config(profile=profile), symbol="RELIANCE", side="buy", quantity=1)
    assert result["status"] == "error"
    assert "paper-only" in result["error"]


@pytest.mark.parametrize("mod, Config", [(dh, dh.DhanConfig), (sh, sh.ShoonyaConfig)])
def test_in_broker_cancel_order_refuses_non_paper(mod, Config) -> None:
    result = mod.cancel_order(Config(profile="live"), "ORD1")
    assert result["status"] == "error"
    assert "paper-only" in result["error"]


@pytest.mark.parametrize("mod, Config", [(dh, dh.DhanConfig), (sh, sh.ShoonyaConfig)])
def test_in_broker_paper_place_order_simulated_locally(mod, Config) -> None:
    """Paper config simulates locally — no real money, no SDK call."""
    result = mod.place_order(Config(profile="paper"), symbol="RELIANCE", side="buy", quantity=10)
    assert result["status"] == "ok"
    assert result["is_paper"] is True
    assert result["order_status"] == "simulated_fill"
    assert result["paper_guard"] == "simulated_locally"


@pytest.mark.parametrize("quantity", [0.5, 1.5, "1.5", "1.00000000000000001"])
def test_dhan_place_order_rejects_fractional_quantity(quantity) -> None:
    """A fractional quantity must not silently truncate to a zero-share fill.

    Before the fix, ``int(0.5)`` truncated to 0 after the ``> 0`` check had
    already passed, so a fractional order came back ``status: ok`` with
    ``quantity: 0`` — a fabricated successful fill for zero shares.
    """
    result = dh.place_order(
        dh.DhanConfig(profile="paper"), symbol="RELIANCE", side="buy", quantity=quantity
    )
    assert result["status"] == "error"
    assert "whole number" in result["error"]


@pytest.mark.parametrize("quantity", [None, 0, -1, "invalid", "", True, [], float("nan"), float("inf"), "-Infinity"])
def test_dhan_invalid_quantity_returns_an_error(quantity) -> None:
    result = dh.place_order(
        dh.DhanConfig(profile="paper"), symbol="RELIANCE", side="buy", quantity=quantity,
    )

    assert result["status"] == "error"
    assert "quantity" in result["error"]
    assert "order_id" not in result


@pytest.mark.parametrize("quantity,expected", [(1, 1), (2.0, 2), ("3.0", 3), ("9007199254740993", 9007199254740993)])
def test_dhan_whole_quantity_is_preserved_in_the_paper_fill(quantity, expected) -> None:
    result = dh.place_order(
        dh.DhanConfig(profile="paper"), symbol="RELIANCE", side="buy", quantity=quantity,
    )

    assert result["status"] == "ok"
    assert result["quantity"] == expected
    assert result["paper_guard"] == "simulated_locally"


@pytest.mark.parametrize("quantity", [0.5, 1.5, "1.5", float("nan"), "invalid"])
def test_shoonya_place_order_rejects_non_whole_quantity(quantity) -> None:
    result = sh.place_order(
        sh.ShoonyaConfig(profile="paper"), symbol="RELIANCE", side="buy", quantity=quantity
    )
    assert result["status"] == "error"
    assert "whole number" in result["error"]


@pytest.mark.parametrize("mod, Config", [(dh, dh.DhanConfig), (sh, sh.ShoonyaConfig)])
def test_in_broker_paper_cancel_order_simulated(mod, Config) -> None:
    placed = mod.place_order(Config(profile="paper"), symbol="RELIANCE", side="buy", quantity=10)
    result = mod.cancel_order(Config(profile="paper"), placed["order_id"])
    assert result["status"] == "ok"
    assert result["cancelled"] is True
    assert result["is_paper"] is True


@pytest.mark.parametrize("mod, Config", [(dh, dh.DhanConfig), (sh, sh.ShoonyaConfig)])
def test_in_broker_paper_cancel_refuses_an_order_it_never_issued(mod, Config) -> None:
    """The paper profile reads the real account, so a live order id can reach
    the simulated cancel; acknowledging it would report a cancel that never
    happened while the real order keeps working."""
    result = mod.cancel_order(Config(profile="paper"), "ORD1")
    assert result["status"] == "error"
    assert "cancelled" not in result
    assert "not issued by this paper simulator" in result["error"]


def test_in_broker_order_ops_classified_write() -> None:
    for name in ("place_order", "modify_order", "cancel_order"):
        assert DHAN_TOOL_CLASS[name] is ToolClass.WRITE
        assert SHOONYA_TOOL_CLASS[name] is ToolClass.WRITE
    for name in ("get_positions", "get_holdings"):
        assert DHAN_TOOL_CLASS[name] is ToolClass.READ
        assert SHOONYA_TOOL_CLASS[name] is ToolClass.READ


def test_dhan_redacts_access_token() -> None:
    cfg = dh.DhanConfig(client_id="C1", access_token="tok-abcdefgh-secret")
    pub = dh._public_config(cfg)
    assert "secret" not in str(pub)
    assert pub["access_token"].endswith("***")


def test_shoonya_redacts_secrets() -> None:
    cfg = sh.ShoonyaConfig(
        user_id="USER1", password="pw", vendor_code="V", api_secret="sec", totp_secret="totp"
    )
    pub = sh._public_config(cfg)
    for secret in ("password", "api_secret", "totp_secret"):
        assert pub[secret] == "***redacted***"
    assert "sec" not in str(pub) or pub["api_secret"] == "***redacted***"
    assert pub["user_id"].endswith("***")


def test_dhan_invalid_profile_rejected() -> None:
    with pytest.raises(dh.DhanConfigError):
        dh.DhanConfig.from_mapping({"profile": "go-live"})


def test_shoonya_invalid_profile_rejected() -> None:
    with pytest.raises(sh.ShoonyaConfigError):
        sh.ShoonyaConfig.from_mapping({"profile": "go-live"})


def test_dhan_service_unconfigured(monkeypatch, tmp_path) -> None:
    monkeypatch.setattr(dh, "get_runtime_root", lambda: tmp_path)
    result = service.check_connection("dhan-paper-sdk")
    assert result["status"] == "error"
    assert result["connector"] == "dhan"
    assert result["transport"] == "broker_sdk"


def test_shoonya_service_unconfigured(monkeypatch, tmp_path) -> None:
    monkeypatch.setattr(sh, "get_runtime_root", lambda: tmp_path)
    result = service.check_connection("shoonya-paper-sdk")
    assert result["status"] == "error"
    assert result["connector"] == "shoonya"
    assert result["transport"] == "broker_sdk"


def test_mt5_instrument_search_refuses_executable_override(monkeypatch):
    def forbidden(*args, **kwargs):
        raise AssertionError("must not attach to an overridden executable")
    monkeypatch.setattr(mt5, "search_instruments", forbidden)
    result = service.search_instruments("XAUUSD", "mt5-paper-sdk", terminal_path="C:/arbitrary.exe")
    assert result["status"] == "error"
    assert result["instruments"] == []