# retoor <retoor@molodetz.nl>

import io

from PIL import Image
from devplacepy.awards.images import enforce_rgba_png, resize_award_png


def _rgba_png(size=512, color=(10, 20, 30, 200)):
    buf = io.BytesIO()
    Image.new("RGBA", (size, size), color).save(buf, format="PNG")
    return buf.getvalue()


def test_resize_award_png_outputs_exact_square_dimensions():
    source = _rgba_png(512)
    for size in (512, 256, 64):
        out = resize_award_png(source, size)
        img = Image.open(io.BytesIO(out))
        assert img.size == (size, size)


def test_resize_award_png_preserves_rgba_mode():
    out = resize_award_png(_rgba_png(512), 256)
    img = Image.open(io.BytesIO(out))
    assert img.mode == "RGBA"


def test_resize_award_png_downscale_is_idempotent_in_size():
    first = resize_award_png(_rgba_png(512), 64)
    second = resize_award_png(first, 64)
    assert Image.open(io.BytesIO(first)).size == (64, 64)
    assert Image.open(io.BytesIO(second)).size == (64, 64)


def test_enforce_rgba_png_returns_valid_png():
    out = enforce_rgba_png(_rgba_png(32))
    img = Image.open(io.BytesIO(out))
    assert img.mode == "RGBA"
    assert img.size == (32, 32)