surfaces/tests/adapter/matrix/test_restart_persistence.py
Mikhail Putilovskij a75b26a1cb test(05-01): add restart reconciliation regression coverage
- add startup reconciliation tests for recovery, idempotence, and startup ordering
- extend restart persistence coverage for legacy platform_chat_id backfill
2026-04-28 01:05:59 +03:00

129 lines
4.2 KiB
Python

from __future__ import annotations
from types import SimpleNamespace
from adapter.matrix.bot import build_runtime
from adapter.matrix.reconciliation import reconcile_startup_state
from adapter.matrix.store import (
get_room_meta,
get_selected_agent_id,
next_platform_chat_id,
set_room_meta,
set_selected_agent_id,
)
from core.store import SQLiteStore
from sdk.mock import MockPlatformClient
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
async def test_startup_reconciliation_backfills_legacy_platform_chat_id_before_restart_routes(
tmp_path,
):
db = str(tmp_path / "state.db")
store = SQLiteStore(db)
await set_room_meta(
store,
"!chat2:example.org",
{
"room_type": "chat",
"chat_id": "C2",
"display_name": "Чат 2",
"matrix_user_id": "@alice:example.org",
"space_id": "!space:example.org",
},
)
runtime = build_runtime(platform=MockPlatformClient(), store=store)
client = SimpleNamespace(
user_id="@bot:example.org",
rooms={
"!space:example.org": SimpleNamespace(
room_id="!space:example.org",
name="Lambda - Alice",
display_name="Lambda - Alice",
users={
"@bot:example.org": SimpleNamespace(user_id="@bot:example.org"),
"@alice:example.org": SimpleNamespace(user_id="@alice:example.org"),
},
space_parents=set(),
),
"!chat2:example.org": SimpleNamespace(
room_id="!chat2:example.org",
name="Чат 2",
display_name="Чат 2",
users={
"@bot:example.org": SimpleNamespace(user_id="@bot:example.org"),
"@alice:example.org": SimpleNamespace(user_id="@alice:example.org"),
},
space_parents={"!space:example.org"},
),
},
)
await reconcile_startup_state(client, runtime)
store2 = SQLiteStore(db)
room_meta = await get_room_meta(store2, "!chat2:example.org")
assert room_meta is not None
assert room_meta["platform_chat_id"] == "1"