"""
Tests for Slack platform adapter.

Covers: app_mention handler, send_document, send_video,
        incoming document handling, message routing.

Note: slack-bolt may not be installed in the test environment.
We mock the slack modules at import time to avoid collection errors.
"""

import asyncio
import contextlib
import os
import sys
from unittest.mock import AsyncMock, MagicMock, patch, call

import pytest

from gateway.config import Platform, PlatformConfig
from gateway.platforms.base import (
    MessageEvent,
    MessageType,
    SUPPORTED_VIDEO_TYPES,
    is_host_excluded_by_no_proxy,
)


# ---------------------------------------------------------------------------
# Mock the slack-bolt package if it's not installed
# ---------------------------------------------------------------------------


def _ensure_slack_mock():
    """Install mock slack modules so SlackAdapter can be imported."""
    if "slack_bolt" in sys.modules and hasattr(sys.modules["slack_bolt"], "__file__"):
        return  # Real library installed

    slack_bolt = MagicMock()
    slack_bolt.async_app.AsyncApp = MagicMock
    slack_bolt.adapter.socket_mode.async_handler.AsyncSocketModeHandler = MagicMock

    slack_sdk = MagicMock()
    slack_sdk.web.async_client.AsyncWebClient = MagicMock

    for name, mod in [
        ("slack_bolt", slack_bolt),
        ("slack_bolt.async_app", slack_bolt.async_app),
        ("slack_bolt.adapter", slack_bolt.adapter),
        ("slack_bolt.adapter.socket_mode", slack_bolt.adapter.socket_mode),
        (
            "slack_bolt.adapter.socket_mode.async_handler",
            slack_bolt.adapter.socket_mode.async_handler,
        ),
        ("slack_sdk", slack_sdk),
        ("slack_sdk.web", slack_sdk.web),
        ("slack_sdk.web.async_client", slack_sdk.web.async_client),
    ]:
        sys.modules.setdefault(name, mod)

    # aiohttp is imported alongside slack-bolt; mock it if missing
    sys.modules.setdefault("aiohttp", MagicMock())


_ensure_slack_mock()

# Patch SLACK_AVAILABLE before importing the adapter
import plugins.platforms.slack.adapter as _slack_mod

_slack_mod.SLACK_AVAILABLE = True

from plugins.platforms.slack.adapter import SlackAdapter  # noqa: E402


async def _pending_for_fake_task():
    # Stay pending so done-callbacks attached by the adapter (which would
    # otherwise schedule a reconnect) don't fire during the test. The pytest
    # event loop will cancel us at teardown, which the adapter's
    # ``_on_socket_mode_task_done`` already treats as intentional shutdown.
    await asyncio.Event().wait()


def _fake_create_task(coro):
    """Test helper: consume the real coroutine and return a real awaitable Task.

    Returning an actual ``asyncio.Task`` (built via ``loop.create_task`` so the
    ``asyncio.create_task`` patch doesn't recurse) keeps the substitute usable
    by code that later cancels, awaits, or attaches ``add_done_callback`` —
    so future tests that exercise ``disconnect()`` after patching
    ``asyncio.create_task`` won't trip over a non-awaitable MagicMock.
    """
    assert asyncio.iscoroutine(coro), (
        f"_fake_create_task expected a coroutine, got {type(coro).__name__}"
    )
    coro.close()
    loop = asyncio.get_event_loop()
    return loop.create_task(_pending_for_fake_task())


# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------


@pytest.fixture()
def adapter():
    config = PlatformConfig(enabled=True, token="xoxb-fake-token")
    a = SlackAdapter(config)
    # Mock the Slack app client
    a._app = MagicMock()
    a._app.client = AsyncMock()
    a._bot_user_id = "U_BOT"
    a._running = True
    # Capture events instead of processing them
    a.handle_message = AsyncMock()
    return a


@pytest.fixture(autouse=True)
def _redirect_cache(tmp_path, monkeypatch):
    """Point document cache to tmp_path so tests don't touch ~/.hermes."""
    monkeypatch.setattr(
        "gateway.platforms.base.DOCUMENT_CACHE_DIR", tmp_path / "doc_cache"
    )
    monkeypatch.setattr(
        "gateway.platforms.base.VIDEO_CACHE_DIR", tmp_path / "video_cache"
    )


# ---------------------------------------------------------------------------
# TestSlashCommandSessionIsolation
# ---------------------------------------------------------------------------


class TestSlashCommandSessionIsolation:
    @pytest.mark.asyncio
    async def test_channel_slash_command_uses_group_session_semantics(self, adapter):
        command = {
            "text": "hello",
            "user_id": "U123",
            "channel_id": "C123",
            "team_id": "T123",
        }

        await adapter._handle_slash_command(command)

        adapter.handle_message.assert_awaited_once()
        event = adapter.handle_message.await_args.args[0]
        assert event.source.chat_type == "group"
        assert event.source.chat_id == "C123"
        assert event.source.user_id == "U123"

    @pytest.mark.asyncio
    async def test_dm_slash_command_keeps_dm_session_semantics(self, adapter):
        command = {
            "text": "hello",
            "user_id": "U123",
            "channel_id": "D123",
            "team_id": "T123",
        }

        await adapter._handle_slash_command(command)

        adapter.handle_message.assert_awaited_once()
        event = adapter.handle_message.await_args.args[0]
        assert event.source.chat_type == "dm"
        assert event.source.chat_id == "D123"
        assert event.source.user_id == "U123"


# ---------------------------------------------------------------------------
# TestAppMentionHandler
# ---------------------------------------------------------------------------


class TestAppMentionHandler:
    """Verify that the app_mention event handler is registered."""

    def test_app_mention_registered_on_connect(self):
        """connect() should register message + assistant lifecycle handlers."""
        config = PlatformConfig(enabled=True, token="xoxb-fake")
        adapter = SlackAdapter(config)

        # Track which events get registered
        registered_events = []
        registered_commands = []

        mock_app = MagicMock()

        def mock_event(event_type):
            def decorator(fn):
                registered_events.append(event_type)
                return fn

            return decorator

        def mock_command(cmd):
            def decorator(fn):
                registered_commands.append(cmd)
                return fn

            return decorator

        mock_app.event = mock_event
        mock_app.command = mock_command
        mock_app.client = AsyncMock()
        mock_app.client.auth_test = AsyncMock(
            return_value={
                "user_id": "U_BOT",
                "user": "testbot",
            }
        )

        # Mock AsyncWebClient so multi-workspace auth_test is awaitable
        mock_web_client = AsyncMock()
        mock_web_client.auth_test = AsyncMock(
            return_value={
                "user_id": "U_BOT",
                "user": "testbot",
                "team_id": "T_FAKE",
                "team": "FakeTeam",
            }
        )

        socket_mode_handler = MagicMock()
        socket_mode_handler.start_async = AsyncMock(return_value=None)

        with (
            patch.object(_slack_mod, "AsyncApp", return_value=mock_app),
            patch.object(_slack_mod, "AsyncWebClient", return_value=mock_web_client),
            patch.object(
                _slack_mod, "AsyncSocketModeHandler", return_value=socket_mode_handler
            ),
            patch.dict(os.environ, {"SLACK_APP_TOKEN": "xapp-fake"}),
            patch("gateway.status.acquire_scoped_lock", return_value=(True, None)),
            patch("asyncio.create_task", side_effect=_fake_create_task),
        ):
            asyncio.run(adapter.connect())

        assert "message" in registered_events
        assert "app_mention" in registered_events
        assert "reaction_added" in registered_events
        assert "reaction_removed" in registered_events
        assert "assistant_thread_started" in registered_events
        assert "assistant_thread_context_changed" in registered_events
        # Slack slash commands are registered via a single regex matcher
        # covering every COMMAND_REGISTRY entry (e.g. /hermes, /btw, /stop,
        # /model, ...) so users get native-slash parity with Discord and
        # Telegram. Verify the regex matches the key expected slashes.
        assert (
            len(registered_commands) == 1
        ), f"expected 1 combined slash matcher, got {registered_commands!r}"
        slash_matcher = registered_commands[0]
        import re as _re

        assert isinstance(slash_matcher, _re.Pattern)
        for expected in ("/hermes", "/btw", "/stop", "/model", "/help"):
            assert slash_matcher.match(
                expected
            ), f"Slack slash regex does not match {expected}"


class TestSlackConnectCleanup:
    """Regression coverage for failed connect() cleanup."""

    @pytest.mark.asyncio
    async def test_releases_platform_lock_when_auth_fails(self):
        config = PlatformConfig(enabled=True, token="xoxb-fake")
        adapter = SlackAdapter(config)

        mock_app = MagicMock()
        mock_web_client = AsyncMock()
        mock_web_client.auth_test = AsyncMock(side_effect=RuntimeError("boom"))

        with (
            patch.object(_slack_mod, "AsyncApp", return_value=mock_app),
            patch.object(_slack_mod, "AsyncWebClient", return_value=mock_web_client),
            patch.object(
                _slack_mod, "AsyncSocketModeHandler", return_value=MagicMock()
            ),
            patch.dict(os.environ, {"SLACK_APP_TOKEN": "xapp-fake"}),
            patch("gateway.status.acquire_scoped_lock", return_value=(True, None)),
            patch("gateway.status.release_scoped_lock") as mock_release,
        ):
            result = await adapter.connect()

        assert result is False
        mock_release.assert_called_once_with("slack-app-token", "xapp-fake")
        assert adapter._platform_lock_identity is None

    @pytest.mark.asyncio
    async def test_reconnect_closes_previous_handler_to_prevent_zombie_socket(self):
        """Regression for #18980: calling connect() on an adapter that already has
        a live handler (e.g. during a gateway restart) must close the old
        AsyncSocketModeHandler before creating a new one.  Without this guard,
        the old Socket Mode websocket stays alive and both connections dispatch
        every Slack event, producing double responses — the same bug that
        affected DiscordAdapter (#18187).
        """
        config = PlatformConfig(enabled=True, token="xoxb-fake")
        adapter = SlackAdapter(config)

        # Simulate state left over from a prior connect() call.
        first_handler = AsyncMock()
        first_handler.close_async = AsyncMock()
        adapter._handler = first_handler

        mock_app = MagicMock()

        def _noop_decorator(event_type):
            def decorator(fn):
                return fn

            return decorator

        mock_app.event = _noop_decorator
        mock_app.command = _noop_decorator
        mock_app.action = _noop_decorator
        mock_app.client = AsyncMock()

        mock_web_client = AsyncMock()
        mock_web_client.auth_test = AsyncMock(
            return_value={
                "user_id": "U_BOT",
                "user": "testbot",
                "team_id": "T_FAKE",
                "team": "FakeTeam",
            }
        )

        second_handler = MagicMock()
        # _start_socket_mode_handler awaits the result of start_async via
        # asyncio.create_task — so the stub must return a real coroutine, not a
        # bare MagicMock.
        second_handler.start_async = AsyncMock(return_value=None)

        with (
            patch.object(_slack_mod, "AsyncApp", return_value=mock_app),
            patch.object(_slack_mod, "AsyncWebClient", return_value=mock_web_client),
            patch.object(
                _slack_mod, "AsyncSocketModeHandler", return_value=second_handler
            ),
            patch.dict(os.environ, {"SLACK_APP_TOKEN": "xapp-fake"}),
            patch("gateway.status.acquire_scoped_lock", return_value=(True, None)),
            patch("gateway.status.release_scoped_lock"),
            patch("asyncio.create_task", side_effect=_fake_create_task),
        ):
            result = await adapter.connect()

        assert result is True
        first_handler.close_async.assert_awaited_once_with()
        assert adapter._handler is second_handler


# ---------------------------------------------------------------------------
# TestSlackSocketWatchdog
# ---------------------------------------------------------------------------


class TestSlackSocketWatchdog:
    """End-to-end behavioural coverage for the Socket Mode watchdog/reconnect.

    These tests drive the adapter through a fake AsyncSocketModeHandler so we
    can simulate Slack silently dropping the websocket (the original P0) and
    assert the adapter heals itself without touching real network/Slack.
    """

    def _make_fake_handler_factory(self):
        """Return ``(factory, instances)`` where each call records a handler."""
        instances: list = []

        class FakeHandler:
            def __init__(self, app, app_token, proxy=None):
                self.app = app
                self.app_token = app_token
                self.proxy = proxy
                self.client = MagicMock()
                self.client.proxy = proxy
                self.client.is_connected = lambda: True
                self._start_event = asyncio.Event()
                self.closed = False
                self.start_calls = 0
                instances.append(self)

            async def start_async(self):
                self.start_calls += 1
                await self._start_event.wait()

            async def close_async(self):
                self.closed = True
                self._start_event.set()

        return FakeHandler, instances

    def _patch_stack(self, fake_factory):
        """Return a list of patcher context managers to keep active for the test."""
        mock_app = MagicMock()

        def _noop_decorator(_):
            def decorator(fn):
                return fn

            return decorator

        mock_app.event = _noop_decorator
        mock_app.command = _noop_decorator
        mock_app.action = _noop_decorator
        mock_app.client = AsyncMock()

        mock_web_client = AsyncMock()
        mock_web_client.auth_test = AsyncMock(
            return_value={
                "user_id": "U_BOT",
                "user": "testbot",
                "team_id": "T_FAKE",
                "team": "FakeTeam",
            }
        )

        return [
            patch.object(_slack_mod, "AsyncApp", return_value=mock_app),
            patch.object(_slack_mod, "AsyncWebClient", return_value=mock_web_client),
            patch.object(_slack_mod, "AsyncSocketModeHandler", fake_factory),
            patch.dict(os.environ, {"SLACK_APP_TOKEN": "xapp-fake"}),
            patch("gateway.status.acquire_scoped_lock", return_value=(True, None)),
            patch("gateway.status.release_scoped_lock"),
        ]

    async def _drain(self, iterations=10):
        for _ in range(iterations):
            await asyncio.sleep(0)

    @pytest.mark.asyncio
    async def test_watchdog_reconnects_when_socket_task_dies_unexpectedly(self):
        adapter = SlackAdapter(PlatformConfig(enabled=True, token="xoxb-fake"))
        adapter._socket_watchdog_interval_s = 0.01
        factory, instances = self._make_fake_handler_factory()

        with contextlib.ExitStack() as stack:
            for p in self._patch_stack(factory):
                stack.enter_context(p)

            try:
                assert await adapter.connect() is True
                assert len(instances) == 1

                instances[0]._start_event.set()
                await self._drain()

                for _ in range(40):
                    if len(instances) >= 2:
                        break
                    await asyncio.sleep(0.01)

                assert len(instances) >= 2, "watchdog/done_callback did not reconnect"
                assert instances[0].closed is True
                assert instances[-1].start_calls == 1
                assert adapter._handler is instances[-1]
            finally:
                await adapter.disconnect()

    @pytest.mark.asyncio
    async def test_watchdog_reconnects_when_transport_reports_disconnected(self):
        adapter = SlackAdapter(PlatformConfig(enabled=True, token="xoxb-fake"))
        adapter._socket_watchdog_interval_s = 0.01
        factory, instances = self._make_fake_handler_factory()

        with contextlib.ExitStack() as stack:
            for p in self._patch_stack(factory):
                stack.enter_context(p)

            try:
                assert await adapter.connect() is True
                assert len(instances) == 1

                instances[0].client.is_connected = lambda: False

                for _ in range(40):
                    if len(instances) >= 2:
                        break
                    await asyncio.sleep(0.01)

                assert len(instances) >= 2, "watchdog did not heal dead transport"
                assert instances[0].closed is True
                assert adapter._handler is instances[-1]
            finally:
                await adapter.disconnect()

    @pytest.mark.asyncio
    async def test_disconnect_stops_watchdog_and_does_not_reconnect(self):
        adapter = SlackAdapter(PlatformConfig(enabled=True, token="xoxb-fake"))
        adapter._socket_watchdog_interval_s = 0.01
        factory, instances = self._make_fake_handler_factory()

        with contextlib.ExitStack() as stack:
            for p in self._patch_stack(factory):
                stack.enter_context(p)

            assert await adapter.connect() is True
            assert len(instances) == 1

            await adapter.disconnect()

            assert adapter._handler is None
            assert adapter._socket_mode_task is None
            assert adapter._socket_watchdog_task is None
            assert instances[0].closed is True

            for _ in range(10):
                await asyncio.sleep(0.01)

            assert len(instances) == 1, "watchdog kept reconnecting after disconnect"

    @pytest.mark.asyncio
    async def test_watchdog_cancellation_does_not_respawn(self):
        """Cancellation is the intentional-shutdown signal — no respawn allowed."""
        adapter = SlackAdapter(PlatformConfig(enabled=True, token="xoxb-fake"))
        adapter._socket_watchdog_interval_s = 0.01
        factory, _instances = self._make_fake_handler_factory()

        with contextlib.ExitStack() as stack:
            for p in self._patch_stack(factory):
                stack.enter_context(p)

            try:
                assert await adapter.connect() is True
                first_watchdog = adapter._socket_watchdog_task

                first_watchdog.cancel()
                for _ in range(20):
                    if first_watchdog.done():
                        break
                    await asyncio.sleep(0.01)

                # Done-callback must treat cancel as a shutdown signal and
                # leave the watchdog unattended (either cleared or unchanged
                # to the same cancelled task — never a fresh respawn).
                assert adapter._socket_watchdog_task is None or (
                    adapter._socket_watchdog_task is first_watchdog
                )
            finally:
                await adapter.disconnect()

    @pytest.mark.asyncio
    async def test_watchdog_unexpected_exit_respawns_via_done_callback(self):
        """A real exception out of the loop body must trigger a respawn."""
        adapter = SlackAdapter(PlatformConfig(enabled=True, token="xoxb-fake"))
        adapter._socket_watchdog_interval_s = 0.01
        factory, _instances = self._make_fake_handler_factory()

        with contextlib.ExitStack() as stack:
            for p in self._patch_stack(factory):
                stack.enter_context(p)

            try:
                assert await adapter.connect() is True
                first_watchdog = adapter._socket_watchdog_task
                assert first_watchdog is not None

                # Build a fake "crashed" task: a coroutine that raises so the
                # done-callback observes a non-cancelled exit with exception.
                async def _boom():
                    raise RuntimeError("simulated watchdog crash")

                crashed = asyncio.create_task(_boom())
                # Wait for it to actually complete with the exception.
                for _ in range(20):
                    if crashed.done():
                        break
                    await asyncio.sleep(0.01)
                assert crashed.done() and crashed.exception() is not None

                # Pretend this crashed task is the current watchdog and drive
                # the done-callback directly — this is the exact signal the
                # event loop fires when the real watchdog blows up.
                adapter._socket_watchdog_task = crashed
                adapter._on_socket_watchdog_done(crashed)

                replacement = adapter._socket_watchdog_task
                assert replacement is not None
                assert replacement is not crashed
                assert not replacement.done()
            finally:
                await adapter.disconnect()

    @pytest.mark.asyncio
    async def test_connect_replaces_prior_watchdog_atomically(self):
        """A reconnect must not leave the adapter without a watchdog."""
        adapter = SlackAdapter(PlatformConfig(enabled=True, token="xoxb-fake"))
        adapter._socket_watchdog_interval_s = 0.01
        factory, instances = self._make_fake_handler_factory()

        with contextlib.ExitStack() as stack:
            for p in self._patch_stack(factory):
                stack.enter_context(p)

            try:
                assert await adapter.connect() is True
                first_watchdog = adapter._socket_watchdog_task
                assert first_watchdog is not None

                # Second connect() must cancel the prior watchdog and install
                # a brand new one — never observe a window with no watchdog.
                assert await adapter.connect() is True
                second_watchdog = adapter._socket_watchdog_task
                assert second_watchdog is not None
                assert second_watchdog is not first_watchdog
                assert first_watchdog.done()
            finally:
                await adapter.disconnect()

    @pytest.mark.asyncio
    async def test_reconnect_refreshes_multi_workspace_state(self):
        """A reconnect that rotates the primary token must drop stale state."""
        adapter = SlackAdapter(PlatformConfig(enabled=True, token="xoxb-fake"))
        adapter._socket_watchdog_interval_s = 9999
        factory, _instances = self._make_fake_handler_factory()

        # Pre-seed stale multi-workspace state as if a prior connect had run.
        adapter._bot_user_id = "U_OLD_BOT"
        adapter._team_clients = {"T_OLD": MagicMock(name="old-client")}
        adapter._team_bot_user_ids = {"T_OLD": "U_OLD_BOT"}

        with contextlib.ExitStack() as stack:
            for p in self._patch_stack(factory):
                stack.enter_context(p)

            try:
                assert await adapter.connect() is True

                # State must reflect the fresh auth, not the stale seed.
                assert adapter._bot_user_id == "U_BOT"
                assert "T_OLD" not in adapter._team_clients
                assert "T_OLD" not in adapter._team_bot_user_ids
                assert "T_FAKE" in adapter._team_clients
                assert adapter._team_bot_user_ids["T_FAKE"] == "U_BOT"
            finally:
                await adapter.disconnect()

    @pytest.mark.asyncio
    async def test_reconnect_lock_prevents_concurrent_reconnects(self):
        adapter = SlackAdapter(PlatformConfig(enabled=True, token="xoxb-fake"))
        adapter._socket_watchdog_interval_s = 9999
        factory, instances = self._make_fake_handler_factory()

        with contextlib.ExitStack() as stack:
            for p in self._patch_stack(factory):
                stack.enter_context(p)

            try:
                assert await adapter.connect() is True
                baseline = len(instances)

                await asyncio.gather(
                    adapter._restart_socket_mode("watchdog"),
                    adapter._restart_socket_mode("done-callback"),
                )

                new_handlers = len(instances) - baseline
                assert new_handlers >= 1
                assert (
                    new_handlers <= 2
                ), f"reconnect lock failed: {new_handlers} new handlers"
            finally:
                await adapter.disconnect()


