feat(gateway): approval buttons for Slack & Telegram + Slack thread context (#5890)
Slack: - Add Block Kit interactive buttons for command approval (Allow Once, Allow Session, Always Allow, Deny) via send_exec_approval() - Register @app.action handlers for each approval button - Add _fetch_thread_context() — fetches thread history via conversations.replies when bot is first @mentioned mid-thread - Fix _has_active_session_for_thread() to use build_session_key() instead of manual key construction (fixes session key mismatch bug where thread_sessions_per_user flag was ignored, ref PR #5833) Telegram: - Add InlineKeyboard approval buttons via send_exec_approval() - Add ea:* callback handling in _handle_callback_query() - Uses monotonic counter + _approval_state dict to map button clicks back to session keys (avoids 64-byte callback_data limit) Both platforms now auto-detected by the gateway runner's _approval_notify_sync() — any adapter with send_exec_approval() on its class gets button-based approval instead of text fallback. Inspired by community PRs #3898 (LevSky22), #2953 (ygd58), #5833 (heathley). Implemented fresh on current main. Tests: 24 new tests covering button rendering, action handling, thread context fetching, session key fix, double-click prevention.
This commit is contained in:
373
tests/gateway/test_slack_approval_buttons.py
Normal file
373
tests/gateway/test_slack_approval_buttons.py
Normal file
@@ -0,0 +1,373 @@
|
||||
"""Tests for Slack Block Kit approval buttons and thread context fetching."""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Ensure the repo root is importable
|
||||
# ---------------------------------------------------------------------------
|
||||
_repo = str(Path(__file__).resolve().parents[2])
|
||||
if _repo not in sys.path:
|
||||
sys.path.insert(0, _repo)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Minimal Slack SDK mock so SlackAdapter can be imported
|
||||
# ---------------------------------------------------------------------------
|
||||
def _ensure_slack_mock():
|
||||
"""Wire up the minimal mocks required to import SlackAdapter."""
|
||||
if "slack_bolt" in sys.modules:
|
||||
return
|
||||
slack_bolt = MagicMock()
|
||||
slack_bolt.async_app.AsyncApp = MagicMock
|
||||
sys.modules["slack_bolt"] = slack_bolt
|
||||
sys.modules["slack_bolt.async_app"] = slack_bolt.async_app
|
||||
handler_mod = MagicMock()
|
||||
handler_mod.AsyncSocketModeHandler = MagicMock
|
||||
sys.modules["slack_bolt.adapter"] = MagicMock()
|
||||
sys.modules["slack_bolt.adapter.socket_mode"] = MagicMock()
|
||||
sys.modules["slack_bolt.adapter.socket_mode.async_handler"] = handler_mod
|
||||
sdk_mod = MagicMock()
|
||||
sdk_mod.web = MagicMock()
|
||||
sdk_mod.web.async_client = MagicMock()
|
||||
sdk_mod.web.async_client.AsyncWebClient = MagicMock
|
||||
sys.modules["slack_sdk"] = sdk_mod
|
||||
sys.modules["slack_sdk.web"] = sdk_mod.web
|
||||
sys.modules["slack_sdk.web.async_client"] = sdk_mod.web.async_client
|
||||
|
||||
|
||||
_ensure_slack_mock()
|
||||
|
||||
from gateway.platforms.slack import SlackAdapter
|
||||
from gateway.config import Platform, PlatformConfig
|
||||
|
||||
|
||||
def _make_adapter():
|
||||
"""Create a SlackAdapter instance with mocked internals."""
|
||||
config = PlatformConfig(enabled=True, token="xoxb-test-token")
|
||||
adapter = SlackAdapter(config)
|
||||
adapter._app = MagicMock()
|
||||
adapter._bot_user_id = "U_BOT"
|
||||
adapter._team_clients = {"T1": AsyncMock()}
|
||||
adapter._team_bot_user_ids = {"T1": "U_BOT"}
|
||||
adapter._channel_team = {"C1": "T1"}
|
||||
return adapter
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# send_exec_approval — Block Kit buttons
|
||||
# ===========================================================================
|
||||
|
||||
class TestSlackExecApproval:
|
||||
"""Test the send_exec_approval method sends Block Kit buttons."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sends_blocks_with_buttons(self):
|
||||
adapter = _make_adapter()
|
||||
mock_client = adapter._team_clients["T1"]
|
||||
mock_client.chat_postMessage = AsyncMock(return_value={"ts": "1234.5678"})
|
||||
|
||||
result = await adapter.send_exec_approval(
|
||||
chat_id="C1",
|
||||
command="rm -rf /important",
|
||||
session_key="agent:main:slack:group:C1:1111",
|
||||
description="dangerous deletion",
|
||||
)
|
||||
|
||||
assert result.success is True
|
||||
assert result.message_id == "1234.5678"
|
||||
|
||||
# Verify chat_postMessage was called with blocks
|
||||
mock_client.chat_postMessage.assert_called_once()
|
||||
kwargs = mock_client.chat_postMessage.call_args[1]
|
||||
assert "blocks" in kwargs
|
||||
blocks = kwargs["blocks"]
|
||||
assert len(blocks) == 2
|
||||
assert blocks[0]["type"] == "section"
|
||||
assert "rm -rf /important" in blocks[0]["text"]["text"]
|
||||
assert "dangerous deletion" in blocks[0]["text"]["text"]
|
||||
assert blocks[1]["type"] == "actions"
|
||||
elements = blocks[1]["elements"]
|
||||
assert len(elements) == 4
|
||||
action_ids = [e["action_id"] for e in elements]
|
||||
assert "hermes_approve_once" in action_ids
|
||||
assert "hermes_approve_session" in action_ids
|
||||
assert "hermes_approve_always" in action_ids
|
||||
assert "hermes_deny" in action_ids
|
||||
# Each button carries the session key as value
|
||||
for e in elements:
|
||||
assert e["value"] == "agent:main:slack:group:C1:1111"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sends_in_thread(self):
|
||||
adapter = _make_adapter()
|
||||
mock_client = adapter._team_clients["T1"]
|
||||
mock_client.chat_postMessage = AsyncMock(return_value={"ts": "1234.5678"})
|
||||
|
||||
await adapter.send_exec_approval(
|
||||
chat_id="C1",
|
||||
command="echo test",
|
||||
session_key="test-session",
|
||||
metadata={"thread_id": "9999.0000"},
|
||||
)
|
||||
|
||||
kwargs = mock_client.chat_postMessage.call_args[1]
|
||||
assert kwargs.get("thread_ts") == "9999.0000"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_not_connected(self):
|
||||
adapter = _make_adapter()
|
||||
adapter._app = None
|
||||
result = await adapter.send_exec_approval(
|
||||
chat_id="C1", command="ls", session_key="s"
|
||||
)
|
||||
assert result.success is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_truncates_long_command(self):
|
||||
adapter = _make_adapter()
|
||||
mock_client = adapter._team_clients["T1"]
|
||||
mock_client.chat_postMessage = AsyncMock(return_value={"ts": "1.2"})
|
||||
|
||||
long_cmd = "x" * 5000
|
||||
await adapter.send_exec_approval(
|
||||
chat_id="C1", command=long_cmd, session_key="s"
|
||||
)
|
||||
|
||||
kwargs = mock_client.chat_postMessage.call_args[1]
|
||||
section_text = kwargs["blocks"][0]["text"]["text"]
|
||||
assert "..." in section_text
|
||||
assert len(section_text) < 5000
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# _handle_approval_action — button click handler
|
||||
# ===========================================================================
|
||||
|
||||
class TestSlackApprovalAction:
|
||||
"""Test the approval button click handler."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_resolves_approval(self):
|
||||
adapter = _make_adapter()
|
||||
adapter._approval_resolved["1234.5678"] = False
|
||||
|
||||
ack = AsyncMock()
|
||||
body = {
|
||||
"message": {
|
||||
"ts": "1234.5678",
|
||||
"blocks": [
|
||||
{"type": "section", "text": {"type": "mrkdwn", "text": "original text"}},
|
||||
{"type": "actions", "elements": []},
|
||||
],
|
||||
},
|
||||
"channel": {"id": "C1"},
|
||||
"user": {"name": "norbert"},
|
||||
}
|
||||
action = {
|
||||
"action_id": "hermes_approve_once",
|
||||
"value": "agent:main:slack:group:C1:1111",
|
||||
}
|
||||
|
||||
mock_client = adapter._team_clients["T1"]
|
||||
mock_client.chat_update = AsyncMock()
|
||||
|
||||
with patch("tools.approval.resolve_gateway_approval", return_value=1) as mock_resolve:
|
||||
await adapter._handle_approval_action(ack, body, action)
|
||||
|
||||
ack.assert_called_once()
|
||||
mock_resolve.assert_called_once_with("agent:main:slack:group:C1:1111", "once")
|
||||
|
||||
# Message should be updated with decision
|
||||
mock_client.chat_update.assert_called_once()
|
||||
update_kwargs = mock_client.chat_update.call_args[1]
|
||||
assert "Approved once by norbert" in update_kwargs["text"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_prevents_double_click(self):
|
||||
adapter = _make_adapter()
|
||||
adapter._approval_resolved["1234.5678"] = True # Already resolved
|
||||
|
||||
ack = AsyncMock()
|
||||
body = {
|
||||
"message": {"ts": "1234.5678", "blocks": []},
|
||||
"channel": {"id": "C1"},
|
||||
"user": {"name": "norbert"},
|
||||
}
|
||||
action = {
|
||||
"action_id": "hermes_approve_once",
|
||||
"value": "some-session",
|
||||
}
|
||||
|
||||
with patch("tools.approval.resolve_gateway_approval") as mock_resolve:
|
||||
await adapter._handle_approval_action(ack, body, action)
|
||||
|
||||
# Should have acked but NOT resolved
|
||||
ack.assert_called_once()
|
||||
mock_resolve.assert_not_called()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_deny_action(self):
|
||||
adapter = _make_adapter()
|
||||
adapter._approval_resolved["1.2"] = False
|
||||
|
||||
ack = AsyncMock()
|
||||
body = {
|
||||
"message": {"ts": "1.2", "blocks": [
|
||||
{"type": "section", "text": {"type": "mrkdwn", "text": "cmd"}},
|
||||
]},
|
||||
"channel": {"id": "C1"},
|
||||
"user": {"name": "alice"},
|
||||
}
|
||||
action = {"action_id": "hermes_deny", "value": "session-key"}
|
||||
|
||||
mock_client = adapter._team_clients["T1"]
|
||||
mock_client.chat_update = AsyncMock()
|
||||
|
||||
with patch("tools.approval.resolve_gateway_approval", return_value=1) as mock_resolve:
|
||||
await adapter._handle_approval_action(ack, body, action)
|
||||
|
||||
mock_resolve.assert_called_once_with("session-key", "deny")
|
||||
update_kwargs = mock_client.chat_update.call_args[1]
|
||||
assert "Denied by alice" in update_kwargs["text"]
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# _fetch_thread_context
|
||||
# ===========================================================================
|
||||
|
||||
class TestSlackThreadContext:
|
||||
"""Test thread context fetching."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fetches_and_formats_context(self):
|
||||
adapter = _make_adapter()
|
||||
mock_client = adapter._team_clients["T1"]
|
||||
mock_client.conversations_replies = AsyncMock(return_value={
|
||||
"messages": [
|
||||
{"ts": "1000.0", "user": "U1", "text": "This is the parent message"},
|
||||
{"ts": "1000.1", "user": "U2", "text": "I think we should refactor"},
|
||||
{"ts": "1000.2", "user": "U1", "text": "Good idea, <@U_BOT> what do you think?"},
|
||||
]
|
||||
})
|
||||
|
||||
# Mock user name resolution
|
||||
adapter._user_name_cache = {"U1": "Alice", "U2": "Bob"}
|
||||
|
||||
context = await adapter._fetch_thread_context(
|
||||
channel_id="C1",
|
||||
thread_ts="1000.0",
|
||||
current_ts="1000.2", # The message that triggered the fetch
|
||||
team_id="T1",
|
||||
)
|
||||
|
||||
assert "[Thread context" in context
|
||||
assert "[thread parent] Alice: This is the parent message" in context
|
||||
assert "Bob: I think we should refactor" in context
|
||||
# Current message should be excluded
|
||||
assert "what do you think" not in context
|
||||
# Bot mention should be stripped from context
|
||||
assert "<@U_BOT>" not in context
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_skips_bot_messages(self):
|
||||
adapter = _make_adapter()
|
||||
mock_client = adapter._team_clients["T1"]
|
||||
mock_client.conversations_replies = AsyncMock(return_value={
|
||||
"messages": [
|
||||
{"ts": "1000.0", "user": "U1", "text": "Parent"},
|
||||
{"ts": "1000.1", "bot_id": "B1", "text": "Bot reply (should be skipped)"},
|
||||
{"ts": "1000.2", "user": "U1", "text": "Current"},
|
||||
]
|
||||
})
|
||||
adapter._user_name_cache = {"U1": "Alice"}
|
||||
|
||||
context = await adapter._fetch_thread_context(
|
||||
channel_id="C1", thread_ts="1000.0", current_ts="1000.2", team_id="T1"
|
||||
)
|
||||
|
||||
assert "Bot reply" not in context
|
||||
assert "Alice: Parent" in context
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_empty_thread(self):
|
||||
adapter = _make_adapter()
|
||||
mock_client = adapter._team_clients["T1"]
|
||||
mock_client.conversations_replies = AsyncMock(return_value={"messages": []})
|
||||
|
||||
context = await adapter._fetch_thread_context(
|
||||
channel_id="C1", thread_ts="1000.0", current_ts="1000.1", team_id="T1"
|
||||
)
|
||||
assert context == ""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_failure_returns_empty(self):
|
||||
adapter = _make_adapter()
|
||||
mock_client = adapter._team_clients["T1"]
|
||||
mock_client.conversations_replies = AsyncMock(side_effect=Exception("API error"))
|
||||
|
||||
context = await adapter._fetch_thread_context(
|
||||
channel_id="C1", thread_ts="1000.0", current_ts="1000.1", team_id="T1"
|
||||
)
|
||||
assert context == ""
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# _has_active_session_for_thread — session key fix (#5833)
|
||||
# ===========================================================================
|
||||
|
||||
class TestSessionKeyFix:
|
||||
"""Test that _has_active_session_for_thread uses build_session_key."""
|
||||
|
||||
def test_uses_build_session_key(self):
|
||||
"""Verify the fix uses build_session_key instead of manual key construction."""
|
||||
adapter = _make_adapter()
|
||||
|
||||
# Mock session store with a known entry
|
||||
mock_store = MagicMock()
|
||||
mock_store._entries = {
|
||||
"agent:main:slack:group:C1:1000.0": MagicMock()
|
||||
}
|
||||
mock_store._ensure_loaded = MagicMock()
|
||||
mock_store.config = MagicMock()
|
||||
mock_store.config.group_sessions_per_user = False # threads don't include user_id
|
||||
mock_store.config.thread_sessions_per_user = False
|
||||
adapter._session_store = mock_store
|
||||
|
||||
# With the fix, build_session_key should be called which respects
|
||||
# group_sessions_per_user=False (no user_id appended)
|
||||
result = adapter._has_active_session_for_thread(
|
||||
channel_id="C1", thread_ts="1000.0", user_id="U123"
|
||||
)
|
||||
|
||||
# Should find the session because build_session_key with
|
||||
# group_sessions_per_user=False doesn't append user_id
|
||||
assert result is True
|
||||
|
||||
def test_no_session_returns_false(self):
|
||||
adapter = _make_adapter()
|
||||
mock_store = MagicMock()
|
||||
mock_store._entries = {}
|
||||
mock_store._ensure_loaded = MagicMock()
|
||||
mock_store.config = MagicMock()
|
||||
mock_store.config.group_sessions_per_user = True
|
||||
mock_store.config.thread_sessions_per_user = False
|
||||
adapter._session_store = mock_store
|
||||
|
||||
result = adapter._has_active_session_for_thread(
|
||||
channel_id="C1", thread_ts="1000.0", user_id="U123"
|
||||
)
|
||||
assert result is False
|
||||
|
||||
def test_no_session_store(self):
|
||||
adapter = _make_adapter()
|
||||
# No _session_store attribute
|
||||
result = adapter._has_active_session_for_thread(
|
||||
channel_id="C1", thread_ts="1000.0", user_id="U123"
|
||||
)
|
||||
assert result is False
|
||||
284
tests/gateway/test_telegram_approval_buttons.py
Normal file
284
tests/gateway/test_telegram_approval_buttons.py
Normal file
@@ -0,0 +1,284 @@
|
||||
"""Tests for Telegram inline keyboard approval buttons."""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Ensure the repo root is importable
|
||||
# ---------------------------------------------------------------------------
|
||||
_repo = str(Path(__file__).resolve().parents[2])
|
||||
if _repo not in sys.path:
|
||||
sys.path.insert(0, _repo)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Minimal Telegram mock so TelegramAdapter can be imported
|
||||
# ---------------------------------------------------------------------------
|
||||
def _ensure_telegram_mock():
|
||||
"""Wire up the minimal mocks required to import TelegramAdapter."""
|
||||
if "telegram" in sys.modules and hasattr(sys.modules["telegram"], "__file__"):
|
||||
return
|
||||
|
||||
mod = MagicMock()
|
||||
mod.ext.ContextTypes.DEFAULT_TYPE = type(None)
|
||||
mod.constants.ParseMode.MARKDOWN = "Markdown"
|
||||
mod.constants.ParseMode.MARKDOWN_V2 = "MarkdownV2"
|
||||
mod.constants.ParseMode.HTML = "HTML"
|
||||
mod.constants.ChatType.PRIVATE = "private"
|
||||
mod.constants.ChatType.GROUP = "group"
|
||||
mod.constants.ChatType.SUPERGROUP = "supergroup"
|
||||
mod.constants.ChatType.CHANNEL = "channel"
|
||||
for name in ("telegram", "telegram.ext", "telegram.constants", "telegram.request", "telegram.error"):
|
||||
sys.modules.setdefault(name, mod)
|
||||
|
||||
|
||||
_ensure_telegram_mock()
|
||||
|
||||
from gateway.platforms.telegram import TelegramAdapter
|
||||
from gateway.config import Platform, PlatformConfig
|
||||
|
||||
|
||||
def _make_adapter():
|
||||
"""Create a TelegramAdapter with mocked internals."""
|
||||
config = PlatformConfig(enabled=True, token="test-token")
|
||||
adapter = TelegramAdapter(config)
|
||||
adapter._bot = AsyncMock()
|
||||
adapter._app = MagicMock()
|
||||
return adapter
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# send_exec_approval — inline keyboard buttons
|
||||
# ===========================================================================
|
||||
|
||||
class TestTelegramExecApproval:
|
||||
"""Test the send_exec_approval method sends InlineKeyboard buttons."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sends_inline_keyboard(self):
|
||||
adapter = _make_adapter()
|
||||
mock_msg = MagicMock()
|
||||
mock_msg.message_id = 42
|
||||
adapter._bot.send_message = AsyncMock(return_value=mock_msg)
|
||||
|
||||
result = await adapter.send_exec_approval(
|
||||
chat_id="12345",
|
||||
command="rm -rf /important",
|
||||
session_key="agent:main:telegram:group:12345:99",
|
||||
description="dangerous deletion",
|
||||
)
|
||||
|
||||
assert result.success is True
|
||||
assert result.message_id == "42"
|
||||
|
||||
adapter._bot.send_message.assert_called_once()
|
||||
kwargs = adapter._bot.send_message.call_args[1]
|
||||
assert kwargs["chat_id"] == 12345
|
||||
assert "rm -rf /important" in kwargs["text"]
|
||||
assert "dangerous deletion" in kwargs["text"]
|
||||
assert kwargs["reply_markup"] is not None # InlineKeyboardMarkup
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stores_approval_state(self):
|
||||
adapter = _make_adapter()
|
||||
mock_msg = MagicMock()
|
||||
mock_msg.message_id = 42
|
||||
adapter._bot.send_message = AsyncMock(return_value=mock_msg)
|
||||
|
||||
await adapter.send_exec_approval(
|
||||
chat_id="12345",
|
||||
command="echo test",
|
||||
session_key="my-session-key",
|
||||
)
|
||||
|
||||
# The approval_id should map to the session_key
|
||||
assert len(adapter._approval_state) == 1
|
||||
approval_id = list(adapter._approval_state.keys())[0]
|
||||
assert adapter._approval_state[approval_id] == "my-session-key"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sends_in_thread(self):
|
||||
adapter = _make_adapter()
|
||||
mock_msg = MagicMock()
|
||||
mock_msg.message_id = 42
|
||||
adapter._bot.send_message = AsyncMock(return_value=mock_msg)
|
||||
|
||||
await adapter.send_exec_approval(
|
||||
chat_id="12345",
|
||||
command="ls",
|
||||
session_key="s",
|
||||
metadata={"thread_id": "999"},
|
||||
)
|
||||
|
||||
kwargs = adapter._bot.send_message.call_args[1]
|
||||
assert kwargs.get("message_thread_id") == 999
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_not_connected(self):
|
||||
adapter = _make_adapter()
|
||||
adapter._bot = None
|
||||
result = await adapter.send_exec_approval(
|
||||
chat_id="12345", command="ls", session_key="s"
|
||||
)
|
||||
assert result.success is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_truncates_long_command(self):
|
||||
adapter = _make_adapter()
|
||||
mock_msg = MagicMock()
|
||||
mock_msg.message_id = 1
|
||||
adapter._bot.send_message = AsyncMock(return_value=mock_msg)
|
||||
|
||||
long_cmd = "x" * 5000
|
||||
await adapter.send_exec_approval(
|
||||
chat_id="12345", command=long_cmd, session_key="s"
|
||||
)
|
||||
|
||||
kwargs = adapter._bot.send_message.call_args[1]
|
||||
assert "..." in kwargs["text"]
|
||||
assert len(kwargs["text"]) < 5000
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# _handle_callback_query — approval button clicks
|
||||
# ===========================================================================
|
||||
|
||||
class TestTelegramApprovalCallback:
|
||||
"""Test the approval callback handling in _handle_callback_query."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_resolves_approval_on_click(self):
|
||||
adapter = _make_adapter()
|
||||
# Set up approval state
|
||||
adapter._approval_state[1] = "agent:main:telegram:group:12345:99"
|
||||
|
||||
# Mock callback query
|
||||
query = AsyncMock()
|
||||
query.data = "ea:once:1"
|
||||
query.message = MagicMock()
|
||||
query.message.chat_id = 12345
|
||||
query.from_user = MagicMock()
|
||||
query.from_user.first_name = "Norbert"
|
||||
query.answer = AsyncMock()
|
||||
query.edit_message_text = AsyncMock()
|
||||
|
||||
update = MagicMock()
|
||||
update.callback_query = query
|
||||
context = MagicMock()
|
||||
|
||||
with patch("tools.approval.resolve_gateway_approval", return_value=1) as mock_resolve:
|
||||
await adapter._handle_callback_query(update, context)
|
||||
|
||||
mock_resolve.assert_called_once_with("agent:main:telegram:group:12345:99", "once")
|
||||
query.answer.assert_called_once()
|
||||
query.edit_message_text.assert_called_once()
|
||||
|
||||
# State should be cleaned up
|
||||
assert 1 not in adapter._approval_state
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_deny_button(self):
|
||||
adapter = _make_adapter()
|
||||
adapter._approval_state[2] = "some-session"
|
||||
|
||||
query = AsyncMock()
|
||||
query.data = "ea:deny:2"
|
||||
query.message = MagicMock()
|
||||
query.message.chat_id = 12345
|
||||
query.from_user = MagicMock()
|
||||
query.from_user.first_name = "Alice"
|
||||
query.answer = AsyncMock()
|
||||
query.edit_message_text = AsyncMock()
|
||||
|
||||
update = MagicMock()
|
||||
update.callback_query = query
|
||||
context = MagicMock()
|
||||
|
||||
with patch("tools.approval.resolve_gateway_approval", return_value=1) as mock_resolve:
|
||||
await adapter._handle_callback_query(update, context)
|
||||
|
||||
mock_resolve.assert_called_once_with("some-session", "deny")
|
||||
edit_kwargs = query.edit_message_text.call_args[1]
|
||||
assert "Denied" in edit_kwargs["text"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_already_resolved(self):
|
||||
adapter = _make_adapter()
|
||||
# No state for approval_id 99 — already resolved
|
||||
|
||||
query = AsyncMock()
|
||||
query.data = "ea:once:99"
|
||||
query.message = MagicMock()
|
||||
query.message.chat_id = 12345
|
||||
query.from_user = MagicMock()
|
||||
query.from_user.first_name = "Bob"
|
||||
query.answer = AsyncMock()
|
||||
|
||||
update = MagicMock()
|
||||
update.callback_query = query
|
||||
context = MagicMock()
|
||||
|
||||
with patch("tools.approval.resolve_gateway_approval") as mock_resolve:
|
||||
await adapter._handle_callback_query(update, context)
|
||||
|
||||
# Should NOT resolve — already handled
|
||||
mock_resolve.assert_not_called()
|
||||
# Should still ack with "already resolved" message
|
||||
query.answer.assert_called_once()
|
||||
assert "already been resolved" in query.answer.call_args[1]["text"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_model_picker_callback_not_affected(self):
|
||||
"""Ensure model picker callbacks still route correctly."""
|
||||
adapter = _make_adapter()
|
||||
|
||||
query = AsyncMock()
|
||||
query.data = "mp:some_provider"
|
||||
query.message = MagicMock()
|
||||
query.message.chat_id = 12345
|
||||
query.from_user = MagicMock()
|
||||
|
||||
update = MagicMock()
|
||||
update.callback_query = query
|
||||
context = MagicMock()
|
||||
|
||||
# Model picker callback should be handled (not crash)
|
||||
# We just verify it doesn't try to resolve an approval
|
||||
with patch("tools.approval.resolve_gateway_approval") as mock_resolve:
|
||||
with patch.object(adapter, "_handle_model_picker_callback", new_callable=AsyncMock):
|
||||
await adapter._handle_callback_query(update, context)
|
||||
|
||||
mock_resolve.assert_not_called()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_prompt_callback_not_affected(self):
|
||||
"""Ensure update prompt callbacks still work."""
|
||||
adapter = _make_adapter()
|
||||
|
||||
query = AsyncMock()
|
||||
query.data = "update_prompt:y"
|
||||
query.message = MagicMock()
|
||||
query.message.chat_id = 12345
|
||||
query.from_user = MagicMock()
|
||||
query.from_user.id = 123
|
||||
query.answer = AsyncMock()
|
||||
query.edit_message_text = AsyncMock()
|
||||
|
||||
update = MagicMock()
|
||||
update.callback_query = query
|
||||
context = MagicMock()
|
||||
|
||||
with patch("tools.approval.resolve_gateway_approval") as mock_resolve:
|
||||
with patch("hermes_constants.get_hermes_home", return_value=Path("/tmp/test")):
|
||||
try:
|
||||
await adapter._handle_callback_query(update, context)
|
||||
except Exception:
|
||||
pass # May fail on file write, that's fine
|
||||
|
||||
# Should NOT have triggered approval resolution
|
||||
mock_resolve.assert_not_called()
|
||||
Reference in New Issue
Block a user