Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: move agent unit tests into dedicated test directories
Split agent tests so unit tests live in tests/slack_bolt/agent/ and
tests/slack_bolt_async/agent/, matching the existing convention where
test directories mirror the source layout. Integration tests that
dispatch through App remain in scenario_tests/.
  • Loading branch information
mwbrooks committed Feb 12, 2026
commit 0492aa087796ae3b1c39cb06d6c43d9b25082515
96 changes: 0 additions & 96 deletions tests/scenario_tests/test_events_agent.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import json
from time import sleep
from unittest.mock import patch, MagicMock

import pytest
from slack_sdk.web import WebClient
from slack_sdk.web.chat_stream import ChatStream

from slack_bolt import App, BoltRequest, BoltContext, BoltAgent
from slack_bolt.agent.agent import BoltAgent as BoltAgentDirect
Expand Down Expand Up @@ -57,90 +55,6 @@ def handle_mention(agent: BoltAgent, context: BoltContext):
assert response.status == 200
assert_target_called()

def test_agent_chat_stream_uses_context_defaults(self):
"""BoltAgent.chat_stream() passes context defaults to WebClient.chat_stream()."""
client = MagicMock(spec=WebClient)
client.chat_stream.return_value = MagicMock(spec=ChatStream)

agent = BoltAgentDirect(
client=client,
channel_id="C111",
thread_ts="1234567890.123456",
team_id="T111",
user_id="W222",
)
stream = agent.chat_stream()

client.chat_stream.assert_called_once_with(
channel="C111",
thread_ts="1234567890.123456",
recipient_team_id="T111",
recipient_user_id="W222",
)
assert stream is not None

def test_agent_chat_stream_overrides_context_defaults(self):
"""Explicit kwargs to chat_stream() override context defaults."""
client = MagicMock(spec=WebClient)
client.chat_stream.return_value = MagicMock(spec=ChatStream)

agent = BoltAgentDirect(
client=client,
channel_id="C111",
thread_ts="1234567890.123456",
team_id="T111",
user_id="W222",
)
stream = agent.chat_stream(
channel="C999",
thread_ts="9999999999.999999",
recipient_team_id="T999",
recipient_user_id="U999",
)

client.chat_stream.assert_called_once_with(
channel="C999",
thread_ts="9999999999.999999",
recipient_team_id="T999",
recipient_user_id="U999",
)
assert stream is not None

def test_agent_chat_stream_rejects_partial_overrides(self):
"""Passing only some of the four context args raises ValueError."""
client = MagicMock(spec=WebClient)
agent = BoltAgentDirect(
client=client,
channel_id="C111",
thread_ts="1234567890.123456",
team_id="T111",
user_id="W222",
)
with pytest.raises(ValueError, match="Either provide all of"):
agent.chat_stream(channel="C999")

def test_agent_chat_stream_passes_extra_kwargs(self):
"""Extra kwargs are forwarded to WebClient.chat_stream()."""
client = MagicMock(spec=WebClient)
client.chat_stream.return_value = MagicMock(spec=ChatStream)

agent = BoltAgentDirect(
client=client,
channel_id="C111",
thread_ts="1234567890.123456",
team_id="T111",
user_id="W222",
)
agent.chat_stream(buffer_size=512)

client.chat_stream.assert_called_once_with(
channel="C111",
thread_ts="1234567890.123456",
recipient_team_id="T111",
recipient_user_id="W222",
buffer_size=512,
)

def test_agent_available_in_action_listener(self):
app = App(client=self.web_client)

Expand Down Expand Up @@ -193,16 +107,6 @@ def handle_mention(context: BoltContext):
assert response.status == 200
assert_target_called()

def test_agent_import_from_slack_bolt(self):
from slack_bolt import BoltAgent as ImportedBoltAgent

assert ImportedBoltAgent is BoltAgentDirect

def test_agent_import_from_agent_module(self):
from slack_bolt.agent import BoltAgent as ImportedBoltAgent

assert ImportedBoltAgent is BoltAgentDirect

def test_agent_kwarg_emits_experimental_warning(self):
app = App(client=self.web_client)

Expand Down
107 changes: 0 additions & 107 deletions tests/scenario_tests_async/test_events_agent.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import asyncio
import json
from unittest.mock import MagicMock

import pytest
from slack_sdk.web.async_client import AsyncWebClient
from slack_sdk.web.async_chat_stream import AsyncChatStream

from slack_bolt.agent.async_agent import AsyncBoltAgent
from slack_bolt.app.async_app import AsyncApp
Expand All @@ -18,17 +16,6 @@
from tests.utils import remove_os_env_temporarily, restore_os_env


def _make_async_chat_stream_mock():
mock_stream = MagicMock(spec=AsyncChatStream)
call_tracker = MagicMock()

async def fake_chat_stream(**kwargs):
call_tracker(**kwargs)
return mock_stream

return fake_chat_stream, call_tracker, mock_stream


class TestAsyncEventsAgent:
valid_token = "xoxb-valid"
mock_api_server_base_url = "http://localhost:8888"
Expand Down Expand Up @@ -73,94 +60,6 @@ async def handle_mention(agent: AsyncBoltAgent, context: AsyncBoltContext):
assert response.status == 200
await assert_target_called()

@pytest.mark.asyncio
async def test_agent_chat_stream_uses_context_defaults(self):
"""AsyncBoltAgent.chat_stream() passes context defaults to AsyncWebClient.chat_stream()."""
client = MagicMock(spec=AsyncWebClient)
client.chat_stream, call_tracker, _ = _make_async_chat_stream_mock()

agent = AsyncBoltAgent(
client=client,
channel_id="C111",
thread_ts="1234567890.123456",
team_id="T111",
user_id="W222",
)
stream = await agent.chat_stream()

call_tracker.assert_called_once_with(
channel="C111",
thread_ts="1234567890.123456",
recipient_team_id="T111",
recipient_user_id="W222",
)
assert stream is not None

@pytest.mark.asyncio
async def test_agent_chat_stream_overrides_context_defaults(self):
"""Explicit kwargs to chat_stream() override context defaults."""
client = MagicMock(spec=AsyncWebClient)
client.chat_stream, call_tracker, _ = _make_async_chat_stream_mock()

agent = AsyncBoltAgent(
client=client,
channel_id="C111",
thread_ts="1234567890.123456",
team_id="T111",
user_id="W222",
)
stream = await agent.chat_stream(
channel="C999",
thread_ts="9999999999.999999",
recipient_team_id="T999",
recipient_user_id="U999",
)

call_tracker.assert_called_once_with(
channel="C999",
thread_ts="9999999999.999999",
recipient_team_id="T999",
recipient_user_id="U999",
)
assert stream is not None

@pytest.mark.asyncio
async def test_agent_chat_stream_rejects_partial_overrides(self):
"""Passing only some of the four context args raises ValueError."""
client = MagicMock(spec=AsyncWebClient)
agent = AsyncBoltAgent(
client=client,
channel_id="C111",
thread_ts="1234567890.123456",
team_id="T111",
user_id="W222",
)
with pytest.raises(ValueError, match="Either provide all of"):
await agent.chat_stream(channel="C999")

@pytest.mark.asyncio
async def test_agent_chat_stream_passes_extra_kwargs(self):
"""Extra kwargs are forwarded to AsyncWebClient.chat_stream()."""
client = MagicMock(spec=AsyncWebClient)
client.chat_stream, call_tracker, _ = _make_async_chat_stream_mock()

agent = AsyncBoltAgent(
client=client,
channel_id="C111",
thread_ts="1234567890.123456",
team_id="T111",
user_id="W222",
)
await agent.chat_stream(buffer_size=512)

