from __future__ import annotations import pytest from PIL import Image from worker.label import LayoutTooLarge, compose, find_label DIE_CUT = find_label("103x164") ENDLESS = find_label("103") def mm(px: int) -> float: return px * 25.4 / 300 def badge_mm(w_mm: float, h_mm: float) -> Image.Image: return Image.new("RGB", (round(w_mm * 300 / 25.4), round(h_mm * 300 / 25.4)), "red") def test_label_specs(): assert (DIE_CUT.width_px, DIE_CUT.height_px, DIE_CUT.die_cut) == (1200, 1822, True) assert (ENDLESS.width_px, ENDLESS.height_px, ENDLESS.die_cut) == (1200, 0, False) with pytest.raises(KeyError): find_label("nope") def test_die_cut_centers_badge(): badge = badge_mm(100, 70) assert (badge.width, badge.height) == (1181, 827) out = compose(badge, DIE_CUT) assert out.size == (1200, 1822) assert out.getpixel((9, 497)) == (255, 0, 0) assert out.getpixel((0, 0)) == (255, 255, 255) assert out.getpixel((1199, 1821)) == (255, 255, 255) def test_endless_uses_badge_height(): out = compose(badge_mm(100, 70), ENDLESS) assert out.size == (1200, 827) assert out.getpixel((9, 0)) == (255, 0, 0) assert out.getpixel((0, 0)) == (255, 255, 255) def test_rotated_when_it_only_fits_rotated(): badge = badge_mm(150, 90) assert badge.width > DIE_CUT.width_px out = compose(badge, DIE_CUT) assert out.size == (1200, 1822) # Rotated 1063x1772 content is centered: margins 68/25 px. assert out.getpixel((600, 25)) == (255, 0, 0) assert out.getpixel((600, 0)) == (255, 255, 255) def test_too_large_raises(): with pytest.raises(LayoutTooLarge) as err: compose(badge_mm(120, 200), DIE_CUT) message = str(err.value) assert "120x200 mm" in message and "103x164" in message def test_overshoot_is_cropped(): out = compose(Image.new("RGB", (1203, 1822), "red"), DIE_CUT) assert out.size == (1200, 1822) def test_grayscale_input_converted(): out = compose(Image.new("L", (1181, 827), 0), DIE_CUT) assert out.mode == "RGB" def test_compose_accepts_identifier_string(): out = compose(badge_mm(100, 70), "103") assert mm(out.width) == pytest.approx(101.6, abs=0.1) assert out.height == 827