Skip to content

Commit e8e3298

Browse files
committed
Add tests for feedback logic
1 parent 45b2c7a commit e8e3298

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

packages/slackBotFunction/tests/test_feedback.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,52 @@ def test_check_feedback_exists(mock_table, mock_env):
171171
assert result is False
172172

173173

174+
@patch("app.slack.slack_events.table")
175+
@patch("time.time")
176+
def test_store_feedback_no_message_ts_fallback(mock_time, mock_table, mock_env):
177+
"""Test store_feedback fallback path when no message_ts"""
178+
mock_time.return_value = 1000
179+
from app.slack.slack_events import store_feedback
180+
181+
with patch("app.slack.slack_events.get_latest_message_ts", return_value=None):
182+
store_feedback("conv-key", "query", "positive", "user-id", "channel-id")
183+
mock_table.put_item.assert_called_once()
184+
# Should use fallback pk/sk format without condition
185+
call_args = mock_table.put_item.call_args[1]
186+
assert "ConditionExpression" not in call_args
187+
item = call_args["Item"]
188+
assert item["pk"] == "feedback#conv-key"
189+
assert "#note#" in item["sk"]
190+
191+
192+
@patch("app.slack.slack_events.table")
193+
@patch("time.time")
194+
def test_store_conversation_session_with_thread(mock_time, mock_table, mock_env):
195+
"""Test store_conversation_session with thread_ts"""
196+
mock_time.return_value = 1000
197+
from app.slack.slack_events import store_conversation_session
198+
199+
store_conversation_session("conv-key", "session-id", "user-id", "channel-id", "thread-123", "msg-456")
200+
mock_table.put_item.assert_called_once()
201+
item = mock_table.put_item.call_args[1]["Item"]
202+
assert item["thread_ts"] == "thread-123"
203+
assert item["latest_message_ts"] == "msg-456"
204+
205+
206+
@patch("app.slack.slack_events.table")
207+
@patch("time.time")
208+
def test_store_conversation_session_without_thread(mock_time, mock_table, mock_env):
209+
"""Test store_conversation_session without thread_ts"""
210+
mock_time.return_value = 1000
211+
from app.slack.slack_events import store_conversation_session
212+
213+
store_conversation_session("conv-key", "session-id", "user-id", "channel-id")
214+
mock_table.put_item.assert_called_once()
215+
item = mock_table.put_item.call_args[1]["Item"]
216+
assert "thread_ts" not in item
217+
assert "latest_message_ts" not in item
218+
219+
174220
@patch("app.slack.slack_events.table")
175221
def test_cleanup_previous_unfeedback_qa_error_handling(mock_table, mock_env):
176222
"""Test cleanup_previous_unfeedback_qa error handling"""
@@ -215,3 +261,28 @@ def test_store_feedback_client_error_reraise(mock_table, mock_env):
215261
with patch("app.slack.slack_events.get_latest_message_ts", return_value="123"):
216262
with pytest.raises(ClientError):
217263
store_feedback("conv-key", "query", "positive", "user-id", "channel-id")
264+
265+
266+
def test_feedback_text_extraction(mock_env):
267+
"""Test feedback text extraction logic"""
268+
# Test the specific logic: note = text.split(":", 1)[1].strip() if ":" in text else ""
269+
270+
# Test with colon - should extract text after colon
271+
text = "feedback: some feedback text"
272+
note = text.split(":", 1)[1].strip() if ":" in text else ""
273+
assert note == "some feedback text"
274+
275+
# Test without colon - should use empty string
276+
text = "feedback"
277+
note = text.split(":", 1)[1].strip() if ":" in text else ""
278+
assert note == ""
279+
280+
# Test with colon but no text after
281+
text = "feedback:"
282+
note = text.split(":", 1)[1].strip() if ":" in text else ""
283+
assert note == ""
284+
285+
# Test with colon and whitespace
286+
text = "feedback: "
287+
note = text.split(":", 1)[1].strip() if ":" in text else ""
288+
assert note == ""

packages/slackBotFunction/tests/test_slack_handlers.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import pytest
2+
import json
3+
import sys
24
from unittest.mock import Mock, patch
35
import os
46
from botocore.exceptions import ClientError
@@ -511,3 +513,37 @@ def test_feedback_handler_storage_error(mock_env):
511513
):
512514
feedback_handler(mock_ack, mock_body, mock_client)
513515
mock_ack.assert_called_once()
516+
517+
518+
@patch("slack_bolt.App")
519+
@patch("aws_lambda_powertools.utilities.parameters.get_parameter")
520+
@patch("boto3.resource")
521+
def test_is_latest_message_logic(mock_boto_resource, mock_get_parameter, mock_app, mock_env):
522+
"""Test _is_latest_message function logic"""
523+
mock_get_parameter.side_effect = [
524+
json.dumps({"token": "test-token"}),
525+
json.dumps({"secret": "test-secret"}),
526+
]
527+
mock_boto_resource.return_value.Table.return_value = Mock()
528+
529+
if "app.slack.slack_handlers" in sys.modules:
530+
del sys.modules["app.slack.slack_handlers"]
531+
532+
from app.slack.slack_handlers import _is_latest_message
533+
534+
with patch("app.slack.slack_handlers.table") as mock_table:
535+
# Test with matching message_ts
536+
mock_table.get_item.return_value = {"Item": {"latest_message_ts": "123"}}
537+
assert _is_latest_message("conv-key", "123") is True
538+
539+
# Test with non-matching message_ts
540+
mock_table.get_item.return_value = {"Item": {"latest_message_ts": "456"}}
541+
assert _is_latest_message("conv-key", "123") is False
542+
543+
# Test with no Item in response
544+
mock_table.get_item.return_value = {}
545+
assert _is_latest_message("conv-key", "123") is False
546+
547+
# Test with exception
548+
mock_table.get_item.side_effect = Exception("DB error")
549+
assert _is_latest_message("conv-key", "123") is False

0 commit comments

Comments
 (0)