# ---------------------------------------------------------------------------
# TestSlackProxyBehavior
# ---------------------------------------------------------------------------


class TestSlackProxyBehavior:
    def test_no_proxy_helper_matches_slack_hosts(self):
        assert is_host_excluded_by_no_proxy("slack.com", "localhost,.slack.com")
        assert is_host_excluded_by_no_proxy("files.slack.com", "localhost slack.com")
        assert is_host_excluded_by_no_proxy("wss-primary.slack.com", "*")
        assert not is_host_excluded_by_no_proxy("slack.com", "localhost,.internal.corp")

    def test_resolve_slack_proxy_url_ignores_unsupported_proxy_schemes(self):
        with patch.object(
            _slack_mod,
            "resolve_proxy_url",
            return_value="socks5://proxy.example.com:1080",
        ):
            assert _slack_mod._resolve_slack_proxy_url() is None

    def test_resolve_slack_proxy_url_checks_all_slack_hosts(self):
        with (
            patch.object(
                _slack_mod,
                "resolve_proxy_url",
                return_value="http://proxy.example.com:3128",
            ),
            patch.object(
                _slack_mod,
                "is_host_excluded_by_no_proxy",
                side_effect=lambda host: host == "wss-primary.slack.com",
            ) as excluded,
        ):
            assert _slack_mod._resolve_slack_proxy_url() is None
            excluded.assert_has_calls(
                [
                    call("slack.com"),
                    call("files.slack.com"),
                    call("wss-primary.slack.com"),
                ]
            )

    @pytest.mark.asyncio
    async def test_connect_uses_proxy_when_not_bypassed(self):
        created_apps = []
        created_clients = []

        class FakeWebClient:
            def __init__(self, token):
                self.token = token
                self.proxy = "constructor-default"
                suffix = token.split("-")[-1]
                self.auth_test = AsyncMock(
                    return_value={
                        "team_id": f"T_{suffix}",
                        "user_id": f"U_{suffix}",
                        "user": f"bot-{suffix}",
                        "team": f"Team {suffix}",
                    }
                )
                created_clients.append(self)

        class FakeApp:
            def __init__(self, token):
                self.token = token
                self.client = FakeWebClient(token)
                self.registered_events = []
                self.registered_commands = []
                self.registered_actions = []
                created_apps.append(self)

            def event(self, event_type):
                self.registered_events.append(event_type)

                def decorator(fn):
                    return fn

                return decorator

            def command(self, command_name):
                self.registered_commands.append(command_name)

                def decorator(fn):
                    return fn

                return decorator

            def action(self, action_id):
                self.registered_actions.append(action_id)

                def decorator(fn):
                    return fn

                return decorator

        class FakeSocketModeHandler:
            def __init__(self, app, app_token, proxy=None):
                self.app = app
                self.app_token = app_token
                self.proxy = proxy
                self.client = MagicMock(proxy="constructor-default")

            async def start_async(self):
                return None

            async def close_async(self):
                return None

        config = PlatformConfig(enabled=True, token="xoxb-primary,xoxb-secondary")
        adapter = SlackAdapter(config)

        with (
            patch.object(_slack_mod, "AsyncApp", side_effect=FakeApp),
            patch.object(_slack_mod, "AsyncWebClient", side_effect=FakeWebClient),
            patch.object(_slack_mod, "AsyncSocketModeHandler", FakeSocketModeHandler),
            patch.object(
                _slack_mod,
                "_resolve_slack_proxy_url",
                return_value="http://proxy.example.com:3128",
            ),
            patch.dict(os.environ, {"SLACK_APP_TOKEN": "xapp-fake"}, clear=False),
            patch("gateway.status.acquire_scoped_lock", return_value=(True, None)),
            patch("asyncio.create_task", side_effect=_fake_create_task),
        ):
            result = await adapter.connect()

        assert result is True
        assert created_apps[0].client.proxy == "http://proxy.example.com:3128"
        assert all(
            client.proxy == "http://proxy.example.com:3128"
            for client in created_clients
        )
        assert adapter._handler is not None
        assert adapter._handler.proxy == "http://proxy.example.com:3128"
        assert adapter._handler.client.proxy == "http://proxy.example.com:3128"

    @pytest.mark.asyncio
    async def test_connect_clears_proxy_when_no_proxy_matches_slack(self):
        created_apps = []
        created_clients = []

        class FakeWebClient:
            def __init__(self, token):
                self.token = token
                self.proxy = "constructor-default"
                suffix = token.split("-")[-1]
                self.auth_test = AsyncMock(
                    return_value={
                        "team_id": f"T_{suffix}",
                        "user_id": f"U_{suffix}",
                        "user": f"bot-{suffix}",
                        "team": f"Team {suffix}",
                    }
                )
                created_clients.append(self)

        class FakeApp:
            def __init__(self, token):
                self.token = token
                self.client = FakeWebClient(token)
                self.registered_events = []
                self.registered_commands = []
                self.registered_actions = []
                created_apps.append(self)

            def event(self, event_type):
                self.registered_events.append(event_type)

                def decorator(fn):
                    return fn

                return decorator

            def command(self, command_name):
                self.registered_commands.append(command_name)

                def decorator(fn):
                    return fn

                return decorator

            def action(self, action_id):
                self.registered_actions.append(action_id)

                def decorator(fn):
                    return fn

                return decorator

        class FakeSocketModeHandler:
            def __init__(self, app, app_token, proxy=None):
                self.app = app
                self.app_token = app_token
                self.proxy = proxy
                self.client = MagicMock(proxy="constructor-default")

            async def start_async(self):
                return None

            async def close_async(self):
                return None

        config = PlatformConfig(enabled=True, token="xoxb-primary")
        adapter = SlackAdapter(config)

        with (
            patch.object(_slack_mod, "AsyncApp", side_effect=FakeApp),
            patch.object(_slack_mod, "AsyncWebClient", side_effect=FakeWebClient),
            patch.object(_slack_mod, "AsyncSocketModeHandler", FakeSocketModeHandler),
            patch.object(_slack_mod, "_resolve_slack_proxy_url", return_value=None),
            patch.dict(os.environ, {"SLACK_APP_TOKEN": "xapp-fake"}, clear=False),
            patch("gateway.status.acquire_scoped_lock", return_value=(True, None)),
            patch("asyncio.create_task", side_effect=_fake_create_task),
        ):
            result = await adapter.connect()

        assert result is True
        assert created_apps[0].client.proxy is None
        assert all(client.proxy is None for client in created_clients)
        assert adapter._handler is not None
        assert adapter._handler.proxy is None
        assert adapter._handler.client.proxy is None


# ---------------------------------------------------------------------------
# TestSendDocument
# ---------------------------------------------------------------------------


class TestSendDocument:
    @pytest.mark.asyncio
    async def test_send_document_success(self, adapter, tmp_path):
        test_file = tmp_path / "report.pdf"
        test_file.write_bytes(b"%PDF-1.4 fake content")

        adapter._app.client.files_upload_v2 = AsyncMock(return_value={"ok": True})

        result = await adapter.send_document(
            chat_id="C123",
            file_path=str(test_file),
            caption="Here's the report",
        )

        assert result.success
        adapter._app.client.files_upload_v2.assert_called_once()
        call_kwargs = adapter._app.client.files_upload_v2.call_args[1]
        assert call_kwargs["channel"] == "C123"
        assert call_kwargs["file"] == str(test_file)
        assert call_kwargs["filename"] == "report.pdf"
        assert call_kwargs["initial_comment"] == "Here's the report"

    @pytest.mark.asyncio
    async def test_send_document_custom_name(self, adapter, tmp_path):
        test_file = tmp_path / "data.csv"
        test_file.write_bytes(b"a,b,c\n1,2,3")

        adapter._app.client.files_upload_v2 = AsyncMock(return_value={"ok": True})

        result = await adapter.send_document(
            chat_id="C123",
            file_path=str(test_file),
            file_name="quarterly-report.csv",
        )

        assert result.success
        call_kwargs = adapter._app.client.files_upload_v2.call_args[1]
        assert call_kwargs["filename"] == "quarterly-report.csv"

    @pytest.mark.asyncio
    async def test_send_document_missing_file(self, adapter):
        result = await adapter.send_document(
            chat_id="C123",
            file_path="/nonexistent/file.pdf",
        )

        assert not result.success
        assert "not found" in result.error.lower()

    @pytest.mark.asyncio
    async def test_send_document_not_connected(self, adapter):
        adapter._app = None
        result = await adapter.send_document(
            chat_id="C123",
            file_path="/some/file.pdf",
        )

        assert not result.success
        assert "Not connected" in result.error

    @pytest.mark.asyncio
    async def test_send_document_api_error_falls_back(self, adapter, tmp_path):
        test_file = tmp_path / "doc.pdf"
        test_file.write_bytes(b"content")

        adapter._app.client.files_upload_v2 = AsyncMock(
            side_effect=RuntimeError("Slack API error")
        )

        # Should fall back to base class (text message)
        result = await adapter.send_document(
            chat_id="C123",
            file_path=str(test_file),
        )

        # Base class send() is also mocked, so check it was attempted
        adapter._app.client.chat_postMessage.assert_called_once()

    @pytest.mark.asyncio
    async def test_send_document_with_thread(self, adapter, tmp_path):
        test_file = tmp_path / "notes.txt"
        test_file.write_bytes(b"some notes")

        adapter._app.client.files_upload_v2 = AsyncMock(return_value={"ok": True})

        result = await adapter.send_document(
            chat_id="C123",
            file_path=str(test_file),
            reply_to="1234567890.123456",
        )

        assert result.success
        call_kwargs = adapter._app.client.files_upload_v2.call_args[1]
        assert call_kwargs["thread_ts"] == "1234567890.123456"

    @pytest.mark.asyncio
    async def test_send_document_thread_upload_marks_bot_participation(
        self, adapter, tmp_path
    ):
        test_file = tmp_path / "notes.txt"
        test_file.write_bytes(b"some notes")

        adapter._app.client.files_upload_v2 = AsyncMock(return_value={"ok": True})

        await adapter.send_document(
            chat_id="C123",
            file_path=str(test_file),
            metadata={"thread_id": "1234567890.123456"},
        )

        assert "1234567890.123456" in adapter._bot_message_ts

    @pytest.mark.asyncio
    async def test_send_document_retries_transient_upload_error(
        self, adapter, tmp_path
    ):
        test_file = tmp_path / "notes.txt"
        test_file.write_bytes(b"some notes")

        adapter._app.client.files_upload_v2 = AsyncMock(
            side_effect=[RuntimeError("Connection reset by peer"), {"ok": True}]
        )

        with patch("asyncio.sleep", new_callable=AsyncMock) as sleep_mock:
            result = await adapter.send_document(
                chat_id="C123",
                file_path=str(test_file),
            )

        assert result.success
        assert adapter._app.client.files_upload_v2.await_count == 2
        sleep_mock.assert_awaited_once()


class TestSendPrivateNotice:
    @pytest.mark.asyncio
    async def test_send_private_notice_uses_ephemeral_api(self, adapter):
        adapter._app.client.chat_postEphemeral = AsyncMock(
            return_value={"message_ts": "123.456"}
        )

        result = await adapter.send_private_notice(
            chat_id="C123",
            user_id="U123",
            content="private hello",
            metadata={"thread_id": "1234567890.123456"},
        )

        assert result.success
        adapter._app.client.chat_postEphemeral.assert_called_once_with(
            channel="C123",
            user="U123",
            text="private hello",
            mrkdwn=True,
            thread_ts="1234567890.123456",
        )


# ---------------------------------------------------------------------------
# TestSendVideo
# ---------------------------------------------------------------------------


