Make ingredient parsing safe against live Mealie rewrites

Co-authored-by: Fizz <fizz@agents.famfallman.com>
Co-authored-by: fredamn76 <fredrik.fallman@gmail.com>
Signed-off-by: fredamn76 <fredrik.fallman@gmail.com>
This commit is contained in:
2026-07-31 08:38:11 +02:00
committed by fredamn76
parent ddc0fb1a5e
commit 1f10bd321c
4 changed files with 300 additions and 47 deletions
+35
View File
@@ -6,11 +6,16 @@ import pytest
from mealie_mcp.normalize import (
clean_title,
find_english_leftovers,
food_matches_line,
has_extraction_failure,
ingredient_display_lines,
line_remainder,
looks_mangled,
match_unit,
normalize_for_parser,
same_line,
slugify,
unit_token,
)
@@ -97,3 +102,33 @@ class TestIngredientDisplayLines:
]
}
assert ingredient_display_lines(recipe) == ["2 dl grädde", "1 gul lök", "salt"]
class TestParserStructureSafety:
def test_reads_the_written_unit_token(self):
assert unit_token("4,7 dl basmatiris") == "dl"
assert unit_token("1-1,5 msk fond") == "msk"
assert unit_token("salt") is None
def test_matches_instance_unit_by_abbreviation(self):
gram = {"id": "g", "name": "gram", "abbreviation": "g"}
assert match_unit("g", [gram]) == gram
assert match_unit("dl", [gram]) is None
def test_rejects_a_nearby_but_different_food(self):
assert food_matches_line("kycklinglårfilé", "800 g kycklinglårfilé")
assert not food_matches_line("kycklingfilé", "800 g kycklinglårfilé")
def test_builds_note_from_only_the_unstructured_remainder(self):
assert (
line_remainder(
"200 g körsbärstomater (i olika färger)",
"g",
"körsbärstomat",
)
== "(i olika färger)"
)
def test_same_line_ignores_only_whitespace(self):
assert same_line("3 dl grädde", " 3 dl grädde ")
assert not same_line("3 dl grädde", "3 liter grädde")