Files
doge_cogs/tests/test_alignment_render.py
T
Ziad ArafatandClaude Opus 5.5 b575ab6f75 doge_alignment: chart pictures, one command group, Discord-free core
Draw the chart with ImageMagick: one 3x3 picture, or nine tiles sent as a
Components V2 media gallery (3x3), with avatars cut into circles. Names and
avatars are looked up live; people who left are greyed out.

Move all logic into doge_alignment/core, which imports neither discord.py
nor Red: immutable Chart dataclasses with pure operations, Config/YAML
storage, a view model, pure layout geometry, a Wand painter and an avatar
fetcher. The cog and a new CLI (python -m doge_alignment) are front ends.

Commands are now one hybrid `alignment` group (text and slash): show, set,
remove, prune, clear, style, export, import, admin add/remove/list. Red's
admin/mod roles, Administrators and the server owner count as admins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GUebqhE8dK19vyn4Ct5g1L
2026-11-05 11:34:36 -07:00

95 lines
3.4 KiB
Python

"""Painting with ImageMagick (skipped where Wand can't load it)."""
import io
import pytest
from alignment_helpers import chart_with
from doge_alignment.core.images import sniff
from doge_alignment.core.layout import Theme
from doge_alignment.core.view import Profile, cells
render = pytest.importorskip(
"doge_alignment.core.render", reason="needs ImageMagick"
)
wand_image = pytest.importorskip("wand.image")
wand_color = pytest.importorskip("wand.color")
def png(color: str, width: int = 40, height: int = 30) -> bytes:
"""Return a solid PNG."""
with wand_image.Image(
width=width, height=height, background=wand_color.Color(color)
) as img:
img.format = "png"
return bytes(img.make_blob())
def load(data: bytes): # noqa: ANN201 (Wand is untyped)
"""Open PNG bytes."""
return wand_image.Image(blob=data, format="png")
def test_sniff() -> None:
"""Only the four avatar formats are recognised."""
assert sniff(png("red")) == "png"
assert sniff(b"\xff\xd8\xff\xe0rest") == "jpeg"
assert sniff(b"GIF89a...") == "gif"
assert sniff(b"RIFF\0\0\0\0WEBPVP8 ") == "webp"
assert sniff(b"<svg xmlns=...>") is None
def test_avatar_disc_is_round() -> None:
"""Corners are transparent, the middle is the avatar."""
theme = Theme()
with render.avatar_disc(
png("red", 300, 200),
64,
present=True,
fallback_text="?",
theme=theme,
) as disc:
assert disc.size == (64, 64)
assert disc[0, 0].alpha == 0
assert disc[63, 63].alpha == 0
middle = disc[32, 32]
assert middle.red > 0.9
assert middle.green < 0.1
def test_absent_people_are_grey() -> None:
"""Someone who left is drawn without colour."""
with render.avatar_disc(
png("red"), 64, present=False, fallback_text="?", theme=Theme()
) as disc:
middle = disc[32, 32]
assert middle.red == pytest.approx(middle.green, abs=0.02)
def test_bad_bytes_fall_back_to_placeholder() -> None:
"""Non-images (here an SVG) are never decoded."""
theme = Theme()
with render.avatar_disc(
b"<svg/>", 64, present=True, fallback_text="A", theme=theme
) as disc:
assert disc[32, 4].alpha == 1
def test_render_chart_and_tiles() -> None:
"""One square PNG for the chart, nine for the tiles."""
chart = chart_with(("1", "Lawful Good"), ("2", "Chaotic Evil"))
chart_cells = cells(chart, {"1": Profile("one")}.get)
avatars = {"1": png("blue")}
theme = Theme()
with load(render.render_chart(chart_cells, avatars, theme)) as img:
side = 2 * theme.margin + 3 * theme.cell_size + 2 * theme.gap
assert img.size == (side, side)
tiles = render.render_tiles(chart_cells, avatars, theme)
assert len(tiles) == 9
with load(tiles[0]) as tile:
assert tile.size == (theme.tile_size, theme.tile_size)
# Tiles are the cell colour edge to edge
corner = tile[0, 0]
assert (corner.red, corner.green, corner.blue) != (0, 0, 0)
assert all(io.BytesIO(t).read(8) == b"\x89PNG\r\n\x1a\n" for t in tiles)