Skip to content

Commit 9cc2bb3

Browse files
Improve based on feedback
1 parent 6c949fa commit 9cc2bb3

File tree

4 files changed

+72
-48
lines changed

4 files changed

+72
-48
lines changed

tests/scenario_tests/test_events_assistant.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
from tests.utils import remove_os_env_temporarily, restore_os_env
1111

1212

13-
def assert_target_called(called: dict, timeout: float = 2.0):
13+
def assert_target_called(called: dict, expected: bool = True, timeout: float = 0.5):
1414
deadline = time.time() + timeout
15-
while called["value"] is False and time.time() < deadline:
15+
while called["value"] is not expected and time.time() < deadline:
1616
time.sleep(0.1)
17-
assert called["value"] is True
17+
assert called["value"] is expected
1818

1919

2020
class TestEventsAssistant:
@@ -91,7 +91,7 @@ def handle_user_message(say: Say, set_status: SetStatus, context: BoltContext):
9191
say("Here you are!")
9292
called["value"] = True
9393
except Exception as e:
94-
say(f"Oops, something went wrong (error: {e}")
94+
say(f"Oops, something went wrong (error: {e})")
9595

9696
app.assistant(assistant)
9797

@@ -115,7 +115,7 @@ def handle_user_message(say: Say, set_status: SetStatus, context: BoltContext):
115115
say("Here you are!")
116116
called["value"] = True
117117
except Exception as e:
118-
say(f"Oops, something went wrong (error: {e}")
118+
say(f"Oops, something went wrong (error: {e})")
119119

120120
app.assistant(assistant)
121121

@@ -127,56 +127,63 @@ def handle_user_message(say: Say, set_status: SetStatus, context: BoltContext):
127127
def test_message_changed(self):
128128
app = App(client=self.web_client)
129129
assistant = Assistant()
130+
called = {"value": False}
130131

131132
@assistant.user_message
132133
def handle_user_message():
133-
assert False, "This handler should not be called"
134+
called["value"] = True
134135

135136
@assistant.bot_message
136137
def handle_bot_message():
137-
assert False, "This handler should not be called"
138+
called["value"] = True
138139

139140
app.assistant(assistant)
140141

141142
request = BoltRequest(body=message_changed_event_body, mode="socket_mode")
142143
response = app.dispatch(request)
143144
assert response.status == 200
145+
assert_target_called(called, key="user_message", expected=False)
146+
assert_target_called(called, key="bot_message", expected=False)
144147

145148
def test_channel_user_message_ignored(self):
146149
app = App(client=self.web_client)
147150
assistant = Assistant()
151+
called = {"value": False}
148152

149153
@assistant.user_message
150154
def handle_user_message():
151-
assert False, "This handler should not be called"
155+
called["value"] = True
152156

153157
@assistant.bot_message
154158
def handle_bot_message():
155-
assert False, "This handler should not be called"
159+
called["value"] = True
156160

157161
app.assistant(assistant)
158162

159163
request = BoltRequest(body=channel_user_message_event_body, mode="socket_mode")
160164
response = app.dispatch(request)
161165
assert response.status == 404
166+
assert_target_called(called, expected=False)
162167

163168
def test_channel_message_changed_ignored(self):
164169
app = App(client=self.web_client)
165170
assistant = Assistant()
171+
called = {"value": False}
166172

167173
@assistant.user_message
168174
def handle_user_message():
169-
assert False, "This handler should not be called"
175+
called["value"] = True
170176

171177
@assistant.bot_message
172178
def handle_bot_message():
173-
assert False, "This handler should not be called"
179+
called["value"] = True
174180

175181
app.assistant(assistant)
176182

177183
request = BoltRequest(body=channel_message_changed_event_body, mode="socket_mode")
178184
response = app.dispatch(request)
179185
assert response.status == 404
186+
assert_target_called(called, expected=False)
180187

181188

182189
def build_payload(event: dict) -> dict:

tests/scenario_tests/test_events_assistant_without_middleware.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import time
2-
31
from slack_sdk.web import WebClient
42

53
from slack_bolt import App, BoltRequest, Say, SetStatus, SetTitle, SaveThreadContext, BoltContext
@@ -18,16 +16,10 @@
1816
user_message_event_body,
1917
user_message_event_body_with_assistant_thread,
2018
)
19+
from tests.scenario_tests.test_events_assistant import assert_target_called
2120
from tests.utils import remove_os_env_temporarily, restore_os_env
2221

