from __future__ import annotations import pytest from PIL import Image from worker.printer import Printer, map_status from worker.config import Config # Verbatim status dict reported by the real QL-1110NWB with DK-22246 loaded. REAL_STATUS = { "status_type": "Reply to status request", "phase_type": "Waiting to receive", "model_name": "QL-1100NWB", "media_type": "Die-cut labels", "media_width": 104, "media_length": 164, "identified_media": type( "Label", (), {"identifier": "103x164", "dots_printable": (1200, 1822), "form_factor": 1}, )(), "errors": [], } def cfg(label: str = "auto") -> Config: return Config( convivo_url="http://x", convivo_anon_key="k", station_token="t", brother_label=label, dry_run=True, ) def test_maps_real_status(): st = map_status(REAL_STATUS, "auto") assert st == { "connected": True, "model": "QL-1100NWB", "ready": True, "media_type": "die-cut", "label_id": "103x164", "media_width_mm": 104, "media_length_mm": 164, "errors": [], } def test_continuous_media_type(): raw = dict(REAL_STATUS, media_type="Continuous length tape") st = map_status(raw, "auto") assert st["media_type"] == "continuous" and st["ready"] def test_errors_block_ready(): raw = dict(REAL_STATUS, errors=["Tape end"]) st = map_status(raw, "auto") assert st["errors"] == ["Tape end"] and not st["ready"] and st["connected"] def test_wrong_roll_mismatch(): st = map_status(REAL_STATUS, "103") assert not st["ready"] assert st["errors"] == ["wrong roll: loaded 103x164, expected 103"] def test_matching_expected_label_ok(): assert map_status(REAL_STATUS, "103x164")["ready"] def test_not_waiting_phase(): raw = dict(REAL_STATUS, phase_type="Printing data to receive buffer") st = map_status(raw, "auto") assert st["connected"] and not st["ready"] def test_no_media(): raw = dict(REAL_STATUS, media_type="No media", identified_media=None) st = map_status(raw, "auto") assert st["media_type"] is None and st["label_id"] is None assert "no media loaded" in st["errors"] and not st["ready"] def test_unidentified_media_not_ready(): raw = dict(REAL_STATUS, identified_media=None) st = map_status(raw, "auto") assert not st["ready"] def test_dry_run_status_and_print(tmp_path, monkeypatch): monkeypatch.setattr("worker.printer.DRYRUN_DIR", tmp_path) printer = Printer(cfg()) st = printer.get_status() assert st["ready"] and st["label_id"] == "103x164" result = printer.print_image(Image.new("RGB", (10, 10)), "103x164") assert result["did_print"] is True assert list(tmp_path.glob("dryrun-*.png")) def test_dry_run_honours_expected_label(): st = Printer(cfg("103")).get_status() assert st["label_id"] == "103" and st["media_type"] == "continuous"