class TestSendVideo:
    @pytest.mark.asyncio
    async def test_send_video_success(self, adapter, tmp_path):
        video = tmp_path / "clip.mp4"
        video.write_bytes(b"fake video data")

        adapter._app.client.files_upload_v2 = AsyncMock(return_value={"ok": True})

        result = await adapter.send_video(
            chat_id="C123",
            video_path=str(video),
            caption="Check this out",
        )

        assert result.success
        call_kwargs = adapter._app.client.files_upload_v2.call_args[1]
        assert call_kwargs["filename"] == "clip.mp4"
        assert call_kwargs["initial_comment"] == "Check this out"

    @pytest.mark.asyncio
    async def test_send_video_missing_file(self, adapter):
        result = await adapter.send_video(
            chat_id="C123",
            video_path="/nonexistent/video.mp4",
        )

        assert not result.success
        assert "not found" in result.error.lower()

    @pytest.mark.asyncio
    async def test_send_video_not_connected(self, adapter):
        adapter._app = None
        result = await adapter.send_video(
            chat_id="C123",
            video_path="/some/video.mp4",
        )

        assert not result.success
        assert "Not connected" in result.error

    @pytest.mark.asyncio
    async def test_send_video_api_error_falls_back(self, adapter, tmp_path):
        video = tmp_path / "clip.mp4"
        video.write_bytes(b"fake video")

        adapter._app.client.files_upload_v2 = AsyncMock(
            side_effect=RuntimeError("Slack API error")
        )

        # Should fall back to base class (text message)
        result = await adapter.send_video(
            chat_id="C123",
            video_path=str(video),
        )

        adapter._app.client.chat_postMessage.assert_called_once()


# ---------------------------------------------------------------------------
# TestBangPrefixCommands
# ---------------------------------------------------------------------------


class TestBangPrefixCommands:
    """``!cmd`` is rewritten to ``/cmd`` so commands work inside Slack threads.

    Slack natively rejects slash commands invoked from a thread reply
    ("/queue is not supported in threads. Sorry!"). Typing ``!queue`` as a
    plain text reply hits the message event pipeline instead, and the
    adapter rewrites the leading ``!`` to ``/`` for any known gateway
    command before downstream processing.
    """

    def _make_event(self, text, thread_ts=None, channel_type="im", channel="D123"):
        evt = {
            "text": text,
            "user": "U_USER",
            "channel": channel,
            "channel_type": channel_type,
            "ts": "1234567890.000001",
        }
        if thread_ts:
            evt["thread_ts"] = thread_ts
        return evt

    @pytest.mark.asyncio
    async def test_bang_known_command_is_rewritten_to_slash(self, adapter):
        """``!queue`` → ``/queue`` and tagged as COMMAND."""
        await adapter._handle_slack_message(self._make_event("!queue"))

        adapter.handle_message.assert_called_once()
        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.text.startswith("/queue")
        assert msg_event.message_type == MessageType.COMMAND

    @pytest.mark.asyncio
    async def test_bang_command_with_args_preserved(self, adapter):
        """``!model gpt-5.4`` → ``/model gpt-5.4``."""
        await adapter._handle_slack_message(self._make_event("!model gpt-5.4"))

        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.text.startswith("/model gpt-5.4")
        assert msg_event.message_type == MessageType.COMMAND

    @pytest.mark.asyncio
    async def test_bang_works_inside_thread(self, adapter):
        """The whole point: ``!stop`` inside a thread reply dispatches."""
        evt = self._make_event("!stop", thread_ts="1111111111.000001")
        await adapter._handle_slack_message(evt)

        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.text.startswith("/stop")
        assert msg_event.message_type == MessageType.COMMAND
        # thread_id is preserved on the source so the reply lands in the
        # same thread.
        assert msg_event.source.thread_id == "1111111111.000001"

    @pytest.mark.asyncio
    async def test_bang_unknown_token_passes_through_unchanged(self, adapter):
        """``!nice work`` is just a casual message — must NOT be rewritten."""
        await adapter._handle_slack_message(self._make_event("!nice work"))

        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.text == "!nice work"
        assert msg_event.message_type != MessageType.COMMAND

    @pytest.mark.asyncio
    async def test_bang_with_bot_suffix_resolves(self, adapter):
        """``!stop@hermes`` matches the get_command() ``@suffix`` stripping."""
        await adapter._handle_slack_message(self._make_event("!stop@hermes"))

        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.text.startswith("/stop@hermes")
        assert msg_event.message_type == MessageType.COMMAND

    @pytest.mark.asyncio
    async def test_plain_slash_still_works(self, adapter):
        """Sanity check — ``/queue`` (top-level channel/DM) still dispatches."""
        await adapter._handle_slack_message(self._make_event("/queue"))

        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.text.startswith("/queue")
        assert msg_event.message_type == MessageType.COMMAND


# ---------------------------------------------------------------------------
# TestIncomingDocumentHandling
# ---------------------------------------------------------------------------


class TestIncomingDocumentHandling:
    def _make_event(
        self, files=None, text="hello", channel_type="im", blocks=None, attachments=None
    ):
        """Build a mock Slack message event with file attachments."""
        return {
            "text": text,
            "user": "U_USER",
            "channel": "D123",
            "channel_type": channel_type,
            "ts": "1234567890.000001",
            "files": files or [],
            "blocks": blocks or [],
            "attachments": attachments or [],
        }

    @pytest.mark.asyncio
    async def test_pdf_document_cached(self, adapter):
        """A PDF attachment should be downloaded, cached, and set as DOCUMENT type."""
        pdf_bytes = b"%PDF-1.4 fake content"

        with patch.object(
            adapter, "_download_slack_file_bytes", new_callable=AsyncMock
        ) as dl:
            dl.return_value = pdf_bytes
            event = self._make_event(
                files=[
                    {
                        "mimetype": "application/pdf",
                        "name": "report.pdf",
                        "url_private_download": "https://files.slack.com/report.pdf",
                        "size": len(pdf_bytes),
                    }
                ]
            )
            await adapter._handle_slack_message(event)

        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.message_type == MessageType.DOCUMENT
        assert len(msg_event.media_urls) == 1
        assert os.path.exists(msg_event.media_urls[0])
        assert msg_event.media_types == ["application/pdf"]

    @pytest.mark.asyncio
    async def test_txt_document_injects_content(self, adapter):
        """A .txt file under 100KB should have its content injected into event text."""
        content = b"Hello from a text file"

        with patch.object(
            adapter, "_download_slack_file_bytes", new_callable=AsyncMock
        ) as dl:
            dl.return_value = content
            event = self._make_event(
                text="summarize this",
                files=[
                    {
                        "mimetype": "text/plain",
                        "name": "notes.txt",
                        "url_private_download": "https://files.slack.com/notes.txt",
                        "size": len(content),
                    }
                ],
            )
            await adapter._handle_slack_message(event)

        msg_event = adapter.handle_message.call_args[0][0]
        assert "Hello from a text file" in msg_event.text
        assert "[Content of notes.txt]" in msg_event.text
        assert "summarize this" in msg_event.text

    @pytest.mark.asyncio
    async def test_md_document_injects_content(self, adapter):
        """A .md file under 100KB should have its content injected."""
        content = b"# Title\nSome markdown content"

        with patch.object(
            adapter, "_download_slack_file_bytes", new_callable=AsyncMock
        ) as dl:
            dl.return_value = content
            event = self._make_event(
                files=[
                    {
                        "mimetype": "text/markdown",
                        "name": "readme.md",
                        "url_private_download": "https://files.slack.com/readme.md",
                        "size": len(content),
                    }
                ],
                text="",
            )
            await adapter._handle_slack_message(event)

        msg_event = adapter.handle_message.call_args[0][0]
        assert "# Title" in msg_event.text

    @pytest.mark.asyncio
    async def test_json_snippet_injects_content(self, adapter):
        """A .json snippet should be treated as a text document and injected."""
        content = b'{"hello": "world", "count": 2}'

        with patch.object(
            adapter, "_download_slack_file_bytes", new_callable=AsyncMock
        ) as dl:
            dl.return_value = content
            event = self._make_event(
                text="can you parse this",
                files=[
                    {
                        "mimetype": "text/plain",
                        "name": "zapfile.json",
                        "filetype": "json",
                        "pretty_type": "JSON",
                        "mode": "snippet",
                        "editable": True,
                        "url_private_download": "https://files.slack.com/zapfile.json",
                        "size": len(content),
                    }
                ],
            )
            await adapter._handle_slack_message(event)

        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.message_type == MessageType.DOCUMENT
        assert len(msg_event.media_urls) == 1
        assert msg_event.media_types == ["application/json"]
        assert "[Content of zapfile.json]" in msg_event.text
        assert '"hello": "world"' in msg_event.text
        assert "can you parse this" in msg_event.text

    @pytest.mark.asyncio
    async def test_large_txt_not_injected(self, adapter):
        """A .txt file over 100KB should be cached but NOT injected."""
        content = b"x" * (200 * 1024)

        with patch.object(
            adapter, "_download_slack_file_bytes", new_callable=AsyncMock
        ) as dl:
            dl.return_value = content
            event = self._make_event(
                files=[
                    {
                        "mimetype": "text/plain",
                        "name": "big.txt",
                        "url_private_download": "https://files.slack.com/big.txt",
                        "size": len(content),
                    }
                ],
                text="",
            )
            await adapter._handle_slack_message(event)

        msg_event = adapter.handle_message.call_args[0][0]
        assert len(msg_event.media_urls) == 1
        assert "[Content of" not in (msg_event.text or "")

    @pytest.mark.asyncio
    async def test_zip_file_cached(self, adapter):
        """A .zip file should be cached as a supported document."""
        with patch.object(
            adapter, "_download_slack_file_bytes", new_callable=AsyncMock
        ) as dl:
            dl.return_value = b"PK\x03\x04zip"
            event = self._make_event(
                files=[
                    {
                        "mimetype": "application/zip",
                        "name": "archive.zip",
                        "url_private_download": "https://files.slack.com/archive.zip",
                        "size": 1024,
                    }
                ]
            )
            await adapter._handle_slack_message(event)

        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.message_type == MessageType.DOCUMENT
        assert len(msg_event.media_urls) == 1
        assert msg_event.media_types == ["application/zip"]

    @pytest.mark.asyncio
    async def test_oversized_document_skipped(self, adapter):
        """A document over 20MB should be skipped."""
        event = self._make_event(
            files=[
                {
                    "mimetype": "application/pdf",
                    "name": "huge.pdf",
                    "url_private_download": "https://files.slack.com/huge.pdf",
                    "size": 25 * 1024 * 1024,
                }
            ]
        )
        await adapter._handle_slack_message(event)

        msg_event = adapter.handle_message.call_args[0][0]
        assert len(msg_event.media_urls) == 0

    @pytest.mark.asyncio
    async def test_document_download_error_handled(self, adapter):
        """If document download fails, handler should not crash."""
        with patch.object(
            adapter, "_download_slack_file_bytes", new_callable=AsyncMock
        ) as dl:
            dl.side_effect = RuntimeError("download failed")
            event = self._make_event(
                files=[
                    {
                        "mimetype": "application/pdf",
                        "name": "report.pdf",
                        "url_private_download": "https://files.slack.com/report.pdf",
                        "size": 1024,
                    }
                ]
            )
            await adapter._handle_slack_message(event)

        # Handler should still be called (the exception is caught)
        adapter.handle_message.assert_called_once()

    @pytest.mark.asyncio
    async def test_image_still_handled(self, adapter):
        """Image attachments should still go through the image path, not document."""
        with patch.object(
            adapter, "_download_slack_file", new_callable=AsyncMock
        ) as dl:
            dl.return_value = "/tmp/cached_image.jpg"
            event = self._make_event(
                files=[
                    {
                        "mimetype": "image/jpeg",
                        "name": "photo.jpg",
                        "url_private_download": "https://files.slack.com/photo.jpg",
                        "size": 1024,
                    }
                ]
            )
            await adapter._handle_slack_message(event)

        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.message_type == MessageType.PHOTO

    @pytest.mark.asyncio
    async def test_video_attachment_cached(self, adapter):
        """Video attachments should be downloaded into the video cache."""
        video_bytes = b"\x00\x00\x00\x18ftypmp42fake-mp4"

        with patch.object(
            adapter, "_download_slack_file_bytes", new_callable=AsyncMock
        ) as dl:
            dl.return_value = video_bytes
            event = self._make_event(
                text="what happens in this?",
                files=[
                    {
                        "mimetype": "video/mp4",
                        "name": "clip.mp4",
                        "url_private_download": "https://files.slack.com/clip.mp4",
                        "size": len(video_bytes),
                    }
                ],
            )
            await adapter._handle_slack_message(event)

        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.message_type == MessageType.VIDEO
        assert len(msg_event.media_urls) == 1
        assert os.path.exists(msg_event.media_urls[0])
        assert msg_event.media_types == [SUPPORTED_VIDEO_TYPES[".mp4"]]
        dl.assert_awaited_once_with("https://files.slack.com/clip.mp4", team_id="")

    @pytest.mark.asyncio
    async def test_file_shared_video_fallback_fetches_file_info(self, adapter):
        """file_shared-only video events should still reach the agent."""
        video_bytes = b"\x00\x00\x00\x18ftypmp42fake-mp4"
        adapter._app.client.files_info = AsyncMock(
            return_value={
                "ok": True,
                "file": {
                    "id": "FVIDEO",
                    "mimetype": "video/mp4",
                    "name": "clip.mp4",
                    "url_private_download": "https://files.slack.com/clip.mp4",
                    "size": len(video_bytes),
                    "user": "U_USER",
                    "shares": {
                        "private": {
                            "D123": [
                                {"ts": "1234567890.000001"},
                            ]
                        }
                    },
                },
            }
        )

        with (
            patch.object(
                adapter, "_download_slack_file_bytes", new_callable=AsyncMock
            ) as dl,
            patch("asyncio.sleep", new_callable=AsyncMock),
        ):
            dl.return_value = video_bytes
            await adapter._handle_slack_file_shared(
                {
                    "type": "file_shared",
                    "channel_id": "D123",
                    "file_id": "FVIDEO",
                    "user_id": "U_USER",
                    "event_ts": "1234567890.000002",
                }
            )

        adapter._app.client.files_info.assert_awaited_once_with(file="FVIDEO")
        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.message_type == MessageType.VIDEO
        assert len(msg_event.media_urls) == 1
        assert os.path.exists(msg_event.media_urls[0])
        assert msg_event.media_types == [SUPPORTED_VIDEO_TYPES[".mp4"]]

    @pytest.mark.asyncio
    async def test_download_failure_is_surfaced_in_message_text(self, adapter):
        """Attachment download failures (401/403/HTML-body/etc.) should be
        translated into a user-facing `[Slack attachment notice]` block so
        the agent can tell the user what to fix (e.g. missing files:read
        scope). No proactive files.info probe is made — the diagnostic
        runs only when the download actually fails.
        """
        import httpx

        req = httpx.Request("GET", "https://files.slack.com/photo.jpg")
        resp = httpx.Response(403, request=req)

        with patch.object(
            adapter, "_download_slack_file", new_callable=AsyncMock
        ) as dl:
            dl.side_effect = httpx.HTTPStatusError("403", request=req, response=resp)
            event = self._make_event(
                text="what's in this?",
                files=[
                    {
                        "id": "F123",
                        "mimetype": "image/jpeg",
                        "name": "photo.jpg",
                        "url_private_download": "https://files.slack.com/photo.jpg",
                        "size": 1024,
                    }
                ],
            )
            await adapter._handle_slack_message(event)

        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.message_type == MessageType.TEXT
        assert "[Slack attachment notice]" in msg_event.text
        assert "403" in msg_event.text
        assert "what's in this?" in msg_event.text

    @pytest.mark.asyncio
    async def test_rich_text_blocks_do_not_duplicate_plain_text(self, adapter):
        """Plain rich_text composer blocks match the plain text field exactly,
        so the dedupe guard keeps the message clean."""
        event = self._make_event(
            text="hello world",
            blocks=[
                {
                    "type": "rich_text",
                    "elements": [
                        {
                            "type": "rich_text_section",
                            "elements": [
                                {"type": "text", "text": "hello world"},
                            ],
                        }
                    ],
                }
            ],
        )

        await adapter._handle_slack_message(event)

        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.text == "hello world"

    @pytest.mark.asyncio
    async def test_rich_text_quotes_and_lists_are_extracted(self, adapter):
        """Nested quote and list content should be surfaced from rich_text blocks."""
        event = self._make_event(
            text="Can you summarize this?",
            blocks=[
                {
                    "type": "rich_text",
                    "elements": [
                        {
                            "type": "rich_text_quote",
                            "elements": [
                                {
                                    "type": "rich_text_section",
                                    "elements": [
                                        {"type": "text", "text": "Quoted line"}
                                    ],
                                }
                            ],
                        },
                        {
                            "type": "rich_text_list",
                            "style": "bullet",
                            "elements": [
                                {
                                    "type": "rich_text_section",
                                    "elements": [
                                        {"type": "text", "text": "First bullet"}
                                    ],
                                },
                                {
                                    "type": "rich_text_section",
                                    "elements": [
                                        {"type": "text", "text": "Second bullet"}
                                    ],
                                },
                            ],
                        },
                    ],
                }
            ],
        )

        await adapter._handle_slack_message(event)

        msg_event = adapter.handle_message.call_args[0][0]
        assert "Can you summarize this?" in msg_event.text
        assert "> Quoted line" in msg_event.text
        assert "• First bullet" in msg_event.text
        assert "• Second bullet" in msg_event.text

    @pytest.mark.asyncio
    async def test_attachments_unfurl_text_is_appended_even_when_url_is_in_message(
        self, adapter
    ):
        """Shared URLs should still expose unfurl preview text to the agent."""
        event = self._make_event(
            text="Look at this doc https://example.com/spec",
            attachments=[
                {
                    "title": "Spec",
                    "from_url": "https://example.com/spec",
                    "text": "The latest product spec preview",
                    "footer": "Notion",
                }
            ],
        )

        await adapter._handle_slack_message(event)

        msg_event = adapter.handle_message.call_args[0][0]
        assert "Look at this doc https://example.com/spec" in msg_event.text
        assert "📎 [Spec](https://example.com/spec)" in msg_event.text
        assert "The latest product spec preview" in msg_event.text
        assert "_Notion_" in msg_event.text

    @pytest.mark.asyncio
    async def test_message_unfurl_attachments_are_skipped(self, adapter):
        """Message unfurls should be skipped to avoid echoing Slack message copies."""
        event = self._make_event(
            text="https://example.com/thread",
            attachments=[
                {
                    "is_msg_unfurl": True,
                    "title": "Thread copy",
                    "text": "This should not be appended",
                }
            ],
        )

        await adapter._handle_slack_message(event)

        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.text == "https://example.com/thread"

    @pytest.mark.asyncio
    async def test_channel_routing_ignores_bot_mentions_inside_block_text(
        self, adapter
    ):
        """Block-extracted text with a bot mention must not satisfy mention
        gating in channels — routing decisions use the original user text so
        quoted/forwarded content can't trick the bot into responding."""
        event = self._make_event(
            text="please review",
            channel_type="channel",
            blocks=[
                {
                    "type": "rich_text",
                    "elements": [
                        {
                            "type": "rich_text_quote",
                            "elements": [
                                {
                                    "type": "rich_text_section",
                                    "elements": [
                                        {
                                            "type": "text",
                                            "text": "Contains <@U_BOT> in quoted text",
                                        }
                                    ],
                                }
                            ],
                        }
                    ],
                }
            ],
        )

        await adapter._handle_slack_message(event)

        adapter.handle_message.assert_not_called()

    @pytest.mark.asyncio
    async def test_quoted_slash_command_text_does_not_change_message_type(
        self, adapter
    ):
        """Quoted slash-like content should not convert a normal message into a command."""
        event = self._make_event(
            text="",
            blocks=[
                {
                    "type": "rich_text",
                    "elements": [
                        {
                            "type": "rich_text_quote",
                            "elements": [
                                {
                                    "type": "rich_text_section",
                                    "elements": [
                                        {"type": "text", "text": "/deploy now"}
                                    ],
                                }
                            ],
                        }
                    ],
                }
            ],
        )

        await adapter._handle_slack_message(event)

        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.message_type == MessageType.TEXT
        assert "> /deploy now" in msg_event.text


