Skip to content

Commit 0492aa0

Browse files
committed
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/.
1 parent 724ea5f commit 0492aa0

File tree

6 files changed

+217
-203
lines changed

6 files changed

+217
-203
lines changed

tests/scenario_tests/test_events_agent.py

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import json
22
from time import sleep
3-
from unittest.mock import patch, MagicMock
43

54
import pytest
65
from slack_sdk.web import WebClient
7-
from slack_sdk.web.chat_stream import ChatStream
86

97
from slack_bolt import App, BoltRequest, BoltContext, BoltAgent
108
from slack_bolt.agent.agent import BoltAgent as BoltAgentDirect
@@ -57,90 +55,6 @@ def handle_mention(agent: BoltAgent, context: BoltContext):
5755
assert response.status == 200
5856
assert_target_called()
5957

60-
def test_agent_chat_stream_uses_context_defaults(self):
61-
"""BoltAgent.chat_stream() passes context defaults to WebClient.chat_stream()."""
62-
client = MagicMock(spec=WebClient)
63-
client.chat_stream.return_value = MagicMock(spec=ChatStream)
64-
65-
agent = BoltAgentDirect(
66-
client=client,
67-
channel_id="C111",
68-
thread_ts="1234567890.123456",
69-
team_id="T111",
70-
user_id="W222",
71-
)
72-
stream = agent.chat_stream()
73-
74-
client.chat_stream.assert_called_once_with(
75-
channel="C111",
76-
thread_ts="1234567890.123456",
77-
recipient_team_id="T111",
78-
recipient_user_id="W222",
79-
)
80-
assert stream is not None
81-
82-
def test_agent_chat_stream_overrides_context_defaults(self):
83-
"""Explicit kwargs to chat_stream() override context defaults."""
84-
client = MagicMock(spec=WebClient)
85-
client.chat_stream.return_value = MagicMock(spec=ChatStream)
86-
87-
agent = BoltAgentDirect(
88-
client=client,
89-
channel_id="C111",
90-
thread_ts="1234567890.123456",
91-
team_id="T111",
92-
user_id="W222",
93-
)
94-
stream = agent.chat_stream(
95-
channel="C999",
96-
thread_ts="9999999999.999999",
97-
recipient_team_id="T999",
98-
recipient_user_id="U999",
99-
)
100-
101-
client.chat_stream.assert_called_once_with(
102-
channel="C999",
103-
thread_ts="9999999999.999999",
104-
recipient_team_id="T999",
105-
recipient_user_id="U999",
106-
)
107-
assert stream is not None
108-
109-
def test_agent_chat_stream_rejects_partial_overrides(self):
110-
"""Passing only some of the four context args raises ValueError."""
111-
client = MagicMock(spec=WebClient)
112-
agent = BoltAgentDirect(
113-
client=client,
114-
channel_id="C111",
115-
thread_ts="1234567890.123456",
116-
team_id="T111",
117-
user_id="W222",
118-
)
119-
with pytest.raises(ValueError, match="Either provide all of"):
120-
agent.chat_stream(channel="C999")
121-
122-
def test_agent_chat_stream_passes_extra_kwargs(self):
123-
"""Extra kwargs are forwarded to WebClient.chat_stream()."""
124-
client = MagicMock(spec=WebClient)
125-
client.chat_stream.return_value = MagicMock(spec=ChatStream)
126-
127-
agent = BoltAgentDirect(
128-
client=client,
129-
channel_id="C111",
130-
thread_ts="1234567890.123456",
131-
team_id="T111",
132-
user_id="W222",
133-
)
134-
agent.chat_stream(buffer_size=512)
135-
136-
client.chat_stream.assert_called_once_with(
137-
channel="C111",
138-
thread_ts="1234567890.123456",
139-
recipient_team_id="T111",
140-
recipient_user_id="W222",
141-
buffer_size=512,
142-
)
143-
14458
def test_agent_available_in_action_listener(self):
14559
app = App(client=self.web_client)
14660

@@ -193,16 +107,6 @@ def handle_mention(context: BoltContext):
193107
assert response.status == 200
194108
assert_target_called()
195109

196-
def test_agent_import_from_slack_bolt(self):
197-
from slack_bolt import BoltAgent as ImportedBoltAgent
198-
199-
assert ImportedBoltAgent is BoltAgentDirect
200-
201-
def test_agent_import_from_agent_module(self):
202-
from slack_bolt.agent import BoltAgent as ImportedBoltAgent
203-
204-
assert ImportedBoltAgent is BoltAgentDirect
205-
206110
def test_agent_kwarg_emits_experimental_warning(self):
207111
app = App(client=self.web_client)
208112