call_tracker.assert_called_once_with(
channel="C111",
thread_ts="1234567890.123456",
recipient_team_id="T111",
recipient_user_id="W222",
buffer_size=512,
)

@pytest.mark.asyncio
async def test_agent_available_in_action_listener(self):
app = AsyncApp(client=self.web_client)
Expand Down Expand Up @@ -215,12 +114,6 @@ async def handle_mention(context: AsyncBoltContext):
assert response.status == 200
await assert_target_called()

@pytest.mark.asyncio
async def test_agent_import_from_agent_module(self):
from slack_bolt.agent.async_agent import AsyncBoltAgent as ImportedAsyncBoltAgent

assert ImportedAsyncBoltAgent is AsyncBoltAgent

@pytest.mark.asyncio
async def test_agent_kwarg_emits_experimental_warning(self):
app = AsyncApp(client=self.web_client)
Expand Down
Empty file.
103 changes: 103 additions & 0 deletions tests/slack_bolt/agent/test_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from unittest.mock import MagicMock

import pytest
from slack_sdk.web import WebClient
from slack_sdk.web.chat_stream import ChatStream

from slack_bolt.agent.agent import BoltAgent


class TestBoltAgent:
def test_chat_stream_uses_context_defaults(self):
"""BoltAgent.chat_stream() passes context defaults to WebClient.chat_stream()."""
client = MagicMock(spec=WebClient)
client.chat_stream.return_value = MagicMock(spec=ChatStream)

agent = BoltAgent(
client=client,
channel_id="C111",
thread_ts="1234567890.123456",
team_id="T111",
user_id="W222",
)
stream = agent.chat_stream()

client.chat_stream.assert_called_once_with(
channel="C111",
thread_ts="1234567890.123456",
recipient_team_id="T111",
recipient_user_id="W222",
)
assert stream is not None

def test_chat_stream_overrides_context_defaults(self):
"""Explicit kwargs to chat_stream() override context defaults."""
client = MagicMock(spec=WebClient)
client.chat_stream.return_value = MagicMock(spec=ChatStream)

agent = BoltAgent(
client=client,
channel_id="C111",
thread_ts="1234567890.123456",
team_id="T111",
user_id="W222",
)
stream = agent.chat_stream(
channel="C999",
thread_ts="9999999999.999999",
recipient_team_id="T999",
recipient_user_id="U999",
)

client.chat_stream.assert_called_once_with(
channel="C999",
thread_ts="9999999999.999999",
recipient_team_id="T999",
recipient_user_id="U999",
)
assert stream is not None

def test_chat_stream_rejects_partial_overrides(self):
"""Passing only some of the four context args raises ValueError."""
client = MagicMock(spec=WebClient)
agent = BoltAgent(
client=client,
channel_id="C111",
thread_ts="1234567890.123456",
team_id="T111",
user_id="W222",
)
with pytest.raises(ValueError, match="Either provide all of"):
agent.chat_stream(channel="C999")

def test_chat_stream_passes_extra_kwargs(self):
"""Extra kwargs are forwarded to WebClient.chat_stream()."""
client = MagicMock(spec=WebClient)
client.chat_stream.return_value = MagicMock(spec=ChatStream)

agent = BoltAgent(
client=client,
channel_id="C111",
thread_ts="1234567890.123456",
team_id="T111",
user_id="W222",
)
agent.chat_stream(buffer_size=512)

client.chat_stream.assert_called_once_with(
channel="C111",
thread_ts="1234567890.123456",
recipient_team_id="T111",
recipient_user_id="W222",
buffer_size=512,
)

def test_import_from_slack_bolt(self):
from slack_bolt import BoltAgent as ImportedBoltAgent

assert ImportedBoltAgent is BoltAgent

def test_import_from_agent_module(self):
from slack_bolt.agent import BoltAgent as ImportedBoltAgent

assert ImportedBoltAgent is BoltAgent
Empty file.
Loading