stage-5: full discord-claude chat integration

Message router, typing indicator, emoji reactions, auto start/resume sessions, message splitting >2000 chars. 34 new tests (141 total).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
MoltBot Service
2026-02-13 12:54:19 +00:00
parent 6cd155b71e
commit a1a6ca9a3f
4 changed files with 467 additions and 3 deletions

View File

@@ -14,6 +14,7 @@ from src.adapters.discord_bot import (
is_admin,
is_owner,
is_registered_channel,
split_message,
)
@@ -390,3 +391,112 @@ class TestOnMessage:
await on_message(message)
assert "dm from admin" in caplog.text.lower()
@pytest.mark.asyncio
@patch("src.adapters.discord_bot.route_message")
async def test_chat_flow(self, mock_route, owned_bot):
"""on_message chat flow: reaction, typing, route, send, cleanup."""
mock_route.return_value = "Hello from Claude!"
on_message = self._get_on_message(owned_bot)
message = AsyncMock(spec=discord.Message)
message.author = MagicMock()
message.author.id = 555
message.author.__eq__ = lambda self, other: False
message.channel = AsyncMock(spec=discord.TextChannel)
message.channel.id = 900 # registered channel
message.content = "test message"
await on_message(message)
# Verify eyes reaction added
message.add_reaction.assert_awaited_once_with("\U0001f440")
# Verify typing indicator was triggered
message.channel.typing.assert_called_once()
# Verify response sent
message.channel.send.assert_awaited_once_with("Hello from Claude!")
# Verify eyes reaction removed
message.remove_reaction.assert_awaited_once()
# --- split_message ---
class TestSplitMessage:
def test_short_text_no_split(self):
result = split_message("hello")
assert result == ["hello"]
def test_long_text_split_at_newline(self):
text = "a" * 10 + "\n" + "b" * 10
result = split_message(text, limit=15)
assert result == ["a" * 10, "b" * 10]
def test_very_long_without_newlines_hard_split(self):
text = "a" * 30
result = split_message(text, limit=10)
assert result == ["a" * 10, "a" * 10, "a" * 10]
# --- /clear slash command ---
class TestClearSlashCommand:
@pytest.mark.asyncio
@patch("src.adapters.discord_bot.clear_session")
async def test_clear_with_session(self, mock_clear, owned_bot):
mock_clear.return_value = True
cmd = _find_command(owned_bot.tree, "clear")
interaction = _mock_interaction(channel_id="900")
await cmd.callback(interaction)
msg = interaction.response.send_message.call_args
assert "session cleared" in msg.args[0].lower()
@pytest.mark.asyncio
@patch("src.adapters.discord_bot.clear_session")
async def test_clear_no_session(self, mock_clear, owned_bot):
mock_clear.return_value = False
cmd = _find_command(owned_bot.tree, "clear")
interaction = _mock_interaction(channel_id="900")
await cmd.callback(interaction)
msg = interaction.response.send_message.call_args
assert "no active session" in msg.args[0].lower()
# --- /status slash command ---
class TestStatusSlashCommand:
@pytest.mark.asyncio
@patch("src.adapters.discord_bot.get_active_session")
async def test_status_with_session(self, mock_get, owned_bot):
mock_get.return_value = {
"session_id": "abcdef1234567890",
"model": "sonnet",
"message_count": 3,
}
cmd = _find_command(owned_bot.tree, "status")
interaction = _mock_interaction(channel_id="900")
await cmd.callback(interaction)
msg = interaction.response.send_message.call_args
text = msg.args[0] if msg.args else msg.kwargs.get("content", "")
assert "sonnet" in text
assert "3" in text
@pytest.mark.asyncio
@patch("src.adapters.discord_bot.get_active_session")
async def test_status_no_session(self, mock_get, owned_bot):
mock_get.return_value = None
cmd = _find_command(owned_bot.tree, "status")
interaction = _mock_interaction(channel_id="900")
await cmd.callback(interaction)
msg = interaction.response.send_message.call_args
assert "no active session" in msg.args[0].lower()