Skip to content

Commit 45b2c7a

Browse files
committed
Add more tests to suits
1 parent e7ceee0 commit 45b2c7a

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

packages/slackBotFunction/tests/test_async_processing.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,47 @@ def test_process_async_slack_event_post_error_message_fails(mock_boto_resource,
364364

365365
# Should not raise exception even when error posting fails
366366
process_async_slack_event(slack_event_data)
367+
368+
369+
@patch("slack_bolt.App")
370+
@patch("aws_lambda_powertools.utilities.parameters.get_parameter")
371+
@patch("boto3.resource")
372+
def test_process_async_slack_event_dm_context(mock_boto_resource, mock_get_parameter, mock_app, mock_env):
373+
"""Test process_async_slack_event with DM context"""
374+
mock_get_parameter.side_effect = [
375+
json.dumps({"token": "test-token"}),
376+
json.dumps({"secret": "test-secret"}),
377+
]
378+
mock_boto_resource.return_value.Table.return_value = Mock()
379+
380+
if "app.slack.slack_events" in sys.modules:
381+
del sys.modules["app.slack.slack_events"]
382+
383+
with patch("slack_sdk.WebClient") as mock_webclient, patch(
384+
"app.slack.slack_events.query_bedrock"
385+
) as mock_bedrock, patch("app.slack.slack_events.get_conversation_session") as mock_get_session, patch(
386+
"boto3.client"
387+
):
388+
389+
mock_client = Mock()
390+
mock_client.chat_postMessage.return_value = {"ts": "123"}
391+
mock_webclient.return_value = mock_client
392+
393+
mock_bedrock.return_value = {"output": {"text": "AI response"}, "sessionId": "new-session"}
394+
mock_get_session.return_value = None
395+
396+
from app.slack.slack_events import process_async_slack_event
397+
398+
slack_event_data = {
399+
"event": {
400+
"text": "test question",
401+
"user": "U456",
402+
"channel": "D789",
403+
"ts": "123",
404+
"channel_type": "im", # DM context
405+
},
406+
"event_id": "evt123",
407+
"bot_token": "bot-token",
408+
}
409+
410+
process_async_slack_event(slack_event_data)

packages/slackBotFunction/tests/test_feedback.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,38 @@ def test_cleanup_previous_unfeedback_qa_error_handling(mock_table, mock_env):
180180
with patch("app.slack.slack_events.check_feedback_exists", side_effect=Exception("Error")):
181181
# Should not raise exception
182182
cleanup_previous_unfeedback_qa("conv-key", "123", session_data)
183+
184+
185+
@patch("app.slack.slack_events.table")
186+
def test_get_conversation_session_data_no_item(mock_table, mock_env):
187+
"""Test get_conversation_session_data when no item exists"""
188+
mock_table.get_item.return_value = {} # No Item key
189+
from app.slack.slack_events import get_conversation_session_data
190+
191+
result = get_conversation_session_data("test-key")
192+
assert result is None
193+
194+
195+
@patch("app.slack.slack_events.table")
196+
def test_get_latest_message_ts_no_item(mock_table, mock_env):
197+
"""Test get_latest_message_ts when no item exists"""
198+
mock_table.get_item.return_value = {} # No Item key
199+
from app.slack.slack_events import get_latest_message_ts
200+
201+
result = get_latest_message_ts("test-key")
202+
assert result is None
203+
204+
205+
@patch("app.slack.slack_events.table")
206+
def test_store_feedback_client_error_reraise(mock_table, mock_env):
207+
"""Test store_feedback re-raises ClientError"""
208+
from botocore.exceptions import ClientError
209+
210+
error = ClientError({"Error": {"Code": "ValidationException"}}, "PutItem")
211+
mock_table.put_item.side_effect = error
212+
213+
from app.slack.slack_events import store_feedback
214+
215+
with patch("app.slack.slack_events.get_latest_message_ts", return_value="123"):
216+
with pytest.raises(ClientError):
217+
store_feedback("conv-key", "query", "positive", "user-id", "channel-id")

packages/slackBotFunction/tests/test_slack_handlers.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,3 +472,42 @@ def test_app_mention_handler_normal_mention(mock_env):
472472
) as mock_trigger:
473473
app_mention_handler(mock_event, mock_ack, mock_body, mock_client)
474474
mock_trigger.assert_called_once()
475+
476+
477+
def test_feedback_handler_conditional_check_failed(mock_env):
478+
"""Test feedback_handler with ConditionalCheckFailedException"""
479+
from app.slack.slack_handlers import feedback_handler
480+
from botocore.exceptions import ClientError
481+
482+
mock_ack = Mock()
483+
mock_body = {
484+
"user": {"id": "U123"},
485+
"actions": [{"action_id": "feedback_yes", "value": '{"ck": "conv-key", "mt": "123"}'}],
486+
}
487+
mock_client = Mock()
488+
489+
error = ClientError({"Error": {"Code": "ConditionalCheckFailedException"}}, "PutItem")
490+
491+
with patch("app.slack.slack_handlers._is_latest_message", return_value=True), patch(
492+
"app.slack.slack_handlers.store_feedback", side_effect=error
493+
):
494+
feedback_handler(mock_ack, mock_body, mock_client)
495+
mock_ack.assert_called_once()
496+
497+
498+
def test_feedback_handler_storage_error(mock_env):
499+
"""Test feedback_handler with storage error"""
500+
from app.slack.slack_handlers import feedback_handler
501+
502+
mock_ack = Mock()
503+
mock_body = {
504+
"user": {"id": "U123"},
505+
"actions": [{"action_id": "feedback_yes", "value": '{"ck": "conv-key", "mt": "123"}'}],
506+
}
507+
mock_client = Mock()
508+
509+
with patch("app.slack.slack_handlers._is_latest_message", return_value=True), patch(
510+
"app.slack.slack_handlers.store_feedback", side_effect=Exception("Storage error")
511+
):
512+
feedback_handler(mock_ack, mock_body, mock_client)
513+
mock_ack.assert_called_once()

0 commit comments

Comments
 (0)