Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

16.3 KB
Raw Blame
"""Unit tests for TutorBot channel dispatch behavior."""

from __future__ import annotations

import asyncio
from contextlib import suppress
from types import SimpleNamespace
from unittest.mock import AsyncMock

import pytest

from deeptutor.partners.bus.events import OutboundMessage
from deeptutor.partners.channels.manager import ChannelManager
from deeptutor.partners.config.schema import ChannelsConfig


def test_empty_allow_list_skips_only_misconfigured_channel(monkeypatch):
    class _Channel:
        display_name = "Test"

        def __init__(self, config, _bus):
            self.config = SimpleNamespace(allow_from=config["allow_from"])
            self.is_running = False
            self.setup_state = {}

    monkeypatch.setattr(
        "deeptutor.partners.channels.registry.discover_all_with_errors",
        lambda: ({"invalid": _Channel, "valid": _Channel}, {}),
    )
    config = ChannelsConfig(
        invalid={"enabled": True, "allow_from": []},
        valid={"enabled": True, "allow_from": ["user-1"]},
    )

    manager = ChannelManager(config, _MultiShotBus([]))  # type: ignore[arg-type]

    assert list(manager.channels) == ["valid"]
    assert manager.get_status()["invalid"]["setup"]["status"] == "action_required"


def test_unavailable_configured_channel_remains_visible(monkeypatch):
    monkeypatch.setattr(
        "deeptutor.partners.channels.registry.discover_all_with_errors",
        lambda: ({}, {"telegram": "optional package missing"}),
    )
    manager = ChannelManager(
        ChannelsConfig(telegram={"enabled": True, "allow_from": ["*"]}),
        _MultiShotBus([]),  # type: ignore[arg-type]
    )

    assert manager.channels == {}
    assert manager.get_status()["telegram"] == {
        "enabled": True,
        "running": False,
        "setup": {
            "status": "unavailable",
            "message": "optional package missing",
        },
    }


class _OneShotBus:
    def __init__(self, msg: OutboundMessage):
        self._msg = msg
        self._calls = 0

    async def consume_outbound(self) -> OutboundMessage:
        self._calls += 1
        if self._calls == 1:
            return self._msg
        raise asyncio.CancelledError


class _DummyChannel:
    def __init__(self):
        self.send = AsyncMock()
        self.send_delta = AsyncMock()
        # Effective flags normally set by ChannelManager._init_channels
        # from the per-channel config.
        self.send_progress = True
        self.send_tool_hints = True


def test_channel_media_is_scoped_to_owning_partner(partners_root):
    from deeptutor.partners.bus.queue import MessageBus
    from deeptutor.partners.channels.base import BaseChannel

    class _MediaChannel(BaseChannel):
        name = "telegram"

        async def start(self):
            pass

        async def stop(self):
            pass

        async def send(self, msg):
            pass

    channel = _MediaChannel({}, MessageBus())
    channel.partner_id = "ada"

    assert channel.media_dir() == partners_root / "ada" / "media" / "telegram"


def test_channel_state_is_scoped_to_owning_partner(partners_root):
    from deeptutor.partners.bus.queue import MessageBus
    from deeptutor.partners.channels.base import BaseChannel

    class _StatefulChannel(BaseChannel):
        name = "telegram"

        async def start(self):
            pass

        async def stop(self):
            pass

        async def send(self, msg):
            pass

    channel = _StatefulChannel({}, MessageBus())
    channel.partner_id = "ada"

    assert channel.state_dir() == partners_root / "ada" / "channels" / "telegram"


def test_partner_id_is_available_while_the_channel_is_constructed(partners_root, monkeypatch):
    """Channels that resolve paths in ``__init__`` must not see an empty owner."""
    from deeptutor.partners.bus.queue import MessageBus
    from deeptutor.partners.channels.base import BaseChannel

    class _EagerChannel(BaseChannel):
        name = "telegram"
        display_name = "Eager"

        def __init__(self, config, bus):
            super().__init__(config, bus)
            self.resolved_at_init = self.state_dir()

        async def start(self):
            pass

        async def stop(self):
            pass

        async def send(self, msg):
            pass

    monkeypatch.setattr(
        "deeptutor.partners.channels.registry.discover_all_with_errors",
        lambda: ({"telegram": _EagerChannel}, {}),
    )
    manager = ChannelManager(
        ChannelsConfig(telegram={"enabled": True}),
        MessageBus(),
        partner_id="ada",
    )

    channel = manager.channels["telegram"]
    assert channel.resolved_at_init == partners_root / "ada" / "channels" / "telegram"


