Add lazy platform chat IDs for Matrix rooms
This commit is contained in:
parent
c666d908da
commit
9cb1657d21
2 changed files with 60 additions and 1 deletions
|
|
@ -9,7 +9,7 @@ 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_room_meta, get_user_meta, set_user_meta
|
||||
from adapter.matrix.store import get_platform_chat_id, get_room_meta, get_user_meta, set_room_meta, set_user_meta
|
||||
from core.protocol import IncomingCallback, IncomingCommand, OutgoingMessage
|
||||
from sdk.interface import PlatformError
|
||||
from sdk.mock import MockPlatformClient
|
||||
|
|
@ -226,6 +226,50 @@ async def test_bot_degrades_platform_errors_to_user_reply():
|
|||
)
|
||||
|
||||
|
||||
async def test_bot_assigns_platform_chat_id_for_existing_managed_room():
|
||||
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")
|
||||
bot = MatrixBot(client, runtime)
|
||||
bot._send_all = AsyncMock()
|
||||
runtime.dispatcher.dispatch = AsyncMock(return_value=[])
|
||||
room = SimpleNamespace(room_id="!chat1:example.org")
|
||||
event = SimpleNamespace(sender="@alice:example.org", body="hello")
|
||||
|
||||
await bot.on_room_message(room, event)
|
||||
|
||||
assert await get_platform_chat_id(runtime.store, "!chat1:example.org") == "matrix:!chat1:example.org"
|
||||
runtime.dispatcher.dispatch.assert_awaited_once()
|
||||
|
||||
|
||||
async def test_bot_leaves_existing_platform_chat_id_unchanged():
|
||||
runtime = build_runtime(platform=MockPlatformClient())
|
||||
await set_room_meta(
|
||||
runtime.store,
|
||||
"!chat1:example.org",
|
||||
{
|
||||
"chat_id": "C1",
|
||||
"matrix_user_id": "@alice:example.org",
|
||||
"platform_chat_id": "matrix:existing",
|
||||
},
|
||||
)
|
||||
client = SimpleNamespace(user_id="@bot:example.org")
|
||||
bot = MatrixBot(client, runtime)
|
||||
bot._send_all = AsyncMock()
|
||||
runtime.dispatcher.dispatch = AsyncMock(return_value=[])
|
||||
room = SimpleNamespace(room_id="!chat1:example.org")
|
||||
event = SimpleNamespace(sender="@alice:example.org", body="hello")
|
||||
|
||||
await bot.on_room_message(room, event)
|
||||
|
||||
assert await get_platform_chat_id(runtime.store, "!chat1:example.org") == "matrix:existing"
|
||||
runtime.dispatcher.dispatch.assert_awaited_once()
|
||||
|
||||
|
||||
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})
|
||||
|
|
@ -293,6 +337,9 @@ async def test_unregistered_room_second_message_reuses_existing_bootstrap():
|
|||
and "Рабочий чат уже создан: C1" in call.args[2]["body"]
|
||||
for call in room_send_calls
|
||||
)
|
||||
entry_meta = await get_room_meta(runtime.store, "!entry:example.org")
|
||||
assert entry_meta is not None
|
||||
assert "platform_chat_id" not in entry_meta
|
||||
|
||||
|
||||
async def test_unregistered_room_creates_new_chat_in_existing_space():
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue