mirror of
https://github.com/agessaman/meshcore-bot.git
synced 2026-03-30 12:05:38 +00:00
- Added support for channel-specific triggers in the CommandManager, allowing triggers to be restricted to designated channels via `channel.<key>` or `channels.<key>` configuration. - Updated `config.ini.example` to include examples of channel restrictions for triggers. - Enhanced tests to verify that triggers only match in specified channels, ensuring proper functionality of the new feature.
149 lines
5.7 KiB
Python
149 lines
5.7 KiB
Python
from types import SimpleNamespace
|
|
from unittest.mock import patch
|
|
|
|
from modules.command_manager import CommandManager
|
|
|
|
|
|
class TestRandomLine:
|
|
def test_match_randomline_exact_match_normalizes_spaces_and_case(self, mock_bot, tmp_path):
|
|
f = tmp_path / "momjoke.txt"
|
|
f.write_text("line one\n\nline two\n", encoding="utf-8")
|
|
|
|
if not mock_bot.config.has_section("RandomLine"):
|
|
mock_bot.config.add_section("RandomLine")
|
|
mock_bot.config.set("RandomLine", "prefix.default", "")
|
|
mock_bot.config.set("RandomLine", "triggers.momjoke", "momjoke,mom joke")
|
|
mock_bot.config.set("RandomLine", "file.momjoke", str(f))
|
|
mock_bot.config.set("RandomLine", "prefix.momjoke", "🥸")
|
|
|
|
manager = CommandManager(mock_bot)
|
|
manager.command_prefix = ""
|
|
|
|
msg = SimpleNamespace(
|
|
content=" MOM JOKE ",
|
|
is_dm=True,
|
|
sender_id="abc",
|
|
channel="general",
|
|
)
|
|
|
|
with patch("modules.command_manager.random.choice", return_value="line two"):
|
|
result = manager.match_randomline(msg)
|
|
|
|
assert result is not None
|
|
key, response = result
|
|
assert key == "momjoke"
|
|
assert response == "🥸 line two"
|
|
|
|
def test_match_randomline_does_not_match_extra_words(self, mock_bot, tmp_path):
|
|
f = tmp_path / "funfacts.txt"
|
|
f.write_text("fact one\n", encoding="utf-8")
|
|
|
|
if not mock_bot.config.has_section("RandomLine"):
|
|
mock_bot.config.add_section("RandomLine")
|
|
mock_bot.config.set("RandomLine", "prefix.default", "")
|
|
mock_bot.config.set("RandomLine", "triggers.funfact", "funfact,fun fact")
|
|
mock_bot.config.set("RandomLine", "file.funfact", str(f))
|
|
mock_bot.config.set("RandomLine", "prefix.funfact", "💡")
|
|
|
|
manager = CommandManager(mock_bot)
|
|
manager.command_prefix = ""
|
|
|
|
msg = SimpleNamespace(
|
|
content="fun fact please",
|
|
is_dm=True,
|
|
sender_id="abc",
|
|
channel="general",
|
|
)
|
|
|
|
assert manager.match_randomline(msg) is None
|
|
|
|
def test_match_randomline_channel_filter_allowed(self, mock_bot, tmp_path):
|
|
"""When channel.<key> is set, trigger only matches in that channel."""
|
|
f = tmp_path / "momjoke.txt"
|
|
f.write_text("line one\n", encoding="utf-8")
|
|
|
|
if not mock_bot.config.has_section("RandomLine"):
|
|
mock_bot.config.add_section("RandomLine")
|
|
mock_bot.config.set("RandomLine", "prefix.default", "")
|
|
mock_bot.config.set("RandomLine", "triggers.momjoke", "momjoke")
|
|
mock_bot.config.set("RandomLine", "file.momjoke", str(f))
|
|
mock_bot.config.set("RandomLine", "prefix.momjoke", "🥸")
|
|
mock_bot.config.set("RandomLine", "channel.momjoke", "#jokes")
|
|
|
|
manager = CommandManager(mock_bot)
|
|
manager.command_prefix = ""
|
|
manager.monitor_channels = ["general", "jokes"]
|
|
|
|
msg = SimpleNamespace(
|
|
content="momjoke",
|
|
is_dm=False,
|
|
sender_id="abc",
|
|
channel="jokes",
|
|
)
|
|
|
|
with patch("modules.command_manager.random.choice", return_value="line one"):
|
|
result = manager.match_randomline(msg)
|
|
|
|
assert result is not None
|
|
key, response = result
|
|
assert key == "momjoke"
|
|
|
|
def test_match_randomline_channel_filter_denied(self, mock_bot, tmp_path):
|
|
"""When channel.<key> is set, trigger does not match in other channels."""
|
|
f = tmp_path / "momjoke.txt"
|
|
f.write_text("line one\n", encoding="utf-8")
|
|
|
|
if not mock_bot.config.has_section("RandomLine"):
|
|
mock_bot.config.add_section("RandomLine")
|
|
mock_bot.config.set("RandomLine", "prefix.default", "")
|
|
mock_bot.config.set("RandomLine", "triggers.momjoke", "momjoke")
|
|
mock_bot.config.set("RandomLine", "file.momjoke", str(f))
|
|
mock_bot.config.set("RandomLine", "prefix.momjoke", "🥸")
|
|
mock_bot.config.set("RandomLine", "channel.momjoke", "#jokes")
|
|
|
|
manager = CommandManager(mock_bot)
|
|
manager.command_prefix = ""
|
|
manager.monitor_channels = ["general", "jokes"]
|
|
|
|
msg = SimpleNamespace(
|
|
content="momjoke",
|
|
is_dm=False,
|
|
sender_id="abc",
|
|
channel="general",
|
|
)
|
|
|
|
assert manager.match_randomline(msg) is None
|
|
|
|
def test_match_randomline_channel_override_allows_channel_not_in_monitor(self, mock_bot, tmp_path):
|
|
"""When channel.<key> is set, trigger works in that channel even if not in monitor_channels."""
|
|
f = tmp_path / "momjoke.txt"
|
|
f.write_text("line one\n", encoding="utf-8")
|
|
|
|
if not mock_bot.config.has_section("RandomLine"):
|
|
mock_bot.config.add_section("RandomLine")
|
|
mock_bot.config.set("RandomLine", "prefix.default", "")
|
|
mock_bot.config.set("RandomLine", "triggers.momjoke", "momjoke,mom joke")
|
|
mock_bot.config.set("RandomLine", "file.momjoke", str(f))
|
|
mock_bot.config.set("RandomLine", "prefix.momjoke", "🥸")
|
|
mock_bot.config.set("RandomLine", "channel.momjoke", "#jokes")
|
|
|
|
manager = CommandManager(mock_bot)
|
|
manager.command_prefix = ""
|
|
# #jokes is NOT in monitor list (e.g. only #bot, BotTest are monitored)
|
|
manager.monitor_channels = ["BotTest", "#bot"]
|
|
|
|
msg = SimpleNamespace(
|
|
content="mom joke",
|
|
is_dm=False,
|
|
sender_id="abc",
|
|
channel="#jokes",
|
|
)
|
|
|
|
with patch("modules.command_manager.random.choice", return_value="line one"):
|
|
result = manager.match_randomline(msg)
|
|
|
|
assert result is not None
|
|
key, response = result
|
|
assert key == "momjoke"
|
|
assert response == "🥸 line one"
|