@pytest.mark.asyncio
async def test_start_failure_is_sanitized_into_runtime_status():
    from deeptutor.partners.bus.queue import MessageBus
    from deeptutor.partners.channels.base import BaseChannel

    class _FailingChannel(BaseChannel):
        name = "telegram"

        async def start(self):
            raise RuntimeError("secret-token-must-not-leak")

        async def stop(self):
            self._running = False

        async def send(self, msg):
            pass

    channel = _FailingChannel({}, MessageBus())
    manager = ChannelManager(ChannelsConfig(), MessageBus())

    await manager._start_channel("telegram", channel)

    assert channel.is_running is False
    assert channel.setup_state == {
        "status": "error",
        "message": "Channel startup failed (RuntimeError).",
    }


@pytest.mark.asyncio
async def test_start_return_without_listener_requests_configuration():
    from deeptutor.partners.bus.queue import MessageBus
    from deeptutor.partners.channels.base import BaseChannel

    class _UnconfiguredChannel(BaseChannel):
        name = "telegram"

        async def start(self):
            return

        async def stop(self):
            self._running = False

        async def send(self, msg):
            pass

    channel = _UnconfiguredChannel({}, MessageBus())
    manager = ChannelManager(ChannelsConfig(), MessageBus())

    await manager._start_channel("telegram", channel)

    assert channel.setup_state["status"] == "action_required"


async def _dispatch_one(
    msg: OutboundMessage,
    *,
    send_progress: bool = True,
    send_tool_hints: bool = True,
    config: ChannelsConfig | None = None,
) -> _DummyChannel:
    channel = _DummyChannel()
    channel.send_progress = send_progress
    channel.send_tool_hints = send_tool_hints
    manager = ChannelManager(config or ChannelsConfig(), _OneShotBus(msg))  # type: ignore[arg-type]
    manager.channels = {msg.channel: channel}  # type: ignore[dict-item]

    await manager._dispatch_outbound()
    return channel


class TestChannelManagerToolHints:
    @pytest.mark.asyncio
    async def test_tool_hint_progress_skipped_by_default(self):
        msg = OutboundMessage(
            channel="zulip",
            chat_id="pm:42",
            content='message("Hello")',
            metadata={"_progress": True, "_tool_hint": True},
        )

        channel = await _dispatch_one(msg, send_tool_hints=False)

        channel.send.assert_not_awaited()

    @pytest.mark.asyncio
    async def test_tool_hint_progress_dispatched_when_enabled(self):
        msg = OutboundMessage(
            channel="zulip",
            chat_id="pm:42",
            content='message("Hello")',
            metadata={"_progress": True, "_tool_hint": True},
        )

        channel = await _dispatch_one(msg, send_tool_hints=True)

        channel.send.assert_awaited_once_with(msg)


class _MultiShotBus:
    """Bus stub that yields queued messages then cancels the dispatcher.

    Messages live in ``outbound`` (a real queue) so the manager's delta
    coalescing — which drains ``bus.outbound`` directly — sees them too.
    """

    def __init__(self, msgs: list[OutboundMessage]):
        self.outbound = asyncio.Queue()
        for m in msgs:
            self.outbound.put_nowait(m)

    async def consume_outbound(self) -> OutboundMessage:
        try:
            return self.outbound.get_nowait()
        except asyncio.QueueEmpty:
            raise asyncio.CancelledError from None


async def _dispatch_many(
    msgs: list[OutboundMessage], config: ChannelsConfig | None = None
) -> _DummyChannel:
    channel = _DummyChannel()
    manager = ChannelManager(config or ChannelsConfig(), _MultiShotBus(msgs))  # type: ignore[arg-type]
    manager.channels = {msgs[0].channel: channel}  # type: ignore[dict-item]
    await manager._dispatch_outbound()
    return channel