tests/scenario_tests_async/test_events_agent.py

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import asyncio
22
import json
3-
from unittest.mock import MagicMock
43

54
import pytest
65
from slack_sdk.web.async_client import AsyncWebClient
7-
from slack_sdk.web.async_chat_stream import AsyncChatStream
86

97
from slack_bolt.agent.async_agent import AsyncBoltAgent
108
from slack_bolt.app.async_app import AsyncApp
@@ -18,17 +16,6 @@
1816
from tests.utils import remove_os_env_temporarily, restore_os_env
1917

2018

21-
def _make_async_chat_stream_mock():
22-
mock_stream = MagicMock(spec=AsyncChatStream)
23-
call_tracker = MagicMock()
24-
25-
async def fake_chat_stream(**kwargs):
26-
call_tracker(**kwargs)
27-
return mock_stream
28-
29-
return fake_chat_stream, call_tracker, mock_stream
30-
31-
3219
class TestAsyncEventsAgent:
3320
valid_token = "xoxb-valid"
3421
mock_api_server_base_url = "http://localhost:8888"
@@ -73,94 +60,6 @@ async def handle_mention(agent: AsyncBoltAgent, context: AsyncBoltContext):
7360
assert response.status == 200
7461
await assert_target_called()
7562

76-
@pytest.mark.asyncio
77-
async def test_agent_chat_stream_uses_context_defaults(self):
78-
"""AsyncBoltAgent.chat_stream() passes context defaults to AsyncWebClient.chat_stream()."""
79-
client = MagicMock(spec=AsyncWebClient)
80-
client.chat_stream, call_tracker, _ = _make_async_chat_stream_mock()
81-
82-
agent = AsyncBoltAgent(
83-
client=client,
84-
channel_id="C111",
85-
thread_ts="1234567890.123456",
86-
team_id="T111",
87-
user_id="W222",
88-
)
89-
stream = await agent.chat_stream()
90-
91-
call_tracker.assert_called_once_with(
92-
channel="C111",
93-
thread_ts="1234567890.123456",
94-
recipient_team_id="T111",
95-
recipient_user_id="W222",
96-
)
97-
assert stream is not None
98-
99-
@pytest.mark.asyncio
100-
async def test_agent_chat_stream_overrides_context_defaults(self):
101-
"""Explicit kwargs to chat_stream() override context defaults."""
102-
client = MagicMock(spec=AsyncWebClient)
103-
client.chat_stream, call_tracker, _ = _make_async_chat_stream_mock()
104-
105-
agent = AsyncBoltAgent(
106-
client=client,
107-
channel_id="C111",
108-
thread_ts="1234567890.123456",
109-
team_id="T111",
110-
user_id="W222",
111-
)
112-
stream = await agent.chat_stream(
113-
channel="C999",
114-
thread_ts="9999999999.999999",
115-
recipient_team_id="T999",
116-
recipient_user_id="U999",
117-
)
118-
119-
call_tracker.assert_called_once_with(
120-
channel="C999",
121-
thread_ts="9999999999.999999",
122-
recipient_team_id="T999",
123-
recipient_user_id="U999",
124-
)
125-
assert stream is not None
126-
127-
@pytest.mark.asyncio
128-
async def test_agent_chat_stream_rejects_partial_overrides(self):
129-
"""Passing only some of the four context args raises ValueError."""
130-
client = MagicMock(spec=AsyncWebClient)
131-
agent = AsyncBoltAgent(
132-
client=client,
133-
channel_id="C111",
134-
thread_ts="1234567890.123456",
135-
team_id="T111",
136-
user_id="W222",
137-
)
138-
with pytest.raises(ValueError, match="Either provide all of"):
139-
await agent.chat_stream(channel="C999")
140-
141-
@pytest.mark.asyncio
142-
async def test_agent_chat_stream_passes_extra_kwargs(self):
143-
"""Extra kwargs are forwarded to AsyncWebClient.chat_stream()."""
144-
client = MagicMock(spec=AsyncWebClient)
145-
client.chat_stream, call_tracker, _ = _make_async_chat_stream_mock()
146-
147-
agent = AsyncBoltAgent(
148-
client=client,
149-
channel_id="C111",
150-
thread_ts="1234567890.123456",
151-
team_id="T111",
152-
user_id="W222",
153-
)
154-
await agent.chat_stream(buffer_size=512)
155-
156-
call_tracker.assert_called_once_with(
157-
channel="C111",
158-
thread_ts="1234567890.123456",
159-
recipient_team_id="T111",
160-
recipient_user_id="W222",
161-
buffer_size=512,
162-
)
163-
16463
@pytest.mark.asyncio
16564
async def test_agent_available_in_action_listener(self):
16665
app = AsyncApp(client=self.web_client)
@@ -215,12 +114,6 @@ async def handle_mention(context: AsyncBoltContext):
215114
assert response.status == 200
216115
await assert_target_called()
217116

