test
This commit is contained in:
parent
5dd20d683f
commit
9a18f3cc8b
|
|
@ -1,7 +1,7 @@
|
||||||
"""
|
"""
|
||||||
FILE: tests/test_WP22_integration.py
|
FILE: tests/test_WP22_integration.py
|
||||||
DESCRIPTION: Integrationstest für WP-22 (Graph Intelligence).
|
DESCRIPTION: Integrationstest für WP-22 (Graph Intelligence).
|
||||||
FIX: Nutzt IsolatedAsyncioTestCase und importiert asyncio korrekt.
|
FIXES: Pydantic Validation & Config Caching Issues.
|
||||||
"""
|
"""
|
||||||
import unittest
|
import unittest
|
||||||
import os
|
import os
|
||||||
|
|
@ -11,19 +11,22 @@ import yaml
|
||||||
import asyncio
|
import asyncio
|
||||||
from unittest.mock import MagicMock, patch, AsyncMock
|
from unittest.mock import MagicMock, patch, AsyncMock
|
||||||
|
|
||||||
# --- Imports der App-Module ---
|
# Wir importieren das Modul direkt, um auf den Cache zuzugreifen
|
||||||
|
import app.routers.chat
|
||||||
|
|
||||||
|
# DTOs und Logik
|
||||||
from app.models.dto import ChatRequest, QueryRequest, QueryHit
|
from app.models.dto import ChatRequest, QueryRequest, QueryHit
|
||||||
from app.services.edge_registry import EdgeRegistry
|
from app.services.edge_registry import EdgeRegistry
|
||||||
from app.core.retriever import _compute_total_score, _get_status_multiplier
|
from app.core.retriever import _compute_total_score, _get_status_multiplier
|
||||||
from app.routers.chat import _classify_intent, get_decision_strategy, chat_endpoint
|
from app.routers.chat import _classify_intent, get_decision_strategy, chat_endpoint
|
||||||
|
|
||||||
# --- Test Suite ---
|
|
||||||
class TestWP22Integration(unittest.IsolatedAsyncioTestCase):
|
class TestWP22Integration(unittest.IsolatedAsyncioTestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""Bereitet eine isolierte Test-Umgebung vor."""
|
"""Bereitet eine isolierte Test-Umgebung vor."""
|
||||||
self.test_dir = "tests/temp_integration"
|
self.test_dir = "tests/temp_integration"
|
||||||
# Environment Patching: Hier simulieren wir Ihre .env
|
|
||||||
|
# 1. Environment Patching
|
||||||
self.os_env_patch = patch.dict(os.environ, {
|
self.os_env_patch = patch.dict(os.environ, {
|
||||||
"MINDNET_VAULT_ROOT": self.test_dir,
|
"MINDNET_VAULT_ROOT": self.test_dir,
|
||||||
"MINDNET_DECISION_CONFIG": os.path.join(self.test_dir, "config", "decision_engine.yaml"),
|
"MINDNET_DECISION_CONFIG": os.path.join(self.test_dir, "config", "decision_engine.yaml"),
|
||||||
|
|
@ -31,17 +34,17 @@ class TestWP22Integration(unittest.IsolatedAsyncioTestCase):
|
||||||
})
|
})
|
||||||
self.os_env_patch.start()
|
self.os_env_patch.start()
|
||||||
|
|
||||||
# Verzeichnisse erstellen
|
# 2. Verzeichnisse erstellen
|
||||||
os.makedirs(os.path.join(self.test_dir, "config"), exist_ok=True)
|
os.makedirs(os.path.join(self.test_dir, "config"), exist_ok=True)
|
||||||
os.makedirs(os.path.join(self.test_dir, "01_User_Manual"), exist_ok=True)
|
os.makedirs(os.path.join(self.test_dir, "01_User_Manual"), exist_ok=True)
|
||||||
os.makedirs(os.path.join(self.test_dir, "data", "logs"), exist_ok=True)
|
os.makedirs(os.path.join(self.test_dir, "data", "logs"), exist_ok=True)
|
||||||
|
|
||||||
# 1. Config: decision_engine.yaml (mit Boosts)
|
# 3. Config: decision_engine.yaml schreiben (Test-Definition)
|
||||||
self.decision_config = {
|
self.decision_config = {
|
||||||
"strategies": {
|
"strategies": {
|
||||||
"FACT": {
|
"FACT": {
|
||||||
"trigger_keywords": ["was ist"],
|
"trigger_keywords": ["was ist"],
|
||||||
"edge_boosts": {"part_of": 2.0}
|
"edge_boosts": {"part_of": 2.0} # Kein 'caused_by' hier!
|
||||||
},
|
},
|
||||||
"CAUSAL": {
|
"CAUSAL": {
|
||||||
"trigger_keywords": ["warum", "weshalb"],
|
"trigger_keywords": ["warum", "weshalb"],
|
||||||
|
|
@ -52,15 +55,17 @@ class TestWP22Integration(unittest.IsolatedAsyncioTestCase):
|
||||||
with open(os.environ["MINDNET_DECISION_CONFIG"], "w") as f:
|
with open(os.environ["MINDNET_DECISION_CONFIG"], "w") as f:
|
||||||
yaml.dump(self.decision_config, f)
|
yaml.dump(self.decision_config, f)
|
||||||
|
|
||||||
# 2. Config: Edge Vocabulary (für Registry)
|
# 4. Config: Edge Vocabulary schreiben
|
||||||
# Wir platzieren die Datei genau dort, wo die Registry sie relativ zu MINDNET_VAULT_ROOT erwartet
|
|
||||||
vocab_path = os.path.join(self.test_dir, "01_User_Manual", "01_edge_vocabulary.md")
|
vocab_path = os.path.join(self.test_dir, "01_User_Manual", "01_edge_vocabulary.md")
|
||||||
with open(vocab_path, "w") as f:
|
with open(vocab_path, "w") as f:
|
||||||
f.write("| **caused_by** | ursache_ist, wegen |\n| **part_of** | teil_von |")
|
f.write("| **caused_by** | ursache_ist, wegen |\n| **part_of** | teil_von |")
|
||||||
|
|
||||||
# 3. Registry Reset
|
# 5. CACHE RESET (WICHTIG!)
|
||||||
# Wir zwingen das Singleton zum Neustart, damit es die neuen ENV-Variablen liest
|
# Damit der Router die oben geschriebene YAML auch wirklich liest:
|
||||||
|
app.routers.chat._DECISION_CONFIG_CACHE = None
|
||||||
EdgeRegistry._instance = None
|
EdgeRegistry._instance = None
|
||||||
|
|
||||||
|
# Registry neu init
|
||||||
self.registry = EdgeRegistry(vault_root=self.test_dir)
|
self.registry = EdgeRegistry(vault_root=self.test_dir)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
|
|
@ -68,131 +73,98 @@ class TestWP22Integration(unittest.IsolatedAsyncioTestCase):
|
||||||
if os.path.exists(self.test_dir):
|
if os.path.exists(self.test_dir):
|
||||||
shutil.rmtree(self.test_dir)
|
shutil.rmtree(self.test_dir)
|
||||||
EdgeRegistry._instance = None
|
EdgeRegistry._instance = None
|
||||||
|
app.routers.chat._DECISION_CONFIG_CACHE = None
|
||||||
|
|
||||||
# ------------------------------------------------------------------------
|
# ------------------------------------------------------------------------
|
||||||
# TEST 1: Edge Registry & Validation (WP-22 B)
|
# TEST 1: Edge Registry & Validation
|
||||||
# ------------------------------------------------------------------------
|
# ------------------------------------------------------------------------
|
||||||
def test_edge_registry_aliases(self):
|
def test_edge_registry_aliases(self):
|
||||||
print("\n🔵 TEST 1: Edge Registry Resolution")
|
print("\n🔵 TEST 1: Edge Registry Resolution")
|
||||||
|
|
||||||
# Prüfung: Hat er die Datei gefunden?
|
|
||||||
# Wenn die Registry leer ist, hat das Laden nicht geklappt.
|
|
||||||
self.assertTrue(len(self.registry.valid_types) > 0,
|
|
||||||
f"Registry ist leer! Pfad war: {self.registry.vault_root}")
|
|
||||||
|
|
||||||
# Test: Alias Auflösung
|
|
||||||
resolved = self.registry.resolve("ursache_ist")
|
resolved = self.registry.resolve("ursache_ist")
|
||||||
self.assertEqual(resolved, "caused_by", "Alias 'ursache_ist' sollte zu 'caused_by' werden.")
|
self.assertEqual(resolved, "caused_by")
|
||||||
|
|
||||||
# Test: Unknown Logging
|
|
||||||
unknown = self.registry.resolve("foobar_link")
|
unknown = self.registry.resolve("foobar_link")
|
||||||
self.assertEqual(unknown, "foobar_link", "Unbekannte Kanten sollen durchgereicht werden.")
|
self.assertEqual(unknown, "foobar_link")
|
||||||
|
|
||||||
log_path = self.registry.unknown_log_path
|
log_path = self.registry.unknown_log_path
|
||||||
self.assertTrue(os.path.exists(log_path), "Logfile für unbekannte Kanten fehlt.")
|
self.assertTrue(os.path.exists(log_path))
|
||||||
with open(log_path, "r") as f:
|
print("✅ Registry funktioniert.")
|
||||||
self.assertIn("foobar_link", f.read())
|
|
||||||
print("✅ Registry funktioniert (Datei gefunden & Alias gelöst).")
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------------
|
# ------------------------------------------------------------------------
|
||||||
# TEST 2: Lifecycle Scoring (WP-22 A)
|
# TEST 2: Lifecycle Scoring
|
||||||
# ------------------------------------------------------------------------
|
# ------------------------------------------------------------------------
|
||||||
def test_lifecycle_scoring_logic(self):
|
def test_lifecycle_scoring_logic(self):
|
||||||
print("\n🔵 TEST 2: Lifecycle Scoring (Draft vs. Stable)")
|
print("\n🔵 TEST 2: Lifecycle Scoring")
|
||||||
# Mock Weights: Sem=1.0, Edge=0.5, Cent=0.0
|
|
||||||
with patch("app.core.retriever._get_scoring_weights", return_value=(1.0, 0.5, 0.0)):
|
with patch("app.core.retriever._get_scoring_weights", return_value=(1.0, 0.5, 0.0)):
|
||||||
base_sem = 0.9
|
base_sem = 0.9
|
||||||
|
|
||||||
# Case A: Draft (Malus)
|
|
||||||
payload_draft = {"status": "draft", "retriever_weight": 1.0}
|
payload_draft = {"status": "draft", "retriever_weight": 1.0}
|
||||||
mult_draft = _get_status_multiplier(payload_draft)
|
mult_draft = _get_status_multiplier(payload_draft)
|
||||||
self.assertEqual(mult_draft, 0.8, "Draft sollte 0.8 Multiplier haben.")
|
self.assertEqual(mult_draft, 0.8)
|
||||||
|
|
||||||
score_draft, _, _ = _compute_total_score(base_sem, payload_draft)
|
|
||||||
|
|
||||||
# Case B: Stable (Bonus)
|
|
||||||
payload_stable = {"status": "stable", "retriever_weight": 1.0}
|
payload_stable = {"status": "stable", "retriever_weight": 1.0}
|
||||||
mult_stable = _get_status_multiplier(payload_stable)
|
mult_stable = _get_status_multiplier(payload_stable)
|
||||||
self.assertEqual(mult_stable, 1.2, "Stable sollte 1.2 Multiplier haben.")
|
self.assertEqual(mult_stable, 1.2)
|
||||||
|
print("✅ Lifecycle Scoring korrekt.")
|
||||||
score_stable, _, _ = _compute_total_score(base_sem, payload_stable)
|
|
||||||
|
|
||||||
print(f" Draft Score: {score_draft:.2f} | Stable Score: {score_stable:.2f}")
|
|
||||||
self.assertGreater(score_stable, score_draft)
|
|
||||||
print("✅ Lifecycle Scoring korrekt implementiert.")
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------------
|
# ------------------------------------------------------------------------
|
||||||
# TEST 3: Semantic Router & Boosting (WP-22 C)
|
# TEST 3: Semantic Router & Boosting
|
||||||
# ------------------------------------------------------------------------
|
# ------------------------------------------------------------------------
|
||||||
async def test_router_integration(self):
|
async def test_router_integration(self):
|
||||||
print("\n🔵 TEST 3: Semantic Router Integration")
|
print("\n🔵 TEST 3: Semantic Router Integration")
|
||||||
|
|
||||||
# Mock LLM Service
|
|
||||||
mock_llm = MagicMock()
|
mock_llm = MagicMock()
|
||||||
mock_llm.prompts = {}
|
mock_llm.prompts = {}
|
||||||
|
|
||||||
# --- Szenario A: Kausal-Frage ("Warum...") ---
|
# Da der Cache im setUp gelöscht wurde, sollte er jetzt CAUSAL finden
|
||||||
query_causal = "Warum ist das Projekt gescheitert?"
|
query_causal = "Warum ist das Projekt gescheitert?"
|
||||||
|
|
||||||
# 1. Intent Detection prüfen
|
|
||||||
intent, source = await _classify_intent(query_causal, mock_llm)
|
intent, source = await _classify_intent(query_causal, mock_llm)
|
||||||
self.assertEqual(intent, "CAUSAL", "Sollte 'CAUSAL' Intent erkennen via Keywords.")
|
|
||||||
|
|
||||||
# 2. Strategy Load prüfen
|
self.assertEqual(intent, "CAUSAL", f"Erwartete CAUSAL, bekam {intent} via {source}")
|
||||||
|
|
||||||
strategy = get_decision_strategy(intent)
|
strategy = get_decision_strategy(intent)
|
||||||
boosts = strategy.get("edge_boosts", {})
|
boosts = strategy.get("edge_boosts", {})
|
||||||
self.assertEqual(boosts.get("caused_by"), 3.0, "Sollte 'caused_by' Boost von 3.0 laden.")
|
self.assertEqual(boosts.get("caused_by"), 3.0)
|
||||||
|
|
||||||
print(f" Intent: {intent} -> Boosts: {boosts}")
|
|
||||||
print("✅ Router lädt Config korrekt.")
|
print("✅ Router lädt Config korrekt.")
|
||||||
|
|
||||||
# ------------------------------------------------------------------------
|
# ------------------------------------------------------------------------
|
||||||
# TEST 4: Full Pipeline (Chat -> Retriever)
|
# TEST 4: Full Pipeline
|
||||||
# ------------------------------------------------------------------------
|
# ------------------------------------------------------------------------
|
||||||
async def test_full_pipeline_flow(self):
|
async def test_full_pipeline_flow(self):
|
||||||
print("\n🔵 TEST 4: Full Chat Pipeline (Integration)")
|
print("\n🔵 TEST 4: Full Chat Pipeline")
|
||||||
|
|
||||||
# Mocks
|
|
||||||
mock_llm = AsyncMock()
|
mock_llm = AsyncMock()
|
||||||
mock_llm.prompts = {}
|
mock_llm.prompts = {}
|
||||||
mock_llm.generate_raw_response.return_value = "Das ist die Antwort."
|
mock_llm.generate_raw_response.return_value = "Antwort."
|
||||||
|
|
||||||
mock_retriever = AsyncMock()
|
mock_retriever = AsyncMock()
|
||||||
# Mock Search Result
|
# FIX: note_id hinzugefügt für Pydantic
|
||||||
mock_hit = QueryHit(
|
mock_hit = QueryHit(
|
||||||
node_id="123", semantic_score=0.9, edge_bonus=0.5, centrality_bonus=0.0, total_score=1.0,
|
node_id="123",
|
||||||
source={"text": "Inhalt"}, payload={"type": "concept"}
|
note_id="test_note_123", # <--- WICHTIG
|
||||||
|
semantic_score=0.9,
|
||||||
|
edge_bonus=0.5,
|
||||||
|
centrality_bonus=0.0,
|
||||||
|
total_score=1.0,
|
||||||
|
source={"text": "Inhalt"},
|
||||||
|
payload={"type": "concept"}
|
||||||
)
|
)
|
||||||
mock_retriever.search.return_value.results = [mock_hit]
|
mock_retriever.search.return_value.results = [mock_hit]
|
||||||
|
|
||||||
# Request: "Warum..."
|
|
||||||
req = ChatRequest(message="Warum ist das passiert?", top_k=3)
|
req = ChatRequest(message="Warum ist das passiert?", top_k=3)
|
||||||
|
|
||||||
# Cache Reset für Config (wichtig, damit er unsere Test-Config neu lädt)
|
|
||||||
import app.routers.chat
|
|
||||||
app.routers.chat._DECISION_CONFIG_CACHE = None
|
|
||||||
|
|
||||||
response = await chat_endpoint(req, llm=mock_llm, retriever=mock_retriever)
|
response = await chat_endpoint(req, llm=mock_llm, retriever=mock_retriever)
|
||||||
|
|
||||||
# ASSERTIONS
|
|
||||||
|
|
||||||
# 1. Wurde der Retriever mit den Boosts aufgerufen?
|
|
||||||
called_query_req = mock_retriever.search.call_args[0][0]
|
called_query_req = mock_retriever.search.call_args[0][0]
|
||||||
|
self.assertEqual(called_query_req.boost_edges.get("caused_by"), 3.0)
|
||||||
self.assertIsNotNone(called_query_req.boost_edges, "Retriever sollte boost_edges erhalten.")
|
|
||||||
self.assertEqual(called_query_req.boost_edges.get("caused_by"), 3.0, "Boost 'caused_by' sollte 3.0 sein.")
|
|
||||||
|
|
||||||
# 2. Wurde der Intent korrekt durchgereicht?
|
|
||||||
self.assertEqual(response.intent, "CAUSAL")
|
self.assertEqual(response.intent, "CAUSAL")
|
||||||
|
print("✅ Pipeline reicht Boosts weiter.")
|
||||||
print(f" Retriever called with: {called_query_req.boost_edges}")
|
|
||||||
print("✅ Pipeline reicht Boosts erfolgreich weiter.")
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------------
|
# ------------------------------------------------------------------------
|
||||||
# TEST 5: Regression Check (Fallback behavior)
|
# TEST 5: Regression Check
|
||||||
# ------------------------------------------------------------------------
|
# ------------------------------------------------------------------------
|
||||||
async def test_regression_standard_query(self):
|
async def test_regression_standard_query(self):
|
||||||
print("\n🔵 TEST 5: Regression (Standard Query)")
|
print("\n🔵 TEST 5: Regression")
|
||||||
|
|
||||||
mock_llm = AsyncMock()
|
mock_llm = AsyncMock()
|
||||||
mock_llm.prompts = {}
|
mock_llm.prompts = {}
|
||||||
|
|
@ -201,22 +173,16 @@ class TestWP22Integration(unittest.IsolatedAsyncioTestCase):
|
||||||
mock_retriever = AsyncMock()
|
mock_retriever = AsyncMock()
|
||||||
mock_retriever.search.return_value.results = []
|
mock_retriever.search.return_value.results = []
|
||||||
|
|
||||||
# Cache Reset
|
req = ChatRequest(message="Was ist das?", top_k=3)
|
||||||
import app.routers.chat
|
|
||||||
app.routers.chat._DECISION_CONFIG_CACHE = None
|
|
||||||
|
|
||||||
req = ChatRequest(message="Hallo Welt", top_k=3)
|
|
||||||
|
|
||||||
response = await chat_endpoint(req, llm=mock_llm, retriever=mock_retriever)
|
response = await chat_endpoint(req, llm=mock_llm, retriever=mock_retriever)
|
||||||
|
|
||||||
called_query_req = mock_retriever.search.call_args[0][0]
|
called_query_req = mock_retriever.search.call_args[0][0]
|
||||||
|
|
||||||
# FACT strategy hat in unserem Test Setup 'part_of': 2.0
|
# FACT strategy hat in unserem Test Setup NUR 'part_of', KEIN 'caused_by'
|
||||||
# Aber keine 'caused_by' boosts.
|
|
||||||
self.assertEqual(response.intent, "FACT")
|
self.assertEqual(response.intent, "FACT")
|
||||||
self.assertNotIn("caused_by", called_query_req.boost_edges or {})
|
self.assertNotIn("caused_by", called_query_req.boost_edges or {})
|
||||||
|
print("✅ Regression Test bestanden.")
|
||||||
print("✅ Regression Test bestanden (Standard-Flow intakt).")
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
Loading…
Reference in New Issue
Block a user