# ---------------------------------------------------------------------------
# TestIncomingAudioHandling — Slack voice messages (regression)
# ---------------------------------------------------------------------------


class TestSlackAudioExtResolution:
    """Unit coverage for the inbound-audio extension resolver.

    Regression for: Slack in-app voice messages are MP4/AAC containers
    (``audio/mp4``, filename ``audio_message*.mp4``) that the old code cached
    as ``.ogg`` (the catch-all fallback), so OpenAI STT — which sniffs the
    container from the filename extension — rejected them. WhatsApp ``.ogg``
    and uploaded ``.m4a`` worked because their extension happened to match.
    """

    def test_slack_voice_message_mp4_keeps_real_extension(self):
        """The core bug: audio/mp4 voice message must NOT become .ogg."""
        f = {"name": "audio_message.mp4", "mimetype": "audio/mp4"}
        ext = _slack_mod._resolve_slack_audio_ext(f, f["mimetype"])
        assert ext != ".ogg", "regression: MP4 voice message mislabeled as .ogg"
        assert ext in {".mp4", ".m4a"}
        assert ext in _slack_mod._SLACK_STT_SUPPORTED_EXTS

    def test_whatsapp_ogg_preserved(self):
        f = {"name": "voice.ogg", "mimetype": "audio/ogg"}
        assert _slack_mod._resolve_slack_audio_ext(f, f["mimetype"]) == ".ogg"

    def test_m4a_upload_preserved(self):
        f = {"name": "clip.m4a", "mimetype": "audio/x-m4a"}
        assert _slack_mod._resolve_slack_audio_ext(f, f["mimetype"]) == ".m4a"

    def test_mp3_upload_preserved(self):
        f = {"name": "song.mp3", "mimetype": "audio/mpeg"}
        assert _slack_mod._resolve_slack_audio_ext(f, f["mimetype"]) == ".mp3"

    def test_mimetype_used_when_filename_extension_missing(self):
        """No usable filename ext → fall back to the mime map, not .ogg."""
        f = {"name": "", "mimetype": "audio/mp4"}
        assert _slack_mod._resolve_slack_audio_ext(f, f["mimetype"]) == ".m4a"

    def test_unknown_audio_defaults_to_m4a_not_ogg(self):
        """A truly unknown audio type defaults to the broadly-decodable .m4a."""
        f = {"name": "weird", "mimetype": "audio/x-some-future-codec"}
        ext = _slack_mod._resolve_slack_audio_ext(f, f["mimetype"])
        assert ext == ".m4a"
        assert ext != ".ogg"


class TestSlackVoiceClipDetection:
    """Unit coverage for the video/mp4-mislabeled voice-clip detector."""

    def test_audio_message_filename_detected(self):
        assert _slack_mod._is_slack_voice_clip(
            {"name": "audio_message.mp4", "mimetype": "video/mp4"}
        )

    def test_slack_audio_subtype_detected(self):
        assert _slack_mod._is_slack_voice_clip(
            {"name": "clip.mp4", "subtype": "slack_audio", "mimetype": "video/mp4"}
        )

    def test_real_video_not_detected(self):
        """A genuine uploaded video must NOT be hijacked into the audio path."""
        assert not _slack_mod._is_slack_voice_clip(
            {"name": "vacation.mp4", "mimetype": "video/mp4"}
        )

    def test_slack_video_clip_not_detected(self):
        """slack_video clips carry a real video track — leave them as video."""
        assert not _slack_mod._is_slack_voice_clip(
            {"name": "screen_recording.mp4", "subtype": "slack_video"}
        )


class TestIncomingAudioHandling:
    def _make_event(self, files=None, text="hello"):
        return {
            "text": text,
            "user": "U_USER",
            "channel": "D123",
            "channel_type": "im",
            "ts": "1234567890.000001",
            "files": files or [],
            "blocks": [],
            "attachments": [],
        }

    @pytest.mark.asyncio
    async def test_voice_message_cached_with_correct_extension(self, adapter, tmp_path):
        """audio/mp4 voice message is cached with an STT-acceptable extension,
        not the old .ogg fallback, and routed as audio."""
        captured = {}

        async def _fake_download(url, ext, audio=False, team_id=""):
            captured["ext"] = ext
            captured["audio"] = audio
            path = tmp_path / f"cached{ext}"
            path.write_bytes(b"\x00\x00\x00\x18ftypmp42fake mp4 bytes")
            return str(path)

        with patch.object(adapter, "_download_slack_file", side_effect=_fake_download):
            event = self._make_event(
                files=[
                    {
                        "mimetype": "audio/mp4",
                        "name": "audio_message.mp4",
                        "subtype": "slack_audio",
                        "url_private_download": "https://files.slack.com/audio_message.mp4",
                        "size": 2048,
                    }
                ]
            )
            await adapter._handle_slack_message(event)

        assert captured.get("audio") is True
        assert captured["ext"] != ".ogg", "regression: voice message cached as .ogg"
        assert captured["ext"] in {".mp4", ".m4a"}

        msg_event = adapter.handle_message.call_args[0][0]
        assert len(msg_event.media_urls) == 1
        # media_type stays audio/* so the gateway routes it to STT
        assert msg_event.media_types[0].startswith("audio/")

    @pytest.mark.asyncio
    async def test_video_mp4_voice_clip_rerouted_to_audio(self, adapter, tmp_path):
        """A voice clip mislabeled video/mp4 is rerouted to the audio path
        (cached as audio, reported as audio/*) instead of video understanding."""
        captured = {}

        async def _fake_download(url, ext, audio=False, team_id=""):
            captured["ext"] = ext
            captured["audio"] = audio
            path = tmp_path / f"cached{ext}"
            path.write_bytes(b"\x00\x00\x00\x18ftypmp42fake mp4 bytes")
            return str(path)

        with patch.object(adapter, "_download_slack_file", side_effect=_fake_download):
            event = self._make_event(
                files=[
                    {
                        "mimetype": "video/mp4",
                        "name": "audio_message.mp4",
                        "subtype": "slack_audio",
                        "url_private_download": "https://files.slack.com/audio_message.mp4",
                        "size": 2048,
                    }
                ]
            )
            await adapter._handle_slack_message(event)

        assert captured.get("audio") is True
        assert captured["ext"] in {".mp4", ".m4a"}
        msg_event = adapter.handle_message.call_args[0][0]
        assert len(msg_event.media_urls) == 1
        assert msg_event.media_types[0].startswith("audio/"), (
            "voice clip should route to STT, not video understanding"
        )

    @pytest.mark.asyncio
    async def test_real_video_still_routed_as_video(self, adapter, tmp_path):
        """A genuine uploaded video must remain on the video path."""

        async def _fake_download_bytes(url, team_id=""):
            return b"\x00\x00\x00\x18ftypisomfake real video"

        with patch.object(
            adapter, "_download_slack_file_bytes", side_effect=_fake_download_bytes
        ):
            event = self._make_event(
                files=[
                    {
                        "mimetype": "video/mp4",
                        "name": "vacation.mp4",
                        "url_private_download": "https://files.slack.com/vacation.mp4",
                        "size": 4096,
                    }
                ]
            )
            await adapter._handle_slack_message(event)

        msg_event = adapter.handle_message.call_args[0][0]
        assert len(msg_event.media_urls) == 1
        assert msg_event.media_types[0].startswith("video/"), (
            "a real video must not be hijacked into the audio path"
        )


# ---------------------------------------------------------------------------
# TestMessageRouting
# ---------------------------------------------------------------------------


class TestMessageRouting:
    @pytest.mark.asyncio
    async def test_dm_processed_without_mention(self, adapter):
        """DM messages should be processed without requiring a bot mention."""
        event = {
            "text": "hello",
            "user": "U_USER",
            "channel": "D123",
            "channel_type": "im",
            "ts": "1234567890.000001",
        }
        await adapter._handle_slack_message(event)
        adapter.handle_message.assert_called_once()

    @pytest.mark.asyncio
    async def test_channel_message_requires_mention(self, adapter):
        """Channel messages without a bot mention should be ignored."""
        event = {
            "text": "just talking",
            "user": "U_USER",
            "channel": "C123",
            "channel_type": "channel",
            "ts": "1234567890.000001",
        }
        await adapter._handle_slack_message(event)
        adapter.handle_message.assert_not_called()

    @pytest.mark.asyncio
    async def test_channel_mention_strips_bot_id(self, adapter):
        """When mentioned in a channel, the bot mention should be stripped."""
        event = {
            "text": "<@U_BOT> what's the weather?",
            "user": "U_USER",
            "channel": "C123",
            "channel_type": "channel",
            "ts": "1234567890.000001",
        }
        await adapter._handle_slack_message(event)
        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.text == "what's the weather?"
        assert "<@U_BOT>" not in msg_event.text

    @pytest.mark.asyncio
    async def test_bot_messages_ignored(self, adapter):
        """Messages from bots should be ignored."""
        event = {
            "text": "bot response",
            "bot_id": "B_OTHER",
            "channel": "C123",
            "channel_type": "im",
            "ts": "1234567890.000001",
        }
        await adapter._handle_slack_message(event)
        adapter.handle_message.assert_not_called()

    @pytest.mark.asyncio
    async def test_message_edits_ignored(self, adapter):
        """Message edits should be ignored."""
        event = {
            "text": "edited message",
            "user": "U_USER",
            "channel": "C123",
            "channel_type": "im",
            "ts": "1234567890.000001",
            "subtype": "message_changed",
        }
        await adapter._handle_slack_message(event)
        adapter.handle_message.assert_not_called()


# ---------------------------------------------------------------------------
# TestSendTyping — assistant.threads.setStatus
# ---------------------------------------------------------------------------


class TestSendTyping:
    """Test typing indicator via assistant.threads.setStatus."""

    @pytest.mark.asyncio
    async def test_sets_status_in_thread(self, adapter):
        adapter._app.client.assistant_threads_setStatus = AsyncMock()
        await adapter.send_typing("C123", metadata={"thread_id": "parent_ts"})
        adapter._app.client.assistant_threads_setStatus.assert_called_once_with(
            channel_id="C123",
            thread_ts="parent_ts",
            status="is thinking...",
        )

    @pytest.mark.asyncio
    async def test_noop_without_thread(self, adapter):
        adapter._app.client.assistant_threads_setStatus = AsyncMock()
        await adapter.send_typing("C123")
        adapter._app.client.assistant_threads_setStatus.assert_not_called()

    @pytest.mark.asyncio
    async def test_handles_missing_scope_gracefully(self, adapter):
        adapter._app.client.assistant_threads_setStatus = AsyncMock(
            side_effect=Exception("missing_scope")
        )
        # Should not raise
        await adapter.send_typing("C123", metadata={"thread_id": "ts1"})

    @pytest.mark.asyncio
    async def test_uses_thread_ts_fallback(self, adapter):
        adapter._app.client.assistant_threads_setStatus = AsyncMock()
        await adapter.send_typing("C123", metadata={"thread_ts": "fallback_ts"})
        adapter._app.client.assistant_threads_setStatus.assert_called_once_with(
            channel_id="C123",
            thread_ts="fallback_ts",
            status="is thinking...",
        )

    @pytest.mark.asyncio
    async def test_stop_typing_clears_tracked_thread(self, adapter):
        adapter._app.client.assistant_threads_setStatus = AsyncMock()
        await adapter.send_typing("C123", metadata={"thread_id": "parent_ts"})

        await adapter.stop_typing("C123", metadata={"thread_id": "parent_ts"})

        assert adapter._app.client.assistant_threads_setStatus.call_args_list[
            1
        ] == call(
            channel_id="C123",
            thread_ts="parent_ts",
            status="",
        )
        assert "C123" not in adapter._active_status_threads

    @pytest.mark.asyncio
    async def test_stop_typing_noop_without_tracked_thread(self, adapter):
        adapter._app.client.assistant_threads_setStatus = AsyncMock()

        await adapter.stop_typing("C123")

        adapter._app.client.assistant_threads_setStatus.assert_not_called()

    @pytest.mark.asyncio
    async def test_stop_typing_handles_api_error_gracefully(self, adapter):
        adapter._active_status_threads["C123"] = "parent_ts"
        adapter._app.client.assistant_threads_setStatus = AsyncMock(
            side_effect=Exception("missing_scope")
        )

        await adapter.stop_typing("C123")

        adapter._app.client.assistant_threads_setStatus.assert_called_once_with(
            channel_id="C123",
            thread_ts="parent_ts",
            status="",
        )
        assert "C123" not in adapter._active_status_threads

    @pytest.mark.asyncio
    async def test_send_clears_status_after_final_post(self, adapter):
        adapter._app.client.chat_postMessage = AsyncMock(
            return_value={"ts": "reply_ts"}
        )
        adapter._app.client.assistant_threads_setStatus = AsyncMock()
        adapter._active_status_threads["C123"] = "parent_ts"

        result = await adapter.send("C123", "done", metadata={"thread_id": "parent_ts"})

        assert result.success
        adapter._app.client.chat_postMessage.assert_called_once()
        adapter._app.client.assistant_threads_setStatus.assert_called_once_with(
            channel_id="C123",
            thread_ts="parent_ts",
            status="",
        )
        assert "C123" not in adapter._active_status_threads

    @pytest.mark.asyncio
    async def test_streaming_final_edit_clears_status(self, adapter):
        adapter._app.client.chat_update = AsyncMock()
        adapter._app.client.assistant_threads_setStatus = AsyncMock()
        adapter._active_status_threads["C123"] = "parent_ts"

        result = await adapter.edit_message(
            "C123",
            "reply_ts",
            "done",
            finalize=True,
        )

        assert result.success
        adapter._app.client.chat_update.assert_called_once_with(
            channel="C123",
            ts="reply_ts",
            text="done",
        )
        adapter._app.client.assistant_threads_setStatus.assert_called_once_with(
            channel_id="C123",
            thread_ts="parent_ts",
            status="",
        )
        assert "C123" not in adapter._active_status_threads

    @pytest.mark.asyncio
    async def test_streaming_intermediate_edit_keeps_status(self, adapter):
        adapter._app.client.chat_update = AsyncMock()
        adapter._app.client.assistant_threads_setStatus = AsyncMock()
        adapter._active_status_threads["C123"] = "parent_ts"

        result = await adapter.edit_message(
            "C123",
            "reply_ts",
            "partial",
            finalize=False,
        )

        assert result.success
        adapter._app.client.assistant_threads_setStatus.assert_not_called()
        assert adapter._active_status_threads["C123"] == "parent_ts"


