fix: ensure lazy platform chat ids before load selection

This commit is contained in:
Mikhail Putilovskij 2026-04-19 17:29:36 +03:00
parent 9cb1657d21
commit 0cdee532c4
2 changed files with 43 additions and 5 deletions

View file

@ -9,7 +9,14 @@ from nio.responses import SyncResponse
from adapter.matrix.bot import MatrixBot, build_runtime, prepare_live_sync
from adapter.matrix.handlers.auth import handle_invite
from adapter.matrix.store import get_platform_chat_id, get_room_meta, get_user_meta, set_room_meta, set_user_meta
from adapter.matrix.store import (
get_platform_chat_id,
get_room_meta,
get_user_meta,
set_load_pending,
set_room_meta,
set_user_meta,
)
from core.protocol import IncomingCallback, IncomingCommand, OutgoingMessage
from sdk.interface import PlatformError
from sdk.mock import MockPlatformClient
@ -270,6 +277,37 @@ async def test_bot_leaves_existing_platform_chat_id_unchanged():
runtime.dispatcher.dispatch.assert_awaited_once()
async def test_bot_assigns_platform_chat_id_before_load_selection():
runtime = build_runtime(platform=MockPlatformClient())
await set_room_meta(
runtime.store,
"!chat1:example.org",
{"chat_id": "C1", "matrix_user_id": "@alice:example.org"},
)
client = SimpleNamespace(
user_id="@bot:example.org",
room_send=AsyncMock(),
)
bot = MatrixBot(client, runtime)
await set_load_pending(
runtime.store,
"@alice:example.org",
"!chat1:example.org",
{"saves": [{"name": "session-a", "created_at": "2026-04-17T00:00:00+00:00"}]},
)
room = SimpleNamespace(room_id="!chat1:example.org")
event = SimpleNamespace(sender="@alice:example.org", body="0")
await bot.on_room_message(room, event)
assert await get_platform_chat_id(runtime.store, "!chat1:example.org") == "matrix:!chat1:example.org"
client.room_send.assert_awaited_once_with(
"!chat1:example.org",
"m.room.message",
{"msgtype": "m.text", "body": "Отменено."},
)
async def test_unregistered_room_bootstraps_space_and_chat_on_first_message():
runtime = build_runtime(platform=MockPlatformClient())
await set_user_meta(runtime.store, "@alice:example.org", {"next_chat_index": 1})