# 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