# ---------------------------------------------------------------------------
# TestFormatMessage — Markdown → mrkdwn conversion
# ---------------------------------------------------------------------------


class TestFormatMessage:
    """Test markdown to Slack mrkdwn conversion."""

    def test_bold_conversion(self, adapter):
        assert adapter.format_message("**hello**") == "*hello*"

    def test_italic_asterisk_conversion(self, adapter):
        assert adapter.format_message("*hello*") == "_hello_"

    def test_italic_underscore_preserved(self, adapter):
        assert adapter.format_message("_hello_") == "_hello_"

    def test_header_to_bold(self, adapter):
        assert adapter.format_message("## Section Title") == "*Section Title*"

    def test_header_with_bold_content(self, adapter):
        # **bold** inside a header should not double-wrap
        assert adapter.format_message("## **Title**") == "*Title*"

    def test_link_conversion(self, adapter):
        result = adapter.format_message("[click here](https://example.com)")
        assert result == "<https://example.com|click here>"

    def test_link_conversion_strips_markdown_angle_brackets(self, adapter):
        result = adapter.format_message("[click here](<https://example.com>)")
        assert result == "<https://example.com|click here>"

    def test_escapes_control_characters(self, adapter):
        result = adapter.format_message("AT&T < 5 > 3")
        assert result == "AT&amp;T &lt; 5 &gt; 3"

    def test_preserves_existing_slack_entities(self, adapter):
        text = "Hey <@U123>, see <https://example.com|example> and <!here>"
        assert adapter.format_message(text) == text

    def test_strikethrough(self, adapter):
        assert adapter.format_message("~~deleted~~") == "~deleted~"

    def test_code_block_preserved(self, adapter):
        code = "```python\nx = **not bold**\n```"
        assert adapter.format_message(code) == code

    def test_inline_code_preserved(self, adapter):
        text = "Use `**raw**` syntax"
        assert adapter.format_message(text) == "Use `**raw**` syntax"

    def test_mixed_content(self, adapter):
        text = "**Bold** and *italic* with `code`"
        result = adapter.format_message(text)
        assert "*Bold*" in result
        assert "_italic_" in result
        assert "`code`" in result

    def test_empty_string(self, adapter):
        assert adapter.format_message("") == ""

    def test_none_passthrough(self, adapter):
        assert adapter.format_message(None) is None

    def test_blockquote_preserved(self, adapter):
        """Single-line blockquote > marker is preserved."""
        assert adapter.format_message("> quoted text") == "> quoted text"

    def test_multiline_blockquote(self, adapter):
        """Multi-line blockquote preserves > on each line."""
        text = "> line one\n> line two"
        assert adapter.format_message(text) == "> line one\n> line two"

    def test_blockquote_with_formatting(self, adapter):
        """Blockquote containing bold text."""
        assert adapter.format_message("> **bold quote**") == "> *bold quote*"

    def test_nested_blockquote(self, adapter):
        """Multiple > characters for nested quotes."""
        assert adapter.format_message(">> deeply quoted") == ">> deeply quoted"

    def test_blockquote_mixed_with_plain(self, adapter):
        """Blockquote lines interleaved with plain text."""
        text = "normal\n> quoted\nnormal again"
        result = adapter.format_message(text)
        assert "> quoted" in result
        assert "normal" in result

    def test_non_prefix_gt_still_escaped(self, adapter):
        """Greater-than in mid-line is still escaped."""
        assert adapter.format_message("5 > 3") == "5 &gt; 3"

    def test_blockquote_with_code(self, adapter):
        """Blockquote containing inline code."""
        result = adapter.format_message("> use `fmt.Println`")
        assert result.startswith(">")
        assert "`fmt.Println`" in result

    def test_bold_italic_combined(self, adapter):
        """Triple-star ***text*** converts to Slack bold+italic *_text_*."""
        assert adapter.format_message("***hello***") == "*_hello_*"

    def test_bold_italic_with_surrounding_text(self, adapter):
        """Bold+italic in a sentence."""
        result = adapter.format_message("This is ***important*** stuff")
        assert "*_important_*" in result

    def test_bold_italic_does_not_break_plain_bold(self, adapter):
        """**bold** still works after adding ***bold italic*** support."""
        assert adapter.format_message("**bold**") == "*bold*"

    def test_bold_italic_does_not_break_plain_italic(self, adapter):
        """*italic* still works after adding ***bold italic*** support."""
        assert adapter.format_message("*italic*") == "_italic_"

    def test_bold_italic_mixed_with_bold(self, adapter):
        """Both ***bold italic*** and **bold** in the same message."""
        result = adapter.format_message("***important*** and **bold**")
        assert "*_important_*" in result
        assert "*bold*" in result

    def test_pre_escaped_ampersand_not_double_escaped(self, adapter):
        """Already-escaped &amp; must not become &amp;amp;."""
        assert adapter.format_message("&amp;") == "&amp;"

    def test_pre_escaped_lt_not_double_escaped(self, adapter):
        """Already-escaped &lt; must not become &amp;lt;."""
        assert adapter.format_message("&lt;") == "&lt;"

    def test_pre_escaped_gt_not_double_escaped(self, adapter):
        """Already-escaped &gt; in plain text must not become &amp;gt;."""
        assert adapter.format_message("5 &gt; 3") == "5 &gt; 3"

    def test_mixed_raw_and_escaped_entities(self, adapter):
        """Raw & and pre-escaped &amp; coexist correctly."""
        result = adapter.format_message("AT&T and &amp; entity")
        assert result == "AT&amp;T and &amp; entity"

    def test_link_with_parentheses_in_url(self, adapter):
        """Wikipedia-style URL with balanced parens is not truncated."""
        result = adapter.format_message(
            "[Foo](https://en.wikipedia.org/wiki/Foo_(bar))"
        )
        assert result == "<https://en.wikipedia.org/wiki/Foo_(bar)|Foo>"

    def test_link_with_multiple_paren_pairs(self, adapter):
        """URL with multiple balanced paren pairs."""
        result = adapter.format_message("[text](https://example.com/a_(b)_c_(d))")
        assert result == "<https://example.com/a_(b)_c_(d)|text>"

    def test_link_without_parens_still_works(self, adapter):
        """Normal URL without parens is unaffected by regex change."""
        result = adapter.format_message("[click](https://example.com/path?q=1)")
        assert result == "<https://example.com/path?q=1|click>"

    def test_link_with_angle_brackets_and_parens(self, adapter):
        """Angle-bracket URL with parens (CommonMark syntax)."""
        result = adapter.format_message(
            "[Foo](<https://en.wikipedia.org/wiki/Foo_(bar)>)"
        )
        assert result == "<https://en.wikipedia.org/wiki/Foo_(bar)|Foo>"

    def test_escaping_is_idempotent(self, adapter):
        """Formatting already-formatted text produces the same result."""
        original = "AT&T < 5 > 3"
        once = adapter.format_message(original)
        twice = adapter.format_message(once)
        assert once == twice

    # --- Entity preservation (spec-compliance) ---

    def test_channel_mention_preserved(self, adapter):
        """<!channel> special mention passes through unchanged."""
        assert adapter.format_message("Attention <!channel>") == "Attention <!channel>"

    def test_everyone_mention_preserved(self, adapter):
        """<!everyone> special mention passes through unchanged."""
        assert adapter.format_message("Hey <!everyone>") == "Hey <!everyone>"

    def test_subteam_mention_preserved(self, adapter):
        """<!subteam^ID> user group mention passes through unchanged."""
        assert (
            adapter.format_message("Paging <!subteam^S12345>")
            == "Paging <!subteam^S12345>"
        )

    def test_date_formatting_preserved(self, adapter):
        """<!date^...> formatting token passes through unchanged."""
        text = "Posted <!date^1392734382^{date_pretty}|Feb 18, 2014>"
        assert adapter.format_message(text) == text

    def test_channel_link_preserved(self, adapter):
        """<#CHANNEL_ID> channel link passes through unchanged."""
        assert adapter.format_message("Join <#C12345>") == "Join <#C12345>"

    # --- Additional edge cases ---

    def test_message_only_code_block(self, adapter):
        """Entire message is a fenced code block — no conversion."""
        code = "```python\nx = 1\n```"
        assert adapter.format_message(code) == code

    def test_multiline_mixed_formatting(self, adapter):
        """Multi-line message with headers, bold, links, code, and blockquotes."""
        text = "## Title\n**bold** and [link](https://x.com)\n> quote\n`code`"
        result = adapter.format_message(text)
        assert result.startswith("*Title*")
        assert "*bold*" in result
        assert "<https://x.com|link>" in result
        assert "> quote" in result
        assert "`code`" in result

    def test_markdown_unordered_list_with_asterisk(self, adapter):
        """Asterisk list items must not trigger italic conversion."""
        text = "* item one\n* item two"
        result = adapter.format_message(text)
        assert "item one" in result
        assert "item two" in result

    def test_nested_bold_in_link(self, adapter):
        """Bold inside link label — label is stashed before bold pass."""
        result = adapter.format_message("[**bold**](https://example.com)")
        assert "https://example.com" in result
        assert "bold" in result

    def test_url_with_query_string_and_ampersand(self, adapter):
        """Ampersand in URL query string must not be escaped."""
        result = adapter.format_message("[link](https://x.com?a=1&b=2)")
        assert result == "<https://x.com?a=1&b=2|link>"

    def test_markdown_image_does_not_create_broken_slack_link(self, adapter):
        """Markdown image syntax should not become '!<url|alt>' in Slack."""
        result = adapter.format_message("![alt](https://img.example.com/cat.png)")
        assert result == "![alt](https://img.example.com/cat.png)"

    def test_literal_asterisks_with_spaces_are_not_treated_as_italic(self, adapter):
        """Asterisks used as plain delimiters should stay literal."""
        result = adapter.format_message("a * b * c")
        assert result == "a * b * c"

    def test_emoji_shortcodes_passthrough(self, adapter):
        """Emoji shortcodes like :smile: pass through unchanged."""
        assert adapter.format_message(":smile: hello :wave:") == ":smile: hello :wave:"


# ---------------------------------------------------------------------------
# TestEditMessage
# ---------------------------------------------------------------------------


class TestEditMessage:
    """Verify that edit_message() applies mrkdwn formatting before sending."""

    @pytest.mark.asyncio
    async def test_edit_message_formats_bold(self, adapter):
        """edit_message converts **bold** to Slack *bold*."""
        adapter._app.client.chat_update = AsyncMock(return_value={"ok": True})
        await adapter.edit_message("C123", "1234.5678", "**hello world**")
        kwargs = adapter._app.client.chat_update.call_args.kwargs
        assert kwargs["text"] == "*hello world*"

    @pytest.mark.asyncio
    async def test_edit_message_formats_links(self, adapter):
        """edit_message converts markdown links to Slack format."""
        adapter._app.client.chat_update = AsyncMock(return_value={"ok": True})
        await adapter.edit_message("C123", "1234.5678", "[click](https://example.com)")
        kwargs = adapter._app.client.chat_update.call_args.kwargs
        assert kwargs["text"] == "<https://example.com|click>"

    @pytest.mark.asyncio
    async def test_edit_message_preserves_blockquotes(self, adapter):
        """edit_message preserves blockquote > markers."""
        adapter._app.client.chat_update = AsyncMock(return_value={"ok": True})
        await adapter.edit_message("C123", "1234.5678", "> quoted text")
        kwargs = adapter._app.client.chat_update.call_args.kwargs
        assert kwargs["text"] == "> quoted text"

    @pytest.mark.asyncio
    async def test_edit_message_escapes_control_chars(self, adapter):
        """edit_message escapes & < > in plain text."""
        adapter._app.client.chat_update = AsyncMock(return_value={"ok": True})
        await adapter.edit_message("C123", "1234.5678", "AT&T < 5 > 3")
        kwargs = adapter._app.client.chat_update.call_args.kwargs
        assert kwargs["text"] == "AT&amp;T &lt; 5 &gt; 3"


# ---------------------------------------------------------------------------
# TestEditMessageStreamingPipeline
# ---------------------------------------------------------------------------


class TestEditMessageStreamingPipeline:
    """E2E: verify that sequential streaming edits all go through format_message.

    Simulates the GatewayStreamConsumer pattern where edit_message is called
    repeatedly with progressively longer accumulated text.  Every call must
    produce properly formatted mrkdwn in the chat_update payload.
    """

    @pytest.mark.asyncio
    async def test_edit_message_formats_streaming_updates(self, adapter):
        """Simulates streaming: multiple edits, each should be formatted."""
        adapter._app.client.chat_update = AsyncMock(return_value={"ok": True})

        # First streaming update — bold
        result1 = await adapter.edit_message("C123", "ts1", "**Processing**...")
        assert result1.success is True
        kwargs1 = adapter._app.client.chat_update.call_args.kwargs
        assert kwargs1["text"] == "*Processing*..."

        # Second streaming update — bold + link
        result2 = await adapter.edit_message(
            "C123", "ts1", "**Done!** See [results](https://example.com)"
        )
        assert result2.success is True
        kwargs2 = adapter._app.client.chat_update.call_args.kwargs
        assert kwargs2["text"] == "*Done!* See <https://example.com|results>"

    @pytest.mark.asyncio
    async def test_edit_message_formats_code_and_bold(self, adapter):
        """Streaming update with code block and bold — code must be preserved."""
        adapter._app.client.chat_update = AsyncMock(return_value={"ok": True})

        content = "**Result:**\n```python\nprint('hello')\n```"
        result = await adapter.edit_message("C123", "ts1", content)
        assert result.success is True
        kwargs = adapter._app.client.chat_update.call_args.kwargs
        assert kwargs["text"].startswith("*Result:*")
        assert "```python\nprint('hello')\n```" in kwargs["text"]

    @pytest.mark.asyncio
    async def test_edit_message_formats_blockquote_in_stream(self, adapter):
        """Streaming update with blockquote — '>' marker must survive."""
        adapter._app.client.chat_update = AsyncMock(return_value={"ok": True})

        content = "> **Important:** do this\nnormal line"
        result = await adapter.edit_message("C123", "ts1", content)
        assert result.success is True
        kwargs = adapter._app.client.chat_update.call_args.kwargs
        assert kwargs["text"].startswith("> *Important:*")
        assert "normal line" in kwargs["text"]

    @pytest.mark.asyncio
    async def test_edit_message_formats_progressive_accumulation(self, adapter):
        """Simulate real streaming: text grows with each edit, all formatted."""
        adapter._app.client.chat_update = AsyncMock(return_value={"ok": True})

        updates = [
            ("**Step 1**", "*Step 1*"),
            ("**Step 1**\n**Step 2**", "*Step 1*\n*Step 2*"),
            (
                "**Step 1**\n**Step 2**\nSee [docs](https://docs.example.com)",
                "*Step 1*\n*Step 2*\nSee <https://docs.example.com|docs>",
            ),
        ]

        for raw, expected in updates:
            result = await adapter.edit_message("C123", "ts1", raw)
            assert result.success is True
            kwargs = adapter._app.client.chat_update.call_args.kwargs
            assert kwargs["text"] == expected, f"Failed for input: {raw!r}"

        # Total edit count should match number of updates
        assert adapter._app.client.chat_update.call_count == len(updates)

    @pytest.mark.asyncio
    async def test_edit_message_formats_bold_italic(self, adapter):
        """Bold+italic ***text*** is formatted as *_text_* in edited messages."""
        adapter._app.client.chat_update = AsyncMock(return_value={"ok": True})
        await adapter.edit_message("C123", "ts1", "***important*** update")
        kwargs = adapter._app.client.chat_update.call_args.kwargs
        assert "*_important_*" in kwargs["text"]

    @pytest.mark.asyncio
    async def test_edit_message_does_not_double_escape(self, adapter):
        """Pre-escaped entities in edited messages must not get double-escaped."""
        adapter._app.client.chat_update = AsyncMock(return_value={"ok": True})
        await adapter.edit_message("C123", "ts1", "5 &gt; 3 and &amp; entity")
        kwargs = adapter._app.client.chat_update.call_args.kwargs
        assert "&amp;gt;" not in kwargs["text"]
        assert "&amp;amp;" not in kwargs["text"]
        assert "&gt;" in kwargs["text"]
        assert "&amp;" in kwargs["text"]

    @pytest.mark.asyncio
    async def test_edit_message_formats_url_with_parens(self, adapter):
        """Wikipedia-style URL with parens survives edit pipeline."""
        adapter._app.client.chat_update = AsyncMock(return_value={"ok": True})
        await adapter.edit_message(
            "C123", "ts1", "See [Foo](https://en.wikipedia.org/wiki/Foo_(bar))"
        )
        kwargs = adapter._app.client.chat_update.call_args.kwargs
        assert "<https://en.wikipedia.org/wiki/Foo_(bar)|Foo>" in kwargs["text"]

    @pytest.mark.asyncio
    async def test_edit_message_not_connected(self, adapter):
        """edit_message returns failure when adapter is not connected."""
        adapter._app = None
        result = await adapter.edit_message("C123", "ts1", "**hello**")
        assert result.success is False
        assert "Not connected" in result.error


