hermes-agent/tests/gateway/test_slack_block_kit_adapter.py
Teknium 39975613b1
test: prune wave 2 + speed fixes — 28,106 → 19,757 test functions, suite wall 315s → 294s
Second, deeper pass over tools/gateway/hermes_cli plus first pass over
the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker,
dashboard, conformance, monitoring, secret_sources, hermes_state,
providers). Same rubric as wave 1 (AGENTS.md test policy); security,
alternation/caching invariants, issue-number regressions, and E2E kept.

Real test-quality fixes found and rooted out along the way:
- tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls
  (DEFAULT_CONFIG smart-approval leaked in) — pinned approval
  mode=manual via autouse fixture: 17.4s → 0.4s.
- test_model_switch_custom_providers.py / test_user_providers_model_switch.py
  silently probed live provider catalogs (~2s/test) — stubbed
  cached_provider_model_ids/provider_model_ids/fetch_api_models.
- test_telegram_noise_filter.py: 15-platform copy-paste matrix over
  shared gateway.run logic → 3 representative platforms (55s → 3.9s).
- test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on
  MagicMock agents — interrupt.side_effect now clears _running_agents
  (22s → 1.0s).
- test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x
  (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps
  patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait
  5s → 0.5s.
- test_telegram_init_deadline.py: loop-block margin restored to 1.0s
  with rationale comment — the watchdog-dump assertion needs the loop
  blocked well past deadline+grace under parallel load (flaked once in
  the 40-worker verification run at a 0.2s margin).

Verification: full hermetic suite via scripts/run_tests.sh —
2,438 files, 21,718 tests passed, 0 failed, 293.9s wall.
Suite totals vs original baseline: 46,820 → 19,757 test functions
(−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
2026-07-29 13:39:40 -07:00

187 lines
6.7 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_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_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"
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_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"
# ---------------------------------------------------------------------------
# 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_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