mirror of
https://github.com/agessaman/meshcore-bot.git
synced 2026-08-01 16:30:02 +00:00
172 lines
6.6 KiB
Python
172 lines
6.6 KiB
Python
from types import SimpleNamespace
|
|
from unittest.mock import patch
|
|
|
|
from modules.command_manager import CommandManager
|
|
|
|
|
|
class TestRandomLine:
|
|
def test_bundled_default_loads_outside_source_tree(self, mock_bot, tmp_path, monkeypatch):
|
|
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")
|
|
mock_bot.config.set("RandomLine", "file.funfact", "data/randomlines/funfacts.txt")
|
|
|
|
manager = CommandManager(mock_bot)
|
|
manager.command_prefix = ""
|
|
monkeypatch.chdir(tmp_path)
|
|
msg = SimpleNamespace(
|
|
content="funfact",
|
|
is_dm=True,
|
|
sender_id="abc",
|
|
channel="general",
|
|
)
|
|
|
|
with patch("modules.command_manager.random.choice", return_value="bundled fact") as choice:
|
|
result = manager.match_randomline(msg)
|
|
|
|
assert result == ("funfact", "bundled fact")
|
|
assert choice.call_args.args[0]
|
|
|
|
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"
|