|
1 | 1 | import base64 |
2 | | -from io import BytesIO |
| 2 | +from io import BufferedReader, BytesIO |
3 | 3 | from types import SimpleNamespace |
4 | | -from unittest.mock import AsyncMock |
| 4 | +from unittest.mock import AsyncMock, MagicMock |
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 |
|
8 | | -from astrbot.api.message_components import Image, Record |
| 8 | +from astrbot.api.message_components import File, Image, Plain, Record, Video |
9 | 9 | from astrbot.api.platform import Group, MessageType |
10 | 10 | from astrbot.core.message.message_event_result import MessageChain |
11 | 11 | from astrbot.core.platform.sources.discord import ( |
|
26 | 26 | _WAV_PATH = "/tmp/discord_voice.wav" |
27 | 27 |
|
28 | 28 |
|
| 29 | +@pytest.mark.asyncio |
| 30 | +@pytest.mark.parametrize("component_type", ["video", "video_url", "file"]) |
| 31 | +@pytest.mark.parametrize("with_text", [False, True]) |
| 32 | +async def test_discord_sends_video_attachment( |
| 33 | + tmp_path, monkeypatch, component_type, with_text |
| 34 | +): |
| 35 | + video_path = tmp_path / ( |
| 36 | + "media_video_test.mp4" if component_type == "video_url" else "repro.mp4" |
| 37 | + ) |
| 38 | + video_bytes = b"video attachment payload" |
| 39 | + video_path.write_bytes(video_bytes) |
| 40 | + if component_type == "video": |
| 41 | + component = Video.fromFileSystem(str(video_path)) |
| 42 | + elif component_type == "video_url": |
| 43 | + component = Video.fromURL("https://example.com/repro.mp4") |
| 44 | + monkeypatch.setattr( |
| 45 | + Video, |
| 46 | + "convert_to_file_path", |
| 47 | + AsyncMock(return_value=str(video_path)), |
| 48 | + ) |
| 49 | + else: |
| 50 | + component = File(name=video_path.name, file=str(video_path)) |
| 51 | + |
| 52 | + channel = MagicMock(spec=discord_platform_event.discord.abc.Messageable) |
| 53 | + channel.send = AsyncMock() |
| 54 | + event = DiscordPlatformEvent.__new__(DiscordPlatformEvent) |
| 55 | + event.interaction_followup_webhook = None |
| 56 | + event._get_channel = AsyncMock(return_value=channel) |
| 57 | + monkeypatch.setattr(discord_platform_event.AstrMessageEvent, "send", AsyncMock()) |
| 58 | + chain = [Plain("Video test"), component] if with_text else [component] |
| 59 | + |
| 60 | + await event.send(MessageChain(chain=chain)) |
| 61 | + |
| 62 | + channel.send.assert_awaited_once() |
| 63 | + kwargs = channel.send.call_args.kwargs |
| 64 | + assert kwargs.get("content", "") == ("Video test" if with_text else "") |
| 65 | + assert len(kwargs["files"]) == 1 |
| 66 | + attachment = kwargs["files"][0] |
| 67 | + try: |
| 68 | + assert attachment.filename == "repro.mp4" |
| 69 | + if component_type != "file": |
| 70 | + assert isinstance(attachment.fp, BufferedReader) |
| 71 | + assert attachment.fp.tell() == 0 |
| 72 | + assert attachment.fp.read() == video_bytes |
| 73 | + finally: |
| 74 | + attachment.close() |
| 75 | + if component_type != "file": |
| 76 | + assert attachment.fp.closed |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.asyncio |
| 80 | +@pytest.mark.parametrize( |
| 81 | + ("url", "expected_name"), |
| 82 | + [ |
| 83 | + ("https://example.com/clips/demo.mp4", "demo.mp4"), |
| 84 | + ("https://example.com/demo.mp4?token=test#preview", "demo.mp4"), |
| 85 | + ("https://example.com/%E6%B5%8B%E8%AF%95%20video.mp4", "测试 video.mp4"), |
| 86 | + ("https://example.com", "media_video_test.mp4"), |
| 87 | + ("https://example.com/clips/?token=test", "media_video_test.mp4"), |
| 88 | + ], |
| 89 | +) |
| 90 | +async def test_discord_video_url_attachment_filename( |
| 91 | + tmp_path, monkeypatch, url, expected_name |
| 92 | +): |
| 93 | + video_path = tmp_path / "media_video_test.mp4" |
| 94 | + video_path.write_bytes(b"video attachment payload") |
| 95 | + monkeypatch.setattr( |
| 96 | + Video, "convert_to_file_path", AsyncMock(return_value=str(video_path)) |
| 97 | + ) |
| 98 | + event = DiscordPlatformEvent.__new__(DiscordPlatformEvent) |
| 99 | + |
| 100 | + _, files, _, _, _ = await event._parse_to_discord( |
| 101 | + MessageChain(chain=[Video.fromURL(url)]) |
| 102 | + ) |
| 103 | + |
| 104 | + assert len(files) == 1 |
| 105 | + try: |
| 106 | + assert files[0].filename == expected_name |
| 107 | + assert files[0].fp.name == str(video_path) |
| 108 | + finally: |
| 109 | + files[0].close() |
| 110 | + |
| 111 | + |
29 | 112 | @pytest.mark.asyncio |
30 | 113 | async def test_discord_group_message_includes_guild_and_channel_name(): |
31 | 114 | adapter = DiscordPlatformAdapter.__new__(DiscordPlatformAdapter) |
|
0 commit comments