Added owlbot.testing package with pytest plugin, event factories, and recording Owncast stubs for module tests.
Audit / Dependencies (push) Successful in 8s
CI / Formatting (push) Successful in 5s
CI / Linting (push) Successful in 5s
CI / Tests (Python 3.12) (push) Successful in 19s
CI / Tests (Python 3.13) (push) Successful in 19s
CI / Tests (Python 3.14) (push) Successful in 16s
CI / Type Checking (push) Successful in 11s
CI / Spelling (push) Successful in 9s

- owlbot.testing.plugin: fixtures that mirror ModuleLoader's wiring (module_context, module_lifecycle, command/event/route dispatchers, in-memory storage, real Config on a tmp YAML, HttpClient with aioresponses interception, and an aiohttp route_client).
- owlbot.testing.helpers: make_user, make_chat_event, and factories for every Owncast event type, with sensible defaults.
- owlbot.testing.stubs: RecordingOwncastClient and RecordingOwncastAdminClient that log each call to a .calls list in place of hitting the API.
- pyproject.toml: added a 'testing' extra for downstream module authors (pytest, pytest-asyncio, pytest-aiohttp, aioresponses).
- tests: migrated in-tree tests to the new helpers, moved module tests under tests/builtin_modules/, and swapped freezegun for time-machine.
This commit is contained in:
2026-04-22 21:49:16 -04:00
parent 32e21a6f9d
commit a671541b13
25 changed files with 8195 additions and 3093 deletions
+1
View File
@@ -0,0 +1 @@
"""Tests for built-in Owlbot modules."""
@@ -0,0 +1 @@
"""Tests for the custom_commands built-in module."""
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+382
View File
@@ -0,0 +1,382 @@
# Copyright 2026 Logan Fick
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for the emoji wall built-in module."""
from __future__ import annotations
import logging
from typing import TYPE_CHECKING, ClassVar
import orjson
import pytest
if TYPE_CHECKING:
from aiohttp import web
from owlbot.builtin_modules.emoji_wall import (
_broadcast,
_extract_emojis,
_resolve_direction,
_resolve_settings,
)
def _img(src: str, class_: str = "emoji") -> str:
"""Build an ``<img>`` tag matching Owncast's emote rendering."""
return f'<img src="{src}" class="{class_}" />'
class _FakeStreamResponse:
"""Minimal stand-in for ``web.StreamResponse`` that records writes."""
def __init__(self, *, fail: bool = False) -> None:
self.written: list[bytes] = []
self._fail = fail
async def write(self, data: bytes) -> None:
"""Record *data* or raise if this client is configured to fail."""
if self._fail:
raise ConnectionResetError("client gone")
self.written.append(data)
_URL = "https://stream.example.com"
class TestExtractEmojis:
"""Tests ``_extract_emojis`` from the emoji wall module."""
@pytest.mark.parametrize(
("raw_body", "expected"),
[
pytest.param("no emojis here", [], id="plain-text"),
pytest.param(
"hello 🔥 world",
[{"type": "unicode", "emoji": "🔥"}],
id="single-emoji",
),
pytest.param(
"🎉🎊✨",
[
{"type": "unicode", "emoji": "🎉"},
{"type": "unicode", "emoji": "🎊"},
{"type": "unicode", "emoji": "✨"},
],
id="multiple-emojis",
),
pytest.param(
"hi 👋🏽",
[{"type": "unicode", "emoji": "👋🏽"}],
id="skin-tone-modifier",
),
pytest.param(
"family 👨‍👩‍👧‍👦 time",
[{"type": "unicode", "emoji": "👨‍👩‍👧‍👦"}],
id="zwj-sequence",
),
pytest.param(
"flag 🇺🇸 here",
[{"type": "unicode", "emoji": "🇺🇸"}],
id="flag-sequence",
),
pytest.param("", [], id="empty-string"),
],
)
def test_unicode(self, raw_body: str, expected: list[dict[str, str]]) -> None:
"""Unicode emojis are extracted from the raw body."""
assert _extract_emojis(raw_body, "", _URL) == expected
def test_custom_emote(self) -> None:
"""Custom emote img tags are resolved against the Owncast URL."""
html = _img("/img/emoji/catjam.gif")
result = _extract_emojis("", html, _URL)
assert result == [
{"type": "emote", "url": f"{_URL}/img/emoji/catjam.gif"},
]
def test_multiple_custom_emotes(self) -> None:
"""Multiple emotes in a single message are all extracted."""
html = _img("/img/emoji/a.gif") + _img("/img/emoji/b.gif")
result = _extract_emojis("", html, _URL)
assert result == [
{"type": "emote", "url": f"{_URL}/img/emoji/a.gif"},
{"type": "emote", "url": f"{_URL}/img/emoji/b.gif"},
]
def test_non_emoji_img_ignored(self) -> None:
"""An <img> without the emoji class is not matched."""
html = '<img src="/photo.jpg" class="avatar" />'
assert _extract_emojis("", html, _URL) == []
def test_class_before_src(self) -> None:
"""Handles <img> where class appears before src."""
html = '<img class="emoji" src="/img/emoji/wave.gif" />'
result = _extract_emojis("", html, _URL)
assert result == [
{"type": "emote", "url": f"{_URL}/img/emoji/wave.gif"},
]
def test_extra_class_ignored(self) -> None:
"""An <img> with a class other than exactly 'emoji' is not matched."""
html = '<img src="/img/emoji/wave.gif" class="emoji custom" />'
assert _extract_emojis("", html, _URL) == []
def test_img_without_src_ignored(self) -> None:
"""An <img> with class emoji but no src is not matched."""
html = '<img class="emoji" />'
assert _extract_emojis("", html, _URL) == []
def test_leading_slashes_deduped(self) -> None:
"""Extra leading slashes on the src don't double up."""
html = _img("///img/emoji/test.gif")
result = _extract_emojis("", html, _URL)
assert result == [
{"type": "emote", "url": f"{_URL}/img/emoji/test.gif"},
]
def test_unicode_and_custom_combined(self) -> None:
"""Unicode emojis and custom emotes are returned together."""
html = f"<p>🔥 {_img('/img/emoji/catjam.gif')}</p>"
result = _extract_emojis("🔥 :catjam:", html, _URL)
assert result == [
{"type": "unicode", "emoji": "🔥"},
{"type": "emote", "url": f"{_URL}/img/emoji/catjam.gif"},
]
def test_empty_message(self) -> None:
"""Empty raw body and HTML body returns an empty list."""
assert _extract_emojis("", "", _URL) == []
class TestResolveDirection:
"""Tests ``_resolve_direction`` from the emoji wall module."""
@pytest.mark.parametrize(
("value", "expected"),
[
pytest.param("up", 0, id="preset-up"),
pytest.param("down", 180, id="preset-down"),
pytest.param("left", 270, id="preset-left"),
pytest.param("right", 90, id="preset-right"),
pytest.param("up-right", 45, id="preset-up-right"),
pytest.param("down-left", 225, id="preset-down-left"),
pytest.param("UP", 0, id="case-insensitive"),
pytest.param(" Down ", 180, id="whitespace-trimmed"),
pytest.param("random", "random", id="random-string"),
pytest.param("RANDOM", "random", id="random-case-insensitive"),
pytest.param("45", 45, id="numeric-string"),
pytest.param("0", 0, id="zero-string"),
pytest.param("359", 359, id="max-degrees-string"),
pytest.param("nonsense", 0, id="invalid-string-fallback"),
pytest.param(90, 90, id="int-value"),
pytest.param(0, 0, id="int-zero"),
pytest.param(359, 359, id="int-max"),
pytest.param(-10, 0, id="negative-clamped"),
pytest.param(500, 359, id="over-max-clamped"),
pytest.param("999", 359, id="numeric-string-clamped-high"),
pytest.param("-5", 0, id="numeric-string-clamped-low"),
],
)
def test_resolution(self, value: str | int, expected: int | str) -> None:
"""Direction values resolve to the expected degree or 'random'."""
assert _resolve_direction(value) == expected
class TestResolveSettings:
"""Tests ``_resolve_settings`` from the emoji wall module."""
_DEFAULTS: ClassVar[dict[str, object]] = {
"min_size": 20,
"max_size": 60,
"min_duration": 3,
"max_duration": 10,
"max_count": 50,
"direction": "up",
"max_rotation": 25,
}
def test_defaults(self) -> None:
"""Config defaults produce expected settings."""
result = _resolve_settings(self._DEFAULTS, {})
assert result == {
"min_size": 20,
"max_size": 60,
"min_duration": 3,
"max_duration": 10,
"max_count": 50,
"direction": 0,
"max_rotation": 25,
}
def test_query_overrides_config(self) -> None:
"""Query parameters take precedence over config values."""
result = _resolve_settings(
self._DEFAULTS,
{"min_size": "30", "max_size": "80"},
)
assert result["min_size"] == 30
assert result["max_size"] == 80
def test_invalid_query_falls_back_to_config(self) -> None:
"""Unparsable query values fall back to config."""
result = _resolve_settings(
self._DEFAULTS,
{"min_size": "not_a_number"},
)
assert result["min_size"] == 20
def test_min_max_size_swapped(self) -> None:
"""Inverted min/max sizes are swapped."""
result = _resolve_settings(
{**self._DEFAULTS, "min_size": 100, "max_size": 10},
{},
)
assert result["min_size"] == 10
assert result["max_size"] == 100
def test_min_max_duration_swapped(self) -> None:
"""Inverted min/max durations are swapped."""
result = _resolve_settings(
{**self._DEFAULTS, "min_duration": 30, "max_duration": 1},
{},
)
assert result["min_duration"] == 1
assert result["max_duration"] == 30
@pytest.mark.parametrize(
("key", "value", "expected"),
[
pytest.param("min_size", -10, 1, id="size-floor"),
pytest.param("max_size", 9999, 500, id="size-ceiling"),
pytest.param("min_duration", -1, 1, id="duration-floor"),
pytest.param("max_duration", 999, 60, id="duration-ceiling"),
pytest.param("max_count", -1, 1, id="count-floor"),
pytest.param("max_count", 5000, 1000, id="count-ceiling"),
pytest.param("max_rotation", -5, 0, id="rotation-floor"),
pytest.param("max_rotation", 999, 360, id="rotation-ceiling"),
],
)
def test_clamping(self, key: str, value: int, expected: int) -> None:
"""Values outside allowed ranges are clamped."""
result = _resolve_settings({**self._DEFAULTS, key: value}, {})
assert result[key] == expected
def test_direction_random(self) -> None:
"""The 'random' direction string passes through."""
result = _resolve_settings(
{**self._DEFAULTS, "direction": "random"},
{},
)
assert result["direction"] == "random"
def test_direction_query_override(self) -> None:
"""Query parameter overrides the config direction."""
result = _resolve_settings(self._DEFAULTS, {"direction": "down"})
assert result["direction"] == 180
def test_empty_config_uses_zero_defaults(self) -> None:
"""Missing config keys produce clamped-to-minimum values."""
result = _resolve_settings({}, {})
assert result["min_size"] == 1
assert result["max_size"] == 1
assert result["min_duration"] == 1
assert result["max_duration"] == 1
assert result["max_count"] == 1
assert result["max_rotation"] == 0
class TestBroadcast:
"""Tests ``_broadcast`` from the emoji wall module."""
async def test_sends_to_all_clients(self) -> None:
"""Payload is written to every connected client."""
c1 = _FakeStreamResponse()
c2 = _FakeStreamResponse()
clients: set[web.StreamResponse] = {c1, c2} # type: ignore[arg-type]
events = [{"type": "unicode", "emoji": "🔥"}]
await _broadcast(clients, events, logging.getLogger("test"))
expected = b"data: " + orjson.dumps(events[0]) + b"\n\n"
assert c1.written == [expected]
assert c2.written == [expected]
assert len(clients) == 2
async def test_failed_client_removed(self) -> None:
"""A client that raises on write is removed from the set."""
good = _FakeStreamResponse()
bad = _FakeStreamResponse(fail=True)
clients: set[web.StreamResponse] = {good, bad} # type: ignore[arg-type]
events = [{"type": "unicode", "emoji": "😀"}]
await _broadcast(clients, events, logging.getLogger("test"))
assert good in clients
assert bad not in clients
assert len(clients) == 1
async def test_failed_client_logged(self, caplog: pytest.LogCaptureFixture) -> None:
"""Removal of failed clients is logged at DEBUG level."""
bad = _FakeStreamResponse(fail=True)
clients: set[web.StreamResponse] = {bad} # type: ignore[arg-type]
with caplog.at_level(logging.DEBUG, logger="test"):
await _broadcast(
clients,
[{"type": "unicode", "emoji": "😀"}],
logging.getLogger("test"),
)
assert "Removed 1 failed client(s) during broadcast" in caplog.text
async def test_multiple_events_concatenated(self) -> None:
"""Multiple events are sent as a single concatenated payload."""
client = _FakeStreamResponse()
clients: set[web.StreamResponse] = {client} # type: ignore[arg-type]
events = [
{"type": "unicode", "emoji": "🎉"},
{"type": "emote", "url": "https://example.com/emote.gif"},
]
await _broadcast(clients, events, logging.getLogger("test"))
assert len(client.written) == 1
payload = client.written[0].decode()
assert "data: " + orjson.dumps(events[0]).decode() + "\n\n" in payload
assert "data: " + orjson.dumps(events[1]).decode() + "\n\n" in payload
async def test_empty_clients_noop(self) -> None:
"""Broadcasting to no clients completes without error."""
clients: set[web.StreamResponse] = set()
await _broadcast(
clients,
[{"type": "unicode", "emoji": "😀"}],
logging.getLogger("test"),
)
assert len(clients) == 0
async def test_all_clients_fail(self) -> None:
"""When every client fails, the set is emptied."""
b1 = _FakeStreamResponse(fail=True)
b2 = _FakeStreamResponse(fail=True)
clients: set[web.StreamResponse] = {b1, b2} # type: ignore[arg-type]
await _broadcast(
clients,
[{"type": "unicode", "emoji": "😀"}],
logging.getLogger("test"),
)
assert len(clients) == 0
+513
View File
@@ -0,0 +1,513 @@
# Copyright 2026 Logan Fick
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for the quotes module."""
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from owlbot.api.event_types import EventType
from owlbot.builtin_modules import quotes as quotes_pkg
from owlbot.builtin_modules.quotes import setup, teardown
from owlbot.builtin_modules.quotes.manager import QuoteManager, get_manager
from owlbot.builtin_modules.quotes.repository import QuoteRepository
from owlbot.builtin_modules.quotes.types import (
Quote,
QuoteError,
QuoteNotFoundError,
)
from owlbot.testing import make_chat_event, make_user
if TYPE_CHECKING:
from types import ModuleType
from typing import Any
from aiohttp.test_utils import TestClient
from owlbot.api.context import ModuleContext
from owlbot.api.storage import ModuleStorage
from owlbot.registries.events import EventDispatcher
from owlbot.testing import RecordingOwncastClient
@pytest.fixture
def module_pkg() -> ModuleType:
"""Auto-wire module_name, templates, and handler registration for quotes."""
return quotes_pkg
@pytest.fixture
async def repo(storage: ModuleStorage) -> QuoteRepository:
"""QuoteRepository backed by storage, with schema initialized."""
repo = QuoteRepository(storage)
await repo.setup()
return repo
@pytest.fixture
def manager(module_context: ModuleContext, repo: QuoteRepository) -> QuoteManager:
"""QuoteManager built directly for unit tests; does not run @on_setup."""
return QuoteManager(module_context, repo)
class TestTypes:
"""Quote dataclass conversion, date formatting, and domain errors."""
def test_builds_from_row(self) -> None:
"""Quote.from_row constructs a Quote with correct field values."""
row = {
"id": 5,
"text": "Test quote",
"added_by": "someone",
"created_at": "2026-03-15T10:00:00+00:00",
}
quote = Quote.from_row(row) # type: ignore[arg-type]
assert quote.id == 5
assert quote.text == "Test quote"
assert quote.added_by == "someone"
assert quote.created_at == "2026-03-15T10:00:00+00:00"
@pytest.mark.parametrize(
("created_at", "expected"),
[
pytest.param("2026-03-15T10:00:00+00:00", "2026-03-15", id="utc"),
pytest.param(
"2026-03-15T01:00:00+05:30",
"2026-03-15",
id="local-date-preserved-across-utc-boundary",
),
],
)
def test_date(self, created_at: str, expected: str) -> None:
"""Quote.date returns the local calendar date, not the UTC date."""
quote = Quote(id=1, text="t", added_by="a", created_at=created_at)
assert quote.date == expected
def test_not_found_inherits_from_quote_error(self) -> None:
"""QuoteNotFoundError is a QuoteError."""
assert issubclass(QuoteNotFoundError, QuoteError)
def test_not_found_stores_quote_id(self) -> None:
"""QuoteNotFoundError stores the quote ID on .quote_id."""
err = QuoteNotFoundError(42)
assert err.quote_id == 42
def test_not_found_str_contains_id(self) -> None:
"""QuoteNotFoundError's str contains the quote ID."""
err = QuoteNotFoundError(42)
assert "42" in str(err)
class TestRepository:
"""QuoteRepository CRUD behavior against real storage."""
async def test_create_returns_quote(self, repo: QuoteRepository) -> None:
"""Create returns a populated Quote snapshot."""
quote = await repo.create("Hello, world!", "tester")
assert quote.id == 1
assert quote.text == "Hello, world!"
assert quote.added_by == "tester"
assert quote.created_at is not None
async def test_create_auto_increments(self, repo: QuoteRepository) -> None:
"""Sequential creates get sequential IDs."""
q1 = await repo.create("First", "tester")
q2 = await repo.create("Second", "tester")
assert q2.id == q1.id + 1
async def test_get_existing(self, repo: QuoteRepository) -> None:
"""Get returns the matching quote."""
created = await repo.create("Test", "tester")
fetched = await repo.get(created.id)
assert fetched.id == created.id
assert fetched.text == "Test"
async def test_get_not_found(self, repo: QuoteRepository) -> None:
"""Get raises QuoteNotFoundError for an unknown ID."""
with pytest.raises(QuoteNotFoundError) as exc_info:
await repo.get(999)
assert exc_info.value.quote_id == 999
async def test_get_random_returns_quote(self, repo: QuoteRepository) -> None:
"""get_random returns the lone quote when the table has one row."""
await repo.create("Only quote", "tester")
quote = await repo.get_random()
assert quote is not None
assert quote.text == "Only quote"
async def test_get_random_empty(self, repo: QuoteRepository) -> None:
"""get_random returns None when the table is empty."""
assert await repo.get_random() is None
async def test_delete_returns_quote(self, repo: QuoteRepository) -> None:
"""Delete returns the deleted snapshot."""
created = await repo.create("Doomed", "tester")
deleted = await repo.delete(created.id)
assert deleted.id == created.id
assert deleted.text == "Doomed"
async def test_delete_removes_from_db(self, repo: QuoteRepository) -> None:
"""A deleted quote is no longer retrievable."""
created = await repo.create("Doomed", "tester")
await repo.delete(created.id)
with pytest.raises(QuoteNotFoundError):
await repo.get(created.id)
async def test_delete_not_found(self, repo: QuoteRepository) -> None:
"""Delete raises QuoteNotFoundError for an unknown ID."""
with pytest.raises(QuoteNotFoundError) as exc_info:
await repo.delete(999)
assert exc_info.value.quote_id == 999
async def test_list_all(self, repo: QuoteRepository) -> None:
"""list_all returns quotes ordered by ID."""
await repo.create("First", "alice")
await repo.create("Second", "bob")
result = await repo.list_all()
assert len(result) == 2
assert result[0].text == "First"
assert result[1].text == "Second"
async def test_list_all_empty(self, repo: QuoteRepository) -> None:
"""list_all returns an empty list when no quotes exist."""
assert await repo.list_all() == []
class TestManager:
"""QuoteManager forwarding behavior and the get_manager helper."""
async def test_add_quote(self, manager: QuoteManager) -> None:
"""add_quote returns a populated Quote with correct fields."""
quote = await manager.add_quote("Test quote", "tester")
assert quote.id == 1
assert quote.text == "Test quote"
assert quote.added_by == "tester"
async def test_delete_quote(self, manager: QuoteManager) -> None:
"""delete_quote removes the quote and returns its snapshot."""
created = await manager.add_quote("Doomed", "tester")
deleted = await manager.delete_quote(created.id)
assert deleted.id == created.id
assert deleted.text == "Doomed"
assert await manager.list_quotes() == []
async def test_delete_quote_not_found(self, manager: QuoteManager) -> None:
"""delete_quote raises QuoteNotFoundError for an unknown ID."""
with pytest.raises(QuoteNotFoundError):
await manager.delete_quote(999)
async def test_get_quote(self, manager: QuoteManager) -> None:
"""get_quote returns the matching quote."""
created = await manager.add_quote("Test", "tester")
fetched = await manager.get_quote(created.id)
assert fetched.id == created.id
assert fetched.text == "Test"
async def test_get_quote_not_found(self, manager: QuoteManager) -> None:
"""get_quote raises QuoteNotFoundError for an unknown ID."""
with pytest.raises(QuoteNotFoundError):
await manager.get_quote(999)
async def test_get_random_quote(self, manager: QuoteManager) -> None:
"""get_random_quote returns a quote when quotes exist."""
await manager.add_quote("Only one", "tester")
quote = await manager.get_random_quote()
assert quote is not None
assert quote.text == "Only one"
async def test_get_random_quote_empty(self, manager: QuoteManager) -> None:
"""get_random_quote returns None when no quotes exist."""
assert await manager.get_random_quote() is None
async def test_list_quotes(self, manager: QuoteManager) -> None:
"""list_quotes returns all quotes ordered by ID."""
await manager.add_quote("First", "alice")
await manager.add_quote("Second", "bob")
result = await manager.list_quotes()
assert len(result) == 2
assert result[0].text == "First"
assert result[1].text == "Second"
async def test_list_quotes_empty(self, manager: QuoteManager) -> None:
"""list_quotes returns an empty list when no quotes exist."""
assert await manager.list_quotes() == []
def test_get_manager_returns_stored_manager(
self, module_context: ModuleContext, repo: QuoteRepository
) -> None:
"""get_manager returns the QuoteManager stored in ctx.state."""
mgr = QuoteManager(module_context, repo)
module_context.state["manager"] = mgr
assert get_manager(module_context) is mgr
def test_get_manager_raises_if_not_initialized(
self, module_context: ModuleContext
) -> None:
"""get_manager raises RuntimeError when manager is missing."""
with pytest.raises(RuntimeError, match="QuoteManager is not initialized"):
get_manager(module_context)
class TestModuleLifecycle:
"""@on_setup and @on_teardown hooks."""
async def test_setup_creates_table_and_manager(
self, module_context: ModuleContext, storage: ModuleStorage
) -> None:
"""Setup creates the quotes table and stashes a QuoteManager in state."""
await setup(module_context)
row = await storage.fetch_one(
"SELECT name FROM sqlite_master WHERE type='table' AND name='quotes'"
)
assert row is not None
assert row["name"] == "quotes"
mgr = module_context.state["manager"]
assert isinstance(mgr, QuoteManager)
async def test_teardown_clears_manager(self, module_context: ModuleContext) -> None:
"""Teardown clears the manager from state."""
await setup(module_context)
await teardown(module_context)
assert module_context.state["manager"] is None
@pytest.mark.usefixtures("module_lifecycle")
class TestQuoteCommand:
"""!quote command handler."""
async def test_quote_empty_db(
self,
event_dispatcher: EventDispatcher,
owncast_client: RecordingOwncastClient,
) -> None:
"""!quote with no stored quotes reports empty."""
event = make_chat_event(raw_body="!quote")
await event_dispatcher.dispatch(EventType.CHAT, event)
assert (
owncast_client.calls[0].kwargs["body"] == "No quotes have been added yet."
)
async def test_quote_random(
self,
module_context: ModuleContext,
event_dispatcher: EventDispatcher,
owncast_client: RecordingOwncastClient,
) -> None:
"""!quote with no ID returns a random quote."""
await get_manager(module_context).add_quote("The only one", "tester")
event = make_chat_event(raw_body="!quote")
await event_dispatcher.dispatch(EventType.CHAT, event)
assert owncast_client.calls[0].kwargs["body"] == '"The only one" (#1)'
async def test_quote_by_id(
self,
module_context: ModuleContext,
event_dispatcher: EventDispatcher,
owncast_client: RecordingOwncastClient,
) -> None:
"""!quote <id> returns the formatted detail for that quote."""
seeded = await get_manager(module_context).add_quote("Hello", "ModUser")
event = make_chat_event(raw_body="!quote 1")
await event_dispatcher.dispatch(EventType.CHAT, event)
assert owncast_client.calls[0].kwargs["body"] == (
f'Quote #1: "Hello" - Added by ModUser on {seeded.date}'
)
async def test_quote_not_found(
self,
event_dispatcher: EventDispatcher,
owncast_client: RecordingOwncastClient,
) -> None:
"""!quote <unknown-id> reports not found."""
event = make_chat_event(raw_body="!quote 999")
await event_dispatcher.dispatch(EventType.CHAT, event)
assert owncast_client.calls[0].kwargs["body"] == "Quote #999 not found."
async def test_quote_invalid_id(
self,
event_dispatcher: EventDispatcher,
owncast_client: RecordingOwncastClient,
) -> None:
"""!quote <non-int> sends usage."""
event = make_chat_event(raw_body="!quote abc")
await event_dispatcher.dispatch(EventType.CHAT, event)
assert owncast_client.calls[0].kwargs["body"] == "Usage: !quote [id]"
@pytest.mark.usefixtures("module_lifecycle")
class TestAddQuoteCommand:
"""!addquote command handler."""
async def test_addquote_attributes_to_user(
self,
module_context: ModuleContext,
event_dispatcher: EventDispatcher,
owncast_client: RecordingOwncastClient,
) -> None:
"""!addquote creates the quote attributed to the moderator's display name."""
user = make_user(display_name="ModUser", is_moderator=True)
event = make_chat_event(raw_body="!addquote Hello there", user=user)
await event_dispatcher.dispatch(EventType.CHAT, event)
assert owncast_client.calls[0].kwargs["body"] == "Quote #1 added."
stored = await get_manager(module_context).list_quotes()
assert len(stored) == 1
assert stored[0].text == "Hello there"
assert stored[0].added_by == "ModUser"
async def test_addquote_no_text(
self,
event_dispatcher: EventDispatcher,
owncast_client: RecordingOwncastClient,
) -> None:
"""!addquote with no text sends usage."""
user = make_user(is_moderator=True)
event = make_chat_event(raw_body="!addquote", user=user)
await event_dispatcher.dispatch(EventType.CHAT, event)
assert owncast_client.calls[0].kwargs["body"] == "Usage: !addquote <quote text>"
@pytest.mark.usefixtures("module_lifecycle")
class TestDeleteQuoteCommand:
"""!deletequote command handler."""
async def test_deletequote_removes(
self,
module_context: ModuleContext,
event_dispatcher: EventDispatcher,
owncast_client: RecordingOwncastClient,
) -> None:
"""!deletequote <id> removes the quote and confirms."""
manager = get_manager(module_context)
await manager.add_quote("Doomed", "tester")
user = make_user(is_moderator=True)
event = make_chat_event(raw_body="!deletequote 1", user=user)
await event_dispatcher.dispatch(EventType.CHAT, event)
assert owncast_client.calls[0].kwargs["body"] == "Quote #1 deleted."
assert await manager.list_quotes() == []
async def test_deletequote_no_args(
self,
event_dispatcher: EventDispatcher,
owncast_client: RecordingOwncastClient,
) -> None:
"""!deletequote with no args sends usage."""
user = make_user(is_moderator=True)
event = make_chat_event(raw_body="!deletequote", user=user)
await event_dispatcher.dispatch(EventType.CHAT, event)
assert owncast_client.calls[0].kwargs["body"] == "Usage: !deletequote <id>"
async def test_deletequote_invalid_id(
self,
event_dispatcher: EventDispatcher,
owncast_client: RecordingOwncastClient,
) -> None:
"""!deletequote <non-int> sends usage."""
user = make_user(is_moderator=True)
event = make_chat_event(raw_body="!deletequote abc", user=user)
await event_dispatcher.dispatch(EventType.CHAT, event)
assert owncast_client.calls[0].kwargs["body"] == "Usage: !deletequote <id>"
async def test_deletequote_not_found(
self,
event_dispatcher: EventDispatcher,
owncast_client: RecordingOwncastClient,
) -> None:
"""!deletequote <unknown-id> reports not found."""
user = make_user(is_moderator=True)
event = make_chat_event(raw_body="!deletequote 999", user=user)
await event_dispatcher.dispatch(EventType.CHAT, event)
assert owncast_client.calls[0].kwargs["body"] == "Quote #999 not found."
@pytest.mark.usefixtures("module_lifecycle")
class TestListQuotesCommand:
"""!listquotes command handler."""
async def test_listquotes_sends_url(
self,
event_dispatcher: EventDispatcher,
owncast_client: RecordingOwncastClient,
) -> None:
"""!listquotes sends the public URL of the /list route."""
event = make_chat_event(raw_body="!listquotes")
await event_dispatcher.dispatch(EventType.CHAT, event)
assert (
owncast_client.calls[0].kwargs["body"]
== "Quotes: http://localhost:8081/owlbot/quotes/list"
)
@pytest.mark.usefixtures("module_lifecycle")
class TestQuotesListRoute:
"""/list HTML route."""
async def test_list_empty(
self,
route_client: TestClient[Any, Any],
) -> None:
"""GET /list with no quotes serves the empty-state message."""
resp = await route_client.get("/owlbot/quotes/list")
assert resp.status == 200
assert resp.content_type == "text/html"
body = await resp.text()
assert "No quotes have been added yet." in body
async def test_list_populated(
self,
module_context: ModuleContext,
route_client: TestClient[Any, Any],
) -> None:
"""GET /list with quotes includes each quote's text and attribution."""
manager = get_manager(module_context)
await manager.add_quote("First quote", "alice")
await manager.add_quote("Second quote", "bob")
resp = await route_client.get("/owlbot/quotes/list")
assert resp.status == 200
body = await resp.text()
assert "First quote" in body
assert "Second quote" in body
assert "alice" in body
assert "bob" in body
@pytest.mark.usefixtures("module_lifecycle")
class TestModeratorPermissions:
"""Moderator-required commands deny non-moderators."""
@pytest.mark.parametrize("command", ["addquote", "deletequote"])
async def test_non_moderator_denied(
self,
command: str,
event_dispatcher: EventDispatcher,
owncast_client: RecordingOwncastClient,
) -> None:
"""Non-moderator invocation triggers the dispatcher denial."""
user = make_user(is_moderator=False)
event = make_chat_event(raw_body=f"!{command}", user=user)
await event_dispatcher.dispatch(EventType.CHAT, event)
denials = [
c.kwargs["body"]
for c in owncast_client.calls
if c.name == "send_system_message_to_client"
]
assert denials == [f"Only moderators can use !{command}."]
File diff suppressed because it is too large Load Diff