218-
@pytest.mark.asyncio
219-
async def test_agent_import_from_agent_module(self):
220-
from slack_bolt.agent.async_agent import AsyncBoltAgent as ImportedAsyncBoltAgent
221-
222-
assert ImportedAsyncBoltAgent is AsyncBoltAgent
223-
224117
@pytest.mark.asyncio
225118
async def test_agent_kwarg_emits_experimental_warning(self):
226119
app = AsyncApp(client=self.web_client)

tests/slack_bolt/agent/__init__.py

Whitespace-only changes.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
from unittest.mock import MagicMock
2+
3+
import pytest
4+
from slack_sdk.web import WebClient
5+
from slack_sdk.web.chat_stream import ChatStream
6+
7+
from slack_bolt.agent.agent import BoltAgent
8+
9+
10+
class TestBoltAgent:
11+
def test_chat_stream_uses_context_defaults(self):
12+
"""BoltAgent.chat_stream() passes context defaults to WebClient.chat_stream()."""
13+
client = MagicMock(spec=WebClient)
14+
client.chat_stream.return_value = MagicMock(spec=ChatStream)
15+
16+
agent = BoltAgent(
17+
client=client,
18+
channel_id="C111",
19+
thread_ts="1234567890.123456",
20+
team_id="T111",
21+
user_id="W222",
22+
)
23+
stream = agent.chat_stream()
24+
25+
client.chat_stream.assert_called_once_with(
26+
channel="C111",
27+
thread_ts="1234567890.123456",
28+
recipient_team_id="T111",
29+
recipient_user_id="W222",
30+
)
31+
assert stream is not None
32+
33+
def test_chat_stream_overrides_context_defaults(self):
34+
"""Explicit kwargs to chat_stream() override context defaults."""
35+
client = MagicMock(spec=WebClient)
36+
client.chat_stream.return_value = MagicMock(spec=ChatStream)
37+
38+
agent = BoltAgent(
39+
client=client,
40+
channel_id="C111",
41+
thread_ts="1234567890.123456",
42+
team_id="T111",
43+
user_id="W222",
44+
)
45+
stream = agent.chat_stream(
46+
channel="C999",
47+
thread_ts="9999999999.999999",
48+
recipient_team_id="T999",
49+
recipient_user_id="U999",
50+
)
51+
52+
client.chat_stream.assert_called_once_with(
53+
channel="C999",
54+
thread_ts="9999999999.999999",
55+
recipient_team_id="T999",
56+
recipient_user_id="U999",
57+
)
58+
assert stream is not None
59+
60+
def test_chat_stream_rejects_partial_overrides(self):
61+
"""Passing only some of the four context args raises ValueError."""
62+
client = MagicMock(spec=WebClient)
63+
agent = BoltAgent(
64+
client=client,
65+
channel_id="C111",
66+
thread_ts="1234567890.123456",
67+
team_id="T111",
68+
user_id="W222",
69+
)
70+
with pytest.raises(ValueError, match="Either provide all of"):
71+
agent.chat_stream(channel="C999")
72+
73+
def test_chat_stream_passes_extra_kwargs(self):
74+
"""Extra kwargs are forwarded to WebClient.chat_stream()."""
75+
client = MagicMock(spec=WebClient)
76+
client.chat_stream.return_value = MagicMock(spec=ChatStream)
77+
78+
agent = BoltAgent(
79+
client=client,
80+
channel_id="C111",
81+
thread_ts="1234567890.123456",
82+
team_id="T111",
83+
user_id="W222",
84+
)
85+
agent.chat_stream(buffer_size=512)
86+
87+
client.chat_stream.assert_called_once_with(
88+
channel="C111",
89+
thread_ts="1234567890.123456",
90+
recipient_team_id="T111",
91+
recipient_user_id="W222",
92+
buffer_size=512,
93+
)
94+
95+
def test_import_from_slack_bolt(self):
96+
from slack_bolt import BoltAgent as ImportedBoltAgent
97+
98+
assert ImportedBoltAgent is BoltAgent
99+
100+
def test_import_from_agent_module(self):
101+
from slack_bolt.agent import BoltAgent as ImportedBoltAgent
102+
103+
assert ImportedBoltAgent is BoltAgent

tests/slack_bolt_async/agent/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)