class TestSendRetry:
    @pytest.mark.asyncio
    async def test_send_retries_on_failure_then_succeeds(self, monkeypatch):
        monkeypatch.setattr("deeptutor.partners.channels.manager._SEND_RETRY_DELAYS", (0, 0, 0))
        msg = OutboundMessage(channel="zulip", chat_id="1", content="hi")
        channel = _DummyChannel()
        channel.send.side_effect = [RuntimeError("boom"), None]
        manager = ChannelManager(ChannelsConfig(), _MultiShotBus([]))  # type: ignore[arg-type]

        await manager._send_with_retry(channel, msg)  # type: ignore[arg-type]

        assert channel.send.await_count == 2

    @pytest.mark.asyncio
    async def test_send_gives_up_after_max_retries(self, monkeypatch):
        monkeypatch.setattr("deeptutor.partners.channels.manager._SEND_RETRY_DELAYS", (0, 0, 0))
        msg = OutboundMessage(channel="zulip", chat_id="1", content="hi")
        channel = _DummyChannel()
        channel.send.side_effect = RuntimeError("boom")
        manager = ChannelManager(
            ChannelsConfig(send_max_retries=2),
            _MultiShotBus([]),  # type: ignore[arg-type]
        )

        await manager._send_with_retry(channel, msg)  # type: ignore[arg-type]

        assert channel.send.await_count == 2


class TestDispatchTimeoutSemantics:
    """_dispatch_outbound's TimeoutError handler is an idle-poll timeout.

    ``except asyncio.TimeoutError: continue`` only observes the 1s poll
    timeout of ``wait_for(consume_outbound(), 1.0)``. No message is in
    hand when it fires: a cancelled ``Queue.get()`` pops nothing, and a
    consume that finishes despite the timeout has its result returned by
    ``wait_for``. Channel send timeouts are ``Exception`` subclasses and
    are retried inside ``_send_with_retry`` instead.
    """

    @pytest.mark.asyncio
    async def test_idle_poll_timeout_does_not_skip_later_message(self):
        from deeptutor.partners.bus.queue import MessageBus

        msg = OutboundMessage(channel="zulip", chat_id="1", content="hi")
        channel = _DummyChannel()
        bus = MessageBus()
        manager = ChannelManager(ChannelsConfig(), bus)
        manager.channels = {msg.channel: channel}  # type: ignore[dict-item]

        task = asyncio.create_task(manager._dispatch_outbound())
        # Cross at least one 1.0s poll timeout with an empty queue.
        await asyncio.sleep(1.3)
        await bus.publish_outbound(msg)

        loop = asyncio.get_running_loop()
        deadline = loop.time() + 2.0
        while channel.send.await_count == 0 and loop.time() < deadline:
            await asyncio.sleep(0.05)
        task.cancel()
        with suppress(asyncio.CancelledError):
            await task

        channel.send.assert_awaited_once_with(msg)

    @pytest.mark.asyncio
    async def test_channel_send_timeout_is_retried_by_dispatcher(self, monkeypatch):
        monkeypatch.setattr("deeptutor.partners.channels.manager._SEND_RETRY_DELAYS", (0, 0, 0))
        msg = OutboundMessage(channel="zulip", chat_id="1", content="hi")
        channel = _DummyChannel()
        channel.send.side_effect = [asyncio.TimeoutError(), None]
        manager = ChannelManager(ChannelsConfig(), _OneShotBus(msg))  # type: ignore[arg-type]
        manager.channels = {msg.channel: channel}  # type: ignore[dict-item]

        await manager._dispatch_outbound()

        assert channel.send.await_count == 2
        channel.send.assert_awaited_with(msg)

    @pytest.mark.asyncio
    async def test_channel_send_timeout_exhausts_retries(self, monkeypatch):
        monkeypatch.setattr("deeptutor.partners.channels.manager._SEND_RETRY_DELAYS", (0, 0, 0))
        msg = OutboundMessage(channel="zulip", chat_id="1", content="hi")
        channel = _DummyChannel()
        channel.send.side_effect = asyncio.TimeoutError
        manager = ChannelManager(
            ChannelsConfig(send_max_retries=3),
            _OneShotBus(msg),  # type: ignore[arg-type]
        )
        manager.channels = {msg.channel: channel}  # type: ignore[dict-item]

        await manager._dispatch_outbound()

        assert channel.send.await_count == 3


