chorus/bot/tests/test_manual_share.py

98 lines
3.4 KiB
Python

from unittest.mock import AsyncMock, MagicMock
import pytest
import discord
from chorus_bot.cogs.manual_share import ManualShareCog
from chorus_bot.ping import parse_ping_embed
class _FakeConfig:
inbox_channel_id = 111
triage_role_id = 222
def _make_attachment(url):
attachment = MagicMock(spec=discord.Attachment)
attachment.url = url
return attachment
def _make_message(*, channel_name, content="hello", author_bot=False, channel_id=999,
attachments=None):
message = MagicMock(spec=discord.Message)
message.author.bot = author_bot
message.channel.id = channel_id
message.channel.name = channel_name
message.content = content
message.attachments = attachments or []
message.created_at = __import__("datetime").datetime(2026, 7, 13, 7, 0, 0)
return message
@pytest.mark.asyncio
async def test_leads_found_channel_forwards_with_outbound_direction():
bot = MagicMock()
inbox = AsyncMock()
bot.get_channel.return_value = inbox
cog = ManualShareCog(bot, _FakeConfig())
message = _make_message(channel_name="leads-found", content="Neighbor posting about book giveaway")
await cog.on_message(message)
sent_embed = inbox.send.call_args.kwargs["embed"]
parsed = parse_ping_embed(sent_embed)
assert parsed["direction"] == "outbound"
assert parsed["modality"] == "lead_found"
@pytest.mark.asyncio
async def test_existing_modality_channel_still_forwards_with_inbound_direction():
bot = MagicMock()
inbox = AsyncMock()
bot.get_channel.return_value = inbox
cog = ManualShareCog(bot, _FakeConfig())
message = _make_message(channel_name="voice", content="call about donation")
await cog.on_message(message)
sent_embed = inbox.send.call_args.kwargs["embed"]
parsed = parse_ping_embed(sent_embed)
assert parsed["direction"] == "inbound"
@pytest.mark.asyncio
async def test_message_with_attachment_includes_image_and_url_in_raw_content():
bot = MagicMock()
inbox = AsyncMock()
bot.get_channel.return_value = inbox
cog = ManualShareCog(bot, _FakeConfig())
attachment = _make_attachment("https://cdn.discordapp.com/attachments/1/2/screenshot.png")
message = _make_message(
channel_name="leads-found", content="Found on Nextdoor", attachments=[attachment],
)
await cog.on_message(message)
sent_embed = inbox.send.call_args.kwargs["embed"]
assert sent_embed.image.url == "https://cdn.discordapp.com/attachments/1/2/screenshot.png"
parsed = parse_ping_embed(sent_embed)
assert "https://cdn.discordapp.com/attachments/1/2/screenshot.png" in parsed["raw_content"]
assert "Found on Nextdoor" in parsed["raw_content"]
@pytest.mark.asyncio
async def test_attachment_only_message_still_includes_image():
bot = MagicMock()
inbox = AsyncMock()
bot.get_channel.return_value = inbox
cog = ManualShareCog(bot, _FakeConfig())
attachment = _make_attachment("https://cdn.discordapp.com/attachments/1/2/screenshot.png")
message = _make_message(channel_name="leads-found", content="", attachments=[attachment])
await cog.on_message(message)
sent_embed = inbox.send.call_args.kwargs["embed"]
assert sent_embed.image.url == "https://cdn.discordapp.com/attachments/1/2/screenshot.png"
parsed = parse_ping_embed(sent_embed)
assert "https://cdn.discordapp.com/attachments/1/2/screenshot.png" in parsed["raw_content"]