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:
@@ -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()
|
||||
|
||||
188
tests/test_router.py
Normal file
188
tests/test_router.py
Normal file
@@ -0,0 +1,188 @@
|
||||
"""Tests for src/router.py — message router."""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from src.router import route_message, _get_channel_config
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def reset_router_config():
|
||||
"""Reset the module-level _config before each test."""
|
||||
import src.router
|
||||
original = src.router._config
|
||||
src.router._config = None
|
||||
yield
|
||||
src.router._config = original
|
||||
|
||||
|
||||
# --- /clear command ---
|
||||
|
||||
|
||||
class TestClearCommand:
|
||||
@patch("src.router.clear_session")
|
||||
def test_clear_active_session(self, mock_clear):
|
||||
mock_clear.return_value = True
|
||||
response, is_cmd = route_message("ch-1", "user-1", "/clear")
|
||||
assert response == "Session cleared."
|
||||
assert is_cmd is True
|
||||
mock_clear.assert_called_once_with("ch-1")
|
||||
|
||||
@patch("src.router.clear_session")
|
||||
def test_clear_no_session(self, mock_clear):
|
||||
mock_clear.return_value = False
|
||||
response, is_cmd = route_message("ch-1", "user-1", "/clear")
|
||||
assert response == "No active session."
|
||||
assert is_cmd is True
|
||||
|
||||
|
||||
# --- /status command ---
|
||||
|
||||
|
||||
class TestStatusCommand:
|
||||
@patch("src.router.get_active_session")
|
||||
def test_status_active_session(self, mock_get):
|
||||
mock_get.return_value = {
|
||||
"model": "sonnet",
|
||||
"session_id": "abcdef123456789",
|
||||
"message_count": 5,
|
||||
}
|
||||
response, is_cmd = route_message("ch-1", "user-1", "/status")
|
||||
assert is_cmd is True
|
||||
assert "sonnet" in response
|
||||
assert "abcdef123456" in response # first 12 chars
|
||||
assert "5" in response
|
||||
|
||||
@patch("src.router.get_active_session")
|
||||
def test_status_no_session(self, mock_get):
|
||||
mock_get.return_value = None
|
||||
response, is_cmd = route_message("ch-1", "user-1", "/status")
|
||||
assert response == "No active session."
|
||||
assert is_cmd is True
|
||||
|
||||
|
||||
# --- Unknown command ---
|
||||
|
||||
|
||||
class TestUnknownCommand:
|
||||
def test_unknown_command(self):
|
||||
response, is_cmd = route_message("ch-1", "user-1", "/foo")
|
||||
assert response == "Unknown command: /foo"
|
||||
assert is_cmd is True
|
||||
|
||||
def test_unknown_command_with_args(self):
|
||||
response, is_cmd = route_message("ch-1", "user-1", "/bar baz")
|
||||
assert response == "Unknown command: /bar"
|
||||
assert is_cmd is True
|
||||
|
||||
|
||||
# --- Regular messages ---
|
||||
|
||||
|
||||
class TestRegularMessage:
|
||||
@patch("src.router._get_channel_config")
|
||||
@patch("src.router._get_config")
|
||||
@patch("src.router.send_message")
|
||||
def test_sends_to_claude(self, mock_send, mock_get_config, mock_chan_cfg):
|
||||
mock_send.return_value = "Hello from Claude!"
|
||||
mock_chan_cfg.return_value = None
|
||||
mock_cfg = MagicMock()
|
||||
mock_cfg.get.return_value = "sonnet"
|
||||
mock_get_config.return_value = mock_cfg
|
||||
|
||||
response, is_cmd = route_message("ch-1", "user-1", "hello")
|
||||
assert response == "Hello from Claude!"
|
||||
assert is_cmd is False
|
||||
mock_send.assert_called_once_with("ch-1", "hello", model="sonnet")
|
||||
|
||||
@patch("src.router.send_message")
|
||||
def test_model_override(self, mock_send):
|
||||
mock_send.return_value = "Response"
|
||||
response, is_cmd = route_message("ch-1", "user-1", "hello", model="opus")
|
||||
assert response == "Response"
|
||||
assert is_cmd is False
|
||||
mock_send.assert_called_once_with("ch-1", "hello", model="opus")
|
||||
|
||||
@patch("src.router._get_channel_config")
|
||||
@patch("src.router._get_config")
|
||||
@patch("src.router.send_message")
|
||||
def test_claude_error(self, mock_send, mock_get_config, mock_chan_cfg):
|
||||
mock_send.side_effect = RuntimeError("API timeout")
|
||||
mock_chan_cfg.return_value = None
|
||||
mock_cfg = MagicMock()
|
||||
mock_cfg.get.return_value = "sonnet"
|
||||
mock_get_config.return_value = mock_cfg
|
||||
|
||||
response, is_cmd = route_message("ch-1", "user-1", "hello")
|
||||
assert "Error: API timeout" in response
|
||||
assert is_cmd is False
|
||||
|
||||
|
||||
# --- _get_channel_config ---
|
||||
|
||||
|
||||
class TestGetChannelConfig:
|
||||
@patch("src.router._get_config")
|
||||
def test_finds_by_id(self, mock_get_config):
|
||||
mock_cfg = MagicMock()
|
||||
mock_cfg.get.return_value = {
|
||||
"general": {"id": "ch-1", "default_model": "haiku"},
|
||||
"dev": {"id": "ch-2"},
|
||||
}
|
||||
mock_get_config.return_value = mock_cfg
|
||||
|
||||
result = _get_channel_config("ch-1")
|
||||
assert result == {"id": "ch-1", "default_model": "haiku"}
|
||||
|
||||
@patch("src.router._get_config")
|
||||
def test_returns_none_when_not_found(self, mock_get_config):
|
||||
mock_cfg = MagicMock()
|
||||
mock_cfg.get.return_value = {"general": {"id": "ch-1"}}
|
||||
mock_get_config.return_value = mock_cfg
|
||||
|
||||
result = _get_channel_config("ch-999")
|
||||
assert result is None
|
||||
|
||||
|
||||
# --- Model resolution ---
|
||||
|
||||
|
||||
class TestModelResolution:
|
||||
@patch("src.router._get_channel_config")
|
||||
@patch("src.router._get_config")
|
||||
@patch("src.router.send_message")
|
||||
def test_channel_default_model(self, mock_send, mock_get_config, mock_chan_cfg):
|
||||
"""Channel config default_model takes priority."""
|
||||
mock_send.return_value = "ok"
|
||||
mock_chan_cfg.return_value = {"id": "ch-1", "default_model": "haiku"}
|
||||
|
||||
route_message("ch-1", "user-1", "hello")
|
||||
mock_send.assert_called_once_with("ch-1", "hello", model="haiku")
|
||||
|
||||
@patch("src.router._get_channel_config")
|
||||
@patch("src.router._get_config")
|
||||
@patch("src.router.send_message")
|
||||
def test_global_default_model(self, mock_send, mock_get_config, mock_chan_cfg):
|
||||
"""Falls back to bot.default_model when channel has no default."""
|
||||
mock_send.return_value = "ok"
|
||||
mock_chan_cfg.return_value = {"id": "ch-1"} # no default_model
|
||||
mock_cfg = MagicMock()
|
||||
mock_cfg.get.return_value = "opus"
|
||||
mock_get_config.return_value = mock_cfg
|
||||
|
||||
route_message("ch-1", "user-1", "hello")
|
||||
mock_send.assert_called_once_with("ch-1", "hello", model="opus")
|
||||
|
||||
@patch("src.router._get_channel_config")
|
||||
@patch("src.router._get_config")
|
||||
@patch("src.router.send_message")
|
||||
def test_sonnet_fallback(self, mock_send, mock_get_config, mock_chan_cfg):
|
||||
"""Falls back to 'sonnet' when no channel or global default."""
|
||||
mock_send.return_value = "ok"
|
||||
mock_chan_cfg.return_value = None
|
||||
mock_cfg = MagicMock()
|
||||
mock_cfg.get.side_effect = lambda key, default=None: default
|
||||
mock_get_config.return_value = mock_cfg
|
||||
|
||||
route_message("ch-1", "user-1", "hello")
|
||||
mock_send.assert_called_once_with("ch-1", "hello", model="sonnet")
|
||||
Reference in New Issue
Block a user