"""Trade tools (contracts + market orders, character + corp), auth & ESI mocked.""" import asyncio import pytest from conftest import route from familiar import auth, trade from familiar.server import mcp CHARS = [ {"character_id": 99, "character_name": "Alpha", "scopes": []}, {"character_id": 100, "character_name": "Beta", "scopes": []}, ] @pytest.fixture def fake_auth(monkeypatch): monkeypatch.setattr(auth, "list_characters", lambda: list(CHARS)) async def get_auth(cid, **kw): return f"token-{cid}", cid monkeypatch.setattr(auth, "get_auth", get_auth) def resolve(identifier): for c in CHARS: if ( str(c["character_id"]) == str(identifier) or (c["character_name"] or "").lower() == str(identifier).lower() ): return c["character_id"] raise auth.NotAuthenticated("no such character") monkeypatch.setattr(auth, "resolve_character", resolve) return CHARS def test_contracts_all(mock_esi, fake_auth): contracts = [{"contract_id": 1, "type": "item_exchange"}] mock_esi(lambda r: route(r, {"/contracts/": contracts})) res = asyncio.run(trade.get_contracts()) assert len(res["characters"]) == 2 assert res["characters"][0]["count"] == 1 def test_my_orders_single(mock_esi, fake_auth): orders = [{"order_id": 1, "price": 10.0}, {"order_id": 2, "price": 20.0}] mock_esi(lambda r: route(r, {"/orders/": orders})) res = asyncio.run(trade.get_my_orders(character="Alpha")) assert res["characters"][0]["count"] == 2 def test_corp_contracts_resolves_corp(mock_esi, fake_auth): fixtures = { "/corporations/": [{"contract_id": 9}], "/characters/": {"corporation_id": 2000}, } mock_esi(lambda r: route(r, fixtures)) res = asyncio.run(trade.get_corp_contracts(character=99)) entry = res["characters"][0] assert entry["corporation_id"] == 2000 assert entry["count"] == 1 def test_corp_orders(mock_esi, fake_auth): fixtures = { "/corporations/": [{"order_id": 3}], "/characters/": {"corporation_id": 2000}, } mock_esi(lambda r: route(r, fixtures)) res = asyncio.run(trade.get_corp_orders(character="Beta")) assert res["characters"][0]["count"] == 1 def test_trade_tools_registered(): tools = {t.name for t in asyncio.run(mcp.list_tools())} assert {"get_contracts", "get_my_orders", "get_corp_contracts", "get_corp_orders"} <= tools