feat: Phase 1 - Fragenergänzung + Strukturierter Container
Backend: - question_augmenter.py (290 Zeilen): Hybrid-Modell für Fragenergänzungen * merge_question_augmentations(): Knotengebundene Fragen überschreiben Prompt-Defaults * augment_prompt_with_questions(): Markdown-formatierte Fragenergänzung * parse_question_augmentations_from_jsonb(): JSONB → QuestionAugmentation[] - result_container_parser.py (250 Zeilen): Markdown-Sektionen-Parsing * parse_result_container(): Extrahiert Analysekern, Entscheidungsanteil, Begründungsanker * validate_decision_signal(): Normalisierung gegen answer_spectrum * Fallback-Parsing bei unstrukturierten Antworten - routers/workflow_questions.py (236 Zeilen): CRUD für workflow_question_catalog * GET /api/workflow/questions (mit active_only Filter) * POST/PUT/DELETE (Admin only, Soft Delete) - prompt_executor.py: Integration in execute_base_prompt() * Fragenergänzung vor LLM-Call (wenn node_questions oder catalog vorhanden) * Result-Container-Parsing nach LLM-Response - main.py: Router-Registrierung (workflow_questions) Tests: - test_phase1_question_augmenter.py (8 Tests): Hybrid-Modell, Formatierung, JSONB-Parsing - test_phase1_result_container_parser.py (17 Tests): Sektion-Extraktion, Decision-Parsing, Validierung Alle 25 Unit-Tests bestanden. version: 0.9j (backend) module: workflow 0.2.0 Konzept: .claude/task/Workflow_engine_prompting_engine/konzept_workflow_engine_konsolidated.md (Phase 1)
This commit is contained in:
1 parent
b5be6e21a5
commit
ca562b7130
8 files changed
+1237
-9
No files matched your search
@@ -0,0 +1,135 @@
|
||||
"""
|
||||
Unit Tests für question_augmenter.py (Phase 1)
|
||||
|
||||
Run with: PYTHONPATH=./backend pytest tests/backend/test_phase1_question_augmenter.py -v
|
||||
"""
|
||||
import pytest
|
||||
from workflow_models import QuestionAugmentation
|
||||
from question_augmenter import (
|
||||
augment_prompt_with_questions,
|
||||
merge_question_augmentations,
|
||||
format_question_list,
|
||||
parse_question_augmentations_from_jsonb
|
||||
)
|
||||
|
||||
|
||||
def test_format_question_list():
|
||||
"""Test: Formatierung der Fragenliste"""
|
||||
questions = [
|
||||
QuestionAugmentation(
|
||||
id="q1",
|
||||
type="relevanz",
|
||||
question="Ist relevant?",
|
||||
answer_spectrum=["ja", "nein", "unklar"]
|
||||
),
|
||||
QuestionAugmentation(
|
||||
id="q2",
|
||||
type="prioritaet",
|
||||
question="Wie hoch?",
|
||||
answer_spectrum=["hoch", "mittel", "niedrig", "unklar"]
|
||||
)
|
||||
]
|
||||
|
||||
result = format_question_list(questions)
|
||||
|
||||
assert "Relevanz" in result
|
||||
assert "[ja/nein/unklar]" in result
|
||||
assert "Prioritaet" in result # Lowercase wird capitalized
|
||||
assert "[hoch/mittel/niedrig/unklar]" in result
|
||||
|
||||
|
||||
def test_augment_prompt_with_questions():
|
||||
"""Test: Prompt-Erweiterung mit Fragenergänzungen"""
|
||||
base_prompt = "Analysiere die Körperdaten."
|
||||
questions = [
|
||||
QuestionAugmentation(
|
||||
id="q1",
|
||||
type="relevanz",
|
||||
question="Ist relevant?",
|
||||
answer_spectrum=["ja", "nein", "unklar"]
|
||||
)
|
||||
]
|
||||
|
||||
augmented = augment_prompt_with_questions(base_prompt, questions)
|
||||
|
||||
assert "Analysiere die Körperdaten." in augmented
|
||||
assert "## Analyse" in augmented
|
||||
assert "## Entscheidungsfragen" in augmented
|
||||
assert "Relevanz" in augmented
|
||||
assert "[ja/nein/unklar]" in augmented
|
||||
|
||||
|
||||
def test_merge_question_augmentations_node_priority():
|
||||
"""Test: Knotengebundene Fragen haben Vorrang (Hybridmodell)"""
|
||||
node_questions = [
|
||||
QuestionAugmentation(id="q1", type="relevanz", question="Q1", answer_spectrum=["ja", "nein"])
|
||||
]
|
||||
prompt_questions = [
|
||||
QuestionAugmentation(id="q2", type="prioritaet", question="Q2", answer_spectrum=["hoch", "niedrig"])
|
||||
]
|
||||
|
||||
result = merge_question_augmentations(node_questions, prompt_questions)
|
||||
|
||||
# Knotengebundene haben Vorrang
|
||||
assert len(result) == 1
|
||||
assert result[0].type == "relevanz"
|
||||
|
||||
|
||||
def test_merge_question_augmentations_prompt_fallback():
|
||||
"""Test: Prompt-Defaults werden verwendet wenn Knoten leer"""
|
||||
node_questions = None
|
||||
prompt_questions = [
|
||||
QuestionAugmentation(id="q2", type="prioritaet", question="Q2", answer_spectrum=["hoch", "niedrig"])
|
||||
]
|
||||
|
||||
result = merge_question_augmentations(node_questions, prompt_questions)
|
||||
|
||||
# Prompt-Defaults werden verwendet
|
||||
assert len(result) == 1
|
||||
assert result[0].type == "prioritaet"
|
||||
|
||||
|
||||
def test_merge_question_augmentations_empty():
|
||||
"""Test: Leere Liste wenn weder Knoten noch Prompt Fragen haben"""
|
||||
result = merge_question_augmentations(None, None)
|
||||
assert result == []
|
||||
|
||||
|
||||
def test_parse_question_augmentations_from_jsonb():
|
||||
"""Test: Parsing aus JSONB-Format"""
|
||||
jsonb_data = [
|
||||
{
|
||||
"id": "q1",
|
||||
"type": "relevanz",
|
||||
"question": "Ist relevant?",
|
||||
"answer_spectrum": ["ja", "nein", "unklar"]
|
||||
},
|
||||
{
|
||||
"id": "q2",
|
||||
"type": "prioritaet",
|
||||
"question": "Wie hoch?",
|
||||
"answer_spectrum": ["hoch", "mittel", "niedrig"]
|
||||
}
|
||||
]
|
||||
|
||||
result = parse_question_augmentations_from_jsonb(jsonb_data)
|
||||
|
||||
assert len(result) == 2
|
||||
assert result[0].type == "relevanz"
|
||||
assert result[1].type == "prioritaet"
|
||||
|
||||
|
||||
def test_parse_question_augmentations_empty_jsonb():
|
||||
"""Test: Leere Liste bei None JSONB"""
|
||||
result = parse_question_augmentations_from_jsonb(None)
|
||||
assert result == []
|
||||
|
||||
|
||||
def test_parse_question_augmentations_invalid_jsonb():
|
||||
"""Test: ValueError bei ungültigem JSONB"""
|
||||
with pytest.raises(ValueError, match="muss ein Array sein"):
|
||||
parse_question_augmentations_from_jsonb({"invalid": "format"})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
@@ -0,0 +1,234 @@
|
||||
"""
|
||||
Unit Tests für result_container_parser.py (Phase 1)
|
||||
|
||||
Run with: PYTHONPATH=./backend pytest tests/backend/test_phase1_result_container_parser.py -v
|
||||
"""
|
||||
import pytest
|
||||
from result_container_parser import (
|
||||
parse_result_container,
|
||||
extract_section,
|
||||
parse_decision_questions,
|
||||
validate_decision_signal,
|
||||
parse_result_container_robust
|
||||
)
|
||||
|
||||
|
||||
def test_extract_section_basic():
|
||||
"""Test: Einfache Sektion extrahieren"""
|
||||
text = """
|
||||
## Analyse
|
||||
Das ist der Analysekern.
|
||||
Mehrere Zeilen.
|
||||
|
||||
## Entscheidungsfragen
|
||||
- Relevanz: ja
|
||||
"""
|
||||
result = extract_section(text, "Analyse")
|
||||
assert result == "Das ist der Analysekern.\nMehrere Zeilen."
|
||||
|
||||
|
||||
def test_extract_section_not_found():
|
||||
"""Test: Nicht vorhandene Sektion"""
|
||||
text = "## Analyse\nInhalt"
|
||||
result = extract_section(text, "Begründung")
|
||||
assert result is None
|
||||
|
||||
|
||||
def test_extract_section_empty():
|
||||
"""Test: Leere Sektion (nur Whitespace am Ende)"""
|
||||
text = "## Analyse\n\n"
|
||||
result = extract_section(text, "Analyse")
|
||||
assert result is None
|
||||
|
||||
|
||||
def test_parse_decision_questions_basic():
|
||||
"""Test: Standard-Format parsen"""
|
||||
section = """
|
||||
- Relevanz: ja
|
||||
- Priorität: hoch
|
||||
- Selektion: nein
|
||||
"""
|
||||
result = parse_decision_questions(section)
|
||||
assert result == {
|
||||
"relevanz": "ja",
|
||||
"priorität": "hoch",
|
||||
"selektion": "nein"
|
||||
}
|
||||
|
||||
|
||||
def test_parse_decision_questions_bold():
|
||||
"""Test: Format mit **bold** Markup"""
|
||||
section = """
|
||||
- **Relevanz**: ja
|
||||
- **Priorität**: hoch
|
||||
"""
|
||||
result = parse_decision_questions(section)
|
||||
assert result == {
|
||||
"relevanz": "ja",
|
||||
"priorität": "hoch"
|
||||
}
|
||||
|
||||
|
||||
def test_parse_decision_questions_without_dash():
|
||||
"""Test: Format ohne führendes Minus"""
|
||||
section = """
|
||||
Relevanz: ja
|
||||
Priorität: hoch
|
||||
"""
|
||||
result = parse_decision_questions(section)
|
||||
assert result == {
|
||||
"relevanz": "ja",
|
||||
"priorität": "hoch"
|
||||
}
|
||||
|
||||
|
||||
def test_parse_decision_questions_brackets():
|
||||
"""Test: Format mit [Klammern]"""
|
||||
section = """
|
||||
- Relevanz: [ja]
|
||||
- Priorität: [hoch]
|
||||
"""
|
||||
result = parse_decision_questions(section)
|
||||
assert result == {
|
||||
"relevanz": "ja",
|
||||
"priorität": "hoch"
|
||||
}
|
||||
|
||||
|
||||
def test_validate_decision_signal_exact_match():
|
||||
"""Test: Exakte Übereinstimmung"""
|
||||
value, status = validate_decision_signal("ja", ["ja", "nein", "unklar"])
|
||||
assert value == "ja"
|
||||
assert status == "valid"
|
||||
|
||||
|
||||
def test_validate_decision_signal_normalized():
|
||||
"""Test: Case-insensitive Normalisierung"""
|
||||
value, status = validate_decision_signal("JA", ["ja", "nein", "unklar"])
|
||||
assert value == "ja"
|
||||
assert status == "normalized"
|
||||
|
||||
|
||||
def test_validate_decision_signal_invalid():
|
||||
"""Test: Ungültige Antwort"""
|
||||
value, status = validate_decision_signal("vielleicht", ["ja", "nein", "unklar"])
|
||||
assert value == "vielleicht"
|
||||
assert status == "invalid"
|
||||
|
||||
|
||||
def test_parse_result_container_complete():
|
||||
"""Test: Vollständiger Container mit allen Sektionen"""
|
||||
llm_output = """
|
||||
## Analyse
|
||||
Der Nutzer zeigt eine positive Gewichtsentwicklung.
|
||||
Kaloriendefizit wird eingehalten.
|
||||
|
||||
## Entscheidungsfragen
|
||||
- Relevanz: ja
|
||||
- Priorität: hoch
|
||||
|
||||
## Begründung
|
||||
Die Gewichtsabnahme ist im Zielbereich von 0.5-1% pro Woche.
|
||||
"""
|
||||
result = parse_result_container(llm_output)
|
||||
|
||||
assert result["parsing_status"] == "complete"
|
||||
assert "Gewichtsentwicklung" in result["analysis_core"]
|
||||
assert result["decision_signals"]["relevanz"] == "ja"
|
||||
assert result["decision_signals"]["priorität"] == "hoch"
|
||||
assert "Zielbereich" in result["reasoning_anchors"]
|
||||
|
||||
|
||||
def test_parse_result_container_partial():
|
||||
"""Test: Container ohne Begründung (partial)"""
|
||||
llm_output = """
|
||||
## Analyse
|
||||
Analyse-Inhalt
|
||||
|
||||
## Entscheidungsfragen
|
||||
- Relevanz: ja
|
||||
"""
|
||||
result = parse_result_container(llm_output)
|
||||
|
||||
assert result["parsing_status"] == "complete"
|
||||
assert result["analysis_core"] == "Analyse-Inhalt"
|
||||
assert result["decision_signals"]["relevanz"] == "ja"
|
||||
assert result["reasoning_anchors"] is None
|
||||
|
||||
|
||||
def test_parse_result_container_no_structure():
|
||||
"""Test: Unstrukturierte Antwort (Fallback)"""
|
||||
llm_output = "Einfache Textantwort ohne Strukturierung."
|
||||
|
||||
result = parse_result_container(llm_output)
|
||||
|
||||
assert result["parsing_status"] == "fallback"
|
||||
assert result["analysis_core"] == "Einfache Textantwort ohne Strukturierung."
|
||||
assert result["decision_signals"] == {}
|
||||
assert result["reasoning_anchors"] is None
|
||||
|
||||
|
||||
def test_parse_result_container_only_questions():
|
||||
"""Test: Nur Entscheidungsfragen, keine Analyse (partial)"""
|
||||
llm_output = """
|
||||
## Entscheidungsfragen
|
||||
- Relevanz: nein
|
||||
- Priorität: niedrig
|
||||
"""
|
||||
result = parse_result_container(llm_output)
|
||||
|
||||
assert result["parsing_status"] == "partial"
|
||||
assert result["analysis_core"] == ""
|
||||
assert result["decision_signals"]["relevanz"] == "nein"
|
||||
|
||||
|
||||
def test_parse_result_container_robust_with_warnings():
|
||||
"""Test: Robuste Variante mit erwarteten Fragen"""
|
||||
llm_output = """
|
||||
## Analyse
|
||||
Inhalt
|
||||
|
||||
## Entscheidungsfragen
|
||||
- Relevanz: ja
|
||||
"""
|
||||
expected_questions = ["relevanz", "prioritaet", "selektion"]
|
||||
|
||||
result = parse_result_container_robust(llm_output, expected_questions)
|
||||
|
||||
assert "warnings" in result
|
||||
assert any("Fehlende Entscheidungsfragen" in w for w in result["warnings"])
|
||||
assert any("prioritaet" in w for w in result["warnings"])
|
||||
|
||||
|
||||
def test_parse_result_container_case_insensitive():
|
||||
"""Test: Case-insensitive Sektion-Matching"""
|
||||
llm_output = """
|
||||
## ANALYSE
|
||||
Großgeschrieben
|
||||
|
||||
## entscheidungsfragen
|
||||
- Relevanz: ja
|
||||
"""
|
||||
result = parse_result_container(llm_output)
|
||||
|
||||
assert result["parsing_status"] == "complete"
|
||||
assert result["analysis_core"] == "Großgeschrieben"
|
||||
assert result["decision_signals"]["relevanz"] == "ja"
|
||||
|
||||
|
||||
def test_parse_decision_questions_mixed_formats():
|
||||
"""Test: Gemischte Formate in einer Sektion"""
|
||||
section = """
|
||||
- **Relevanz**: ja
|
||||
Priorität: hoch
|
||||
- Selektion: [nein]
|
||||
"""
|
||||
result = parse_decision_questions(section)
|
||||
|
||||
assert result["relevanz"] == "ja"
|
||||
assert result["priorität"] == "hoch"
|
||||
assert result["selektion"] == "nein"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
Reference in new issue
Block a user