surfaces/tests/adapter/max/test_converter.py

154 lines
4 KiB
Python

from __future__ import annotations
import pytest
from adapter.max.converter import (
attachment_from_max_dict,
collect_max_attachments,
incoming_from_message_callback_payload,
incoming_from_text_commands,
)
from core.protocol import Attachment, IncomingCallback, IncomingCommand, IncomingMessage
@pytest.mark.parametrize(
"text,expect_type",
[
("Hello", IncomingMessage),
(" plain ", IncomingMessage),
],
)
def test_plain_text_to_message(text, expect_type):
r = incoming_from_text_commands(
text=text,
max_user_id="10",
platform_chat_id="pc-1",
attachments=[],
)
assert type(r) is expect_type
assert r.text == text
def test_slash_command_split():
r = incoming_from_text_commands(
text="/new title here",
max_user_id="10",
platform_chat_id="pc-1",
attachments=[],
)
assert isinstance(r, IncomingCommand)
assert r.command == "new"
assert r.args == ["title", "here"]
def test_slash_command_no_args():
r = incoming_from_text_commands(
text="/help",
max_user_id="10",
platform_chat_id="pc-1",
attachments=[],
)
assert isinstance(r, IncomingCommand)
assert r.command == "help"
assert r.args == []
def test_bang_prefix_is_plain_message_not_command():
"""MAX: только / считается командой."""
r = incoming_from_text_commands(
text="!help",
max_user_id="10",
platform_chat_id="pc-1",
attachments=[],
)
assert isinstance(r, IncomingMessage)
assert r.text == "!help"
def test_yes_no_callbacks():
yes = incoming_from_text_commands(
text="/yes",
max_user_id="10",
platform_chat_id="pc-1",
attachments=[],
)
assert isinstance(yes, IncomingCallback)
assert yes.action == "confirm"
no = incoming_from_text_commands(
text="/NO",
max_user_id="10",
platform_chat_id="pc-1",
attachments=[],
)
assert isinstance(no, IncomingCallback)
assert no.action == "cancel"
def test_incoming_message_keeps_attachments():
at = [Attachment(type="document", filename="a.txt")]
r = incoming_from_text_commands(
text="see file",
max_user_id="10",
platform_chat_id="pc-1",
attachments=at,
)
assert isinstance(r, IncomingMessage)
assert r.attachments == at
def test_message_callback_known_actions():
c = incoming_from_message_callback_payload(
max_user_id="10",
platform_chat_id="pc",
payload_raw="confirm",
callback_message_id="mid",
)
assert c is not None
assert isinstance(c, IncomingCallback)
assert c.action == "confirm"
assert c.payload.get("message_id") == "mid"
def test_message_callback_unknown_becomes_max_callback():
c = incoming_from_message_callback_payload(
max_user_id="10",
platform_chat_id="pc",
payload_raw="my_payload",
callback_message_id=None,
)
assert c is not None
assert c.action == "max_callback"
assert c.payload["payload"] == "my_payload"
def test_attachment_from_max_file():
parsed = attachment_from_max_dict(
{
"type": "file",
"payload": {
"url": "https://cdn.example/f",
"filename": "doc.pdf",
"token": "tok",
},
}
)
assert parsed is not None
att, raw = parsed
assert att.filename == "doc.pdf"
assert att.type == "document"
assert att.url == "https://cdn.example/f"
assert raw.get("_download_token_hint") == "tok"
def test_collect_max_attachments_skips_unknown():
core, raw = collect_max_attachments(
{
"attachments": [
{"type": "file", "payload": {"url": "u", "filename": "x.bin"}},
{"type": "sticker", "payload": {}},
]
}
)
assert len(core) == len(raw) == 1
assert core[0].filename == "x.bin"