feat: auto-join channels on admin invite
This commit is contained in:
81
tests/test_chanmgmt.py
Normal file
81
tests/test_chanmgmt.py
Normal file
@@ -0,0 +1,81 @@
|
||||
"""Tests for the chanmgmt plugin (invite-join)."""
|
||||
|
||||
import asyncio
|
||||
import importlib.util
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from derp.irc import Message
|
||||
|
||||
# plugins/ is not a Python package -- load the module from file path
|
||||
_spec = importlib.util.spec_from_file_location(
|
||||
"plugins.chanmgmt",
|
||||
Path(__file__).resolve().parent.parent / "plugins" / "chanmgmt.py",
|
||||
)
|
||||
_mod = importlib.util.module_from_spec(_spec)
|
||||
sys.modules[_spec.name] = _mod
|
||||
_spec.loader.exec_module(_mod)
|
||||
|
||||
from plugins.chanmgmt import on_invite # noqa: E402 # isort: skip
|
||||
|
||||
|
||||
# -- Helpers -----------------------------------------------------------------
|
||||
|
||||
class _FakeConn:
|
||||
"""Minimal connection stand-in."""
|
||||
|
||||
def __init__(self):
|
||||
self.sent: list[str] = []
|
||||
|
||||
async def send(self, raw: str) -> None:
|
||||
self.sent.append(raw)
|
||||
|
||||
|
||||
class _FakeBot:
|
||||
"""Minimal bot stand-in."""
|
||||
|
||||
def __init__(self, *, admin: bool = False):
|
||||
self.joined: list[str] = []
|
||||
self._admin = admin
|
||||
self.conn = _FakeConn()
|
||||
|
||||
def _is_admin(self, message) -> bool:
|
||||
return self._admin
|
||||
|
||||
async def join(self, channel: str) -> None:
|
||||
self.joined.append(channel)
|
||||
|
||||
|
||||
def _invite(nick: str, channel: str) -> Message:
|
||||
"""Create an INVITE message: :nick!user@host INVITE botname #channel."""
|
||||
return Message(
|
||||
raw="",
|
||||
prefix=f"{nick}!~{nick}@host",
|
||||
nick=nick,
|
||||
command="INVITE",
|
||||
params=["derp", channel],
|
||||
tags={},
|
||||
)
|
||||
|
||||
|
||||
# -- Tests -------------------------------------------------------------------
|
||||
|
||||
class TestOnInvite:
|
||||
def test_admin_joins(self):
|
||||
bot = _FakeBot(admin=True)
|
||||
asyncio.run(on_invite(bot, _invite("oper", "#secret")))
|
||||
assert "#secret" in bot.joined
|
||||
|
||||
def test_non_admin_ignored(self):
|
||||
bot = _FakeBot(admin=False)
|
||||
asyncio.run(on_invite(bot, _invite("rando", "#trap")))
|
||||
assert bot.joined == []
|
||||
|
||||
def test_missing_channel_ignored(self):
|
||||
bot = _FakeBot(admin=True)
|
||||
msg = Message(
|
||||
raw="", prefix="oper!~oper@host", nick="oper",
|
||||
command="INVITE", params=["derp"], tags={},
|
||||
)
|
||||
asyncio.run(on_invite(bot, msg))
|
||||
assert bot.joined == []
|
||||
Reference in New Issue
Block a user