Files
dogebot/tests/test_selftest.py
Ziad ArafatandClaude Opus 5.5 85bcee8c1a Self-test cog, cog requirement conflict check, health records
- dogebot.depcheck: find lib packages whose requirements Red's venv
  shadows (Red appends Downloader's lib to sys.path). Catches the
  2026-11-09 anyio/typing_extensions break; verified on the old lib.
- dogesetup.after_update: run it after provisioning and every cog
  update, write the deps health record, dispatch dogebot_cogs_ready.
- dogebot.health: /data/health/<check>.json for the Kuma push script.
- cogs/selftest: after every cog update and on [p]selftest run, runs
  watch search + watch status + alignment as the owner in the test
  channel, renders a sample chart, and plays music at volume 0 in an
  empty voice channel; reports to the channel and health.
- start.install_bundled_cogs ships every cog in cogs/; manifest loads
  selftest last.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 09:05:05 -07:00

189 lines
5.6 KiB
Python

"""The self-test's runner, verdicts and voice channel choice."""
import asyncio
import pytest
from dogebot.selftest import (
Check,
Reply,
Result,
Skip,
VoiceOption,
judge_replies,
pick_voice_channel,
run_checks,
summarize,
)
async def passes() -> str:
"""Pass with a detail."""
return "3 results"
async def raises() -> str:
"""Break the way d!watch did."""
msg = "cannot import name 'sentinel'"
raise ImportError(msg)
async def hangs() -> str:
"""Never finish."""
await asyncio.sleep(10)
return "never"
async def skips() -> str:
"""Decline to run."""
msg = "no empty voice channel"
raise Skip(msg)
def ticks(*times: float): # noqa: ANN201 (a clock stub)
"""Return a clock that yields ``times`` in order."""
it = iter(times)
return lambda: next(it)
async def test_each_outcome_is_recorded() -> None:
"""Pass, error, timeout and skip each become their own result."""
results = await run_checks(
[
Check("ok", passes),
Check("broken", raises),
Check("stuck", hangs, timeout=0.01),
Check("music", skips),
],
clock=ticks(0, 1.5, 2, 2, 3, 3, 4, 4),
)
assert results == [
Result("ok", "pass", "3 results", 1.5),
Result(
"broken",
"fail",
"ImportError: cannot import name 'sentinel'",
0,
),
Result("stuck", "fail", "timed out after 0.01 s", 0),
Result("music", "skip", "no empty voice channel", 0),
]
async def test_a_failing_check_does_not_stop_the_rest() -> None:
"""Later checks still run after one fails."""
ran: list[str] = []
async def record() -> str:
ran.append("after")
return "ok"
await run_checks([Check("broken", raises), Check("after", record)])
assert ran == ["after"]
def test_summary_fails_on_any_failure() -> None:
"""Skips don't fail the run; one failure does, and is listed."""
ok, summary, details = summarize(
[
Result("a", "pass", "fine", 1),
Result("b", "skip", "no voice", 0),
Result("c", "fail", "ImportError: x", 2),
]
)
assert not ok
assert summary == "1 failed, 1 passed, 1 skipped"
assert details == ["c: ImportError: x"]
def test_summary_passes_with_only_passes_and_skips() -> None:
"""A run with nothing failing is ok."""
ok, summary, details = summarize(
[Result("a", "pass", "fine", 1), Result("b", "skip", "x", 0)]
)
assert ok
assert summary == "0 failed, 1 passed, 1 skipped"
assert details == []
@pytest.mark.parametrize(
("replies", "verdict"),
[
([], "no reply"),
(
[Reply("Couldn't get that: HTTP 500")],
"Couldn't get that: HTTP 500",
),
(
[Reply("Search failed: boom")],
"Search failed: boom",
),
],
)
def test_bad_replies_fail(replies: list[Reply], verdict: str) -> None:
"""No reply, or a reply that reports an error, is a failure."""
with pytest.raises(AssertionError, match=verdict):
judge_replies(replies, failures=("Couldn't", "Search failed"))
def test_good_replies_are_described() -> None:
"""The detail says what came back."""
detail = judge_replies(
[Reply("`#12` **Dark Matter** · done"), Reply("", files=1)],
failures=("Couldn't",),
)
assert detail == "2 replies, 1 file(s)"
@pytest.mark.parametrize(
("options", "preferred", "chosen"),
[
# The configured channel when it's empty
(
[
VoiceOption(1, 0, can_join=True),
VoiceOption(2, 0, can_join=True),
],
2,
2,
),
# Never a channel with people in it, even the configured one
(
[
VoiceOption(1, 3, can_join=True),
VoiceOption(2, 0, can_join=True),
],
1,
2,
),
# Never one the bot can't join
(
[
VoiceOption(1, 0, can_join=False),
VoiceOption(2, 0, can_join=True),
],
None,
2,
),
# Nothing suitable
(
[
VoiceOption(1, 2, can_join=True),
VoiceOption(2, 0, can_join=False),
],
None,
None,
),
],
)
def test_voice_channel_choice(
options: list[VoiceOption], preferred: int | None, chosen: int | None
) -> None:
"""Music plays only where nobody can hear it."""
assert pick_voice_channel(options, preferred) == chosen