From 620d3b4fe2977ae8208ddd3146683487cacf11b9 Mon Sep 17 00:00:00 2001 From: Honey Date: Fri, 31 Jul 2026 09:32:59 +0200 Subject: [PATCH] Reject malformed text imports before creating junk recipes Co-authored-by: fredamn76 Signed-off-by: fredamn76 --- mealie_mcp/server.py | 11 +++++++++++ tests/test_server.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/mealie_mcp/server.py b/mealie_mcp/server.py index 66534b0..ac9a52d 100644 --- a/mealie_mcp/server.py +++ b/mealie_mcp/server.py @@ -288,6 +288,17 @@ def import_recipe_text(text: str, source_url: str | None = None) -> dict[str, An roundup, a YouTube description, or a social caption. Build the text in Swedish with ``Titel`` / ``Portioner`` / ``Ingredienser`` / ``Gör så här`` sections. """ + required = ("Titel", "Ingredienser", "Gör så här") + missing = [ + heading + for heading in required + if not re.search(rf"(?im)^\s*{re.escape(heading)}\s*:", text) + ] + if missing: + raise ValueError( + "Text import requires explicit Swedish section headings; missing: " + + ", ".join(missing) + ) payload: dict[str, Any] = {"data": text} if source_url: payload["url"] = source_url diff --git a/tests/test_server.py b/tests/test_server.py index f75d793..4e71bb3 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -96,6 +96,36 @@ class TestImportReport: assert server._import_report(recipe)["suggested_title"] is None +class TestTextImport: + def test_rejects_text_without_explicit_sections_before_writing(self, monkeypatch): + monkeypatch.setattr( + server, + "_request", + lambda *_args, **_kwargs: pytest.fail("must not create a junk recipe"), + ) + + with pytest.raises(ValueError, match="Titel"): + server.import_recipe_text( + "Namnlöst recept\n\nIngredienser:\n1 dl vatten\n\nGör så här:\nBlanda." + ) + + def test_accepts_the_documented_swedish_section_format(self, monkeypatch): + text = ( + "Titel: Testsoppa\n\nPortioner: 2\n\nIngredienser:\n1 dl vatten\n\n" + "Gör så här:\nBlanda." + ) + recipe = { + "slug": "testsoppa", + "name": "Testsoppa", + "recipeIngredient": [{"display": "1 dl vatten"}], + "recipeInstructions": [{"text": "Blanda."}], + } + monkeypatch.setattr(server, "_request", lambda *_args, **_kwargs: "testsoppa") + monkeypatch.setattr(server, "get_recipe", lambda _slug: recipe) + + assert server.import_recipe_text(text)["recipe"] == recipe + + class TestParseIngredients: def test_reverts_structure_when_mealie_rewords_the_swedish_line(self, mock_mealie): canonical = "4,7 dl basmatiris"