# ---------------------------------------------------------------------------
# TestReactions
# ---------------------------------------------------------------------------


class TestReactions:
    """Test emoji reaction methods."""

    @pytest.mark.asyncio
    async def test_add_reaction_calls_api(self, adapter):
        adapter._app.client.reactions_add = AsyncMock()
        result = await adapter._add_reaction("C123", "ts1", "eyes")
        assert result is True
        adapter._app.client.reactions_add.assert_called_once_with(
            channel="C123", timestamp="ts1", name="eyes"
        )

    @pytest.mark.asyncio
    async def test_add_reaction_handles_error(self, adapter):
        adapter._app.client.reactions_add = AsyncMock(
            side_effect=Exception("already_reacted")
        )
        result = await adapter._add_reaction("C123", "ts1", "eyes")
        assert result is False

    @pytest.mark.asyncio
    async def test_remove_reaction_calls_api(self, adapter):
        adapter._app.client.reactions_remove = AsyncMock()
        result = await adapter._remove_reaction("C123", "ts1", "eyes")
        assert result is True

    @pytest.mark.asyncio
    async def test_reactions_in_message_flow(self, adapter):
        """Reactions should be bracketed around actual processing via hooks."""
        adapter._app.client.reactions_add = AsyncMock()
        adapter._app.client.reactions_remove = AsyncMock()
        adapter._app.client.users_info = AsyncMock(
            return_value={"user": {"profile": {"display_name": "Tyler"}}}
        )

        event = {
            "text": "hello",
            "user": "U_USER",
            "channel": "C123",
            "channel_type": "im",
            "ts": "1234567890.000001",
        }
        await adapter._handle_slack_message(event)

        # _handle_slack_message should register the message for reactions
        assert "1234567890.000001" in adapter._reacting_message_ids

        # Simulate the base class calling on_processing_start
        from gateway.platforms.base import MessageEvent, MessageType, SessionSource
        from gateway.config import Platform

        source = SessionSource(
            platform=Platform.SLACK,
            chat_id="C123",
            chat_type="dm",
            user_id="U_USER",
        )
        msg_event = MessageEvent(
            text="hello",
            message_type=MessageType.TEXT,
            source=source,
            message_id="1234567890.000001",
        )
        await adapter.on_processing_start(msg_event)

        add_calls = adapter._app.client.reactions_add.call_args_list
        assert len(add_calls) == 1
        assert add_calls[0].kwargs["name"] == "eyes"

        # Simulate the base class calling on_processing_complete
        from gateway.platforms.base import ProcessingOutcome

        await adapter.on_processing_complete(msg_event, ProcessingOutcome.SUCCESS)

        add_calls = adapter._app.client.reactions_add.call_args_list
        remove_calls = adapter._app.client.reactions_remove.call_args_list
        assert len(add_calls) == 2
        assert add_calls[1].kwargs["name"] == "white_check_mark"
        assert len(remove_calls) == 1
        assert remove_calls[0].kwargs["name"] == "eyes"

        # Message ID should be cleaned up
        assert "1234567890.000001" not in adapter._reacting_message_ids

    @pytest.mark.asyncio
    async def test_reactions_failure_outcome(self, adapter):
        """Failed processing should add :x: instead of :white_check_mark:."""
        adapter._app.client.reactions_add = AsyncMock()
        adapter._app.client.reactions_remove = AsyncMock()

        from gateway.platforms.base import (
            MessageEvent,
            MessageType,
            SessionSource,
            ProcessingOutcome,
        )
        from gateway.config import Platform

        source = SessionSource(
            platform=Platform.SLACK,
            chat_id="C123",
            chat_type="dm",
            user_id="U_USER",
        )
        adapter._reacting_message_ids.add("1234567890.000002")
        msg_event = MessageEvent(
            text="hello",
            message_type=MessageType.TEXT,
            source=source,
            message_id="1234567890.000002",
        )
        await adapter.on_processing_complete(msg_event, ProcessingOutcome.FAILURE)

        add_calls = adapter._app.client.reactions_add.call_args_list
        remove_calls = adapter._app.client.reactions_remove.call_args_list
        assert len(add_calls) == 1
        assert add_calls[0].kwargs["name"] == "x"
        assert len(remove_calls) == 1
        assert remove_calls[0].kwargs["name"] == "eyes"

    @pytest.mark.asyncio
    async def test_reactions_skipped_for_non_dm_non_mention(self, adapter):
        """Non-DM, non-mention messages should not get reactions."""
        adapter._app.client.reactions_add = AsyncMock()
        adapter._app.client.reactions_remove = AsyncMock()
        adapter._app.client.users_info = AsyncMock(
            return_value={"user": {"profile": {"display_name": "Tyler"}}}
        )

        event = {
            "text": "hello",
            "user": "U_USER",
            "channel": "C123",
            "channel_type": "channel",
            "ts": "1234567890.000003",
        }
        await adapter._handle_slack_message(event)

        # Should NOT register for reactions when not mentioned in a channel
        assert "1234567890.000003" not in adapter._reacting_message_ids
        adapter._app.client.reactions_add.assert_not_called()
        adapter._app.client.reactions_remove.assert_not_called()

    @pytest.mark.asyncio
    async def test_reactions_disabled_via_env(self, adapter, monkeypatch):
        """SLACK_REACTIONS=false should suppress all reaction lifecycle."""
        monkeypatch.setenv("SLACK_REACTIONS", "false")
        adapter._app.client.reactions_add = AsyncMock()
        adapter._app.client.reactions_remove = AsyncMock()
        adapter._app.client.users_info = AsyncMock(
            return_value={"user": {"profile": {"display_name": "Tyler"}}}
        )

        event = {
            "text": "hello",
            "user": "U_USER",
            "channel": "C123",
            "channel_type": "im",
            "ts": "1234567890.000004",
        }
        await adapter._handle_slack_message(event)

        # Should NOT register for reactions when toggle is off
        assert "1234567890.000004" not in adapter._reacting_message_ids

        # Hooks should also be no-ops when disabled
        from gateway.platforms.base import (
            MessageEvent,
            MessageType,
            SessionSource,
            ProcessingOutcome,
        )
        from gateway.config import Platform

        source = SessionSource(
            platform=Platform.SLACK,
            chat_id="C123",
            chat_type="dm",
            user_id="U_USER",
        )
        msg_event = MessageEvent(
            text="hello",
            message_type=MessageType.TEXT,
            source=source,
            message_id="1234567890.000004",
        )
        # Force-add to verify hooks respect the toggle independently
        adapter._reacting_message_ids.add("1234567890.000004")
        await adapter.on_processing_start(msg_event)
        await adapter.on_processing_complete(msg_event, ProcessingOutcome.SUCCESS)

        adapter._app.client.reactions_add.assert_not_called()
        adapter._app.client.reactions_remove.assert_not_called()

    @pytest.mark.asyncio
    async def test_reactions_enabled_by_default(self, adapter):
        """SLACK_REACTIONS defaults to true (matches existing behavior)."""
        assert adapter._reactions_enabled() is True


# ---------------------------------------------------------------------------
# TestThreadReplyHandling
# ---------------------------------------------------------------------------


class TestThreadReplyHandling:
    """Test thread reply processing without explicit bot mentions."""

    @pytest.fixture()
    def mock_session_store(self):
        """Create a mock session store with entries dict."""
        store = MagicMock()
        store._entries = {}
        store._ensure_loaded = MagicMock()
        store.config = MagicMock()
        store.config.group_sessions_per_user = True
        return store

    @pytest.fixture()
    def adapter_with_session_store(self, mock_session_store):
        """Create an adapter with a mock session store attached."""
        config = PlatformConfig(enabled=True, token="***")
        a = SlackAdapter(config)
        a._app = MagicMock()
        a._app.client = AsyncMock()
        a._bot_user_id = "U_BOT"
        a._team_bot_user_ids = {"T_TEAM": "U_BOT"}
        a._running = True
        a.handle_message = AsyncMock()
        a.set_session_store(mock_session_store)
        return a

    @pytest.mark.asyncio
    async def test_thread_reply_without_mention_no_session_ignored(
        self, adapter_with_session_store, mock_session_store
    ):
        """Thread replies without mention should be ignored if no active session."""
        mock_session_store._entries = {}  # No active sessions

        event = {
            "text": "Just replying in the thread",
            "user": "U_USER",
            "channel": "C123",
            "ts": "123.456",
            "thread_ts": "123.000",  # Different from ts - this is a reply
            "channel_type": "channel",
            "team": "T_TEAM",
        }
        await adapter_with_session_store._handle_slack_message(event)
        adapter_with_session_store.handle_message.assert_not_called()

    @pytest.mark.asyncio
    async def test_thread_reply_without_mention_with_session_processed(
        self, adapter_with_session_store, mock_session_store
    ):
        """Thread replies without mention should be processed if there's an active session."""
        # Simulate an active session for this thread
        session_key = "agent:main:slack:group:C123:123.000:U_USER"
        mock_session_store._entries = {session_key: MagicMock()}

        event = {
            "text": "Follow-up question",
            "user": "U_USER",
            "channel": "C123",
            "ts": "123.456",
            "thread_ts": "123.000",  # Reply in thread 123.000
            "channel_type": "channel",
            "team": "T_TEAM",
        }
        await adapter_with_session_store._handle_slack_message(event)
        adapter_with_session_store.handle_message.assert_called_once()

        # Verify the text is passed through unchanged (no mention stripping needed)
        msg_event = adapter_with_session_store.handle_message.call_args[0][0]
        assert msg_event.text == "Follow-up question"

    @pytest.mark.asyncio
    async def test_thread_reply_with_mention_strips_bot_id(
        self, adapter_with_session_store, mock_session_store
    ):
        """Thread replies with @mention should still strip the bot ID."""
        # Even with a session, mentions should be stripped
        session_key = "agent:main:slack:group:C123:123.000:U_USER"
        mock_session_store._entries = {session_key: MagicMock()}

        event = {
            "text": "<@U_BOT> thanks for the help",
            "user": "U_USER",
            "channel": "C123",
            "ts": "123.456",
            "thread_ts": "123.000",
            "channel_type": "channel",
            "team": "T_TEAM",
        }
        await adapter_with_session_store._handle_slack_message(event)
        adapter_with_session_store.handle_message.assert_called_once()

        msg_event = adapter_with_session_store.handle_message.call_args[0][0]
        assert "<@U_BOT>" not in msg_event.text
        assert msg_event.text == "thanks for the help"

    @pytest.mark.asyncio
    async def test_top_level_message_requires_mention_even_with_session(
        self, adapter_with_session_store, mock_session_store
    ):
        """Top-level channel messages should require mention even if session exists."""
        # Session exists but this is a top-level message (no thread_ts)
        session_key = "agent:main:slack:group:C123:123.000:U_USER"
        mock_session_store._entries = {session_key: MagicMock()}

        event = {
            "text": "New question without mention",
            "user": "U_USER",
            "channel": "C123",
            "ts": "456.789",
            # No thread_ts - this is a top-level message
            "channel_type": "channel",
            "team": "T_TEAM",
        }
        await adapter_with_session_store._handle_slack_message(event)
        adapter_with_session_store.handle_message.assert_not_called()

    @pytest.mark.asyncio
    async def test_no_session_store_ignores_thread_replies(self, adapter):
        """If no session store is attached, thread replies without mention should be ignored."""
        # adapter fixture has no session store attached
        event = {
            "text": "Thread reply without mention",
            "user": "U_USER",
            "channel": "C123",
            "ts": "123.456",
            "thread_ts": "123.000",
            "channel_type": "channel",
            "team": "T_TEAM",
        }
        await adapter._handle_slack_message(event)
        adapter.handle_message.assert_not_called()


# ---------------------------------------------------------------------------
# TestAssistantThreadLifecycle
# ---------------------------------------------------------------------------


class TestAssistantThreadLifecycle:
    """Slack Assistant lifecycle events should seed session/user context."""

    @pytest.fixture()
    def mock_session_store(self):
        store = MagicMock()
        store._entries = {}
        store._ensure_loaded = MagicMock()
        store.config = MagicMock()
        store.config.group_sessions_per_user = True
        store.get_or_create_session = MagicMock()
        return store

    @pytest.fixture()
    def assistant_adapter(self, mock_session_store):
        config = PlatformConfig(enabled=True, token="***")
        a = SlackAdapter(config)
        a._app = MagicMock()
        a._app.client = AsyncMock()
        a._bot_user_id = "U_BOT"
        a._team_bot_user_ids = {"T_TEAM": "U_BOT"}
        a._running = True
        a.handle_message = AsyncMock()
        a.set_session_store(mock_session_store)
        return a

    @pytest.mark.asyncio
    async def test_lifecycle_event_seeds_session_store(
        self, assistant_adapter, mock_session_store
    ):
        event = {
            "type": "assistant_thread_started",
            "team_id": "T_TEAM",
            "assistant_thread": {
                "channel_id": "D123",
                "thread_ts": "171.000",
                "user_id": "U_USER",
                "context": {"channel_id": "C_ORIGIN"},
            },
        }

        await assistant_adapter._handle_assistant_thread_lifecycle_event(event)

        assert (
            assistant_adapter._assistant_threads[("D123", "171.000")]["user_id"]
            == "U_USER"
        )
        mock_session_store.get_or_create_session.assert_called_once()
        source = mock_session_store.get_or_create_session.call_args[0][0]
        assert source.chat_id == "D123"
        assert source.chat_type == "dm"
        assert source.user_id == "U_USER"
        assert source.thread_id == "171.000"
        assert source.chat_topic == "C_ORIGIN"

    @pytest.mark.asyncio
    async def test_message_uses_cached_assistant_thread_identity(
        self, assistant_adapter
    ):
        assistant_adapter._assistant_threads[("D123", "171.000")] = {
            "channel_id": "D123",
            "thread_ts": "171.000",
            "user_id": "U_USER",
            "team_id": "T_TEAM",
        }
        assistant_adapter._app.client.users_info = AsyncMock(
            return_value={"user": {"profile": {"display_name": "Tyler"}}}
        )
        assistant_adapter._app.client.reactions_add = AsyncMock()
        assistant_adapter._app.client.reactions_remove = AsyncMock()

        event = {
            "text": "hello from assistant dm",
            "channel": "D123",
            "channel_type": "im",
            "thread_ts": "171.000",
            "ts": "171.111",
            "team": "T_TEAM",
        }

        await assistant_adapter._handle_slack_message(event)

        msg_event = assistant_adapter.handle_message.call_args[0][0]
        assert msg_event.source.user_id == "U_USER"
        assert msg_event.source.thread_id == "171.000"
        assert msg_event.source.user_name == "Tyler"

    def test_assistant_threads_cache_eviction(self, assistant_adapter):
        """Cache should evict oldest entries when exceeding the size limit."""
        assistant_adapter._ASSISTANT_THREADS_MAX = 10
        # Fill to the limit
        for i in range(10):
            assistant_adapter._cache_assistant_thread_metadata(
                {
                    "channel_id": f"D{i}",
                    "thread_ts": f"{i}.000",
                    "user_id": f"U{i}",
                }
            )
        assert len(assistant_adapter._assistant_threads) == 10

        # Adding one more should trigger eviction (down to max // 2 = 5)
        assistant_adapter._cache_assistant_thread_metadata(
            {
                "channel_id": "D999",
                "thread_ts": "999.000",
                "user_id": "U999",
            }
        )
        assert len(assistant_adapter._assistant_threads) <= 10
        # The newest entry must survive eviction
        assert ("D999", "999.000") in assistant_adapter._assistant_threads


# ---------------------------------------------------------------------------
# TestUserNameResolution
# ---------------------------------------------------------------------------


class TestUserNameResolution:
    """Test user identity resolution."""

    @pytest.mark.asyncio
    async def test_resolves_display_name(self, adapter):
        adapter._app.client.users_info = AsyncMock(
            return_value={
                "user": {"profile": {"display_name": "Tyler", "real_name": "Tyler B"}}
            }
        )
        name = await adapter._resolve_user_name("U123")
        assert name == "Tyler"

    @pytest.mark.asyncio
    async def test_falls_back_to_real_name(self, adapter):
        adapter._app.client.users_info = AsyncMock(
            return_value={
                "user": {"profile": {"display_name": "", "real_name": "Tyler B"}}
            }
        )
        name = await adapter._resolve_user_name("U123")
        assert name == "Tyler B"

    @pytest.mark.asyncio
    async def test_caches_result(self, adapter):
        adapter._app.client.users_info = AsyncMock(
            return_value={"user": {"profile": {"display_name": "Tyler"}}}
        )
        await adapter._resolve_user_name("U123")
        await adapter._resolve_user_name("U123")
        # Only one API call despite two lookups
        assert adapter._app.client.users_info.call_count == 1

    @pytest.mark.asyncio
    async def test_handles_api_error(self, adapter):
        adapter._app.client.users_info = AsyncMock(
            side_effect=Exception("rate limited")
        )
        name = await adapter._resolve_user_name("U123")
        assert name == "U123"  # Falls back to user_id

    @pytest.mark.asyncio
    async def test_user_name_in_message_source(self, adapter):
        """Message source should include resolved user name."""
        adapter._app.client.users_info = AsyncMock(
            return_value={"user": {"profile": {"display_name": "Tyler"}}}
        )
        adapter._app.client.reactions_add = AsyncMock()
        adapter._app.client.reactions_remove = AsyncMock()

        event = {
            "text": "hello",
            "user": "U_USER",
            "channel": "C123",
            "channel_type": "im",
            "ts": "1234567890.000001",
        }
        await adapter._handle_slack_message(event)

        # Check the source in the MessageEvent passed to handle_message
        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.source.user_name == "Tyler"


