import pytest import respx from httpx import Response from traderai.tools import ToolRegistry from traderai.uex_client import UEXClient class FakeUEX: async def get(self, path, params=None, authenticated=False): assert path == "marketplace_listings" return { "data": [ { "id": 1, "slug": "gold-haul", "title": "Gold haul escort", "description": "Escort service", "operation": "sell", "type": "service", "price": 5000, "currency": "UEC", "unit": "run", "location": "Port Tressler", "user_username": "pilot_a", "date_expiration": 123, }, { "id": 2, "slug": "armor-set", "title": "Armor set", "description": "Clean set", "operation": "sell", "type": "item", "price": 15000, "currency": "UEC", "unit": "set", "location": "Area18", "user_username": "pilot_b", "date_expiration": 456, }, ] } @pytest.mark.asyncio async def test_search_marketplace_listings_filters_locally(): registry = ToolRegistry(FakeUEX()) result = await registry.search_marketplace_listings(query="gold", type="service", max_price=6000) assert result["count"] == 1 assert result["listings"][0]["slug"] == "gold-haul" @pytest.mark.asyncio async def test_draft_message_creates_pending_action(): registry = ToolRegistry(FakeUEX()) result = await registry.draft_negotiation_message(hash="abc", message="Would you take 4500 UEC?") pending = result["pending_action"] assert pending["endpoint"] == "marketplace_negotiations_messages" assert pending["payload"]["message"] == "Would you take 4500 UEC?" assert pending["id"] in registry.pending_actions def test_uex_client_uses_bearer_and_secret_headers(): client = UEXClient("https://api.uexcorp.space/2.0", secret_key="secret", bearer_token="bearer") headers = client._headers(authenticated=True) assert headers["secret-key"] == "secret" assert headers["Authorization"] == "Bearer bearer" @pytest.mark.asyncio @respx.mock async def test_uex_client_get_user_normalizes_user_payload(): respx.get("https://api.uexcorp.space/2.0/user/").mock( return_value=Response(200, json={"status": "ok", "data": [{"username": "pilot_hudson"}]}) ) client = UEXClient("https://api.uexcorp.space/2.0", bearer_token="bearer") result = await client.get_user(authenticated=True) assert result == {"status": "ok", "user": {"username": "pilot_hudson"}} @pytest.mark.asyncio @respx.mock async def test_uex_client_get_user_notifications_normalizes_payload(): respx.get("https://api.uexcorp.space/2.0/user_notifications/").mock( return_value=Response(200, json={"status": "ok", "data": {"id": 7, "message": "Reply waiting", "date_read": 0}}) ) client = UEXClient("https://api.uexcorp.space/2.0", bearer_token="bearer") result = await client.get_user_notifications() assert result == {"status": "ok", "notifications": [{"id": 7, "message": "Reply waiting", "date_read": 0}]}