# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence. """The imageinfo query both Commons commands send.""" from __future__ import annotations import pytest from signal_bot.commons import ( CAPTION_LIMIT, SUFFIXES, THUMB_PATH, caption_for, imageinfo_params, page_of, ) from signal_bot.http_client import ImageFetchError def test_a_file_can_be_named_by_title() -> None: params = imageinfo_params(640, title="File:1868 Mausefalle.jpg") assert params["titles"] == "File:1868 Mausefalle.jpg" assert "pageids" not in params assert params["iiurlwidth"] == "640" assert params["prop"] == "imageinfo" def test_a_file_can_be_named_by_page_id() -> None: # The page id arrives as an int and has to reach Commons as a string; # sending the int would raise inside httpx rather than here. params = imageinfo_params(480, page_id=111419226) assert params["pageids"] == "111419226" assert "titles" not in params @pytest.mark.parametrize( ("title", "page_id"), [(None, None), ("File:x.jpg", 1)], ) def test_naming_a_file_neither_way_or_both_ways_is_refused( title: str | None, page_id: int | None ) -> None: # Preferring one silently would make a caller's mistake look like a 404, # which is the one failure shape that already exists downstream. with pytest.raises(ValueError, match="exactly one"): imageinfo_params(640, title=title, page_id=page_id) def test_the_thumbnail_path_is_where_commons_actually_puts_it() -> None: # Pinned because both callers walk it and neither can see it fail until # a real fetch returns something unwalkable. assert THUMB_PATH == ("query", "pages", 0, "imageinfo", 0, "thumburl") def test_the_suffix_allowlist_is_what_signal_can_render() -> None: assert ".jpg" in SUFFIXES assert ".svg" not in SUFFIXES def _page( description: str | None = None, *, title: str = "File:Molchreuse.JPG", url: str = "" ) -> dict[str, object]: """One imageinfo page, shaped the way Commons shapes it.""" info: dict[str, object] = {"thumburl": "https://cdn/x.jpg"} if url: info["descriptionurl"] = url if description is not None: info["extmetadata"] = {"ImageDescription": {"value": description}} return {"title": title, "imageinfo": [info]} def test_asking_for_a_description_widens_the_query() -> None: plain = imageinfo_params(640, page_id=1) described = imageinfo_params(640, page_id=1, describe=True) assert plain["iiprop"] == "url" assert "iiextmetadatafilter" not in plain assert described["iiprop"] == "url|extmetadata" assert described["iiextmetadatafilter"] == "ImageDescription" def test_the_caption_strips_markup_and_links_the_page() -> None: page = _page("Eine Molchreuse & ein Korb", url="https://commons/File:M.JPG") assert caption_for(page) == "Eine Molchreuse & ein Korb\nhttps://commons/File:M.JPG" def test_the_catalogue_list_is_cut_and_the_sentence_kept() -> None: # The Fisheries records append their filing fields as an HTML list. page = _page("Round Fyke (Bollreuse). Prussia\n") assert caption_for(page) == "Round Fyke (Bollreuse). Prussia" @pytest.mark.parametrize("description", [None, "", ""]) def test_a_description_that_says_nothing_falls_back_to_the_title(description: str | None) -> None: assert caption_for(_page(description)) == "Molchreuse" def test_a_long_description_is_cut_rather_than_burying_the_photo() -> None: page = _page("word " * 200) caption = caption_for(page) assert caption.endswith("…") # rstrip() before the ellipsis means the exact length depends on where # the cut landed; the ceiling is what matters. assert len(caption) <= CAPTION_LIMIT + 1 def test_a_page_commons_could_not_find_is_an_image_fetch_error() -> None: # A deleted or renumbered page id comes back as {"missing": true}. for document in ({"query": {"pages": [{"missing": True}]}}, {"query": {"pages": []}}, {}, []): with pytest.raises(ImageFetchError, match="named no image"): page_of(document) def test_the_page_comes_back_when_commons_did_find_it() -> None: assert page_of({"query": {"pages": [_page("x")]}})["title"] == "File:Molchreuse.JPG" def test_a_template_that_is_not_a_description_falls_back_to_the_title() -> None: # Measured on a Maltese fish trap: the filename said more than Commons' # own description field did. page = _page( "This photo has been taken in the country: Malta", title="File:Hand woven fish trap.jpg", ) assert caption_for(page) == "Hand woven fish trap"