signal-bot/fake_signal/routes_fixtures.py
Krzysztof kuhy Rudnicki 558dd297a4 release: 0.0.7 «Telesfor» — !papież, and the bot can test itself
Adds !papież / !papiez / !pope: a random pope as a photo, captioned with his
Latin name, reign and pl.wikipedia link. popes.json already carried a Commons
portrait filename for all 267 and nothing had ever consumed it.

The Latin name comes from the Latin column of Poczet papieży, not from the
la.wikipedia sitelink. The sitelink resolved for all 267 and was still wrong:
la.wikipedia numbers several popes differently, so #154 came out as
"Stefan IX — Stephanus X" in a single caption, which reads as an off-by-one.
The table's own column says "Stephanus Nonus" and cannot disagree with the
name beside it. LATIN_OVERRIDES ships empty; it is a tripwire, not a backlog.

Portraits are cached on disk under CacheDirectory=signal-bot, named from the
ordinal so no upstream string ever reaches a path, and bounded at 267 entries
so nothing needs evicting. Verified that CacheDirectory wins against
ProtectHome=read-only before relying on it.

Also adds POST /_selftest and scripts/selftest.sh. Signal never delivers an
account its own sends, so no command has ever been exercised end-to-end
without asking a human to type it. This injects a real frame into the live
dispatcher; it is loopback-only and refuses any group not in SELFTEST_GROUPS,
which is empty by default.

Verified against the real endpoints, not just the fake: 267/267 portraits
resolve on Commons, regeneration is byte-identical on every previously
shipped field, and !papież was driven 7 times through the live dispatcher in
testbot, returning distinct popes.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015d48t4qsQLBJnDwDeo2Nom
2026-08-30 17:06:25 +02:00

60 lines
2.2 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""Local stand-ins for the image APIs, shaped like the real responses."""
from __future__ import annotations
from fastapi import APIRouter, Request
from fastapi.responses import Response
from fake_signal.state import PIXEL_PNG, State
def _image_url(request: Request) -> str:
"""Absolute URL of the fixture image, as this server sees itself."""
state: State = request.app.state.fake
return f"{state.base_url}/_fixtures/image.png"
def build_fixture_router() -> APIRouter:
"""Build the router serving fixture images and image-API responses."""
router = APIRouter(prefix="/_fixtures")
@router.get("/cat-api")
async def cat_api(request: Request) -> list[dict[str, str]]:
return [{"url": _image_url(request)}]
@router.get("/dog-api")
async def dog_api(request: Request) -> dict[str, str]:
return {"message": _image_url(request)}
@router.get("/gecko-api")
async def gecko_api(request: Request) -> dict[str, object]:
state: State = request.app.state.fake
photo = {"url": f"{state.base_url}/_fixtures/photos/square.png"}
return {"results": [{"photos": [photo]}]}
# Commons answers an imageinfo query, not a fixed URL. Echoing the
# requested titles= back proves the bot asked for the pope it drew,
# rather than a hard-coded file that would hide a lookup regression.
@router.get("/commons-api")
async def commons_api(request: Request) -> dict[str, object]:
page = {
"title": request.query_params.get("titles", ""),
"imageinfo": [{"thumburl": _image_url(request)}],
}
return {"query": {"pages": [page]}}
@router.get("/image.png")
async def image() -> Response:
return Response(content=PIXEL_PNG, media_type="image/png")
# Only the rewritten URL is served. iNaturalist names a square thumbnail
# and the bot is supposed to swap it for the full-size photo, so a
# regression in that rewrite must fail the run with a 404 rather than
# quietly posting a 75px image nobody looks at closely.
@router.get("/photos/large.png")
async def large_photo() -> Response:
return Response(content=PIXEL_PNG, media_type="image/png")
return router