feat: finalize matrix platform audit and docs

This commit is contained in:
Mikhail Putilovskij 2026-04-21 15:35:03 +03:00
parent 6422c7db58
commit 4524a6abc8
30 changed files with 3093 additions and 176 deletions

View file

@ -1,8 +1,7 @@
from __future__ import annotations
from types import SimpleNamespace
from unittest.mock import AsyncMock, patch
from unittest.mock import AsyncMock
import pytest
@ -15,7 +14,6 @@ from adapter.matrix.handlers.context_commands import (
)
from adapter.matrix.store import (
get_load_pending,
set_load_pending,
set_room_meta,
)
@ -48,7 +46,7 @@ async def test_save_command_auto_name_records_session():
await set_room_meta(
store,
"!room:example.org",
{"chat_id": "C1", "matrix_user_id": "u1", "platform_chat_id": "matrix:room-1"},
{"chat_id": "C1", "matrix_user_id": "u1", "platform_chat_id": "41"},
)
handler = make_handle_save(
agent_api=platform._agent_api,
@ -71,7 +69,7 @@ async def test_save_command_auto_name_records_session():
sessions = await platform._prototype_state.list_saved_sessions("u1")
assert len(sessions) == 1
assert sessions[0]["name"].startswith("context-")
assert sessions[0]["source_context_id"] == "matrix:room-1"
assert sessions[0]["source_context_id"] == "41"
@pytest.mark.asyncio
@ -81,7 +79,7 @@ async def test_save_command_with_name_uses_given_name():
await set_room_meta(
store,
"!room:example.org",
{"chat_id": "C1", "matrix_user_id": "u1", "platform_chat_id": "matrix:room-1"},
{"chat_id": "C1", "matrix_user_id": "u1", "platform_chat_id": "41"},
)
handler = make_handle_save(
agent_api=platform._agent_api,
@ -119,7 +117,13 @@ async def test_load_command_shows_numbered_list_and_sets_pending():
handler = make_handle_load(store=runtime.store, prototype_state=platform._prototype_state)
event = IncomingCommand(user_id="u1", platform="matrix", chat_id="C1", command="load", args=[])
result = await handler(event, runtime.auth_mgr, platform, runtime.chat_mgr, runtime.settings_mgr)
result = await handler(
event,
runtime.auth_mgr,
platform,
runtime.chat_mgr,
runtime.settings_mgr,
)
assert "1. session-a" in result[0].text
assert "2. session-b" in result[0].text
@ -150,16 +154,28 @@ async def test_reset_command_assigns_new_platform_chat_id():
runtime = build_runtime(platform=platform)
store = runtime.store
await set_room_meta(store, "!room:example.org", {"platform_chat_id": "matrix:!room:example.org"})
await set_room_meta(store, "!room:example.org", {"platform_chat_id": "7"})
handler = make_handle_reset(store=store, prototype_state=prototype_state)
event = IncomingCommand(user_id="u1", platform="matrix", chat_id="!room:example.org", command="reset", args=[])
event = IncomingCommand(
user_id="u1",
platform="matrix",
chat_id="!room:example.org",
command="reset",
args=[],
)
result = await handler(event, runtime.auth_mgr, platform, runtime.chat_mgr, runtime.settings_mgr)
result = await handler(
event,
runtime.auth_mgr,
platform,
runtime.chat_mgr,
runtime.settings_mgr,
)
new_id = await get_platform_chat_id(store, "!room:example.org")
assert new_id != "matrix:!room:example.org"
assert new_id.startswith("matrix:!room:example.org#")
assert new_id != "7"
assert new_id == "1"
assert "сброшен" in result[0].text.lower()
@ -177,17 +193,29 @@ async def test_context_command_shows_current_snapshot():
await set_room_meta(
runtime.store,
"!room:example.org",
{"chat_id": "C1", "matrix_user_id": "u1", "platform_chat_id": "matrix:room-1"},
{"chat_id": "C1", "matrix_user_id": "u1", "platform_chat_id": "41"},
)
await platform._prototype_state.set_current_session("matrix:room-1", "session-a")
await platform._prototype_state.set_last_tokens_used("matrix:room-1", 99)
await platform._prototype_state.set_current_session("41", "session-a")
await platform._prototype_state.set_last_tokens_used("41", 99)
await platform._prototype_state.add_saved_session("u1", "session-a")
handler = make_handle_context(store=runtime.store, prototype_state=platform._prototype_state)
event = IncomingCommand(user_id="u1", platform="matrix", chat_id="C1", command="context", args=[])
event = IncomingCommand(
user_id="u1",
platform="matrix",
chat_id="C1",
command="context",
args=[],
)
result = await handler(event, runtime.auth_mgr, platform, runtime.chat_mgr, runtime.settings_mgr)
result = await handler(
event,
runtime.auth_mgr,
platform,
runtime.chat_mgr,
runtime.settings_mgr,
)
assert "Контекст чата: matrix:room-1" in result[0].text
assert "Контекст чата: 41" in result[0].text
assert "Сессия: session-a" in result[0].text
assert "Токены (последний ответ): 99" in result[0].text
assert "session-a" in result[0].text
@ -203,7 +231,7 @@ async def test_bot_intercepts_numeric_load_selection():
{
"chat_id": "C1",
"matrix_user_id": "@alice:example.org",
"platform_chat_id": "matrix:room-1",
"platform_chat_id": "41",
},
)
client = SimpleNamespace(
@ -223,7 +251,7 @@ async def test_bot_intercepts_numeric_load_selection():
await bot.on_room_message(room, event)
platform.send_message.assert_awaited_once()
assert await platform._prototype_state.get_current_session("matrix:room-1") == "session-a"
assert await platform._prototype_state.get_current_session("41") == "session-a"
assert await platform._prototype_state.get_current_session("C1") == "session-a"
client.room_send.assert_awaited_once_with(
"!room:example.org",