# ---------------------------------------------------------------------------
# TestSlashCommands — expanded command set
# ---------------------------------------------------------------------------


class TestSlashCommands:
    """Test slash command routing."""

    @pytest.mark.asyncio
    async def test_compact_maps_to_compress(self, adapter):
        command = {"text": "compact", "user_id": "U1", "channel_id": "C1"}
        await adapter._handle_slash_command(command)
        msg = adapter.handle_message.call_args[0][0]
        assert msg.text == "/compress"

    @pytest.mark.asyncio
    async def test_resume_command(self, adapter):
        command = {"text": "resume my session", "user_id": "U1", "channel_id": "C1"}
        await adapter._handle_slash_command(command)
        msg = adapter.handle_message.call_args[0][0]
        assert msg.text == "/resume my session"

    @pytest.mark.asyncio
    async def test_background_command(self, adapter):
        command = {"text": "background run tests", "user_id": "U1", "channel_id": "C1"}
        await adapter._handle_slash_command(command)
        msg = adapter.handle_message.call_args[0][0]
        assert msg.text == "/background run tests"

    @pytest.mark.asyncio
    async def test_usage_command(self, adapter):
        command = {"text": "usage", "user_id": "U1", "channel_id": "C1"}
        await adapter._handle_slash_command(command)
        msg = adapter.handle_message.call_args[0][0]
        assert msg.text == "/usage"

    @pytest.mark.asyncio
    async def test_reasoning_command(self, adapter):
        command = {"text": "reasoning", "user_id": "U1", "channel_id": "C1"}
        await adapter._handle_slash_command(command)
        msg = adapter.handle_message.call_args[0][0]
        assert msg.text == "/reasoning"

    # ------------------------------------------------------------------
    # Native slash commands — /btw, /stop, /model, ... dispatched directly
    # instead of as /hermes subcommands. This is the Discord/Telegram parity
    # fix: the slash name itself becomes the command.
    # ------------------------------------------------------------------

    @pytest.mark.asyncio
    async def test_native_btw_slash(self, adapter):
        """/btw with args must dispatch to /background, not /hermes btw."""
        command = {
            "command": "/btw",
            "text": "fix the failing test",
            "user_id": "U1",
            "channel_id": "C1",
        }
        await adapter._handle_slash_command(command)
        msg = adapter.handle_message.call_args[0][0]
        # The gateway command dispatcher resolves /btw -> background via
        # resolve_command() — our handler's job is just to deliver
        # "/btw <args>" to the gateway runner, which is what this asserts.
        assert msg.text == "/btw fix the failing test"

    @pytest.mark.asyncio
    async def test_native_stop_slash_no_args(self, adapter):
        command = {
            "command": "/stop",
            "text": "",
            "user_id": "U1",
            "channel_id": "C1",
        }
        await adapter._handle_slash_command(command)
        msg = adapter.handle_message.call_args[0][0]
        assert msg.text == "/stop"

    @pytest.mark.asyncio
    async def test_native_model_slash_with_args(self, adapter):
        command = {
            "command": "/model",
            "text": "anthropic/claude-sonnet-4",
            "user_id": "U1",
            "channel_id": "C1",
        }
        await adapter._handle_slash_command(command)
        msg = adapter.handle_message.call_args[0][0]
        assert msg.text == "/model anthropic/claude-sonnet-4"

    @pytest.mark.asyncio
    async def test_legacy_hermes_prefix_still_works(self, adapter):
        """Backward compat: /hermes btw foo must still route to /btw foo.

        Old workspace manifests only declared /hermes as the single slash.
        After users refresh their manifest they get /btw natively, but the
        legacy form must keep working during the transition.
        """
        command = {
            "command": "/hermes",
            "text": "btw run the tests",
            "user_id": "U1",
            "channel_id": "C1",
        }
        await adapter._handle_slash_command(command)
        msg = adapter.handle_message.call_args[0][0]
        assert msg.text == "/btw run the tests"

    @pytest.mark.asyncio
    async def test_legacy_hermes_freeform_question(self, adapter):
        """/hermes <free-form text> must stay as the raw text (non-command)."""
        command = {
            "command": "/hermes",
            "text": "what's the weather today?",
            "user_id": "U1",
            "channel_id": "C1",
        }
        await adapter._handle_slash_command(command)
        msg = adapter.handle_message.call_args[0][0]
        assert msg.text == "what's the weather today?"


# ---------------------------------------------------------------------------
# TestMessageSplitting
# ---------------------------------------------------------------------------


class TestMessageSplitting:
    """Test that long messages are split before sending."""

    @pytest.mark.asyncio
    async def test_long_message_split_into_chunks(self, adapter):
        """Messages over MAX_MESSAGE_LENGTH should be split."""
        long_text = "x" * 45000  # Over Slack's 40k API limit
        adapter._app.client.chat_postMessage = AsyncMock(return_value={"ts": "ts1"})
        await adapter.send("C123", long_text)
        # Should have been called multiple times
        assert adapter._app.client.chat_postMessage.call_count >= 2

    @pytest.mark.asyncio
    async def test_short_message_single_send(self, adapter):
        """Short messages should be sent in one call."""
        adapter._app.client.chat_postMessage = AsyncMock(return_value={"ts": "ts1"})
        await adapter.send("C123", "hello world")
        assert adapter._app.client.chat_postMessage.call_count == 1

    @pytest.mark.asyncio
    async def test_send_preserves_blockquote_formatting(self, adapter):
        """Blockquote '>' markers must survive format → chunk → send pipeline."""
        adapter._app.client.chat_postMessage = AsyncMock(return_value={"ts": "ts1"})
        await adapter.send("C123", "> quoted text\nnormal text")
        kwargs = adapter._app.client.chat_postMessage.call_args.kwargs
        sent_text = kwargs["text"]
        assert sent_text.startswith("> quoted text")
        assert "normal text" in sent_text

    @pytest.mark.asyncio
    async def test_send_formats_bold_italic(self, adapter):
        """Bold+italic ***text*** is formatted as *_text_* in sent messages."""
        adapter._app.client.chat_postMessage = AsyncMock(return_value={"ts": "ts1"})
        await adapter.send("C123", "***important*** update")
        kwargs = adapter._app.client.chat_postMessage.call_args.kwargs
        assert "*_important_*" in kwargs["text"]

    @pytest.mark.asyncio
    async def test_send_explicitly_enables_mrkdwn(self, adapter):
        adapter._app.client.chat_postMessage = AsyncMock(return_value={"ts": "ts1"})
        await adapter.send("C123", "**hello**")
        kwargs = adapter._app.client.chat_postMessage.call_args.kwargs
        assert kwargs.get("mrkdwn") is True

    @pytest.mark.asyncio
    async def test_send_does_not_double_escape_entities(self, adapter):
        """Pre-escaped &amp; in sent messages must not become &amp;amp;."""
        adapter._app.client.chat_postMessage = AsyncMock(return_value={"ts": "ts1"})
        await adapter.send("C123", "Use &amp; for ampersand")
        kwargs = adapter._app.client.chat_postMessage.call_args.kwargs
        assert "&amp;amp;" not in kwargs["text"]
        assert "&amp;" in kwargs["text"]

    @pytest.mark.asyncio
    async def test_send_formats_url_with_parens(self, adapter):
        """Wikipedia-style URL with parens survives send pipeline."""
        adapter._app.client.chat_postMessage = AsyncMock(return_value={"ts": "ts1"})
        await adapter.send("C123", "See [Foo](https://en.wikipedia.org/wiki/Foo_(bar))")
        kwargs = adapter._app.client.chat_postMessage.call_args.kwargs
        assert "<https://en.wikipedia.org/wiki/Foo_(bar)|Foo>" in kwargs["text"]


# ---------------------------------------------------------------------------
# TestReplyBroadcast
# ---------------------------------------------------------------------------


class TestReplyBroadcast:
    """Test reply_broadcast config option."""

    @pytest.mark.asyncio
    async def test_broadcast_disabled_by_default(self, adapter):
        adapter._app.client.chat_postMessage = AsyncMock(return_value={"ts": "ts1"})
        await adapter.send("C123", "hi", metadata={"thread_id": "parent_ts"})
        kwargs = adapter._app.client.chat_postMessage.call_args.kwargs
        assert "reply_broadcast" not in kwargs

    @pytest.mark.asyncio
    async def test_broadcast_enabled_via_config(self, adapter):
        adapter.config.extra["reply_broadcast"] = True
        adapter._app.client.chat_postMessage = AsyncMock(return_value={"ts": "ts1"})
        await adapter.send("C123", "hi", metadata={"thread_id": "parent_ts"})
        kwargs = adapter._app.client.chat_postMessage.call_args.kwargs
        assert kwargs.get("reply_broadcast") is True


# ---------------------------------------------------------------------------
# TestFallbackPreservesThreadContext
# ---------------------------------------------------------------------------


class TestFallbackPreservesThreadContext:
    """Bug fix: file upload fallbacks lost thread context (metadata) when
    calling super() without metadata, causing replies to appear outside
    the thread."""

    @pytest.mark.asyncio
    async def test_send_image_file_fallback_preserves_thread(self, adapter, tmp_path):
        test_file = tmp_path / "photo.jpg"
        test_file.write_bytes(b"\xff\xd8\xff\xe0")

        adapter._app.client.files_upload_v2 = AsyncMock(
            side_effect=Exception("upload failed")
        )
        adapter._app.client.chat_postMessage = AsyncMock(return_value={"ts": "msg_ts"})

        metadata = {"thread_id": "parent_ts_123"}
        await adapter.send_image_file(
            chat_id="C123",
            image_path=str(test_file),
            caption="test image",
            metadata=metadata,
        )

        call_kwargs = adapter._app.client.chat_postMessage.call_args.kwargs
        assert call_kwargs.get("thread_ts") == "parent_ts_123"

    @pytest.mark.asyncio
    async def test_send_video_fallback_preserves_thread(self, adapter, tmp_path):
        test_file = tmp_path / "clip.mp4"
        test_file.write_bytes(b"\x00\x00\x00\x1c")

        adapter._app.client.files_upload_v2 = AsyncMock(
            side_effect=Exception("upload failed")
        )
        adapter._app.client.chat_postMessage = AsyncMock(return_value={"ts": "msg_ts"})

        metadata = {"thread_id": "parent_ts_456"}
        await adapter.send_video(
            chat_id="C123",
            video_path=str(test_file),
            metadata=metadata,
        )

        call_kwargs = adapter._app.client.chat_postMessage.call_args.kwargs
        assert call_kwargs.get("thread_ts") == "parent_ts_456"

    @pytest.mark.asyncio
    async def test_send_document_fallback_preserves_thread(self, adapter, tmp_path):
        test_file = tmp_path / "report.pdf"
        test_file.write_bytes(b"%PDF-1.4")

        adapter._app.client.files_upload_v2 = AsyncMock(
            side_effect=Exception("upload failed")
        )
        adapter._app.client.chat_postMessage = AsyncMock(return_value={"ts": "msg_ts"})

        metadata = {"thread_id": "parent_ts_789"}
        await adapter.send_document(
            chat_id="C123",
            file_path=str(test_file),
            caption="report",
            metadata=metadata,
        )

        call_kwargs = adapter._app.client.chat_postMessage.call_args.kwargs
        assert call_kwargs.get("thread_ts") == "parent_ts_789"

    @pytest.mark.asyncio
    async def test_send_image_file_fallback_includes_caption(self, adapter, tmp_path):
        test_file = tmp_path / "photo.jpg"
        test_file.write_bytes(b"\xff\xd8\xff\xe0")

        adapter._app.client.files_upload_v2 = AsyncMock(
            side_effect=Exception("upload failed")
        )
        adapter._app.client.chat_postMessage = AsyncMock(return_value={"ts": "msg_ts"})

        await adapter.send_image_file(
            chat_id="C123",
            image_path=str(test_file),
            caption="important screenshot",
        )

        call_kwargs = adapter._app.client.chat_postMessage.call_args.kwargs
        assert "important screenshot" in call_kwargs["text"]


# ---------------------------------------------------------------------------
# TestSendImageSSRFGuards
# ---------------------------------------------------------------------------


class TestSendImageSSRFGuards:
    """send_image should reject redirects that land on private/internal hosts."""

    @pytest.mark.asyncio
    async def test_send_image_blocks_private_redirect_target(self, adapter):
        redirect_response = MagicMock()
        redirect_response.is_redirect = True
        redirect_response.next_request = MagicMock(
            url="http://169.254.169.254/latest/meta-data"
        )

        client_kwargs = {}
        mock_client = AsyncMock()
        mock_client.__aenter__ = AsyncMock(return_value=mock_client)
        mock_client.__aexit__ = AsyncMock(return_value=False)

        async def fake_get(_url):
            for hook in client_kwargs["event_hooks"]["response"]:
                await hook(redirect_response)

        mock_client.get = AsyncMock(side_effect=fake_get)
        adapter._app.client.files_upload_v2 = AsyncMock(return_value={"ok": True})
        adapter._app.client.chat_postMessage = AsyncMock(
            return_value={"ts": "reply_ts"}
        )

        def fake_async_client(*args, **kwargs):
            client_kwargs.update(kwargs)
            return mock_client

        def fake_is_safe_url(url):
            return url == "https://public.example/image.png"

        with (
            patch("tools.url_safety.is_safe_url", side_effect=fake_is_safe_url),
            patch("httpx.AsyncClient", side_effect=fake_async_client),
        ):
            result = await adapter.send_image(
                chat_id="C123",
                image_url="https://public.example/image.png",
                caption="see this",
            )

        assert result.success
        assert client_kwargs["follow_redirects"] is True
        assert client_kwargs["event_hooks"]["response"]
        adapter._app.client.files_upload_v2.assert_not_awaited()
        adapter._app.client.chat_postMessage.assert_awaited_once()
        call_kwargs = adapter._app.client.chat_postMessage.call_args.kwargs
        assert "see this" in call_kwargs["text"]
        assert "https://public.example/image.png" in call_kwargs["text"]

    @pytest.mark.asyncio
    async def test_send_image_fallback_preserves_thread_metadata(self, adapter):
        redirect_response = MagicMock()
        redirect_response.is_redirect = True
        redirect_response.next_request = MagicMock(
            url="http://169.254.169.254/latest/meta-data"
        )

        client_kwargs = {}
        mock_client = AsyncMock()
        mock_client.__aenter__ = AsyncMock(return_value=mock_client)
        mock_client.__aexit__ = AsyncMock(return_value=False)

        async def fake_get(_url):
            for hook in client_kwargs["event_hooks"]["response"]:
                await hook(redirect_response)

        mock_client.get = AsyncMock(side_effect=fake_get)
        adapter._app.client.files_upload_v2 = AsyncMock(return_value={"ok": True})
        adapter._app.client.chat_postMessage = AsyncMock(
            return_value={"ts": "reply_ts"}
        )

        def fake_async_client(*args, **kwargs):
            client_kwargs.update(kwargs)
            return mock_client

        def fake_is_safe_url(url):
            return url == "https://public.example/image.png"

        with (
            patch("tools.url_safety.is_safe_url", side_effect=fake_is_safe_url),
            patch("httpx.AsyncClient", side_effect=fake_async_client),
        ):
            await adapter.send_image(
                chat_id="C123",
                image_url="https://public.example/image.png",
                caption="see this",
                metadata={"thread_id": "parent_ts_789"},
            )

        call_kwargs = adapter._app.client.chat_postMessage.call_args.kwargs
        assert call_kwargs.get("thread_ts") == "parent_ts_789"


# ---------------------------------------------------------------------------
# TestProgressMessageThread
# ---------------------------------------------------------------------------


