2026-04-11 21:58:29 +02:00
|
|
|
|
"""Tests für {{key|d}}, {{key|x}}, ai_caption und Unbekannt-Erkennung."""
|
2026-04-11 21:36:29 +02:00
|
|
|
|
from placeholder_registry import (
|
|
|
|
|
|
PlaceholderMetadata,
|
|
|
|
|
|
PlaceholderType,
|
|
|
|
|
|
OutputType,
|
|
|
|
|
|
build_ai_placeholder_caption,
|
|
|
|
|
|
)
|
|
|
|
|
|
import placeholder_resolver as pr
|
2026-04-11 21:47:08 +02:00
|
|
|
|
from placeholder_resolver import format_value_with_d_modifier
|
2026-04-11 21:58:29 +02:00
|
|
|
|
from prompt_executor import resolve_placeholders
|
2026-04-11 21:36:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
2026-04-11 21:58:29 +02:00
|
|
|
|
def test_build_ai_caption_is_explanation_only():
|
2026-04-11 21:36:29 +02:00
|
|
|
|
m = PlaceholderMetadata(
|
|
|
|
|
|
key="test_x",
|
|
|
|
|
|
category="Test",
|
|
|
|
|
|
description="Kurzbeschreibung",
|
|
|
|
|
|
resolver_module="m",
|
|
|
|
|
|
resolver_function="f",
|
|
|
|
|
|
semantic_contract="Lang Vertrag " * 50,
|
|
|
|
|
|
business_meaning="Kernbedeutung für die KI.",
|
|
|
|
|
|
unit="g/day",
|
|
|
|
|
|
placeholder_type=PlaceholderType.INTERPRETED,
|
|
|
|
|
|
output_type=OutputType.NUMERIC,
|
|
|
|
|
|
)
|
|
|
|
|
|
cap = build_ai_placeholder_caption(m)
|
|
|
|
|
|
assert "Kernbedeutung" in cap
|
2026-04-11 21:58:29 +02:00
|
|
|
|
assert "Kurzbeschreibung" not in cap
|
2026-04-11 21:36:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
2026-04-11 21:58:29 +02:00
|
|
|
|
def test_build_ai_caption_protein_avg_no_description_prefix():
|
2026-04-11 21:47:08 +02:00
|
|
|
|
m = PlaceholderMetadata(
|
|
|
|
|
|
key="protein_avg",
|
|
|
|
|
|
category="Ernährung",
|
|
|
|
|
|
description="Durchschn. Protein in g (30d)",
|
|
|
|
|
|
resolver_module="m",
|
|
|
|
|
|
resolver_function="f",
|
|
|
|
|
|
business_meaning="Zentraler Placeholder für Muskelerhalt.",
|
|
|
|
|
|
unit="g/day",
|
|
|
|
|
|
placeholder_type=PlaceholderType.INTERPRETED,
|
|
|
|
|
|
output_type=OutputType.NUMERIC,
|
|
|
|
|
|
)
|
|
|
|
|
|
cap = build_ai_placeholder_caption(m)
|
2026-04-11 21:58:29 +02:00
|
|
|
|
assert cap.startswith("Zentraler Placeholder")
|
|
|
|
|
|
assert "Durchschn. Protein" not in cap
|
2026-04-11 21:47:08 +02:00
|
|
|
|
assert "Technischer Bezug" not in cap
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-04-11 21:36:29 +02:00
|
|
|
|
def test_build_ai_caption_score_adds_scale():
|
|
|
|
|
|
m = PlaceholderMetadata(
|
|
|
|
|
|
key="test_score",
|
|
|
|
|
|
category="Test",
|
|
|
|
|
|
description="Score",
|
|
|
|
|
|
resolver_module="m",
|
|
|
|
|
|
resolver_function="f",
|
|
|
|
|
|
business_meaning="Gewichteter Gesamtscore.",
|
|
|
|
|
|
unit="Score (0-100)",
|
|
|
|
|
|
placeholder_type=PlaceholderType.SCORE,
|
|
|
|
|
|
output_type=OutputType.NUMERIC,
|
|
|
|
|
|
)
|
|
|
|
|
|
cap = build_ai_placeholder_caption(m)
|
|
|
|
|
|
assert "0–100" in cap or "0-100" in cap
|
|
|
|
|
|
assert "Gewichteter" in cap
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_placeholder_token_regex_optional_modifier():
|
|
|
|
|
|
m0 = pr._PLACEHOLDER_TOKEN_RE.search("{{fat_avg}}")
|
|
|
|
|
|
assert m0 and m0.group(1) == "fat_avg" and m0.group(2) is None
|
|
|
|
|
|
m1 = pr._PLACEHOLDER_TOKEN_RE.search("{{fat_avg|d}}")
|
|
|
|
|
|
assert m1 and m1.group(1) == "fat_avg" and m1.group(2).strip() == "d"
|
|
|
|
|
|
m2 = pr._PLACEHOLDER_TOKEN_RE.search("{{ protein_avg | d }}")
|
|
|
|
|
|
assert m2 and m2.group(1) == "protein_avg" and m2.group(2).strip() == "d"
|
2026-04-11 21:58:29 +02:00
|
|
|
|
m3 = pr._PLACEHOLDER_TOKEN_RE.search("{{k|d,x}}")
|
|
|
|
|
|
assert m3 and m3.group(1) == "k" and m3.group(2).strip() == "d,x"
|
2026-04-11 21:36:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_unknown_placeholders_strips_modifier():
|
|
|
|
|
|
unk = pr.get_unknown_placeholders("{{not_a_real_key|d}}")
|
|
|
|
|
|
assert set(unk) == {"not_a_real_key"}
|
2026-04-11 21:47:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
2026-04-11 21:58:29 +02:00
|
|
|
|
def test_format_value_with_d_modifier_uses_description_only():
|
2026-04-11 21:47:08 +02:00
|
|
|
|
row = {
|
|
|
|
|
|
"key": "protein_avg",
|
|
|
|
|
|
"description": "Durchschn. Protein in g (30d)",
|
2026-04-11 21:58:29 +02:00
|
|
|
|
"ai_caption": "Nur für |x",
|
2026-04-11 21:47:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
out = format_value_with_d_modifier("119g/Tag", row)
|
2026-04-11 21:58:29 +02:00
|
|
|
|
assert out == "119g/Tag — Durchschn. Protein in g (30d)"
|
|
|
|
|
|
assert "Nur für |x" not in out
|
2026-04-11 21:47:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_format_value_with_d_modifier_falls_back_to_description():
|
|
|
|
|
|
row = {"description": "Nur Beschreibung", "key": "x"}
|
|
|
|
|
|
assert format_value_with_d_modifier("42", row) == "42 — Nur Beschreibung"
|
2026-04-11 21:58:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_prompt_executor_modifiers_d_x_combined():
|
|
|
|
|
|
catalog = {"E": [{"key": "p", "description": "Desc", "ai_caption": "Expl"}]}
|
|
|
|
|
|
v = {"p": "99"}
|
|
|
|
|
|
assert resolve_placeholders("{{p|d}}", v, None, catalog) == "99 — Desc"
|
|
|
|
|
|
assert resolve_placeholders("{{p|x}}", v, None, catalog) == "Expl"
|
|
|
|
|
|
assert resolve_placeholders("{{p|d,x}}", v, None, catalog) == "99 — Desc — Expl"
|
|
|
|
|
|
assert resolve_placeholders("{{p}}", v, None, catalog) == "99"
|