Skip to content

Commit 4553ddf

Browse files
test: test cases for new methods
1 parent b2284f6 commit 4553ddf

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed

packages/slackBotFunction/tests/test_handler_utils.py

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,169 @@ def test_extract_pull_request_id_extracts_when_there_is_a_mention():
359359
# assertions
360360
assert pr_id == "12345"
361361
assert text == "<@U123> some question"
362+
363+
364+
def test_was_bot_mentioned_in_thread_root_with_mention(mock_env: Mock):
365+
"""test that was_bot_mentioned_in_thread_root returns true when this bot is mentioned"""
366+
mock_client = Mock()
367+
mock_client.auth_test.return_value = {"ok": True, "user_id": "U123ABC"}
368+
mock_client.conversations_replies.return_value = {
369+
"ok": True,
370+
"messages": [{"text": "<@U123ABC> hello bot", "ts": "1234567890.123456"}],
371+
}
372+
373+
if "app.utils.handler_utils" in sys.modules:
374+
del sys.modules["app.utils.handler_utils"]
375+
from app.utils.handler_utils import was_bot_mentioned_in_thread_root
376+
377+
result = was_bot_mentioned_in_thread_root("C123", "1234567890.123456", mock_client)
378+
379+
assert result is True
380+
mock_client.conversations_replies.assert_called_once_with(channel="C123", ts="1234567890.123456", inclusive=True)
381+
382+
383+
def test_was_bot_mentioned_in_thread_root_without_mention(mock_env: Mock):
384+
"""test that was_bot_mentioned_in_thread_root returns false when bot is not mentioned"""
385+
mock_client = Mock()
386+
mock_client.auth_test.return_value = {"ok": True, "user_id": "U123ABC"}
387+
mock_client.conversations_replies.return_value = {
388+
"ok": True,
389+
"messages": [{"text": "hello everyone", "ts": "1234567890.123456"}],
390+
}
391+
392+
if "app.utils.handler_utils" in sys.modules:
393+
del sys.modules["app.utils.handler_utils"]
394+
from app.utils.handler_utils import was_bot_mentioned_in_thread_root
395+
396+
result = was_bot_mentioned_in_thread_root("C123", "1234567890.123456", mock_client)
397+
398+
assert result is False
399+
400+
401+
def test_was_bot_mentioned_in_thread_root_exception(mock_env: Mock):
402+
"""tes that was_bot_mentioned_in_thread_root fails open on exception"""
403+
mock_client = Mock()
404+
mock_client.conversations_replies.side_effect = Exception("API Error")
405+
406+
if "app.utils.handler_utils" in sys.modules:
407+
del sys.modules["app.utils.handler_utils"]
408+
from app.utils.handler_utils import was_bot_mentioned_in_thread_root
409+
410+
result = was_bot_mentioned_in_thread_root("C123", "1234567890.123456", mock_client)
411+
412+
assert result is True
413+
414+
415+
def test_should_reply_to_message_dm(mock_env: Mock):
416+
"""test should_reply_to_message returns True for DMs"""
417+
if "app.utils.handler_utils" in sys.modules:
418+
del sys.modules["app.utils.handler_utils"]
419+
from app.utils.handler_utils import should_reply_to_message
420+
421+
event = {"channel_type": "im", "type": "message", "channel": "D123", "ts": "123"}
422+
result = should_reply_to_message(event)
423+
424+
assert result is True
425+
426+
427+
def test_should_reply_to_message_group_without_thread(mock_env: Mock):
428+
"""test should_reply_to_message returns False for group messages without thread"""
429+
if "app.utils.handler_utils" in sys.modules:
430+
del sys.modules["app.utils.handler_utils"]
431+
from app.utils.handler_utils import should_reply_to_message
432+
433+
event = {"channel_type": "group", "type": "message", "channel": "G123", "ts": "123"}
434+
result = should_reply_to_message(event)
435+
436+
assert result is False
437+
438+
439+
def test_should_reply_to_message_channel_thread_with_bot_mention(mock_env: Mock):
440+
"""test should_reply_to_message returns True for channel thread where bot was mentioned"""
441+
mock_client = Mock()
442+
mock_client.auth_test.return_value = {"ok": True, "user_id": "U123"}
443+
mock_client.conversations_replies.return_value = {
444+
"ok": True,
445+
"messages": [{"text": "<@U123> help me", "ts": "1234567890.123456"}],
446+
}
447+
448+
if "app.utils.handler_utils" in sys.modules:
449+
del sys.modules["app.utils.handler_utils"]
450+
from app.utils.handler_utils import should_reply_to_message
451+
452+
event = {"channel_type": "channel", "type": "message", "channel": "C123", "thread_ts": "1234567890.123456"}
453+
result = should_reply_to_message(event, mock_client)
454+
455+
assert result is True
456+
457+
458+
def test_should_reply_to_message_channel_thread_without_bot_mention(mock_env: Mock):
459+
"""test should_reply_to_message returns False for channel thread where bot was not mentioned"""
460+
mock_client = Mock()
461+
mock_client.auth_test.return_value = {"ok": True, "user_id": "U123"}
462+
mock_client.conversations_replies.return_value = {
463+
"ok": True,
464+
"messages": [{"text": "some discussion", "ts": "1234567890.123456"}],
465+
}
466+
467+
if "app.utils.handler_utils" in sys.modules:
468+
del sys.modules["app.utils.handler_utils"]
469+
from app.utils.handler_utils import should_reply_to_message
470+
471+
event = {"channel_type": "channel", "type": "message", "channel": "C123", "thread_ts": "1234567890.123456"}
472+
result = should_reply_to_message(event, mock_client)
473+
474+
assert result is False
475+
476+
477+
def test_should_reply_to_message_channel_thread_no_client(mock_env: Mock):
478+
"""test should_reply_to_message fails open when no client provided"""
479+
# setup mocks
480+
if "app.utils.handler_utils" in sys.modules:
481+
del sys.modules["app.utils.handler_utils"]
482+
from app.utils.handler_utils import should_reply_to_message
483+
484+
event = {"channel_type": "channel", "type": "message", "channel": "C123", "thread_ts": "1234567890.123456"}
485+
result = should_reply_to_message(event, None)
486+
487+
assert result is True
488+
489+
490+
def test_was_bot_mentioned_in_thread_different_bot(mock_env: Mock):
491+
"""test that was_bot_mentioned_in_thread_root returns false when a different bot is mentioned"""
492+
mock_client = Mock()
493+
mock_client.auth_test.return_value = {"ok": True, "user_id": "U123ABC"}
494+
mock_client.conversations_replies.return_value = {
495+
"ok": True,
496+
"messages": [{"text": "<@U999XYZ> hello other bot", "ts": "1234567890.123456"}], # Different bot
497+
}
498+
499+
if "app.utils.handler_utils" in sys.modules:
500+
del sys.modules["app.utils.handler_utils"]
501+
from app.utils.handler_utils import was_bot_mentioned_in_thread_root
502+
503+
result = was_bot_mentioned_in_thread_root("C123", "1234567890.123456", mock_client)
504+
505+
assert result is False
506+
507+
508+
def test_was_bot_mentioned_later_in_thread(mock_env: Mock):
509+
"""test that bot mention later in thread is detected"""
510+
mock_client = Mock()
511+
mock_client.auth_test.return_value = {"ok": True, "user_id": "U123ABC"}
512+
mock_client.conversations_replies.return_value = {
513+
"ok": True,
514+
"messages": [
515+
{"text": "Starting a discussion", "ts": "1234567890.123456"},
516+
{"text": "Some reply", "ts": "1234567890.123457"},
517+
{"text": "<@U123ABC> can you help with this?", "ts": "1234567890.123458"}, # Bot mentioned later
518+
],
519+
}
520+
521+
if "app.utils.handler_utils" in sys.modules:
522+
del sys.modules["app.utils.handler_utils"]
523+
from app.utils.handler_utils import was_bot_mentioned_in_thread_root
524+
525+
result = was_bot_mentioned_in_thread_root("C123", "1234567890.123456", mock_client)
526+
527+
assert result is True

0 commit comments

Comments
 (0)