|
| 1 | +import pytest |
| 2 | +from slack_sdk.web.async_client import AsyncWebClient |
| 3 | + |
| 4 | +from slack_bolt.middleware.attaching_agent_kwargs.async_attaching_agent_kwargs import AsyncAttachingAgentKwargs |
| 5 | +from slack_bolt.request.async_request import AsyncBoltRequest |
| 6 | +from slack_bolt.response import BoltResponse |
| 7 | +from tests.scenario_tests_async.test_events_assistant import ( |
| 8 | + thread_started_event_body, |
| 9 | + user_message_event_body, |
| 10 | + channel_user_message_event_body, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +async def next(): |
| 15 | + return BoltResponse(status=200) |
| 16 | + |
| 17 | + |
| 18 | +AGENT_KWARGS = ("say", "set_status", "set_title", "set_suggested_prompts", "get_thread_context", "save_thread_context") |
| 19 | + |
| 20 | + |
| 21 | +class TestAsyncAttachingAgentKwargs: |
| 22 | + @pytest.mark.asyncio |
| 23 | + async def test_assistant_event_attaches_kwargs(self): |
| 24 | + middleware = AsyncAttachingAgentKwargs() |
| 25 | + req = AsyncBoltRequest(body=thread_started_event_body, mode="socket_mode") |
| 26 | + req.context["client"] = AsyncWebClient(token="xoxb-test") |
| 27 | + resp = BoltResponse(status=404) |
| 28 | + |
| 29 | + resp = await middleware.async_process(req=req, resp=resp, next=next) |
| 30 | + |
| 31 | + assert resp.status == 200 |
| 32 | + for key in AGENT_KWARGS: |
| 33 | + assert key in req.context, f"{key} should be set on context" |
| 34 | + assert req.context["say"].thread_ts == "1726133698.626339" |
| 35 | + |
| 36 | + @pytest.mark.asyncio |
| 37 | + async def test_user_message_assistant_event_attaches_kwargs(self): |
| 38 | + middleware = AsyncAttachingAgentKwargs() |
| 39 | + req = AsyncBoltRequest(body=user_message_event_body, mode="socket_mode") |
| 40 | + req.context["client"] = AsyncWebClient(token="xoxb-test") |
| 41 | + resp = BoltResponse(status=404) |
| 42 | + |
| 43 | + resp = await middleware.async_process(req=req, resp=resp, next=next) |
| 44 | + |
| 45 | + assert resp.status == 200 |
| 46 | + for key in AGENT_KWARGS: |
| 47 | + assert key in req.context, f"{key} should be set on context" |
| 48 | + assert req.context["say"].thread_ts == "1726133698.626339" |
| 49 | + |
| 50 | + @pytest.mark.asyncio |
| 51 | + async def test_non_assistant_event_does_not_attach_kwargs(self): |
| 52 | + middleware = AsyncAttachingAgentKwargs() |
| 53 | + req = AsyncBoltRequest(body=channel_user_message_event_body, mode="socket_mode") |
| 54 | + req.context["client"] = AsyncWebClient(token="xoxb-test") |
| 55 | + resp = BoltResponse(status=404) |
| 56 | + |
| 57 | + resp = await middleware.async_process(req=req, resp=resp, next=next) |
| 58 | + |
| 59 | + assert resp.status == 200 |
| 60 | + for key in AGENT_KWARGS: |
| 61 | + assert key not in req.context, f"{key} should not be set on context" |
| 62 | + |
| 63 | + @pytest.mark.asyncio |
| 64 | + async def test_non_event_body_does_not_attach_kwargs(self): |
| 65 | + middleware = AsyncAttachingAgentKwargs() |
| 66 | + req = AsyncBoltRequest(body="payload={}", headers={}) |
| 67 | + resp = BoltResponse(status=404) |
| 68 | + |
| 69 | + resp = await middleware.async_process(req=req, resp=resp, next=next) |
| 70 | + |
| 71 | + assert resp.status == 200 |
| 72 | + for key in AGENT_KWARGS: |
| 73 | + assert key not in req.context, f"{key} should not be set on context" |
| 74 | + |
| 75 | + @pytest.mark.asyncio |
| 76 | + async def test_next_always_called(self): |
| 77 | + middleware = AsyncAttachingAgentKwargs() |
| 78 | + call_state = {"called": False} |
| 79 | + |
| 80 | + async def tracking_next(): |
| 81 | + call_state["called"] = True |
| 82 | + return BoltResponse(status=200) |
| 83 | + |
| 84 | + # With assistant event |
| 85 | + req = AsyncBoltRequest(body=thread_started_event_body, mode="socket_mode") |
| 86 | + req.context["client"] = AsyncWebClient(token="xoxb-test") |
| 87 | + await middleware.async_process(req=req, resp=BoltResponse(status=404), next=tracking_next) |
| 88 | + assert call_state["called"] is True |
| 89 | + |
| 90 | + # With non-event body |
| 91 | + call_state["called"] = False |
| 92 | + req = AsyncBoltRequest(body="payload={}", headers={}) |
| 93 | + await middleware.async_process(req=req, resp=BoltResponse(status=404), next=tracking_next) |
| 94 | + assert call_state["called"] is True |
0 commit comments