"""Thumbnails against the tile they are drawn into, in a real browser. `THUMB_WIDTH` is derived from a LAYOUT number: the widest gallery tile on the desktop grid, doubled for a 2x screen. A Python test cannot see a CSS width, so without this file the constant and the grid could drift apart silently, which is how the tiles went soft in the first place. If the grid widens its tiles, this goes red and the constant is revisited, rather than the operator finding it by eye. Scoped to the desktop layout (3 columns, 1440px and up), which is where tiles are measured at 321-361 CSS px. Narrower windows reflow to 2 columns (up to 472 px) or 1 (up to 650 px): at 2x density those are softer than this bound covers, and that is a known limit, not a defect this file asserts against. """ from __future__ import annotations import os import pytest from test_embed_browser import browser, live # noqa: F401 (fixtures) from booth.thumbs import THUMB_WIDTH PIL = pytest.importorskip("PIL.Image", reason="Pillow is not installed") @pytest.mark.parametrize("viewport", [(1440, 900), (1920, 1080), (2560, 1440)]) def test_a_thumbnail_covers_its_tile_at_2x_density(browser, live, viewport): # noqa: F811 base, root = live b = root / "g" b.mkdir() for n in ("a.png", "b.png", "c.png"): PIL.frombytes("RGB", (1024, 1024), os.urandom(1024 * 1024 * 3)).save(b / n, "PNG") pg = browser.new_page(viewport={"width": viewport[0], "height": viewport[1]}) try: pg.goto(f"{base}/b/g/", wait_until="load") pg.wait_for_function( "Array.from(document.querySelectorAll('.gallery .item img'))" ".every(i => i.complete && i.naturalWidth)", timeout=15000) tiles = pg.evaluate( "Array.from(document.querySelectorAll('.gallery .item img'))" ".map(i => [i.currentSrc, i.clientWidth, i.naturalWidth])") finally: pg.close() assert tiles, "no gallery tiles rendered" for src, shown, natural in tiles: assert "thumb=1" in src, f"{src} is not the thumbnail" assert natural >= 2 * shown, ( f"{src}: a {shown}px tile needs {2 * shown}px at 2x, the thumbnail has {natural}px" f" (THUMB_WIDTH={THUMB_WIDTH})")