class TestProgressMessageThread:
    """Verify that progress messages go to the correct thread.

    Issue #2954: For Slack DM top-level messages, source.thread_id is None
    but the final reply is threaded under the user's message via reply_to.
    Progress messages must use the same thread anchor (the original message's
    ts) so they appear in the thread instead of the DM root.
    """

    @pytest.mark.asyncio
    async def test_dm_toplevel_progress_uses_message_ts_as_thread(self, adapter):
        """Progress messages for a top-level DM should go into the reply thread."""
        # Simulate a top-level DM: no thread_ts in the event
        event = {
            "channel": "D_DM",
            "channel_type": "im",
            "user": "U_USER",
            "text": "Hello bot",
            "ts": "1234567890.000001",
            # No thread_ts — this is a top-level DM
        }

        captured_events = []
        adapter.handle_message = AsyncMock(
            side_effect=lambda e: captured_events.append(e)
        )

        # Patch _resolve_user_name to avoid async Slack API call
        with patch.object(
            adapter, "_resolve_user_name", new=AsyncMock(return_value="testuser")
        ):
            await adapter._handle_slack_message(event)

        assert len(captured_events) == 1
        msg_event = captured_events[0]
        source = msg_event.source

        # With default dm_top_level_threads_as_sessions=True, source.thread_id
        # should equal the message ts so each DM thread gets its own session.
        assert source.thread_id == "1234567890.000001", (
            "source.thread_id must equal the message ts for top-level DMs "
            "so each reply thread gets its own session"
        )

        # The message_id should be the event's ts — this is what the gateway
        # passes as event_message_id so progress messages can thread correctly
        assert msg_event.message_id == "1234567890.000001", (
            "message_id must equal the event ts so _run_agent can use it as "
            "the fallback thread anchor for progress messages"
        )

        # Verify that the Slack send() method correctly threads a message
        # when metadata contains thread_id equal to the original ts
        adapter._app.client.chat_postMessage = AsyncMock(
            return_value={"ts": "reply_ts"}
        )
        result = await adapter.send(
            chat_id="D_DM",
            content="⚙️ working...",
            metadata={"thread_id": msg_event.message_id},
        )
        assert result.success
        call_kwargs = adapter._app.client.chat_postMessage.call_args[1]
        assert call_kwargs.get("thread_ts") == "1234567890.000001", (
            "send() must pass thread_ts when metadata has thread_id, "
            "ensuring progress messages land in the thread"
        )

    @pytest.mark.asyncio
    async def test_dm_toplevel_shares_session_when_disabled(self, adapter):
        """Opting out restores legacy single-session-per-DM-channel behavior."""
        adapter.config.extra["dm_top_level_threads_as_sessions"] = False

        event = {
            "channel": "D_DM",
            "channel_type": "im",
            "user": "U_USER",
            "text": "Hello bot",
            "ts": "1234567890.000001",
        }

        captured_events = []
        adapter.handle_message = AsyncMock(
            side_effect=lambda e: captured_events.append(e)
        )

        with patch.object(
            adapter, "_resolve_user_name", new=AsyncMock(return_value="testuser")
        ):
            await adapter._handle_slack_message(event)

        assert len(captured_events) == 1
        msg_event = captured_events[0]
        source = msg_event.source

        assert source.thread_id is None, (
            "source.thread_id must stay None when "
            "dm_top_level_threads_as_sessions is disabled"
        )

    @pytest.mark.asyncio
    async def test_channel_mention_progress_uses_thread_ts(self, adapter):
        """Progress messages for a channel @mention should go into the reply thread."""
        # Simulate an @mention in a channel: the event ts becomes the thread anchor
        event = {
            "channel": "C_CHAN",
            "channel_type": "channel",
            "user": "U_USER",
            "text": f"<@U_BOT> help me",
            "ts": "2000000000.000001",
            # No thread_ts — top-level channel message
        }

        captured_events = []
        adapter.handle_message = AsyncMock(
            side_effect=lambda e: captured_events.append(e)
        )

        with patch.object(
            adapter, "_resolve_user_name", new=AsyncMock(return_value="testuser")
        ):
            await adapter._handle_slack_message(event)

        assert len(captured_events) == 1
        msg_event = captured_events[0]
        source = msg_event.source

        # For channel @mention: thread_id should equal the event ts (fallback)
        assert source.thread_id == "2000000000.000001", (
            "source.thread_id must equal the event ts for channel messages "
            "so each @mention starts its own thread"
        )
        assert msg_event.message_id == "2000000000.000001"


class TestSlackReplyToText:
    """Ensure MessageEvent.reply_to_text is populated on thread replies so
    gateway.run can inject a ``[Replying to: "..."]`` prefix (parity with
    Telegram/Discord/Feishu/WeCom)."""

    @pytest.mark.asyncio
    async def test_slack_reply_to_text_set_on_thread_reply(self, adapter):
        """When a thread reply arrives and the parent was posted by a bot
        (e.g. cron summary), reply_to_text must carry the parent's text."""
        adapter._channel_team = {}  # primary workspace only
        adapter._team_bot_user_ids = {}

        # Mock conversations_replies to return a bot-posted parent
        adapter._app.client.conversations_replies = AsyncMock(
            return_value={
                "messages": [
                    {
                        "ts": "1000.0",
                        "bot_id": "B_CRON",
                        "text": "メール要約: 新着メール3件あります",
                    },
                    {"ts": "1000.5", "user": "U_USER", "text": "詳細を教えて"},
                ]
            }
        )

        # Use a DM so mention-gating doesn't short-circuit the handler.
        event = {
            "text": "詳細を教えて",
            "user": "U_USER",
            "channel": "D123",
            "channel_type": "im",
            "ts": "1000.5",
            "thread_ts": "1000.0",  # thread reply
        }

        with patch.object(
            adapter, "_resolve_user_name", new=AsyncMock(return_value="Alice")
        ):
            await adapter._handle_slack_message(event)

        assert (
            adapter.handle_message.call_args is not None
        ), "handle_message must be invoked for thread-reply DM"
        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.reply_to_message_id == "1000.0"
        # The critical assertion: parent text is exposed as reply_to_text so the
        # gateway can inject it when not already in the session history.
        assert msg_event.reply_to_text is not None
        assert "メール要約" in msg_event.reply_to_text

    @pytest.mark.asyncio
    async def test_slack_reply_to_text_none_for_top_level_message(self, adapter):
        """Top-level messages (no thread_ts) must not set reply_to_text."""
        event = {
            "text": "hello",
            "user": "U_USER",
            "channel": "D123",
            "channel_type": "im",
            "ts": "1000.0",
            # no thread_ts — top-level DM
        }

        with patch.object(
            adapter, "_resolve_user_name", new=AsyncMock(return_value="Alice")
        ):
            await adapter._handle_slack_message(event)

        assert adapter.handle_message.call_args is not None
        msg_event = adapter.handle_message.call_args[0][0]
        assert msg_event.reply_to_text is None
        # Top-level message: reply_to_message_id must be falsy (None or empty).
        assert not msg_event.reply_to_message_id


# ---------------------------------------------------------------------------
# Slash-command ephemeral ack and routing (#18182)
# ---------------------------------------------------------------------------


class TestSlashEphemeralAck:
    """Slash commands should produce an ephemeral ack and route replies ephemerally."""

    @pytest.mark.asyncio
    async def test_slash_command_stashes_response_url(self, adapter):
        """_handle_slash_command stashes response_url for later ephemeral routing."""
        command = {
            "command": "/q",
            "text": "follow-up question",
            "user_id": "U_SLASH",
            "channel_id": "C_SLASH",
            "response_url": "https://hooks.slack.com/commands/T123/456/abc",
        }
        await adapter._handle_slash_command(command)

        # The context should be stashed under (channel_id, user_id).
        key = ("C_SLASH", "U_SLASH")
        assert key in adapter._slash_command_contexts
        ctx = adapter._slash_command_contexts[key]
        assert ctx["response_url"] == "https://hooks.slack.com/commands/T123/456/abc"
        assert "ts" in ctx

    @pytest.mark.asyncio
    async def test_slash_command_without_response_url_does_not_stash(self, adapter):
        """Commands without a response_url should not create a context."""
        command = {
            "command": "/stop",
            "text": "",
            "user_id": "U1",
            "channel_id": "C1",
            # no response_url
        }
        await adapter._handle_slash_command(command)
        assert len(adapter._slash_command_contexts) == 0

    @pytest.mark.asyncio
    async def test_pop_slash_context_returns_and_removes(self, adapter):
        """_pop_slash_context returns the context and removes it."""
        import time

        adapter._slash_command_contexts[("C1", "U1")] = {
            "response_url": "https://hooks.slack.com/test",
            "ts": time.monotonic(),
        }

        ctx = adapter._pop_slash_context("C1")
        assert ctx is not None
        assert ctx["response_url"] == "https://hooks.slack.com/test"
        # Must be removed after pop
        assert len(adapter._slash_command_contexts) == 0

    @pytest.mark.asyncio
    async def test_pop_slash_context_returns_none_for_no_match(self, adapter):
        """_pop_slash_context returns None when no context exists."""
        ctx = adapter._pop_slash_context("C_NONEXISTENT")
        assert ctx is None

    @pytest.mark.asyncio
    async def test_pop_slash_context_discards_stale_entries(self, adapter):
        """Stale contexts older than TTL are cleaned up."""
        import time

        adapter._slash_command_contexts[("C1", "U1")] = {
            "response_url": "https://hooks.slack.com/stale",
            "ts": time.monotonic() - adapter._SLASH_CTX_TTL - 1,
        }

        ctx = adapter._pop_slash_context("C1")
        assert ctx is None
        assert len(adapter._slash_command_contexts) == 0

    @pytest.mark.asyncio
    async def test_send_uses_response_url_when_context_exists(self, adapter):
        """send() should POST to response_url for slash command replies."""
        import time

        adapter._slash_command_contexts[("C_SLASH", "U_SLASH")] = {
            "response_url": "https://hooks.slack.com/commands/T123/456/abc",
            "ts": time.monotonic(),
        }

        mock_resp = AsyncMock()
        mock_resp.status = 200
        mock_resp.__aenter__ = AsyncMock(return_value=mock_resp)
        mock_resp.__aexit__ = AsyncMock(return_value=False)

        mock_session = AsyncMock()
        mock_session.post = MagicMock(return_value=mock_resp)
        mock_session.__aenter__ = AsyncMock(return_value=mock_session)
        mock_session.__aexit__ = AsyncMock(return_value=False)

        with patch(
            "plugins.platforms.slack.adapter.aiohttp.ClientSession", return_value=mock_session
        ):
            result = await adapter.send("C_SLASH", "Queued for the next turn.")

        assert result.success is True
        # Verify response_url was POSTed to
        mock_session.post.assert_called_once()
        call_args = mock_session.post.call_args
        assert call_args[0][0] == "https://hooks.slack.com/commands/T123/456/abc"
        payload = call_args[1]["json"]
        assert payload["response_type"] == "ephemeral"
        assert payload["replace_original"] is True
        assert "Queued for the next turn" in payload["text"]

        # Context must be consumed
        assert len(adapter._slash_command_contexts) == 0

    @pytest.mark.asyncio
    async def test_send_falls_through_without_context(self, adapter):
        """send() should use normal chat_postMessage when no slash context exists."""
        mock_result = {"ts": "1234.5678", "ok": True}
        adapter._app.client.chat_postMessage = AsyncMock(return_value=mock_result)

        result = await adapter.send("C_NORMAL", "Hello world")

        assert result.success is True
        adapter._app.client.chat_postMessage.assert_called_once()

    @pytest.mark.asyncio
    async def test_send_slash_ephemeral_fallback_on_post_failure(self, adapter):
        """_send_slash_ephemeral returns success=True even if POST fails."""
        import time

        adapter._slash_command_contexts[("C1", "U1")] = {
            "response_url": "https://hooks.slack.com/commands/bad",
            "ts": time.monotonic(),
        }

        mock_resp = AsyncMock()
        mock_resp.status = 500
        mock_resp.text = AsyncMock(return_value="Internal Server Error")
        mock_resp.__aenter__ = AsyncMock(return_value=mock_resp)
        mock_resp.__aexit__ = AsyncMock(return_value=False)

        mock_session = AsyncMock()
        mock_session.post = MagicMock(return_value=mock_resp)
        mock_session.__aenter__ = AsyncMock(return_value=mock_session)
        mock_session.__aexit__ = AsyncMock(return_value=False)

        with patch(
            "plugins.platforms.slack.adapter.aiohttp.ClientSession", return_value=mock_session
        ):
            result = await adapter.send("C1", "Some response")

        # Still success — the user saw the initial ack already
        assert result.success is True

    @pytest.mark.asyncio
    async def test_send_slash_ephemeral_fallback_on_exception(self, adapter):
        """_send_slash_ephemeral returns success=True even if aiohttp raises."""
        import time

        adapter._slash_command_contexts[("C1", "U1")] = {
            "response_url": "https://hooks.slack.com/commands/timeout",
            "ts": time.monotonic(),
        }

        mock_session = AsyncMock()
        mock_session.post = MagicMock(side_effect=Exception("connection timeout"))
        mock_session.__aenter__ = AsyncMock(return_value=mock_session)
        mock_session.__aexit__ = AsyncMock(return_value=False)

        with patch(
            "plugins.platforms.slack.adapter.aiohttp.ClientSession", return_value=mock_session
        ):
            result = await adapter.send("C1", "Some response")

        assert result.success is True

    @pytest.mark.asyncio
    async def test_native_slash_stashes_context_and_dispatches(self, adapter):
        """Full flow: native /q slash → stash + handle_message dispatch."""
        command = {
            "command": "/q",
            "text": "do something",
            "user_id": "U_Q",
            "channel_id": "C_Q",
            "response_url": "https://hooks.slack.com/commands/T1/2/q",
        }
        await adapter._handle_slash_command(command)

        # 1. handle_message was called with the right event
        adapter.handle_message.assert_called_once()
        event = adapter.handle_message.call_args[0][0]
        assert event.text == "/q do something"
        assert event.message_type == MessageType.COMMAND

        # 2. Context stashed for ephemeral routing
        assert ("C_Q", "U_Q") in adapter._slash_command_contexts

    @pytest.mark.asyncio
    async def test_legacy_hermes_slash_stashes_context(self, adapter):
        """Legacy /hermes <subcommand> also stashes context."""
        command = {
            "command": "/hermes",
            "text": "help",
            "user_id": "U_H",
            "channel_id": "C_H",
            "response_url": "https://hooks.slack.com/commands/T1/3/h",
        }
        await adapter._handle_slash_command(command)

        adapter.handle_message.assert_called_once()
        assert ("C_H", "U_H") in adapter._slash_command_contexts

    @pytest.mark.asyncio
    async def test_freeform_hermes_question_does_not_stash_context(self, adapter):
        """Free-form /hermes <question> must NOT route agent reply ephemeral."""
        command = {
            "command": "/hermes",
            "text": "what's the weather",
            "user_id": "U_FREE",
            "channel_id": "C_FREE",
            "response_url": "https://hooks.slack.com/commands/T1/4/free",
        }
        await adapter._handle_slash_command(command)

        adapter.handle_message.assert_called_once()
        event = adapter.handle_message.call_args[0][0]
        # Free-form text — not a command
        assert event.message_type == MessageType.TEXT
        assert event.text == "what's the weather"
        # Context must NOT be stashed — agent reply should be public
        assert len(adapter._slash_command_contexts) == 0

    @pytest.mark.asyncio
    async def test_concurrent_users_same_channel_isolates_contexts(self, adapter):
        """Two users slash on the same channel — each gets their own context."""
        import time
        from plugins.platforms.slack.adapter import _slash_user_id

        # Simulate two users stashing contexts on the same channel.
        adapter._slash_command_contexts[("C_SHARED", "U_ALICE")] = {
            "response_url": "https://hooks.slack.com/alice",
            "ts": time.monotonic(),
        }
        adapter._slash_command_contexts[("C_SHARED", "U_BOB")] = {
            "response_url": "https://hooks.slack.com/bob",
            "ts": time.monotonic(),
        }

        # Alice's send() — ContextVar set to Alice's user_id.
        token = _slash_user_id.set("U_ALICE")
        try:
            ctx = adapter._pop_slash_context("C_SHARED")
        finally:
            _slash_user_id.reset(token)

        assert ctx is not None
        assert ctx["response_url"] == "https://hooks.slack.com/alice"
        # Bob's context must still be there.
        assert ("C_SHARED", "U_BOB") in adapter._slash_command_contexts
        assert len(adapter._slash_command_contexts) == 1

        # Bob's send() — ContextVar set to Bob's user_id.
        token = _slash_user_id.set("U_BOB")
        try:
            ctx = adapter._pop_slash_context("C_SHARED")
        finally:
            _slash_user_id.reset(token)

        assert ctx is not None
        assert ctx["response_url"] == "https://hooks.slack.com/bob"
        assert len(adapter._slash_command_contexts) == 0

    @pytest.mark.asyncio
    async def test_no_contextvar_does_not_match_any_context(self, adapter):
        """send() without ContextVar (non-slash path) must not steal contexts."""
        import time
        from plugins.platforms.slack.adapter import _slash_user_id

        adapter._slash_command_contexts[("C1", "U1")] = {
            "response_url": "https://hooks.slack.com/test",
            "ts": time.monotonic(),
        }

        # ContextVar is unset (default=None) — simulates a normal message send.
        assert _slash_user_id.get() is None
        ctx = adapter._pop_slash_context("C1")
        # Fallback scan still finds it (channel-only) — this is fine for
        # the normal single-user case; the ContextVar path is the precise one.
        # The key invariant is: when the ContextVar IS set, it matches exactly.
        assert ctx is not None  # fallback path finds the entry
