from __future__ import annotations import uuid from adapter.max.handlers.attachments import AttachmentHandler from adapter.max.handlers.chat import ChatHandler as MaxChatHandler from adapter.max.store import ChatStore, RoomMeta def test_chat_store_room_roundtrip(): store = ChatStore() r = RoomMeta( platform_chat_id="pid-1", max_chat_id="100", name="Main", user_id="42", agent_id="agent-0", workspace_path="/agents/0", ) store.add_room(r) assert store.get_room_by_max_chat_id("100") is r assert store.get_room_by_platform_chat_id("pid-1") is r def test_staged_attachments(): store = ChatStore() store.stage_attachment("100", ("rel/path.txt", "path.txt")) assert store.get_attachments("100") popped = store.pop_attachments("100") assert len(popped) == 1 assert store.pop_attachments("100") == [] def test_remove_room_clears_staging(): store = ChatStore() store.stage_attachment("100", ("a", "a")) store.add_room( RoomMeta( platform_chat_id="x", max_chat_id="100", name="", user_id="u", agent_id="a", ) ) store.remove_room("100") assert store.get_room_by_max_chat_id("100") is None assert store.get_attachments("100") == [] def test_chat_handler_clear_rotates_platform_id(): store = ChatStore() h = MaxChatHandler(store) pid1 = str(uuid.uuid4()) store.add_room( RoomMeta( platform_chat_id=pid1, max_chat_id="100", name="Tab", user_id="42", agent_id="agent-0", workspace_path="/agents/0", ) ) h.handle_clear("100") room = store.get_room_by_max_chat_id("100") assert room is not None assert room.platform_chat_id != pid1 def test_attachment_handler_list_remove(): store = ChatStore() h = AttachmentHandler(store) store.stage_attachment("100", ("a", "f1.bin")) assert "f1.bin" in h.handle_list("100") msg = h.handle_remove("100", "1") assert "Удалено" in msg or "удалено" in msg.lower() assert "пуста" in h.handle_list("100").lower() or "пусто" in h.handle_list("100").lower()