2322

24-
def assert_target_called(called: dict, timeout: float = 2.0):
25-
deadline = time.time() + timeout
26-
while called["value"] is False and time.time() < deadline:
27-
time.sleep(0.1)
28-
assert called["value"] is True
29-
30-
3123
class TestEventsAssistantWithoutMiddleware:
3224
valid_token = "xoxb-valid"
3325
mock_api_server_base_url = "http://localhost:8888"
@@ -91,7 +83,7 @@ def handle_assistant_thread_context_changed(
9183
):
9284
assert context.channel_id == "D111"
9385
assert context.thread_ts == "1726133698.626339"
94-
assert say is not None
86+
assert say.thread_ts == context.thread_ts
9587
assert set_status is not None
9688
assert set_title is not None
9789
assert set_suggested_prompts is not None
@@ -121,6 +113,7 @@ def handle_message(
121113
assert context.channel_id == "D111"
122114
assert context.thread_ts == "1726133698.626339"
123115
assert say.thread_ts == context.thread_ts
116+
assert set_status is not None
124117
assert set_title is not None
125118
assert set_suggested_prompts is not None
126119
assert get_thread_context is not None
@@ -130,7 +123,7 @@ def handle_message(
130123
say("Here you are!")
131124
called["value"] = True
132125
except Exception as e:
133-
say(f"Oops, something went wrong (error: {e}")
126+
say(f"Oops, something went wrong (error: {e})")
134127

135128
request = BoltRequest(body=user_message_event_body, mode="socket_mode")
136129
response = app.dispatch(request)
@@ -154,6 +147,7 @@ def handle_message(
154147
assert context.channel_id == "D111"
155148
assert context.thread_ts == "1726133698.626339"
156149
assert say.thread_ts == context.thread_ts
150+
assert set_status is not None
157151
assert set_title is not None
158152
assert set_suggested_prompts is not None
159153
assert get_thread_context is not None
@@ -163,7 +157,7 @@ def handle_message(
163157
say("Here you are!")
164158
called["value"] = True
165159
except Exception as e:
166-
say(f"Oops, something went wrong (error: {e}")
160+
say(f"Oops, something went wrong (error: {e})")
167161

168162
request = BoltRequest(body=user_message_event_body_with_assistant_thread, mode="socket_mode")
169163
response = app.dispatch(request)
@@ -172,6 +166,7 @@ def handle_message(
172166

173167
def test_message_changed(self):
174168
app = App(client=self.web_client)
169+
called = {"value": False}
175170

176171
@app.event("message")
177172
def handle_message_event(
@@ -190,13 +185,16 @@ def handle_message_event(
190185
assert set_suggested_prompts is not None
191186
assert get_thread_context is not None
192187
assert save_thread_context is not None
188+
called["value"] = True
193189

194190
request = BoltRequest(body=message_changed_event_body, mode="socket_mode")
195191
response = app.dispatch(request)
196192
assert response.status == 200
193+
assert_target_called(called)
197194

198195
def test_channel_user_message(self):
199196
app = App(client=self.web_client)
197+
called = {"value": False}
200198

201199
@app.event("message")
202200
def handle_message_event(
@@ -215,13 +213,16 @@ def handle_message_event(
215213
assert set_suggested_prompts is not None
216214
assert get_thread_context is not None
217215
assert save_thread_context is not None
216+
called["value"] = True
218217

219218
request = BoltRequest(body=channel_user_message_event_body, mode="socket_mode")
220219
response = app.dispatch(request)
221220
assert response.status == 200
221+
assert_target_called(called)
222222

223223
def test_channel_message_changed(self):
224224
app = App(client=self.web_client)
225+
called = {"value": False}
225226

226227
@app.event("message")
227228
def handle_message_event(
@@ -240,7 +241,9 @@ def handle_message_event(
240241
assert set_suggested_prompts is not None
241242
assert get_thread_context is not None
242243
assert save_thread_context is not None
244+
called["value"] = True
243245

244246
request = BoltRequest(body=channel_message_changed_event_body, mode="socket_mode")
245247
response = app.dispatch(request)
246248
assert response.status == 200
249+
assert_target_called(called)

tests/scenario_tests_async/test_events_assistant.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
from tests.utils import remove_os_env_temporarily, restore_os_env
1919

2020

21-
async def assert_target_called(called: dict, timeout: float = 2.0):
21+
async def assert_target_called(called: dict, expected: bool = True, timeout: float = 0.5):
2222
deadline = time.time() + timeout
23-
while called["value"] is False and time.time() < deadline:
23+
while called["value"] is not expected and time.time() < deadline:
2424
await asyncio.sleep(0.1)
25-
assert called["value"] is True
25+
assert called["value"] is expected
2626

2727

2828
class TestAsyncEventsAssistant:
@@ -51,9 +51,15 @@ async def test_thread_started(self):
5151
called = {"value": False}
5252

5353
@assistant.thread_started
54-
async def start_thread(say: AsyncSay, set_suggested_prompts: AsyncSetSuggestedPrompts, context: AsyncBoltContext):
54+
async def start_thread(
55+
say: AsyncSay,
56+
set_suggested_prompts: AsyncSetSuggestedPrompts,
57+
set_status: AsyncSetStatus,
58+
context: AsyncBoltContext,
59+
):
5560
assert context.channel_id == "D111"
5661
assert context.thread_ts == "1726133698.626339"
62+
assert set_status.thread_ts == context.thread_ts
5763
assert say.thread_ts == context.thread_ts
5864
await say("Hi, how can I help you today?")
5965
await set_suggested_prompts(
@@ -107,7 +113,7 @@ async def handle_user_message(say: AsyncSay, set_status: AsyncSetStatus, context
107113
await say("Here you are!")
108114
called["value"] = True
109115
except Exception as e:
110-
await say(f"Oops, something went wrong (error: {e}")
116+
await say(f"Oops, something went wrong (error: {e})")
111117

112118
app.assistant(assistant)
113119

@@ -132,7 +138,7 @@ async def handle_user_message(say: AsyncSay, set_status: AsyncSetStatus, context
132138
await say("Here you are!")
133139
called["value"] = True
134140
except Exception as e:
135-
await say(f"Oops, something went wrong (error: {e}")
141+
await say(f"Oops, something went wrong (error: {e})")
136142

137143
app.assistant(assistant)
138144

@@ -145,58 +151,64 @@ async def handle_user_message(say: AsyncSay, set_status: AsyncSetStatus, context
145151
async def test_message_changed(self):
146152
app = AsyncApp(client=self.web_client)
147153
assistant = AsyncAssistant()
154+
called = {"value": False}
148155

149156
@assistant.user_message
150157
async def handle_user_message():
151-
assert False, "This handler should not be called"
158+
called["value"] = True
152159

153160
@assistant.bot_message
154161
async def handle_bot_message():
155-
assert False, "This handler should not be called"
162+
called["value"] = True
156163

157164
app.assistant(assistant)
158165

159166
request = AsyncBoltRequest(body=message_changed_event_body, mode="socket_mode")
160167
response = await app.async_dispatch(request)
161168
assert response.status == 200
169+
await assert_target_called(called, expected=False)
162170

163171
@pytest.mark.asyncio
164172
async def test_channel_user_message_ignored(self):
165173
app = AsyncApp(client=self.web_client)
166174
assistant = AsyncAssistant()
175+
called = {"value": False}
167176

168177
@assistant.user_message
169178
async def handle_user_message():
170-
assert False, "This handler should not be called"
179+
called["value"] = True
171180

172181
@assistant.bot_message
173182
async def handle_bot_message():
174-
assert False, "This handler should not be called"
183+
called["value"] = True
175184

176185
app.assistant(assistant)
177186

178187
request = AsyncBoltRequest(body=channel_user_message_event_body, mode="socket_mode")
179188
response = await app.async_dispatch(request)
180189
assert response.status == 404
190+
await assert_target_called(called, expected=False)
181191

182192
@pytest.mark.asyncio
183193
async def test_channel_message_changed_ignored(self):
184194
app = AsyncApp(client=self.web_client)
185195
assistant = AsyncAssistant()
196+
called = {"value": False}
186197

187198
@assistant.user_message
188199
async def handle_user_message():
189-
assert False, "This handler should not be called"
200+
called["value"] = True
190201

191202
@assistant.bot_message
192203
async def handle_bot_message():
193-
assert False, "This handler should not be called"
204+
called["value"] = True
194205

195206
app.assistant(assistant)
196207

197208
request = AsyncBoltRequest(body=channel_message_changed_event_body, mode="socket_mode")
198209
response = await app.async_dispatch(request)
199210
assert response.status == 404
211+
await assert_target_called(called, expected=False)
200212

201213

202214
def build_payload(event: dict) -> dict:

0 commit comments

Comments
 (0)