Files
derp/tests/test_encode.py
user 9abf8dce64 feat: add !paste command and unit tests for 5 core plugins
Add cmd_paste to flaskpaste plugin (create paste, return URL).
Add test suites for encode, hash, defang, cidr, and dns plugins
(83 new test cases, 1093 total).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-21 16:54:18 +01:00

142 lines
4.3 KiB
Python

"""Tests for the encode/decode plugin."""
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.encode", Path(__file__).resolve().parent.parent / "plugins" / "encode.py",
)
_mod = importlib.util.module_from_spec(_spec)
sys.modules[_spec.name] = _mod
_spec.loader.exec_module(_mod)
from plugins.encode import cmd_decode, cmd_encode # noqa: E402
# -- Helpers -----------------------------------------------------------------
class _FakeBot:
def __init__(self):
self.replied: list[str] = []
async def reply(self, message, text: str) -> None:
self.replied.append(text)
def _msg(text: str) -> Message:
return Message(
raw="", prefix="alice!~alice@host", nick="alice",
command="PRIVMSG", params=["#test", text], tags={},
)
# -- Encode ------------------------------------------------------------------
class TestEncodeB64:
def test_encode(self):
bot = _FakeBot()
asyncio.run(cmd_encode(bot, _msg("!encode b64 hello")))
assert bot.replied[0] == "aGVsbG8="
def test_decode(self):
bot = _FakeBot()
asyncio.run(cmd_decode(bot, _msg("!decode b64 aGVsbG8=")))
assert bot.replied[0] == "hello"
def test_roundtrip(self):
bot = _FakeBot()
asyncio.run(cmd_encode(bot, _msg("!encode b64 test data")))
encoded = bot.replied[0]
asyncio.run(cmd_decode(bot, _msg(f"!decode b64 {encoded}")))
assert bot.replied[1] == "test data"
class TestEncodeHex:
def test_encode(self):
bot = _FakeBot()
asyncio.run(cmd_encode(bot, _msg("!encode hex hello")))
assert bot.replied[0] == "68656c6c6f"
def test_decode(self):
bot = _FakeBot()
asyncio.run(cmd_decode(bot, _msg("!decode hex 68656c6c6f")))
assert bot.replied[0] == "hello"
def test_decode_invalid(self):
bot = _FakeBot()
asyncio.run(cmd_decode(bot, _msg("!decode hex zzzz")))
assert "error" in bot.replied[0].lower()
class TestEncodeUrl:
def test_encode(self):
bot = _FakeBot()
asyncio.run(cmd_encode(bot, _msg("!encode url hello world")))
assert bot.replied[0] == "hello%20world"
def test_decode(self):
bot = _FakeBot()
asyncio.run(cmd_decode(bot, _msg("!decode url hello%20world")))
assert bot.replied[0] == "hello world"
def test_special_chars(self):
bot = _FakeBot()
asyncio.run(cmd_encode(bot, _msg("!encode url a=1&b=2")))
assert "%3D" in bot.replied[0]
assert "%26" in bot.replied[0]
class TestEncodeRot13:
def test_encode(self):
bot = _FakeBot()
asyncio.run(cmd_encode(bot, _msg("!encode rot13 hello")))
assert bot.replied[0] == "uryyb"
def test_roundtrip(self):
bot = _FakeBot()
asyncio.run(cmd_encode(bot, _msg("!encode rot13 secret")))
encoded = bot.replied[0]
asyncio.run(cmd_decode(bot, _msg(f"!decode rot13 {encoded}")))
assert bot.replied[1] == "secret"
class TestEncodeErrors:
def test_unknown_format_encode(self):
bot = _FakeBot()
asyncio.run(cmd_encode(bot, _msg("!encode bad hello")))
assert "Unknown format" in bot.replied[0]
def test_unknown_format_decode(self):
bot = _FakeBot()
asyncio.run(cmd_decode(bot, _msg("!decode bad hello")))
assert "Unknown format" in bot.replied[0]
def test_missing_args_encode(self):
bot = _FakeBot()
asyncio.run(cmd_encode(bot, _msg("!encode")))
assert "Usage" in bot.replied[0]
def test_missing_args_decode(self):
bot = _FakeBot()
asyncio.run(cmd_decode(bot, _msg("!decode")))
assert "Usage" in bot.replied[0]
def test_missing_text_encode(self):
bot = _FakeBot()
asyncio.run(cmd_encode(bot, _msg("!encode b64")))
assert "Usage" in bot.replied[0]
def test_missing_text_decode(self):
bot = _FakeBot()
asyncio.run(cmd_decode(bot, _msg("!decode hex")))
assert "Usage" in bot.replied[0]
def test_decode_b64_invalid(self):
bot = _FakeBot()
asyncio.run(cmd_decode(bot, _msg("!decode b64 !!!invalid!!!")))
assert "error" in bot.replied[0].lower()