"""Thumbnail resize: pure bytes in/out, no R2, no DB.""" from io import BytesIO from PIL import Image as PILImage from shared.thumbnails import resize_to_thumbnail def _jpeg(width: int, height: int) -> bytes: buf = BytesIO() PILImage.new("RGB", (width, height), (200, 30, 30)).save(buf, "JPEG", quality=90) return buf.getvalue() def test_resize_bounds_long_side(): out = resize_to_thumbnail(_jpeg(2000, 1000)) with PILImage.open(BytesIO(out)) as im: assert max(im.size) <= 512 assert im.format == "JPEG" def test_resize_small_image_not_upscaled(): out = resize_to_thumbnail(_jpeg(100, 60)) with PILImage.open(BytesIO(out)) as im: assert im.size == (100, 60) def test_resize_output_is_smaller(): big = _jpeg(1920, 1080) assert len(resize_to_thumbnail(big)) < len(big)