Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

11.0 KB
Raw Blame
import importlib.util
import sys
import types
import unittest
from pathlib import Path
from unittest.mock import AsyncMock, Mock, patch

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))

from modules.im import MessageContext


def _load_message_handler_class():
    originals = {}

    def _set_module(name, module):
        originals.setdefault(name, sys.modules.get(name))
        sys.modules[name] = module

    agents_module = types.ModuleType("modules.agents")
    agents_module.__path__ = [str(ROOT / "modules" / "agents")]
    setattr(agents_module, "AgentRequest", type("AgentRequest", (), {}))
    _set_module("modules.agents", agents_module)

    agents_base_module = types.ModuleType("modules.agents.base")
    setattr(agents_base_module, "AgentRequest", type("AgentRequest", (), {}))
    _set_module("modules.agents.base", agents_base_module)

    core_pkg = types.ModuleType("core")
    core_pkg.__path__ = [str(ROOT / "core")]
    _set_module("core", core_pkg)

    handlers_pkg = types.ModuleType("core.handlers")
    handlers_pkg.__path__ = [str(ROOT / "core" / "handlers")]
    _set_module("core.handlers", handlers_pkg)

    try:
        for module_name, relative_path in (
            ("core.handlers.base", ROOT / "core" / "handlers" / "base.py"),
            ("core.handlers.message_handler", ROOT / "core" / "handlers" / "message_handler.py"),
        ):
            originals.setdefault(module_name, sys.modules.get(module_name))
            spec = importlib.util.spec_from_file_location(module_name, relative_path)
            assert spec is not None
            assert spec.loader is not None
            module = importlib.util.module_from_spec(spec)
            sys.modules[module_name] = module
            spec.loader.exec_module(module)

        return sys.modules["core.handlers.message_handler"].MessageHandler
    finally:
        for module_name, original in reversed(list(originals.items())):
            if original is None:
                sys.modules.pop(module_name, None)
            else:
                sys.modules[module_name] = original


MessageHandler = _load_message_handler_class()


class _StubSessions:
    def __init__(self):
        self.recorded = []
        self._claimed = set()

    def seed_legacy_claim(self, channel_id, thread_ts, message_ts):
        """Simulate a dedup row written before keys were platform-namespaced."""
        self._claimed.add((channel_id, thread_ts, message_ts))

    def is_message_already_processed(self, channel_id, thread_ts, message_ts):
        return (channel_id, thread_ts, message_ts) in self._claimed

    def record_processed_message(self, channel_id, thread_ts, message_ts):
        self.recorded.append((channel_id, thread_ts, message_ts))
        return None

    def try_record_processed_message(self, channel_id, thread_ts, message_ts):
        key = (channel_id, thread_ts, message_ts)
        if key in self._claimed:
            return False
        self._claimed.add(key)
        self.recorded.append(key)
        return True

    def seed_legacy_claim(self, channel_id, thread_ts, message_ts):
        self._claimed.add((channel_id, thread_ts, message_ts))


class _StubSettingsManager:
    def __init__(self):
        self.sessions = _StubSessions()

    def get_channel_routing(self, settings_key):
        return None

    def is_message_type_hidden(self, settings_key, canonical_type):
        return False


class _StubIMClient:
    def __init__(self):
        self.sent_messages = []
        self.formatter = types.SimpleNamespace(format_error=lambda text: text, format_warning=lambda text: text)

    async def send_message(self, context, text, parse_mode=None, reply_to=None):
        self.sent_messages.append((context.channel_id, text))
        return "msg-1"


class _StubController:
    def __init__(self):
        self.config = types.SimpleNamespace(
            platform="slack",
            ack_mode="reaction",
            include_time_info=False,
            include_user_info=False,
            language="en",
        )
        self.im_client = _StubIMClient()
        self.settings_manager = _StubSettingsManager()
        self.session_manager = object()
        self.session_handler = None
        self.receiver_tasks = {}
        self.agent_service = types.SimpleNamespace(default_agent="codex")
        self.settings_handler = types.SimpleNamespace()
        self.command_handler = types.SimpleNamespace()
        self.agent_auth_service = types.SimpleNamespace(handle_setup_callback=AsyncMock())

    def get_im_client_for_context(self, context):
        return self.im_client

    def _get_settings_key(self, context):
        return context.channel_id

    def _get_session_key(self, context):
        return f"slack::{context.channel_id}"

    def _get_lang(self):
        return "en"


