fix(gateway): include replied-to media attachments (#46107)
This commit is contained in:
@@ -5,6 +5,7 @@ Covers the fix for slash commands not being recognized when sent via
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
@@ -104,3 +105,51 @@ class TestAutoThreadingPreservesCommand:
|
||||
response = get_response_text(discord_adapter)
|
||||
assert response is not None
|
||||
assert "/new" in response
|
||||
|
||||
|
||||
class TestRepliedToMediaDispatch:
|
||||
async def test_reply_to_image_message_caches_referenced_attachment(
|
||||
self, discord_adapter, bot_user, monkeypatch
|
||||
):
|
||||
"""A text reply to an image-bearing Discord message should give the agent that image."""
|
||||
cached_path = "/tmp/replied-discord-image.png"
|
||||
|
||||
async def fake_cache_image_from_url(url, *, ext=".jpg"):
|
||||
assert url == "https://cdn.discordapp.com/attachments/image.png"
|
||||
assert ext == ".png"
|
||||
return cached_path
|
||||
|
||||
monkeypatch.setattr(
|
||||
"plugins.platforms.discord.adapter.cache_image_from_url",
|
||||
fake_cache_image_from_url,
|
||||
)
|
||||
discord_adapter.handle_message = AsyncMock()
|
||||
|
||||
attachment = SimpleNamespace(
|
||||
content_type="image/png",
|
||||
filename="image.png",
|
||||
url="https://cdn.discordapp.com/attachments/image.png",
|
||||
size=1234,
|
||||
)
|
||||
referenced_message = SimpleNamespace(
|
||||
id=12345,
|
||||
content="",
|
||||
attachments=[attachment],
|
||||
)
|
||||
msg = make_discord_message(
|
||||
content=f"<@{BOT_USER_ID}> what's in this image?",
|
||||
mentions=[bot_user],
|
||||
)
|
||||
msg.type = 19
|
||||
msg.reference = SimpleNamespace(message_id=12345, resolved=referenced_message)
|
||||
|
||||
await discord_adapter._handle_message(msg)
|
||||
|
||||
discord_adapter.handle_message.assert_awaited_once()
|
||||
await_args = discord_adapter.handle_message.await_args
|
||||
assert await_args is not None
|
||||
event = await_args.args[0]
|
||||
assert event.reply_to_message_id == "12345"
|
||||
assert event.media_urls == [cached_path]
|
||||
assert event.media_types == ["image/png"]
|
||||
assert event.message_type.value == "photo"
|
||||
|
||||
@@ -1007,6 +1007,53 @@ def test_triggered_voice_message_uses_shared_session_in_observe_mode():
|
||||
asyncio.run(_run())
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Replied-to media caching
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def test_text_reply_to_photo_caches_referenced_media(monkeypatch, tmp_path):
|
||||
async def _run():
|
||||
adapter = _make_adapter(require_mention=False)
|
||||
adapter.handle_message = AsyncMock()
|
||||
cached_path = tmp_path / "reply_photo.png"
|
||||
monkeypatch.setattr(
|
||||
"gateway.platforms.base.cache_image_from_bytes",
|
||||
lambda _data, ext=".jpg": str(cached_path),
|
||||
)
|
||||
file_obj = SimpleNamespace(
|
||||
file_path="photos/replied.png",
|
||||
download_as_bytearray=AsyncMock(return_value=bytearray(b"\x89PNG\r\n\x1a\n reply")),
|
||||
)
|
||||
photo = SimpleNamespace(file_size=1234, get_file=AsyncMock(return_value=file_obj))
|
||||
replied = SimpleNamespace(
|
||||
message_id=51,
|
||||
text=None,
|
||||
caption=None,
|
||||
photo=[photo],
|
||||
video=None,
|
||||
audio=None,
|
||||
voice=None,
|
||||
document=None,
|
||||
)
|
||||
msg = _group_message("what's in this image?", reply_to_bot=False)
|
||||
msg.reply_to_message = replied
|
||||
update = SimpleNamespace(update_id=3010, message=msg, effective_message=msg)
|
||||
|
||||
await adapter._handle_text_message(update, SimpleNamespace())
|
||||
await asyncio.sleep(0.05)
|
||||
|
||||
adapter.handle_message.assert_awaited_once()
|
||||
await_args = adapter.handle_message.await_args
|
||||
assert await_args is not None
|
||||
event = await_args.args[0]
|
||||
assert event.reply_to_message_id == "51"
|
||||
assert event.media_urls == [str(cached_path)]
|
||||
assert event.media_types == ["image/png"]
|
||||
assert event.message_type == MessageType.PHOTO
|
||||
|
||||
asyncio.run(_run())
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Observed-media caching (unmentioned group attachments)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user