feat: commit staged matrix attachments on next message

This commit is contained in:
Mikhail Putilovskij 2026-04-20 21:39:37 +03:00
parent f111ed3348
commit 323a6d3144
3 changed files with 155 additions and 23 deletions

View file

@ -527,6 +527,89 @@ async def test_staged_attachment_commands_are_scoped_by_room_and_user():
assert "bob-room-one.pdf" not in body
async def test_next_normal_message_commits_staged_attachments():
runtime = build_runtime(platform=MockPlatformClient())
await set_room_meta(
runtime.store,
"!r:example.org",
{
"chat_id": "C1",
"matrix_user_id": "@alice:example.org",
"platform_chat_id": "matrix:ctx-1",
},
)
await add_staged_attachment(
runtime.store,
"!r:example.org",
"@alice:example.org",
{
"type": "document",
"filename": "report.pdf",
"workspace_path": "surfaces/matrix/alice/r/inbox/report.pdf",
"mime_type": "application/pdf",
},
)
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="!r:example.org")
event = SimpleNamespace(
sender="@alice:example.org",
body="Проанализируй",
msgtype="m.text",
replyto_event_id=None,
)
await bot.on_room_message(room, event)
dispatched = runtime.dispatcher.dispatch.await_args.args[0]
assert isinstance(dispatched, IncomingMessage)
assert dispatched.text == "Проанализируй"
assert [a.workspace_path for a in dispatched.attachments] == [
"surfaces/matrix/alice/r/inbox/report.pdf"
]
assert await get_staged_attachments(runtime.store, "!r:example.org", "@alice:example.org") == []
async def test_failed_commit_preserves_staged_attachments():
runtime = build_runtime(platform=MockPlatformClient())
await set_room_meta(
runtime.store,
"!r:example.org",
{
"chat_id": "C1",
"matrix_user_id": "@alice:example.org",
"platform_chat_id": "matrix:ctx-1",
},
)
await add_staged_attachment(
runtime.store,
"!r:example.org",
"@alice:example.org",
{
"type": "document",
"filename": "report.pdf",
"workspace_path": "surfaces/matrix/alice/r/inbox/report.pdf",
},
)
client = SimpleNamespace(user_id="@bot:example.org", room_send=AsyncMock())
bot = MatrixBot(client, runtime)
runtime.dispatcher.dispatch = AsyncMock(side_effect=PlatformError("boom"))
room = SimpleNamespace(room_id="!r:example.org")
event = SimpleNamespace(
sender="@alice:example.org",
body="Проанализируй",
msgtype="m.text",
replyto_event_id=None,
)
await bot.on_room_message(room, event)
staged = await get_staged_attachments(runtime.store, "!r:example.org", "@alice:example.org")
assert [item["filename"] for item in staged] == ["report.pdf"]
async def test_bot_keeps_commands_on_local_chat_id():
runtime = build_runtime(platform=MockPlatformClient())
await set_room_meta(