Compare commits
1 Commits
test/chat-
...
claude/iss
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e69228b793 |
@@ -242,64 +242,6 @@ def produce_agent_state(agent_id: str, presence: dict) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def _get_agents_online() -> int:
|
||||
"""Return the count of agents with a non-offline status."""
|
||||
try:
|
||||
from timmy.agents.loader import list_agents
|
||||
|
||||
agents = list_agents()
|
||||
return sum(1 for a in agents if a.get("status", "") not in ("offline", ""))
|
||||
except Exception as exc:
|
||||
logger.debug("Failed to count agents: %s", exc)
|
||||
return 0
|
||||
|
||||
|
||||
def _get_visitors() -> int:
|
||||
"""Return the count of active WebSocket visitor clients."""
|
||||
try:
|
||||
from dashboard.routes.world import _ws_clients
|
||||
|
||||
return len(_ws_clients)
|
||||
except Exception as exc:
|
||||
logger.debug("Failed to count visitors: %s", exc)
|
||||
return 0
|
||||
|
||||
|
||||
def _get_uptime_seconds() -> int:
|
||||
"""Return seconds elapsed since application start."""
|
||||
try:
|
||||
from config import APP_START_TIME
|
||||
|
||||
return int((datetime.now(UTC) - APP_START_TIME).total_seconds())
|
||||
except Exception as exc:
|
||||
logger.debug("Failed to calculate uptime: %s", exc)
|
||||
return 0
|
||||
|
||||
|
||||
def _get_thinking_active() -> bool:
|
||||
"""Return True if the thinking engine is enabled and running."""
|
||||
try:
|
||||
from config import settings
|
||||
from timmy.thinking import thinking_engine
|
||||
|
||||
return settings.thinking_enabled and thinking_engine is not None
|
||||
except Exception as exc:
|
||||
logger.debug("Failed to check thinking status: %s", exc)
|
||||
return False
|
||||
|
||||
|
||||
def _get_memory_count() -> int:
|
||||
"""Return total entries in the vector memory store."""
|
||||
try:
|
||||
from timmy.memory_system import get_memory_stats
|
||||
|
||||
stats = get_memory_stats()
|
||||
return stats.get("total_entries", 0)
|
||||
except Exception as exc:
|
||||
logger.debug("Failed to count memories: %s", exc)
|
||||
return 0
|
||||
|
||||
|
||||
def produce_system_status() -> dict:
|
||||
"""Generate a system_status message for the Matrix.
|
||||
|
||||
@@ -328,14 +270,64 @@ def produce_system_status() -> dict:
|
||||
"ts": 1742529600,
|
||||
}
|
||||
"""
|
||||
# Count agents with status != offline
|
||||
agents_online = 0
|
||||
try:
|
||||
from timmy.agents.loader import list_agents
|
||||
|
||||
agents = list_agents()
|
||||
agents_online = sum(1 for a in agents if a.get("status", "") not in ("offline", ""))
|
||||
except Exception as exc:
|
||||
logger.debug("Failed to count agents: %s", exc)
|
||||
|
||||
# Count visitors from WebSocket clients
|
||||
visitors = 0
|
||||
try:
|
||||
from dashboard.routes.world import _ws_clients
|
||||
|
||||
visitors = len(_ws_clients)
|
||||
except Exception as exc:
|
||||
logger.debug("Failed to count visitors: %s", exc)
|
||||
|
||||
# Calculate uptime
|
||||
uptime_seconds = 0
|
||||
try:
|
||||
from datetime import UTC
|
||||
|
||||
from config import APP_START_TIME
|
||||
|
||||
uptime_seconds = int((datetime.now(UTC) - APP_START_TIME).total_seconds())
|
||||
except Exception as exc:
|
||||
logger.debug("Failed to calculate uptime: %s", exc)
|
||||
|
||||
# Check thinking engine status
|
||||
thinking_active = False
|
||||
try:
|
||||
from config import settings
|
||||
from timmy.thinking import thinking_engine
|
||||
|
||||
thinking_active = settings.thinking_enabled and thinking_engine is not None
|
||||
except Exception as exc:
|
||||
logger.debug("Failed to check thinking status: %s", exc)
|
||||
|
||||
# Count memories in vector store
|
||||
memory_count = 0
|
||||
try:
|
||||
from timmy.memory_system import get_memory_stats
|
||||
|
||||
stats = get_memory_stats()
|
||||
memory_count = stats.get("total_entries", 0)
|
||||
except Exception as exc:
|
||||
logger.debug("Failed to count memories: %s", exc)
|
||||
|
||||
return {
|
||||
"type": "system_status",
|
||||
"data": {
|
||||
"agents_online": _get_agents_online(),
|
||||
"visitors": _get_visitors(),
|
||||
"uptime_seconds": _get_uptime_seconds(),
|
||||
"thinking_active": _get_thinking_active(),
|
||||
"memory_count": _get_memory_count(),
|
||||
"agents_online": agents_online,
|
||||
"visitors": visitors,
|
||||
"uptime_seconds": uptime_seconds,
|
||||
"thinking_active": thinking_active,
|
||||
"memory_count": memory_count,
|
||||
},
|
||||
"ts": int(time.time()),
|
||||
}
|
||||
|
||||
@@ -1,247 +0,0 @@
|
||||
"""Unit tests for src/infrastructure/chat_store.py."""
|
||||
|
||||
import sqlite3
|
||||
import threading
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from src.infrastructure.chat_store import MAX_MESSAGES, Message, MessageLog, _get_conn
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def tmp_db(tmp_path: Path) -> Path:
|
||||
"""Return a temporary database path."""
|
||||
return tmp_path / "test_chat.db"
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def log(tmp_db: Path) -> MessageLog:
|
||||
"""Return a MessageLog backed by a temp database."""
|
||||
ml = MessageLog(db_path=tmp_db)
|
||||
yield ml
|
||||
ml.close()
|
||||
|
||||
|
||||
# ── Message dataclass ──────────────────────────────────────────────────
|
||||
|
||||
|
||||
class TestMessage:
|
||||
def test_default_source(self):
|
||||
m = Message(role="user", content="hi", timestamp="2026-01-01T00:00:00")
|
||||
assert m.source == "browser"
|
||||
|
||||
def test_custom_source(self):
|
||||
m = Message(role="agent", content="ok", timestamp="t1", source="telegram")
|
||||
assert m.source == "telegram"
|
||||
|
||||
def test_fields(self):
|
||||
m = Message(role="error", content="boom", timestamp="t2", source="api")
|
||||
assert m.role == "error"
|
||||
assert m.content == "boom"
|
||||
assert m.timestamp == "t2"
|
||||
|
||||
|
||||
# ── _get_conn context manager ──────────────────────────────────────────
|
||||
|
||||
|
||||
class TestGetConn:
|
||||
def test_creates_db_and_table(self, tmp_db: Path):
|
||||
with _get_conn(tmp_db) as conn:
|
||||
tables = conn.execute(
|
||||
"SELECT name FROM sqlite_master WHERE type='table'"
|
||||
).fetchall()
|
||||
names = [t["name"] for t in tables]
|
||||
assert "chat_messages" in names
|
||||
|
||||
def test_creates_parent_dirs(self, tmp_path: Path):
|
||||
deep = tmp_path / "a" / "b" / "c" / "chat.db"
|
||||
with _get_conn(deep) as conn:
|
||||
assert deep.parent.exists()
|
||||
|
||||
def test_connection_closed_after_context(self, tmp_db: Path):
|
||||
with _get_conn(tmp_db) as conn:
|
||||
conn.execute("SELECT 1")
|
||||
# Connection should be closed — operations should fail
|
||||
with pytest.raises(Exception):
|
||||
conn.execute("SELECT 1")
|
||||
|
||||
|
||||
# ── MessageLog core operations ─────────────────────────────────────────
|
||||
|
||||
|
||||
class TestMessageLogAppendAndAll:
|
||||
def test_append_and_all(self, log: MessageLog):
|
||||
log.append("user", "hello", "t1")
|
||||
log.append("agent", "hi back", "t2", source="api")
|
||||
msgs = log.all()
|
||||
assert len(msgs) == 2
|
||||
assert msgs[0].role == "user"
|
||||
assert msgs[0].content == "hello"
|
||||
assert msgs[0].source == "browser"
|
||||
assert msgs[1].role == "agent"
|
||||
assert msgs[1].source == "api"
|
||||
|
||||
def test_all_returns_ordered_by_id(self, log: MessageLog):
|
||||
for i in range(5):
|
||||
log.append("user", f"msg{i}", f"t{i}")
|
||||
msgs = log.all()
|
||||
assert [m.content for m in msgs] == [f"msg{i}" for i in range(5)]
|
||||
|
||||
def test_all_empty_store(self, log: MessageLog):
|
||||
assert log.all() == []
|
||||
|
||||
|
||||
class TestMessageLogRecent:
|
||||
def test_recent_returns_newest(self, log: MessageLog):
|
||||
for i in range(10):
|
||||
log.append("user", f"msg{i}", f"t{i}")
|
||||
recent = log.recent(limit=3)
|
||||
assert len(recent) == 3
|
||||
assert recent[0].content == "msg7"
|
||||
assert recent[2].content == "msg9"
|
||||
|
||||
def test_recent_oldest_first(self, log: MessageLog):
|
||||
for i in range(5):
|
||||
log.append("user", f"msg{i}", f"t{i}")
|
||||
recent = log.recent(limit=3)
|
||||
# Should be oldest-first within the window
|
||||
assert recent[0].content == "msg2"
|
||||
assert recent[1].content == "msg3"
|
||||
assert recent[2].content == "msg4"
|
||||
|
||||
def test_recent_more_than_exists(self, log: MessageLog):
|
||||
log.append("user", "only", "t0")
|
||||
recent = log.recent(limit=100)
|
||||
assert len(recent) == 1
|
||||
|
||||
def test_recent_empty_store(self, log: MessageLog):
|
||||
assert log.recent() == []
|
||||
|
||||
|
||||
class TestMessageLogClear:
|
||||
def test_clear_removes_all(self, log: MessageLog):
|
||||
for i in range(5):
|
||||
log.append("user", f"msg{i}", f"t{i}")
|
||||
assert len(log) == 5
|
||||
log.clear()
|
||||
assert len(log) == 0
|
||||
assert log.all() == []
|
||||
|
||||
def test_clear_empty_store(self, log: MessageLog):
|
||||
log.clear() # Should not raise
|
||||
assert len(log) == 0
|
||||
|
||||
|
||||
class TestMessageLogLen:
|
||||
def test_len_empty(self, log: MessageLog):
|
||||
assert len(log) == 0
|
||||
|
||||
def test_len_after_appends(self, log: MessageLog):
|
||||
for i in range(7):
|
||||
log.append("user", f"msg{i}", f"t{i}")
|
||||
assert len(log) == 7
|
||||
|
||||
|
||||
class TestMessageLogClose:
|
||||
def test_close_sets_conn_none(self, tmp_db: Path):
|
||||
ml = MessageLog(db_path=tmp_db)
|
||||
ml.append("user", "x", "t0")
|
||||
ml.close()
|
||||
assert ml._conn is None
|
||||
|
||||
def test_close_idempotent(self, tmp_db: Path):
|
||||
ml = MessageLog(db_path=tmp_db)
|
||||
ml.close()
|
||||
ml.close() # Should not raise
|
||||
|
||||
def test_reopen_after_close(self, tmp_db: Path):
|
||||
ml = MessageLog(db_path=tmp_db)
|
||||
ml.append("user", "before", "t0")
|
||||
ml.close()
|
||||
# Should reconnect on next use
|
||||
ml.append("user", "after", "t1")
|
||||
assert len(ml) == 2
|
||||
ml.close()
|
||||
|
||||
|
||||
# ── Pruning ────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
class TestPrune:
|
||||
def test_prune_keeps_max_messages(self, tmp_db: Path):
|
||||
with patch("src.infrastructure.chat_store.MAX_MESSAGES", 5):
|
||||
ml = MessageLog(db_path=tmp_db)
|
||||
for i in range(10):
|
||||
ml.append("user", f"msg{i}", f"t{i}")
|
||||
# Should have pruned to 5
|
||||
assert len(ml) == 5
|
||||
msgs = ml.all()
|
||||
# Oldest should be pruned, newest kept
|
||||
assert msgs[0].content == "msg5"
|
||||
assert msgs[-1].content == "msg9"
|
||||
ml.close()
|
||||
|
||||
def test_no_prune_under_limit(self, tmp_db: Path):
|
||||
with patch("src.infrastructure.chat_store.MAX_MESSAGES", 100):
|
||||
ml = MessageLog(db_path=tmp_db)
|
||||
for i in range(10):
|
||||
ml.append("user", f"msg{i}", f"t{i}")
|
||||
assert len(ml) == 10
|
||||
ml.close()
|
||||
|
||||
|
||||
# ── Thread safety ──────────────────────────────────────────────────────
|
||||
|
||||
|
||||
class TestThreadSafety:
|
||||
def test_concurrent_appends(self, tmp_db: Path):
|
||||
ml = MessageLog(db_path=tmp_db)
|
||||
errors = []
|
||||
|
||||
def writer(start: int):
|
||||
try:
|
||||
for i in range(20):
|
||||
ml.append("user", f"msg{start + i}", f"t{start + i}")
|
||||
except Exception as e:
|
||||
errors.append(e)
|
||||
|
||||
threads = [threading.Thread(target=writer, args=(i * 20,)) for i in range(5)]
|
||||
for t in threads:
|
||||
t.start()
|
||||
for t in threads:
|
||||
t.join()
|
||||
|
||||
assert not errors, f"Thread errors: {errors}"
|
||||
assert len(ml) == 100
|
||||
ml.close()
|
||||
|
||||
|
||||
# ── Edge cases ─────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
class TestEdgeCases:
|
||||
def test_empty_content(self, log: MessageLog):
|
||||
log.append("user", "", "t0")
|
||||
msgs = log.all()
|
||||
assert len(msgs) == 1
|
||||
assert msgs[0].content == ""
|
||||
|
||||
def test_unicode_content(self, log: MessageLog):
|
||||
log.append("user", "こんにちは 🎉 مرحبا", "t0")
|
||||
msgs = log.all()
|
||||
assert msgs[0].content == "こんにちは 🎉 مرحبا"
|
||||
|
||||
def test_multiline_content(self, log: MessageLog):
|
||||
content = "line1\nline2\nline3"
|
||||
log.append("user", content, "t0")
|
||||
assert log.all()[0].content == content
|
||||
|
||||
def test_special_sql_characters(self, log: MessageLog):
|
||||
log.append("user", "Robert'; DROP TABLE chat_messages;--", "t0")
|
||||
msgs = log.all()
|
||||
assert len(msgs) == 1
|
||||
assert "DROP TABLE" in msgs[0].content
|
||||
@@ -1,10 +1,21 @@
|
||||
"""Tests for the async event bus (infrastructure.events.bus)."""
|
||||
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from infrastructure.events.bus import Event, EventBus, emit, event_bus, on
|
||||
import infrastructure.events.bus as bus_module
|
||||
from infrastructure.events.bus import (
|
||||
Event,
|
||||
EventBus,
|
||||
emit,
|
||||
event_bus,
|
||||
get_event_bus,
|
||||
init_event_bus_persistence,
|
||||
on,
|
||||
)
|
||||
|
||||
|
||||
class TestEvent:
|
||||
@@ -349,3 +360,111 @@ class TestEventBusPersistence:
|
||||
assert mode == "wal"
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
async def test_persist_event_exception_is_swallowed(self, tmp_path):
|
||||
"""_persist_event must not propagate SQLite errors."""
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
bus = EventBus()
|
||||
bus.enable_persistence(tmp_path / "events.db")
|
||||
|
||||
# Make the INSERT raise an OperationalError
|
||||
mock_conn = MagicMock()
|
||||
mock_conn.execute.side_effect = sqlite3.OperationalError("simulated failure")
|
||||
|
||||
from contextlib import contextmanager
|
||||
|
||||
@contextmanager
|
||||
def fake_ctx():
|
||||
yield mock_conn
|
||||
|
||||
with patch.object(bus, "_get_persistence_conn", fake_ctx):
|
||||
# Should not raise
|
||||
bus._persist_event(Event(type="x", source="s"))
|
||||
|
||||
async def test_replay_exception_returns_empty(self, tmp_path):
|
||||
"""replay() must return [] when SQLite query fails."""
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
bus = EventBus()
|
||||
bus.enable_persistence(tmp_path / "events.db")
|
||||
|
||||
mock_conn = MagicMock()
|
||||
mock_conn.execute.side_effect = sqlite3.OperationalError("simulated failure")
|
||||
|
||||
from contextlib import contextmanager
|
||||
|
||||
@contextmanager
|
||||
def fake_ctx():
|
||||
yield mock_conn
|
||||
|
||||
with patch.object(bus, "_get_persistence_conn", fake_ctx):
|
||||
result = bus.replay()
|
||||
assert result == []
|
||||
|
||||
|
||||
# ── Singleton helpers ─────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
class TestSingletonHelpers:
|
||||
"""Test get_event_bus(), init_event_bus_persistence(), and module __getattr__."""
|
||||
|
||||
def test_get_event_bus_returns_same_instance(self):
|
||||
"""get_event_bus() is a true singleton."""
|
||||
a = get_event_bus()
|
||||
b = get_event_bus()
|
||||
assert a is b
|
||||
|
||||
def test_module_event_bus_attr_is_singleton(self):
|
||||
"""Accessing bus_module.event_bus via __getattr__ returns the singleton."""
|
||||
assert bus_module.event_bus is get_event_bus()
|
||||
|
||||
def test_module_getattr_unknown_raises(self):
|
||||
"""Accessing an unknown module attribute raises AttributeError."""
|
||||
with pytest.raises(AttributeError):
|
||||
_ = bus_module.no_such_attr # type: ignore[attr-defined]
|
||||
|
||||
def test_init_event_bus_persistence_sets_path(self, tmp_path):
|
||||
"""init_event_bus_persistence() enables persistence on the singleton."""
|
||||
bus = get_event_bus()
|
||||
original_path = bus._persistence_db_path
|
||||
try:
|
||||
bus._persistence_db_path = None # reset for the test
|
||||
db_path = tmp_path / "test_init.db"
|
||||
init_event_bus_persistence(db_path)
|
||||
assert bus._persistence_db_path == db_path
|
||||
finally:
|
||||
bus._persistence_db_path = original_path
|
||||
|
||||
def test_init_event_bus_persistence_is_idempotent(self, tmp_path):
|
||||
"""Calling init_event_bus_persistence() twice keeps the first path."""
|
||||
bus = get_event_bus()
|
||||
original_path = bus._persistence_db_path
|
||||
try:
|
||||
bus._persistence_db_path = None
|
||||
first_path = tmp_path / "first.db"
|
||||
second_path = tmp_path / "second.db"
|
||||
init_event_bus_persistence(first_path)
|
||||
init_event_bus_persistence(second_path) # should be ignored
|
||||
assert bus._persistence_db_path == first_path
|
||||
finally:
|
||||
bus._persistence_db_path = original_path
|
||||
|
||||
def test_init_event_bus_persistence_default_path(self):
|
||||
"""init_event_bus_persistence() uses 'data/events.db' when no path given."""
|
||||
bus = get_event_bus()
|
||||
original_path = bus._persistence_db_path
|
||||
try:
|
||||
bus._persistence_db_path = None
|
||||
# Patch enable_persistence to capture what path it receives
|
||||
captured = {}
|
||||
|
||||
def fake_enable(path: Path) -> None:
|
||||
captured["path"] = path
|
||||
|
||||
with patch.object(bus, "enable_persistence", side_effect=fake_enable):
|
||||
init_event_bus_persistence()
|
||||
|
||||
assert captured["path"] == Path("data/events.db")
|
||||
finally:
|
||||
bus._persistence_db_path = original_path
|
||||
|
||||
@@ -6,12 +6,7 @@ import pytest
|
||||
|
||||
from infrastructure.presence import (
|
||||
DEFAULT_PIP_STATE,
|
||||
_get_agents_online,
|
||||
_get_familiar_state,
|
||||
_get_memory_count,
|
||||
_get_thinking_active,
|
||||
_get_uptime_seconds,
|
||||
_get_visitors,
|
||||
produce_agent_state,
|
||||
produce_bark,
|
||||
produce_system_status,
|
||||
@@ -505,36 +500,3 @@ class TestProduceSystemStatus:
|
||||
"""produce_system_status always returns a plain dict."""
|
||||
result = produce_system_status()
|
||||
assert isinstance(result, dict)
|
||||
|
||||
|
||||
class TestSystemStatusHelpers:
|
||||
"""Tests for the helper functions extracted from produce_system_status()."""
|
||||
|
||||
def test_get_agents_online_returns_int(self):
|
||||
"""_get_agents_online returns a non-negative int."""
|
||||
result = _get_agents_online()
|
||||
assert isinstance(result, int)
|
||||
assert result >= 0
|
||||
|
||||
def test_get_visitors_returns_int(self):
|
||||
"""_get_visitors returns a non-negative int."""
|
||||
result = _get_visitors()
|
||||
assert isinstance(result, int)
|
||||
assert result >= 0
|
||||
|
||||
def test_get_uptime_seconds_returns_int(self):
|
||||
"""_get_uptime_seconds returns a non-negative int."""
|
||||
result = _get_uptime_seconds()
|
||||
assert isinstance(result, int)
|
||||
assert result >= 0
|
||||
|
||||
def test_get_thinking_active_returns_bool(self):
|
||||
"""_get_thinking_active returns a bool."""
|
||||
result = _get_thinking_active()
|
||||
assert isinstance(result, bool)
|
||||
|
||||
def test_get_memory_count_returns_int(self):
|
||||
"""_get_memory_count returns a non-negative int."""
|
||||
result = _get_memory_count()
|
||||
assert isinstance(result, int)
|
||||
assert result >= 0
|
||||
|
||||
Reference in New Issue
Block a user