mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-07-04 13:23:07 +02:00
277 lines
9.4 KiB
Python
277 lines
9.4 KiB
Python
"""
|
|
Unit tests for Signal Bot main functionality
|
|
"""
|
|
import pytest
|
|
import json
|
|
import os
|
|
from unittest.mock import patch, MagicMock, AsyncMock
|
|
|
|
|
|
class TestStringCounter:
|
|
"""Tests for StringCounter class"""
|
|
|
|
def test_init(self):
|
|
"""Test StringCounter initialization"""
|
|
from main import StringCounter
|
|
counter = StringCounter()
|
|
assert counter.string_map == {}
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_string_map_new_key(self):
|
|
"""Test updating string map with a new key"""
|
|
from main import StringCounter
|
|
counter = StringCounter()
|
|
result = await counter.update_string_map("uuid1", "User One")
|
|
assert "uuid1" in result
|
|
assert result["uuid1"]["common_name"] == "User One"
|
|
assert result["uuid1"]["count"] == 1
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_string_map_existing_key(self):
|
|
"""Test updating string map with an existing key increments count"""
|
|
from main import StringCounter
|
|
counter = StringCounter()
|
|
await counter.update_string_map("uuid1", "User One")
|
|
result = await counter.update_string_map("uuid1", "User One")
|
|
assert result["uuid1"]["count"] == 2
|
|
|
|
def test_get_common_name_exists(self):
|
|
"""Test getting common name for existing key"""
|
|
from main import StringCounter
|
|
counter = StringCounter()
|
|
counter.string_map["uuid1"] = {"common_name": "User One", "count": 1}
|
|
assert counter.get_common_name("uuid1") == "User One"
|
|
|
|
def test_get_common_name_not_exists(self):
|
|
"""Test getting common name for non-existing key"""
|
|
from main import StringCounter
|
|
counter = StringCounter()
|
|
assert counter.get_common_name("nonexistent") is None
|
|
|
|
|
|
class TestMessageParsing:
|
|
"""Tests for message parsing functions"""
|
|
|
|
def test_message_message(self):
|
|
"""Test extracting message from inside_message"""
|
|
from main import message_message
|
|
inside_message = {"message": "Hello World"}
|
|
assert message_message(inside_message) == "Hello World"
|
|
|
|
def test_message_message_no_message(self):
|
|
"""Test extracting message when no message key"""
|
|
from main import message_message
|
|
inside_message = {}
|
|
assert message_message(inside_message) is None
|
|
|
|
def test_message_group_id(self):
|
|
"""Test extracting group ID from message"""
|
|
from main import message_group_id
|
|
inside_message = {"groupInfo": {"groupId": "group123"}}
|
|
assert message_group_id(inside_message) == "group123"
|
|
|
|
def test_message_group_id_no_group(self):
|
|
"""Test extracting group ID when no group info"""
|
|
from main import message_group_id
|
|
inside_message = {}
|
|
assert message_group_id(inside_message) == {}
|
|
|
|
def test_extract_message_content_data_message(self):
|
|
"""Test extracting content from dataMessage"""
|
|
from main import extract_message_content
|
|
message = json.dumps({
|
|
"envelope": {
|
|
"dataMessage": {
|
|
"message": "Test message",
|
|
"groupInfo": {"groupId": "group123"}
|
|
}
|
|
}
|
|
})
|
|
content = extract_message_content(message)
|
|
assert content["message"] == "Test message"
|
|
|
|
def test_extract_message_content_sync_message(self):
|
|
"""Test extracting content from syncMessage"""
|
|
from main import extract_message_content
|
|
message = json.dumps({
|
|
"envelope": {
|
|
"syncMessage": {
|
|
"sentMessage": {
|
|
"message": "Sync message"
|
|
}
|
|
}
|
|
}
|
|
})
|
|
content = extract_message_content(message)
|
|
assert content["message"] == "Sync message"
|
|
|
|
def test_extract_source_uuid(self):
|
|
"""Test extracting source UUID from message"""
|
|
from main import extract_source_uuid
|
|
message = {"sourceUuid": "uuid123"}
|
|
assert extract_source_uuid(message) == "uuid123"
|
|
|
|
def test_extract_source_name(self):
|
|
"""Test extracting source name from message"""
|
|
from main import extract_source_name
|
|
message = {"sourceName": "Test User"}
|
|
assert extract_source_name(message) == "Test User"
|
|
|
|
|
|
class TestReactionDetection:
|
|
"""Tests for message reaction detection"""
|
|
|
|
def test_is_message_reaction_data_message(self):
|
|
"""Test detecting reaction in dataMessage"""
|
|
from main import is_message_reaction
|
|
message = json.dumps({
|
|
"envelope": {
|
|
"dataMessage": {
|
|
"reaction": {"emoji": "👍"}
|
|
}
|
|
}
|
|
})
|
|
assert is_message_reaction(message) is False # dataMessage reactions return False
|
|
|
|
def test_is_message_reaction_sync_message(self):
|
|
"""Test detecting reaction in syncMessage"""
|
|
from main import is_message_reaction
|
|
message = json.dumps({
|
|
"envelope": {
|
|
"syncMessage": {
|
|
"reaction": {"emoji": "👍"}
|
|
}
|
|
}
|
|
})
|
|
assert is_message_reaction(message) is True
|
|
|
|
def test_is_not_reaction(self):
|
|
"""Test message that is not a reaction"""
|
|
from main import is_message_reaction
|
|
message = json.dumps({
|
|
"envelope": {
|
|
"dataMessage": {
|
|
"message": "Regular message"
|
|
}
|
|
}
|
|
})
|
|
assert is_message_reaction(message) is False
|
|
|
|
|
|
class TestShouldCount:
|
|
"""Tests for should_count function"""
|
|
|
|
def test_should_count_regular_message(self):
|
|
"""Test that regular messages should be counted"""
|
|
from main import should_count
|
|
message_content = {
|
|
"dataMessage": {
|
|
"message": "Test message"
|
|
}
|
|
}
|
|
assert should_count(message_content) is True
|
|
|
|
def test_should_not_count_sticker(self):
|
|
"""Test that sticker messages should not be counted"""
|
|
from main import should_count
|
|
message_content = {
|
|
"dataMessage": {
|
|
"sticker": {
|
|
"packId": "123",
|
|
"stickerId": 1
|
|
}
|
|
}
|
|
}
|
|
assert should_count(message_content) is False
|
|
|
|
def test_should_count_empty_content(self):
|
|
"""Test that empty dict returns True (no sticker found means message is counted)"""
|
|
from main import should_count
|
|
# Empty dict is counted (no sticker found)
|
|
assert should_count({}) is True
|
|
|
|
|
|
class TestDebugMode:
|
|
"""Tests for debug mode functionality"""
|
|
|
|
def test_get_recipient_production_mode(self):
|
|
"""Test recipient in production mode"""
|
|
import main
|
|
original_debug = main.DEBUG_MODE
|
|
original_group_id_send = main.GROUP_ID_SEND
|
|
try:
|
|
main.DEBUG_MODE = False
|
|
main.GROUP_ID_SEND = "group_send_123"
|
|
assert main.get_recipient() == "group_send_123"
|
|
finally:
|
|
main.DEBUG_MODE = original_debug
|
|
main.GROUP_ID_SEND = original_group_id_send
|
|
|
|
def test_get_recipient_debug_mode(self):
|
|
"""Test recipient in debug mode (note to self)"""
|
|
import main
|
|
original_debug = main.DEBUG_MODE
|
|
original_phone = main.PHONE_NUMBER
|
|
try:
|
|
main.DEBUG_MODE = True
|
|
main.PHONE_NUMBER = "+1234567890"
|
|
assert main.get_recipient() == "+1234567890"
|
|
finally:
|
|
main.DEBUG_MODE = original_debug
|
|
main.PHONE_NUMBER = original_phone
|
|
|
|
def test_get_source_group_production_mode(self):
|
|
"""Test source group in production mode"""
|
|
import main
|
|
original_debug = main.DEBUG_MODE
|
|
original_group_id = main.GROUP_ID
|
|
try:
|
|
main.DEBUG_MODE = False
|
|
main.GROUP_ID = "group_receive_123"
|
|
assert main.get_source_group() == "group_receive_123"
|
|
finally:
|
|
main.DEBUG_MODE = original_debug
|
|
main.GROUP_ID = original_group_id
|
|
|
|
def test_get_source_group_debug_mode(self):
|
|
"""Test source group in debug mode (None = all self messages)"""
|
|
import main
|
|
original_debug = main.DEBUG_MODE
|
|
try:
|
|
main.DEBUG_MODE = True
|
|
assert main.get_source_group() is None
|
|
finally:
|
|
main.DEBUG_MODE = original_debug
|
|
|
|
|
|
class TestCommandMap:
|
|
"""Tests for command map configuration"""
|
|
|
|
def test_command_map_exists(self):
|
|
"""Test that command map is defined"""
|
|
from main import command_map
|
|
assert command_map is not None
|
|
assert len(command_map) > 0
|
|
|
|
def test_cat_commands_exist(self):
|
|
"""Test that cat commands are in the command map"""
|
|
from main import command_map
|
|
# Check that one of the cat command keys exists
|
|
cat_command_found = False
|
|
for commands in command_map.keys():
|
|
if "!cat" in commands or "!kot" in commands:
|
|
cat_command_found = True
|
|
break
|
|
assert cat_command_found
|
|
|
|
def test_dog_commands_exist(self):
|
|
"""Test that dog commands are in the command map"""
|
|
from main import command_map
|
|
# Check that one of the dog command keys exists
|
|
dog_command_found = False
|
|
for commands in command_map.keys():
|
|
if "!dog" in commands or "!pies" in commands:
|
|
dog_command_found = True
|
|
break
|
|
assert dog_command_found
|