mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-24 16:54:43 +00:00
Slack's Block Kit `markdown` block accepts standard markdown (tables, headers, task lists, fenced code with syntax highlighting, links) and lets Slack translate it natively — eliminating the lossy markdown→mrkdwn conversion for the rendered layout. Enable via platforms.slack.extra.markdown_blocks. Safety rails added on top of the original design: * opt-in (default off) — Slack documents the block for 'apps that use platform AI features' and does not guarantee availability across all app types / surfaces, so unconditional adoption is not safe yet * the mrkdwn-converted text field is ALWAYS kept as the notification/search/accessibility fallback * content over Slack's 12k cumulative markdown-block cap declines to the rich_blocks renderer / plain text path * the existing block-rejection retry (invalid_blocks / msg_too_long / too_many_blocks) re-sends the plain mrkdwn payload, so an unsupported surface degrades gracefully instead of dropping the message * when both modes are enabled, markdown_blocks is preferred over the local rich_blocks renderer; rich_blocks remains the fallback Adapted from #8554 by @shivasymbl — the original patched the deleted gateway/platforms/slack.py and switched unconditionally; reimplemented against the plugin adapter's _maybe_blocks/sanitize_blocks pipeline. Fixes #8552.
431 lines
17 KiB
Python
431 lines
17 KiB
Python
"""Integration tests: SlackAdapter wiring of Block Kit into send paths.
|
|
|
|
Verifies the opt-in behaviour contract:
|
|
* rich_blocks off (default) => no ``blocks`` kwarg, plain ``text`` only
|
|
* rich_blocks on => ``blocks`` present AND ``text`` fallback set
|
|
* edit_message: blocks only on finalize (streaming edits stay plain)
|
|
* multi-chunk (>39k) messages fall back to plain text
|
|
"""
|
|
|
|
from unittest.mock import AsyncMock, MagicMock, call
|
|
|
|
import pytest
|
|
|
|
from gateway.config import PlatformConfig
|
|
from plugins.platforms.slack import adapter as slack_module
|
|
from plugins.platforms.slack.adapter import SlackAdapter
|
|
|
|
|
|
def _make_adapter(extra=None):
|
|
config = PlatformConfig(enabled=True, token="xoxb-fake", extra=extra or {})
|
|
a = SlackAdapter(config)
|
|
a._app = MagicMock()
|
|
client = AsyncMock()
|
|
client.chat_postMessage = AsyncMock(return_value={"ts": "111.222"})
|
|
client.chat_update = AsyncMock(return_value={"ts": "111.222"})
|
|
a._get_client = MagicMock(return_value=client)
|
|
a.stop_typing = AsyncMock()
|
|
a._running = True
|
|
return a, client
|
|
|
|
|
|
RICH_MD = "# Title\n\n- a\n - nested\n\n---\n\nbody text"
|
|
RICH_TABLE_MD = (
|
|
"| Item | Status | Note |\n"
|
|
"|---|---:|---|\n"
|
|
"| Hermes | ok | table |"
|
|
)
|
|
|
|
|
|
class SlackRejectedBlocks(Exception):
|
|
def __init__(self, error="invalid_blocks"):
|
|
super().__init__(f"Slack API rejected blocks: {error}")
|
|
self.response = {"error": error}
|
|
|
|
|
|
def _slack_connection_key():
|
|
from aiohttp.client_reqrep import ConnectionKey
|
|
|
|
return ConnectionKey(
|
|
host="slack.com",
|
|
port=443,
|
|
is_ssl=True,
|
|
ssl=True,
|
|
proxy=None,
|
|
proxy_auth=None,
|
|
proxy_headers_hash=None,
|
|
)
|
|
|
|
|
|
class TestSendMessageBlocks:
|
|
@pytest.mark.asyncio
|
|
async def test_disabled_by_default_no_blocks(self):
|
|
adapter, client = _make_adapter()
|
|
await adapter.send("C1", RICH_MD)
|
|
kwargs = client.chat_postMessage.await_args.kwargs
|
|
assert "blocks" not in kwargs
|
|
assert kwargs["text"] # plain text still sent
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_enabled_sends_blocks_with_text_fallback(self):
|
|
adapter, client = _make_adapter({"rich_blocks": True})
|
|
await adapter.send("C1", RICH_MD)
|
|
kwargs = client.chat_postMessage.await_args.kwargs
|
|
assert "blocks" in kwargs and kwargs["blocks"]
|
|
# text fallback is ALWAYS present alongside blocks (notifications/a11y)
|
|
assert kwargs["text"]
|
|
types = [b["type"] for b in kwargs["blocks"]]
|
|
assert "header" in types
|
|
assert "divider" in types
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_enabled_but_unrenderable_falls_back_to_text(self):
|
|
# 60 dividers -> renderer returns None -> no blocks kwarg, text stands
|
|
adapter, client = _make_adapter({"rich_blocks": True})
|
|
await adapter.send("C1", "\n\n".join(["---"] * 60))
|
|
kwargs = client.chat_postMessage.await_args.kwargs
|
|
assert "blocks" not in kwargs
|
|
assert kwargs["text"]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_string_true_coerced(self):
|
|
adapter, client = _make_adapter({"rich_blocks": "true"})
|
|
await adapter.send("C1", RICH_MD)
|
|
assert "blocks" in client.chat_postMessage.await_args.kwargs
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_multichunk_message_no_blocks(self):
|
|
adapter, client = _make_adapter({"rich_blocks": True})
|
|
huge = "word " * 20000 # well over MAX_MESSAGE_LENGTH -> chunked
|
|
await adapter.send("C1", huge)
|
|
# every posted chunk is plain text, none carry blocks
|
|
for c in client.chat_postMessage.await_args_list:
|
|
assert "blocks" not in c.kwargs
|
|
assert c.kwargs["text"]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_feedback_buttons_opt_in_appended_to_blocks(self):
|
|
adapter, client = _make_adapter({"rich_blocks": True, "feedback_buttons": True})
|
|
|
|
await adapter.send("C1", "final answer")
|
|
|
|
blocks = client.chat_postMessage.await_args.kwargs["blocks"]
|
|
feedback = blocks[-1]
|
|
assert feedback["type"] == "context_actions"
|
|
assert feedback["elements"][0]["type"] == "feedback_buttons"
|
|
assert feedback["elements"][0]["action_id"] == "hermes_feedback"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_feedback_buttons_require_rich_blocks(self):
|
|
"""feedback_buttons alone must not implicitly enable Block Kit rendering."""
|
|
adapter, client = _make_adapter({"feedback_buttons": True})
|
|
|
|
await adapter.send("C1", "final answer")
|
|
|
|
assert "blocks" not in client.chat_postMessage.await_args.kwargs
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_block_rejection_retries_send_without_blocks_using_workspace_client(self):
|
|
adapter, client = _make_adapter({"rich_blocks": True})
|
|
client.chat_postMessage = AsyncMock(
|
|
side_effect=[SlackRejectedBlocks("invalid_blocks"), {"ts": "111.333"}]
|
|
)
|
|
|
|
result = await adapter.send(
|
|
"C1", RICH_TABLE_MD, metadata={"team_id": "T_SECONDARY"}
|
|
)
|
|
|
|
assert result.success is True
|
|
assert adapter._get_client.call_args_list == [
|
|
call("C1", team_id="T_SECONDARY"),
|
|
call("C1", team_id="T_SECONDARY"),
|
|
]
|
|
assert client.chat_postMessage.await_count == 2
|
|
first = client.chat_postMessage.await_args_list[0].kwargs
|
|
second = client.chat_postMessage.await_args_list[1].kwargs
|
|
assert "blocks" in first and first["blocks"]
|
|
assert "blocks" not in second
|
|
assert second["text"]
|
|
|
|
|
|
class TestEditMessageBlocks:
|
|
@pytest.mark.asyncio
|
|
async def test_intermediate_edit_no_blocks(self):
|
|
adapter, client = _make_adapter({"rich_blocks": True})
|
|
await adapter.edit_message("C1", "111.222", RICH_MD, finalize=False)
|
|
kwargs = client.chat_update.await_args.kwargs
|
|
assert "blocks" not in kwargs
|
|
assert kwargs["text"]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_finalize_edit_gets_blocks(self):
|
|
adapter, client = _make_adapter({"rich_blocks": True})
|
|
await adapter.edit_message("C1", "111.222", RICH_MD, finalize=True)
|
|
kwargs = client.chat_update.await_args.kwargs
|
|
assert "blocks" in kwargs and kwargs["blocks"]
|
|
assert kwargs["text"]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_finalize_edit_gets_feedback_buttons_when_enabled(self):
|
|
adapter, client = _make_adapter({"rich_blocks": True, "feedback_buttons": True})
|
|
await adapter.edit_message("C1", "111.222", RICH_MD, finalize=True)
|
|
blocks = client.chat_update.await_args.kwargs["blocks"]
|
|
assert blocks[-1]["elements"][0]["type"] == "feedback_buttons"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_finalize_edit_disabled_no_blocks(self):
|
|
adapter, client = _make_adapter() # rich_blocks off
|
|
await adapter.edit_message("C1", "111.222", RICH_MD, finalize=True)
|
|
assert "blocks" not in client.chat_update.await_args.kwargs
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_block_rejection_retries_edit_without_blocks_using_workspace_client(self):
|
|
adapter, client = _make_adapter({"rich_blocks": True})
|
|
client.chat_update = AsyncMock(
|
|
side_effect=[SlackRejectedBlocks("invalid_blocks"), {"ts": "111.222"}]
|
|
)
|
|
|
|
result = await adapter.edit_message(
|
|
"C1",
|
|
"111.222",
|
|
RICH_TABLE_MD,
|
|
finalize=True,
|
|
metadata={"team_id": "T_SECONDARY"},
|
|
)
|
|
|
|
assert result.success is True
|
|
assert adapter._get_client.call_args_list == [
|
|
call("C1", team_id="T_SECONDARY"),
|
|
call("C1", team_id="T_SECONDARY"),
|
|
]
|
|
assert client.chat_update.await_count == 2
|
|
first = client.chat_update.await_args_list[0].kwargs
|
|
second = client.chat_update.await_args_list[1].kwargs
|
|
assert "blocks" in first and first["blocks"]
|
|
assert second["blocks"] == []
|
|
assert second["text"]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_timeout_error_on_edit_is_retryable_transient(self):
|
|
adapter, client = _make_adapter()
|
|
client.chat_update = AsyncMock(side_effect=TimeoutError("timed out"))
|
|
|
|
result = await adapter.edit_message("C1", "111.222", RICH_MD, finalize=True)
|
|
|
|
assert result.success is False
|
|
assert result.retryable is True
|
|
assert result.error_kind == "transient"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_dns_connection_error_on_edit_is_retryable_transient(self):
|
|
from aiohttp import ClientConnectorDNSError
|
|
|
|
adapter, client = _make_adapter()
|
|
client.chat_update = AsyncMock(
|
|
side_effect=ClientConnectorDNSError(
|
|
_slack_connection_key(),
|
|
OSError(8, "nodename nor servname provided, or not known"),
|
|
)
|
|
)
|
|
|
|
result = await adapter.edit_message("C1", "111.222", RICH_MD, finalize=True)
|
|
|
|
assert result.success is False
|
|
assert result.retryable is True
|
|
assert result.error_kind == "transient"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_slack_api_error_on_edit_is_not_retryable(self):
|
|
# Real slack_sdk required: the test pins that a genuine SlackApiError
|
|
# is never misclassified as transient. CI shards without the slack
|
|
# extras skip (adapter classification is still covered by the
|
|
# OSError/timeout tests above, which use stdlib exceptions).
|
|
errors_mod = pytest.importorskip("slack_sdk.errors")
|
|
SlackApiError = errors_mod.SlackApiError
|
|
|
|
adapter, client = _make_adapter()
|
|
client.chat_update = AsyncMock(
|
|
side_effect=SlackApiError(
|
|
"message_not_found",
|
|
{"ok": False, "error": "message_not_found"},
|
|
)
|
|
)
|
|
|
|
result = await adapter.edit_message("C1", "111.222", RICH_MD, finalize=True)
|
|
|
|
assert result.success is False
|
|
assert result.retryable is not True
|
|
assert result.error_kind != "transient"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_certificate_error_on_edit_is_not_retryable(self):
|
|
import ssl
|
|
|
|
from aiohttp import ClientConnectorCertificateError
|
|
|
|
adapter, client = _make_adapter()
|
|
client.chat_update = AsyncMock(
|
|
side_effect=ClientConnectorCertificateError(
|
|
_slack_connection_key(),
|
|
ssl.SSLCertVerificationError("certificate verify failed"),
|
|
)
|
|
)
|
|
|
|
result = await adapter.edit_message("C1", "111.222", RICH_MD, finalize=True)
|
|
|
|
assert result.success is False
|
|
assert result.retryable is not True
|
|
assert result.error_kind != "transient"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_tls_integrity_errors_on_edit_are_not_retryable(self):
|
|
import ssl
|
|
|
|
from aiohttp import ClientConnectorSSLError, ServerFingerprintMismatch
|
|
|
|
errors = (
|
|
ClientConnectorSSLError(
|
|
_slack_connection_key(), ssl.SSLError("handshake failed")
|
|
),
|
|
ServerFingerprintMismatch(b"expected", b"got", "slack.com", 443),
|
|
)
|
|
for error in errors:
|
|
adapter, client = _make_adapter()
|
|
client.chat_update = AsyncMock(side_effect=error)
|
|
|
|
result = await adapter.edit_message("C1", "111.222", RICH_MD, finalize=True)
|
|
|
|
assert result.success is False
|
|
assert result.retryable is not True
|
|
assert result.error_kind != "transient"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_plain_os_error_on_edit_is_not_retryable(self):
|
|
adapter, client = _make_adapter()
|
|
client.chat_update = AsyncMock(side_effect=OSError("invalid local socket state"))
|
|
|
|
result = await adapter.edit_message("C1", "111.222", RICH_MD, finalize=True)
|
|
|
|
assert result.success is False
|
|
assert result.retryable is not True
|
|
assert result.error_kind != "transient"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_lazy_rebound_aiohttp_connection_error_is_retryable(
|
|
self, monkeypatch
|
|
):
|
|
# Exercises the REAL lazy-import rebind path in
|
|
# check_slack_requirements — requires slack_bolt/slack_sdk installed.
|
|
pytest.importorskip("slack_bolt")
|
|
pytest.importorskip("slack_sdk")
|
|
import tools.lazy_deps as lazy_deps
|
|
|
|
monkeypatch.setattr(slack_module, "SLACK_AVAILABLE", False)
|
|
monkeypatch.delattr(slack_module, "aiohttp", raising=False)
|
|
|
|
def ensure_and_bind(_group, import_fn, target_globals, *, prompt):
|
|
assert prompt is False
|
|
target_globals.update(import_fn())
|
|
return True
|
|
|
|
monkeypatch.setattr(lazy_deps, "ensure_and_bind", ensure_and_bind)
|
|
|
|
assert slack_module.check_slack_requirements() is True
|
|
adapter, client = _make_adapter()
|
|
client.chat_update = AsyncMock(
|
|
side_effect=slack_module.aiohttp.ClientConnectionError("connection dropped")
|
|
)
|
|
|
|
result = await adapter.edit_message("C1", "111.222", RICH_MD, finalize=True)
|
|
|
|
assert result.success is False
|
|
assert result.retryable is True
|
|
assert result.error_kind == "transient"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# markdown_blocks mode — Slack's native ``markdown`` Block Kit block (#8552)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestMarkdownBlockMode:
|
|
"""Opt-in ``markdown_blocks`` renders raw standard markdown via Slack's
|
|
native ``markdown`` block, keeping the mrkdwn ``text`` fallback."""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_disabled_by_default(self):
|
|
adapter, client = _make_adapter()
|
|
await adapter.send("C1", RICH_TABLE_MD)
|
|
kwargs = client.chat_postMessage.await_args.kwargs
|
|
assert "blocks" not in kwargs
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_enabled_sends_markdown_block_with_raw_content(self):
|
|
adapter, client = _make_adapter({"markdown_blocks": True})
|
|
await adapter.send("C1", RICH_TABLE_MD)
|
|
kwargs = client.chat_postMessage.await_args.kwargs
|
|
blocks = kwargs["blocks"]
|
|
assert blocks[0]["type"] == "markdown"
|
|
# RAW standard markdown, not mrkdwn-converted — Slack translates it
|
|
assert blocks[0]["text"] == RICH_TABLE_MD
|
|
# mrkdwn fallback text is still present for notifications/search
|
|
assert kwargs["text"]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_text_fallback_is_mrkdwn_converted(self):
|
|
adapter, client = _make_adapter({"markdown_blocks": True})
|
|
await adapter.send("C1", "**bold**")
|
|
kwargs = client.chat_postMessage.await_args.kwargs
|
|
assert kwargs["blocks"][0]["text"] == "**bold**"
|
|
assert kwargs["text"] == "*bold*" # mrkdwn conversion for fallback
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_markdown_block_preferred_over_rich_blocks(self):
|
|
adapter, client = _make_adapter(
|
|
{"markdown_blocks": True, "rich_blocks": True}
|
|
)
|
|
await adapter.send("C1", RICH_TABLE_MD)
|
|
blocks = client.chat_postMessage.await_args.kwargs["blocks"]
|
|
assert blocks[0]["type"] == "markdown"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_over_cap_falls_back_to_rich_or_text(self):
|
|
adapter, client = _make_adapter({"markdown_blocks": True})
|
|
big = "x" * (SlackAdapter._MARKDOWN_BLOCK_MAX + 1)
|
|
payload = adapter._markdown_block_payload(big)
|
|
assert payload is None # declines >12k cumulative markdown cap
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_rejection_retries_without_blocks(self):
|
|
"""Workspaces/surfaces without markdown-block support degrade to
|
|
the plain mrkdwn text payload instead of dropping the message."""
|
|
adapter, client = _make_adapter({"markdown_blocks": True})
|
|
client.chat_postMessage = AsyncMock(
|
|
side_effect=[SlackRejectedBlocks(), {"ts": "111.222"}]
|
|
)
|
|
result = await adapter.send("C1", RICH_TABLE_MD)
|
|
assert result.success is True
|
|
assert client.chat_postMessage.await_count == 2
|
|
retry_kwargs = client.chat_postMessage.await_args_list[1].kwargs
|
|
assert "blocks" not in retry_kwargs
|
|
assert retry_kwargs["text"]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_edit_finalize_uses_markdown_block(self):
|
|
adapter, client = _make_adapter({"markdown_blocks": True})
|
|
await adapter.edit_message("C1", "111.222", RICH_TABLE_MD, finalize=True)
|
|
kwargs = client.chat_update.await_args.kwargs
|
|
assert kwargs["blocks"][0]["type"] == "markdown"
|
|
assert kwargs["blocks"][0]["text"] == RICH_TABLE_MD
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_edit_streaming_stays_plain(self):
|
|
adapter, client = _make_adapter({"markdown_blocks": True})
|
|
await adapter.edit_message("C1", "111.222", RICH_TABLE_MD, finalize=False)
|
|
kwargs = client.chat_update.await_args.kwargs
|
|
assert "blocks" not in kwargs
|
|
|
|
def test_empty_content_declines(self):
|
|
adapter, _ = _make_adapter({"markdown_blocks": True})
|
|
assert adapter._markdown_block_payload("") is None
|
|
assert adapter._markdown_block_payload(" ") is None
|