import json from uuid import uuid4 from mealie.schema.recipe.recipe_step import IngredientReferences, StepCooking from tests.utils import api_routes def operation_recipe(api_client, unique_user): slug = api_client.post(api_routes.recipes, json={"name": f"Operations {uuid4()}"}, headers=unique_user.token).json() route = api_routes.recipes_slug(slug) recipe = api_client.get(route, headers=unique_user.token).json() ref_id, step_id = str(uuid4()), str(uuid4()) recipe["recipeIngredient"] = [{"referenceId": ref_id, "quantity": 100, "note": "flour"}] recipe["recipeInstructions"] = [ { "id": step_id, "text": "Mix and check consistency", "durationSeconds": 0.5, "ingredientReferences": [ { "referenceId": ref_id, "quantityKind": "partial", "quantity": 20, "weighing": "weigh", "role": "addition", "destination": "bowl", "quantityMinimum": 19, "quantityMaximum": 21, "approximate": True, "containerCount": 2, "sourceText": "two portions", } ], "cooking": { "appliance": "mixer", "applianceInstance": "left", "actionType": "machine", "timerType": "machine", "durationKind": "range", "temperatureCelsius": 37.5, "speed": 1.5, "reverse": False, "mode": "pulse", "durationRangeSeconds": {"minimum": 0.5, "maximum": 2.5}, "durationCondition": "Until combined", "speedKind": "progression", "speedRange": {"minimum": 1, "maximum": 5}, "speedProgression": [1, 3, 5], "accessories": [ {"name": name, "state": state} for name, state in [ ("steamer", "installed"), ("basket", "removed"), ("whisk", "unknown"), ("lid", "closed"), ] ], "modeParameters": [ {"name": "thickness", "kind": "text", "value": "coarse"}, {"name": "doneness", "kind": "text", "value": "medium soft"}, {"name": "pulses", "kind": "number", "value": 3}, {"name": "cover", "kind": "boolean", "value": False}, {"name": "pulseRange", "kind": "range", "minimum": 2, "maximum": 4}, ], "optional": True, "condition": "If too coarse", "requiresConfirmation": True, "repetitions": 3, "repetitionRange": {"minimum": 2, "maximum": 4}, "reviews": [ { "category": "ingredient", "source": "add some flour", "reason": "Ambiguous portion", "candidateIngredientIds": [ref_id], } ], }, }, { "text": "Reuse mixture", "ingredientReferences": [{"referenceId": ref_id, "quantityKind": "prepared"}], "cooking": {"actionType": "manual", "dependsOnStepIds": [step_id]}, }, ] response = api_client.put(route, json=recipe, headers=unique_user.token) assert response.status_code == 200, response.text saved = api_client.get(route, headers=unique_user.token).json() desired = StepCooking.model_validate(recipe["recipeInstructions"][0]["cooking"]).model_dump( mode="json", by_alias=True ) actual = saved["recipeInstructions"][0]["cooking"] assert {key: value for key, value in actual.items() if key != "reviews"} == { key: value for key, value in desired.items() if key != "reviews" } desired_reference = IngredientReferences.model_validate( recipe["recipeInstructions"][0]["ingredientReferences"][0] ).model_dump(mode="json", by_alias=True) assert { key: value for key, value in saved["recipeInstructions"][0]["ingredientReferences"][0].items() if key != "allocationId" } == {key: value for key, value in desired_reference.items() if key != "allocationId"} return saved def test_full_operations_round_trip_edit_duplicate_and_export(api_client, unique_user): recipe = operation_recipe(api_client, unique_user) step = recipe["recipeInstructions"][0] assert step["durationSeconds"] == 0.5 assert step["ingredientReferences"][0]["weighing"] == "weigh" assert step["cooking"]["modeParameters"][3]["value"] is False allocation_id = step["ingredientReferences"][0]["allocationId"] review = step["cooking"]["reviews"][0] assert review["history"] and review["status"] == "unresolved" review["resolution"], review["status"] = "Confirmed portion and manual check", "resolved" route = api_routes.recipes_slug(recipe["slug"]) assert api_client.put(route, json=recipe, headers=unique_user.token).status_code == 200 saved = api_client.get(route, headers=unique_user.token).json() assert saved["recipeInstructions"][0]["ingredientReferences"][0]["allocationId"] == allocation_id assert saved["recipeInstructions"][0]["cooking"]["reviews"][0]["approvedContent"] assert saved["recipeInstructions"][0]["id"] == step["id"] # Reordering never renumbers instruction/allocation identities or loses dependencies. saved["recipeInstructions"].reverse() assert api_client.put(route, json=saved, headers=unique_user.token).status_code == 200 reordered = api_client.get(route, headers=unique_user.token).json() assert reordered["recipeInstructions"][1]["id"] == step["id"] assert reordered["recipeInstructions"][1]["ingredientReferences"][0]["allocationId"] == allocation_id saved["recipeInstructions"].reverse() assert api_client.put(route, json=saved, headers=unique_user.token).status_code == 200 duplicated = api_client.post( api_routes.recipes_slug_duplicate(recipe["slug"]), json={"name": f"Copy {uuid4()}"}, headers=unique_user.token ) assert duplicated.status_code == 201, duplicated.text copy = duplicated.json() copied_step = copy["recipeInstructions"][0] assert copied_step["id"] != step["id"] assert copied_step["ingredientReferences"][0]["allocationId"] != allocation_id assert copy["recipeInstructions"][1]["cooking"]["dependsOnStepIds"] == [copied_step["id"]] assert copied_step["cooking"]["reviews"][0]["candidateIngredientIds"] == [ copy["recipeIngredient"][0]["referenceId"] ] assert copied_step["cooking"]["modeParameters"] == step["cooking"]["modeParameters"] response = api_client.post( api_routes.recipes_create_html_or_json, json={"data": json.dumps(saved), "include_tags": True, "include_categories": True}, headers=unique_user.token, ) assert response.status_code == 201, response.text imported = api_client.get(api_routes.recipes_slug(response.json()), headers=unique_user.token).json() assert imported["recipeInstructions"][0]["cooking"]["modeParameters"] == step["cooking"]["modeParameters"] assert imported["recipeInstructions"][0]["ingredientReferences"][0]["quantityKind"] == "partial" assert imported["recipeInstructions"][1]["cooking"]["dependsOnStepIds"] == [imported["recipeInstructions"][0]["id"]] def test_changed_native_content_reopens_review_without_erasing_history(api_client, unique_user): recipe = operation_recipe(api_client, unique_user) route = api_routes.recipes_slug(recipe["slug"]) review = recipe["recipeInstructions"][0]["cooking"]["reviews"][0] review.update(status="resolved", resolution="Checked") assert api_client.put(route, json=recipe, headers=unique_user.token).status_code == 200 saved = api_client.get(route, headers=unique_user.token).json() history = saved["recipeInstructions"][0]["cooking"]["reviews"][0]["history"] saved["recipeInstructions"][0]["cooking"]["speed"] = 2 assert api_client.put(route, json=saved, headers=unique_user.token).status_code == 200 changed = api_client.get(route, headers=unique_user.token).json() review = changed["recipeInstructions"][0]["cooking"]["reviews"][0] assert review["status"] == "unresolved" assert review["history"][: len(history)] == history assert review["source"] == "add some flour" changed["recipeInstructions"][0]["cooking"] = None assert api_client.put(route, json=changed, headers=unique_user.token).status_code == 200 assert api_client.get(route, headers=unique_user.token).json()["recipeInstructions"][0]["cooking"]["reviews"] def test_all_allocation_kinds_survive_readback(api_client, unique_user): recipe = operation_recipe(api_client, unique_user) reference_id = recipe["recipeIngredient"][0]["referenceId"] kinds = ["full", "partial", "remaining", "unknown", "prepared", "asNeeded", "referenceOnly"] recipe["recipeInstructions"] = [ { "text": kind, "ingredientReferences": [ {"referenceId": reference_id, "quantityKind": kind, "quantity": 0 if kind == "partial" else None} ], } for kind in kinds ] route = api_routes.recipes_slug(recipe["slug"]) response = api_client.put(route, json=recipe, headers=unique_user.token) assert response.status_code == 200, response.text saved = api_client.get(route, headers=unique_user.token).json() refs = [step["ingredientReferences"][0] for step in saved["recipeInstructions"]] assert [ref["quantityKind"] for ref in refs] == kinds assert [ref["quantity"] for ref in refs] == [None, 0, 0, 0, 0, 0, 0] assert saved["recipeIngredient"][0]["quantity"] == 100 assert all(ref["weighing"] is None for ref in refs)