Inital Commit
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import pytest
|
||||
|
||||
from traderai.agent import OllamaAgent
|
||||
from traderai.memory import MemoryStore
|
||||
|
||||
|
||||
class EmptyTools:
|
||||
schemas = []
|
||||
|
||||
@property
|
||||
def pending_actions(self):
|
||||
return {}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_chat_events_warns_when_ollama_offline():
|
||||
agent = OllamaAgent("http://127.0.0.1:1", "missing-model", EmptyTools())
|
||||
events = []
|
||||
async for event in agent.chat_events("hello"):
|
||||
events.append(event)
|
||||
|
||||
assert events[0]["type"] == "warning"
|
||||
assert "Ollama is offline" in events[0]["message"]
|
||||
assert events[-1]["type"] == "done"
|
||||
|
||||
|
||||
def test_runtime_context_uses_previous_interaction_not_current_message(tmp_path):
|
||||
memory = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
||||
memory.add_conversation("assistant", "older answer")
|
||||
previous = memory.last_interaction()
|
||||
assert previous is not None
|
||||
|
||||
memory.add_conversation("user", "current question")
|
||||
current = memory.last_interaction()
|
||||
|
||||
agent = OllamaAgent("http://127.0.0.1:1", "missing-model", EmptyTools(), memory=memory)
|
||||
context = agent._runtime_context("current question", previous_interaction=previous)
|
||||
|
||||
assert f"Previous interaction before this message: {previous['created_at']}" in context
|
||||
assert f"Previous interaction before this message: {current['created_at']}" not in context
|
||||
assert "Current local date/time:" in context
|
||||
|
||||
|
||||
def test_runtime_context_includes_uex_user_identity(tmp_path):
|
||||
memory = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
||||
memory.set_profile(
|
||||
"uex_user",
|
||||
{
|
||||
"username": "pilot_hudson",
|
||||
"name": "Hudson",
|
||||
"email": "hudson@example.test",
|
||||
"timezone": "America/New_York",
|
||||
"specializations": "trading,hauling",
|
||||
},
|
||||
)
|
||||
|
||||
agent = OllamaAgent("http://127.0.0.1:1", "missing-model", EmptyTools(), memory=memory)
|
||||
context = agent._runtime_context("")
|
||||
|
||||
assert "You are speaking with UEX user pilot_hudson (Hudson)." in context
|
||||
assert "timezone: America/New_York" in context
|
||||
assert "specializations: trading,hauling" in context
|
||||
assert "hudson@example.test" not in context
|
||||
@@ -0,0 +1,27 @@
|
||||
from traderai.memory import MemoryStore
|
||||
|
||||
|
||||
def test_memory_store_recalls_saved_fact(tmp_path):
|
||||
store = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
||||
store.remember("preference", "The user prefers Polaris Bits searches to include barter listings.", importance=5)
|
||||
|
||||
results = store.recall("Polaris barter")
|
||||
|
||||
assert results
|
||||
assert "Polaris Bits" in results[0]["content"]
|
||||
|
||||
|
||||
def test_memory_store_clear_selected_sections(tmp_path):
|
||||
store = MemoryStore(str(tmp_path / "memory.sqlite3"))
|
||||
store.remember("note", "Forgettable note")
|
||||
store.add_conversation("user", "hello")
|
||||
store.set_profile("configured_name", "Hudson")
|
||||
|
||||
deleted = store.clear(include_profile=False)
|
||||
snapshot = store.inspect()
|
||||
|
||||
assert deleted["memories"] == 1
|
||||
assert deleted["conversations"] == 1
|
||||
assert snapshot["memories"] == []
|
||||
assert snapshot["conversations"] == []
|
||||
assert snapshot["profile"][0]["key"] == "configured_name"
|
||||
@@ -0,0 +1,83 @@
|
||||
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"}}
|
||||
Reference in New Issue
Block a user