test(telegram): cover local attachment filenames

Add a regression test for preserving the original basename when Telegram sends local media bytes.

Made-with: Cursor
This commit is contained in:
Xubin Ren 2026-04-28 07:11:53 +00:00 committed by Xubin Ren
parent e36e70fe16
commit 50698c3d1c

View File

@ -1,4 +1,3 @@
import asyncio
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import AsyncMock
@ -13,8 +12,12 @@ except ImportError:
from nanobot.bus.events import OutboundMessage
from nanobot.bus.queue import MessageBus
from nanobot.channels.telegram import TELEGRAM_REPLY_CONTEXT_MAX_LEN, TelegramChannel, _StreamBuf
from nanobot.channels.telegram import TelegramConfig
from nanobot.channels.telegram import (
TELEGRAM_REPLY_CONTEXT_MAX_LEN,
TelegramChannel,
TelegramConfig,
_StreamBuf,
)
class _FakeHTTPXRequest:
@ -752,6 +755,36 @@ async def test_send_remote_media_url_after_security_validation(monkeypatch) -> N
]
@pytest.mark.asyncio
async def test_send_local_media_preserves_filename(tmp_path: Path) -> None:
channel = TelegramChannel(
TelegramConfig(enabled=True, token="123:abc", allow_from=["*"]),
MessageBus(),
)
channel._app = _FakeApp(lambda: None)
attachment = tmp_path / "report.final.md"
attachment.write_bytes(b"# Report\n")
await channel.send(
OutboundMessage(
channel="telegram",
chat_id="123",
content="",
media=[str(attachment)],
)
)
assert channel._app.bot.sent_media == [
{
"kind": "document",
"chat_id": 123,
"document": b"# Report\n",
"reply_parameters": None,
"filename": "report.final.md",
}
]
@pytest.mark.asyncio
async def test_send_blocks_unsafe_remote_media_url(monkeypatch) -> None:
channel = TelegramChannel(