surfaces/tests/adapter/matrix/test_restart_persistence.py
Mikhail Putilovskij e733119d1e feat: enforce agent routing and persist restart state
Task 4: stale room blocking + agent_id binding
- MatrixBot._check_agent_routing: blocks normal messages when user has no
  selected agent or room is bound to a different agent
- agent_routing_enabled flag on MatrixRuntime activates the check only
  in real multi-agent mode (RoutedPlatformClient)
- make_handle_new_chat now writes agent_id into new room metadata when
  user already has a selected agent

Task 5: durable restart state tests
- test_restart_persistence.py proves selected_agent_id, room agent_id,
  platform_chat_id, and the sequence counter all survive SQLiteStore
  close/reopen; also covers clean startup with no prior state
2026-04-24 14:01:49 +03:00

75 lines
2.4 KiB
Python

from __future__ import annotations
import pytest
from core.store import SQLiteStore
from adapter.matrix.store import (
PLATFORM_CHAT_SEQ_KEY,
get_room_meta,
get_selected_agent_id,
next_platform_chat_id,
set_room_meta,
set_selected_agent_id,
)
async def test_selected_agent_id_survives_restart(tmp_path):
db = str(tmp_path / "state.db")
store = SQLiteStore(db)
await set_selected_agent_id(store, "@alice:example.org", "agent-2")
store2 = SQLiteStore(db)
assert await get_selected_agent_id(store2, "@alice:example.org") == "agent-2"
async def test_room_agent_id_and_platform_chat_id_survive_restart(tmp_path):
db = str(tmp_path / "state.db")
store = SQLiteStore(db)
await set_room_meta(store, "!room:example.org", {
"room_type": "chat",
"agent_id": "agent-1",
"platform_chat_id": "42",
})
store2 = SQLiteStore(db)
meta = await get_room_meta(store2, "!room:example.org")
assert meta is not None
assert meta["agent_id"] == "agent-1"
assert meta["platform_chat_id"] == "42"
async def test_platform_chat_seq_survives_restart(tmp_path):
db = str(tmp_path / "state.db")
store = SQLiteStore(db)
assert await next_platform_chat_id(store) == "1"
assert await next_platform_chat_id(store) == "2"
assert await next_platform_chat_id(store) == "3"
store2 = SQLiteStore(db)
assert await next_platform_chat_id(store2) == "4"
async def test_routing_state_survives_restart_and_routes_correctly(tmp_path):
db = str(tmp_path / "state.db")
store = SQLiteStore(db)
await set_selected_agent_id(store, "@bob:example.org", "agent-1")
await set_room_meta(store, "!convo:example.org", {
"room_type": "chat",
"agent_id": "agent-1",
"platform_chat_id": "10",
})
store2 = SQLiteStore(db)
selected = await get_selected_agent_id(store2, "@bob:example.org")
meta = await get_room_meta(store2, "!convo:example.org")
assert selected == "agent-1"
assert meta is not None
assert meta["agent_id"] == selected
assert meta["platform_chat_id"] == "10"
async def test_missing_durable_store_starts_clean(tmp_path):
db = str(tmp_path / "brand_new.db")
store = SQLiteStore(db)
assert await get_selected_agent_id(store, "@nobody:example.org") is None
assert await get_room_meta(store, "!nonexistent:example.org") is None