class TestDuplicateSuppression:
    @pytest.mark.asyncio
    async def test_same_reply_to_same_origin_suppressed(self):
        meta = {"origin_message_id": "m1"}
        msgs = [
            OutboundMessage(channel="zulip", chat_id="1", content="same", metadata=dict(meta)),
            OutboundMessage(channel="zulip", chat_id="1", content="same", metadata=dict(meta)),
        ]
        channel = await _dispatch_many(msgs)
        assert channel.send.await_count == 1

    @pytest.mark.asyncio
    async def test_same_reply_to_different_origin_delivered(self):
        msgs = [
            OutboundMessage(
                channel="zulip",
                chat_id="1",
                content="same",
                metadata={"origin_message_id": "m1"},
            ),
            OutboundMessage(
                channel="zulip",
                chat_id="1",
                content="same",
                metadata={"origin_message_id": "m2"},
            ),
        ]
        channel = await _dispatch_many(msgs)
        assert channel.send.await_count == 2


class TestStreamDispatch:
    @pytest.mark.asyncio
    async def test_consecutive_deltas_coalesce(self):
        sid = {"_stream_delta": True, "_stream_id": "t:1"}
        msgs = [
            OutboundMessage(channel="zulip", chat_id="1", content="a", metadata=dict(sid)),
            OutboundMessage(channel="zulip", chat_id="1", content="b", metadata=dict(sid)),
            OutboundMessage(channel="zulip", chat_id="1", content="c", metadata=dict(sid)),
        ]
        channel = await _dispatch_many(msgs)
        # First delta waits on the queue; the rest coalesce into one edit.
        assert channel.send_delta.await_count <= 2
        total = "".join(c.args[1] for c in channel.send_delta.await_args_list)
        assert total == "abc"
        channel.send.assert_not_awaited()

    @pytest.mark.asyncio
    async def test_stream_end_routed_to_send_delta(self):
        msgs = [
            OutboundMessage(
                channel="zulip",
                chat_id="1",
                content="",
                metadata={"_stream_end": True, "_stream_id": "t:1"},
            ),
        ]
        channel = await _dispatch_many(msgs)
        channel.send_delta.assert_awaited_once()
        channel.send.assert_not_awaited()

    @pytest.mark.asyncio
    async def test_streamed_final_skips_plain_send(self):
        msgs = [
            OutboundMessage(
                channel="zulip",
                chat_id="1",
                content="final",
                metadata={"_streamed": True},
            ),
        ]
        channel = await _dispatch_many(msgs)
        channel.send.assert_not_awaited()
        channel.send_delta.assert_not_awaited()

    @pytest.mark.asyncio
    async def test_deltas_of_different_streams_not_merged(self):
        msgs = [
            OutboundMessage(
                channel="zulip",
                chat_id="1",
                content="a",
                metadata={"_stream_delta": True, "_stream_id": "t:1"},
            ),
            OutboundMessage(
                channel="zulip",
                chat_id="1",
                content="b",
                metadata={"_stream_delta": True, "_stream_id": "t:2"},
            ),
        ]
        channel = await _dispatch_many(msgs)
        assert channel.send_delta.await_count == 2
        chunks = [c.args[1] for c in channel.send_delta.await_args_list]
        assert chunks == ["a", "b"]


def test_channel_registry_discovers_builtin_channels() -> None:
    from deeptutor.partners.channels.base import BaseChannel
    from deeptutor.partners.channels.registry import discover_all, discover_channel_names

    names = set(discover_channel_names())
    assert {"telegram", "slack", "discord", "zulip"} <= names

    channels = discover_all()
    assert {"telegram", "slack", "discord", "zulip"} <= set(channels)
    assert all(issubclass(cls, BaseChannel) for cls in channels.values())