class MessageHandlerAuthSetupTests(unittest.IsolatedAsyncioTestCase):
    async def test_auth_setup_callback_routes_to_agent_auth_service(self):
        controller = _StubController()
        controller.settings_handler.handle_info_message_types = AsyncMock()
        controller.settings_handler.handle_info_how_it_works = AsyncMock()
        controller.command_handler.handle_cwd = AsyncMock()
        controller.command_handler.handle_change_cwd_modal = AsyncMock()
        controller.command_handler.handle_new = AsyncMock()
        controller.command_handler.handle_resume = AsyncMock()
        controller.settings_handler.handle_settings = AsyncMock()
        controller.settings_handler.handle_routing = AsyncMock()

        handler = MessageHandler(controller)
        context = MessageContext(user_id="U1", channel_id="C1", platform="slack")

        await handler.handle_callback_query(context, "auth_setup:auto")

        controller.agent_auth_service.handle_setup_callback.assert_awaited_once_with(context, "auth_setup:auto")

    async def test_plain_claude_callback_value_is_consumed_before_agent_routing(self):
        controller = _StubController()
        controller.agent_auth_service.maybe_consume_setup_reply = AsyncMock(return_value=True)

        handler = MessageHandler(controller)
        context = MessageContext(user_id="U1", channel_id="C1", platform="slack", message_id="m1")

        result = await handler.handle_user_message(context, "auth-code#oauth-state")

        self.assertIsNone(result)
        controller.agent_auth_service.maybe_consume_setup_reply.assert_awaited_once_with(
            context,
            "auth-code#oauth-state",
        )
        self.assertEqual(controller.settings_manager.sessions.recorded, [("im:slack:C1", "im:slack:m1", "im:slack:m1")])

    async def test_platform_qualified_dedup_keeps_colliding_native_ids_distinct(self):
        controller = _StubController()
        controller.agent_auth_service.maybe_consume_setup_reply = AsyncMock(return_value=True)
        handler = MessageHandler(controller)

        slack = MessageContext(user_id="U1", channel_id="C1", platform="slack", message_id="m1")
        duplicate_slack = MessageContext(user_id="U1", channel_id="C1", platform="slack", message_id="m1")
        discord = MessageContext(user_id="U1", channel_id="C1", platform="discord", message_id="m1")

        await handler.handle_user_message(slack, "first")
        await handler.handle_user_message(duplicate_slack, "duplicate")
        await handler.handle_user_message(discord, "second platform")

        self.assertEqual(
            controller.settings_manager.sessions.recorded,
            [
                ("im:slack:C1", "im:slack:m1", "im:slack:m1"),
                ("im:discord:C1", "im:discord:m1", "im:discord:m1"),
            ],
        )
        self.assertEqual(controller.agent_auth_service.maybe_consume_setup_reply.await_count, 2)

    async def test_legacy_unprefixed_dedup_row_blocks_post_upgrade_redelivery(self):
        controller = _StubController()
        controller.agent_auth_service.maybe_consume_setup_reply = AsyncMock(return_value=True)
        handler = MessageHandler(controller)
        sessions = controller.settings_manager.sessions
        # A previous version claimed this Slack event with raw native ids.
        sessions.seed_legacy_claim("C1", "m1", "m1")

        redelivered = MessageContext(user_id="U1", channel_id="C1", platform="slack", message_id="m1")
        await handler.handle_user_message(redelivered, "redelivered after upgrade")

        self.assertEqual(sessions.recorded, [])
        controller.agent_auth_service.maybe_consume_setup_reply.assert_not_awaited()

        # The legacy row must not suppress any other event in the same channel.
        fresh = MessageContext(user_id="U1", channel_id="C1", platform="slack", message_id="m2")
        await handler.handle_user_message(fresh, "a new message")

        self.assertEqual(sessions.recorded, [("im:slack:C1", "im:slack:m2", "im:slack:m2")])
        controller.agent_auth_service.maybe_consume_setup_reply.assert_awaited_once()

    async def test_durable_setup_reply_claims_native_event_before_consuming(self):
        controller = _StubController()
        controller.session_turns = types.SimpleNamespace(deliver=AsyncMock())
        consumed = []
        flow_active = True

        async def consume_once(context, message, *, claim_native_event):
            nonlocal flow_active
            if not flow_active:
                return False
            if not claim_native_event():
                return True
            consumed.append(message)
            flow_active = False
            return True

        controller.agent_auth_service.maybe_consume_setup_reply = AsyncMock(
            side_effect=consume_once
        )
        handler = MessageHandler(controller)
        handler._is_duplicate_human_delivery = Mock(
            side_effect=handler._native_human_event_processed
        )
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            platform="slack",
            message_id="m-setup",
        )

        await handler.handle_user_message(context, "sk-opencode-secret")
        await handler.handle_user_message(context, "sk-opencode-secret")

        self.assertEqual(consumed, ["sk-opencode-secret"])
        self.assertEqual(
            controller.settings_manager.sessions.recorded,
            [("im:slack:C1", "im:slack:m-setup", "im:slack:m-setup")],
        )
        self.assertEqual(
            controller.agent_auth_service.maybe_consume_setup_reply.await_count,
            1,
        )
        controller.session_turns.deliver.assert_not_awaited()

    async def test_consumed_control_fence_precedes_delivery_lookup(self):
        controller = _StubController()
        handler = MessageHandler(controller)
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            platform="telegram",
            message_id="1",
        )
        assert handler._claim_native_human_event(context)

        with patch(
            "storage.db.get_cached_sqlite_engine",
            side_effect=AssertionError("Delivery lookup must not run"),
        ):
            assert handler._is_duplicate_human_delivery(context)


if __name__ == "__main__":
    unittest.main()