"""Checks against the real (private, untracked) workbook. Skipped when it isn't there.""" import io import json import os import zipfile import pytest from PIL import Image from cookit_import.bundle import MAX_PHOTO_EDGE, write_bundle from cookit_import.recipe import build_recipe, is_recipe_sheet, real_photos, template_photo_hashes from cookit_import.workbook import read_sheets WORKBOOK = os.environ.get("COOKIT_WORKBOOK", os.path.join(os.path.dirname(__file__), "..", "..", "..", "Wochenspeiseplan.xlsm")) pytestmark = pytest.mark.skipif(not os.path.exists(WORKBOOK), reason="Wochenspeiseplan.xlsm not available") @pytest.fixture(scope="module") def recipes(): all_sheets = read_sheets(WORKBOOK) placeholders = template_photo_hashes(all_sheets) return {s.title: (build_recipe(s), real_photos(s, placeholders)) for s in all_sheets if is_recipe_sheet(s)} def test_all_recipe_sheets_found(recipes): assert len(recipes) == 214 assert "Leeres Rezept" not in recipes and "Spinat-Feta-Pizza" not in recipes def test_focaccia(recipes): r, photos = recipes["Focaccia"] assert [s["title"] for s in r["stepSections"]][:2] == ["Vorteig", "Teig"] assert len(r["ingredientGroups"]) == 3 assert len(r["sources"]) == 2 assert r["notes"] == "22x22 cm eckige Form" assert photos == [] # only the template's stock picture was on the sheet def test_template_stock_photo_is_dropped_but_real_photos_kept(recipes): without = [title for title, (_, photos) in recipes.items() if not photos] assert 25 <= len(without) <= 35 assert len(recipes["Melonen-Minze-Shake"][1]) == 3 def test_rinderrouladen_groups_from_comments(recipes): r, _ = recipes["Klassische Rinderrouladen"] assert [g["name"] for g in r["ingredientGroups"]] == ["Rouladen", "Sauce", "Zusätzliche Zutaten"] def test_bundle_round_trip(recipes, tmp_path): subset = [recipes[t] for t in ("Focaccia", "Melonen-Minze-Shake", "Hähnchen")] out = tmp_path / "bundle.zip" write_bundle(str(out), [(dict(r), photos) for r, photos in subset]) with zipfile.ZipFile(out) as z: manifest = json.loads(z.read("bundle.json")) assert manifest["format"] == "cookit.recipe-bundle" and manifest["version"] == 1 shake = next(r for r in manifest["recipes"] if r["name"] == "Melonen-Minze-Shake") assert len(shake["photos"]) == 3 for name in shake["photos"]: img = Image.open(io.BytesIO(z.read(name))) assert img.format == "JPEG" and max(img.size) <= MAX_PHOTO_EDGE