728 lines
31 KiB
Python
728 lines
31 KiB
Python
"""
|
|
V8.2 Tests: A1-A4 Implementation.
|
|
|
|
A1: Onboarding-Widerspruchsprüfung
|
|
A2: Butler-Nachklärung bei Ablehnung (Strategie-Modus)
|
|
A3: Lernfunktion-Persistenz in room-map.json + Demotion
|
|
A4: Butler in Catch-All DM mit Strategie-Modus
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from datetime import datetime, timedelta
|
|
from pathlib import Path
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
|
|
|
|
from plugin.archivar.room_map import RoomMap, RoomEntry
|
|
from plugin.archivar.archivar_state import ArchivarState
|
|
|
|
|
|
# ═══════════════════════════════════════════════════════════════════════════════
|
|
# A1: Onboarding-Widerspruchsprüfung
|
|
# ═══════════════════════════════════════════════════════════════════════════════
|
|
|
|
class TestA1OnboardingConflictCheck(unittest.TestCase):
|
|
"""A1: Beim Onboarding 100%-Keyword-Konflikte erkennen."""
|
|
|
|
def setUp(self):
|
|
self.temp_dir = tempfile.TemporaryDirectory()
|
|
self.temp_path = Path(self.temp_dir.name) / "room-map.json"
|
|
|
|
self.test_data = {
|
|
"version": 2,
|
|
"rooms": {
|
|
"catchall_id": {
|
|
"name": "#CatchAll",
|
|
"theme": "Catch-all room",
|
|
"keywords_100": [],
|
|
"keywords_context": [],
|
|
"catch_all": True,
|
|
},
|
|
"plugin_room_id": {
|
|
"name": "#Rocketchat-Plugin",
|
|
"theme": "Plugin-Entwicklung",
|
|
"keywords_100": ["plugin", "rocketchat", "gateway"],
|
|
"keywords_context": ["adapter", "websocket"],
|
|
"catch_all": False,
|
|
},
|
|
},
|
|
"excluded_rooms": [],
|
|
"fact_store_path": str(self.temp_path.parent / "docs"),
|
|
}
|
|
with open(self.temp_path, "w") as f:
|
|
json.dump(self.test_data, f)
|
|
|
|
def tearDown(self):
|
|
self.temp_dir.cleanup()
|
|
|
|
def test_a1_no_conflict_when_keywords_unique(self):
|
|
"""Kein Konflikt wenn 100%-Keywords einzigartig sind."""
|
|
room_map = RoomMap._from_dict(self.test_data)
|
|
conflicts = room_map.check_keyword_conflicts(
|
|
keywords=["dokumentation", "netzwerk"],
|
|
exclude_room_id="new_room_id",
|
|
)
|
|
self.assertEqual(len(conflicts), 0)
|
|
|
|
def test_a1_conflict_detected_for_existing_100_keyword(self):
|
|
"""Konflikt wenn 100%-Keyword bereits in anderem Raum existiert."""
|
|
room_map = RoomMap._from_dict(self.test_data)
|
|
conflicts = room_map.check_keyword_conflicts(
|
|
keywords=["plugin", "neues-thema"],
|
|
exclude_room_id="new_room_id",
|
|
)
|
|
self.assertEqual(len(conflicts), 1)
|
|
self.assertIn("plugin", conflicts[0]["keyword"])
|
|
self.assertIn("plugin_room_id", conflicts[0]["room_id"])
|
|
self.assertIn("#Rocketchat-Plugin", conflicts[0]["room_name"])
|
|
|
|
def test_a1_conflict_detected_for_multiple_keywords(self):
|
|
"""Mehrere Konflikte werden erkannt."""
|
|
room_map = RoomMap._from_dict(self.test_data)
|
|
conflicts = room_map.check_keyword_conflicts(
|
|
keywords=["plugin", "rocketchat", "gateway"],
|
|
exclude_room_id="new_room_id",
|
|
)
|
|
self.assertEqual(len(conflicts), 3)
|
|
|
|
def test_a1_no_conflict_when_same_room_excluded(self):
|
|
"""Kein Konflikt wenn der eigene Raum excluded wird (Update)."""
|
|
room_map = RoomMap._from_dict(self.test_data)
|
|
conflicts = room_map.check_keyword_conflicts(
|
|
keywords=["plugin", "rocketchat"],
|
|
exclude_room_id="plugin_room_id",
|
|
)
|
|
self.assertEqual(len(conflicts), 0)
|
|
|
|
|
|
# ═══════════════════════════════════════════════════════════════════════════════
|
|
# A2: Butler-Nachklärung bei Ablehnung (Strategie-Modus)
|
|
# ═══════════════════════════════════════════════════════════════════════════════
|
|
|
|
class TestA2StrategieNachklaerung(unittest.TestCase):
|
|
"""A2: Nach Ablehnung des Move-Requests → Nachklärung mit Strategie-Buttons."""
|
|
|
|
def setUp(self):
|
|
self.temp_dir = tempfile.TemporaryDirectory()
|
|
self.temp_path = Path(self.temp_dir.name) / "room-map.json"
|
|
self.state_path = Path(self.temp_dir.name) / "archivar_state.json"
|
|
|
|
self.test_data = {
|
|
"version": 2,
|
|
"rooms": {
|
|
"catchall_id": {
|
|
"name": "#CatchAll",
|
|
"theme": "Catch-all room",
|
|
"keywords_100": [],
|
|
"keywords_context": [],
|
|
"catch_all": True,
|
|
},
|
|
"plugin_room_id": {
|
|
"name": "#Rocketchat-Plugin",
|
|
"theme": "Plugin-Entwicklung",
|
|
"keywords_100": ["plugin"],
|
|
"keywords_context": ["adapter"],
|
|
"catch_all": False,
|
|
},
|
|
},
|
|
"excluded_rooms": [],
|
|
"fact_store_path": str(self.temp_path.parent / "docs"),
|
|
}
|
|
with open(self.temp_path, "w") as f:
|
|
json.dump(self.test_data, f)
|
|
|
|
def tearDown(self):
|
|
self.temp_dir.cleanup()
|
|
|
|
def test_a2_stay_once_triggers_nachklaerung(self):
|
|
"""'Nur diesmal' (stay_once) → Nachklärung wird getriggert."""
|
|
from plugin.archivar.adapter_integration import ArchivarMessageHandler
|
|
|
|
with patch('archivar.adapter_integration.ROOM_MAP_PATH', str(self.temp_path)):
|
|
with patch('archivar.room_map.DEFAULT_MAP_PATH', str(self.temp_path)):
|
|
handler = ArchivarMessageHandler()
|
|
handler._state._state_file_path = str(self.state_path)
|
|
handler._state._loaded = True
|
|
|
|
# Simuliere stay_once response
|
|
result = handler._should_trigger_nachklaerung("stay_once")
|
|
self.assertTrue(result)
|
|
|
|
def test_a2_stay_here_does_not_trigger_nachklaerung(self):
|
|
"""'Generell hier' (stay_here) → KEINE Nachklärung (Lernen direkt)."""
|
|
from plugin.archivar.adapter_integration import ArchivarMessageHandler
|
|
|
|
with patch('archivar.adapter_integration.ROOM_MAP_PATH', str(self.temp_path)):
|
|
with patch('archivar.room_map.DEFAULT_MAP_PATH', str(self.temp_path)):
|
|
handler = ArchivarMessageHandler()
|
|
handler._state._state_file_path = str(self.state_path)
|
|
handler._state._loaded = True
|
|
|
|
result = handler._should_trigger_nachklaerung("stay_here")
|
|
self.assertFalse(result)
|
|
|
|
def test_a2_confirm_move_does_not_trigger_nachklaerung(self):
|
|
"""'Rüber' (confirm_move) → KEINE Nachklärung."""
|
|
from plugin.archivar.adapter_integration import ArchivarMessageHandler
|
|
|
|
with patch('archivar.adapter_integration.ROOM_MAP_PATH', str(self.temp_path)):
|
|
with patch('archivar.room_map.DEFAULT_MAP_PATH', str(self.temp_path)):
|
|
handler = ArchivarMessageHandler()
|
|
handler._state._state_file_path = str(self.state_path)
|
|
handler._state._loaded = True
|
|
|
|
result = handler._should_trigger_nachklaerung("confirm_move")
|
|
self.assertFalse(result)
|
|
|
|
def test_a2_strategie_mode_sets_ring_jetzt(self):
|
|
"""Strategie-Auswahl → Ring-Zustand 'jetzt' (temporär, 30 Min)."""
|
|
state = ArchivarState()
|
|
state._loaded = True
|
|
|
|
state.set_ring_state("test_room_123", "jetzt", ttl_minutes=30)
|
|
ring = state.get_ring_state("test_room_123")
|
|
self.assertEqual(ring["state"], "jetzt")
|
|
self.assertIsNotNone(ring["expires_at"])
|
|
|
|
def test_a2_strategie_mode_no_learning(self):
|
|
"""Strategie-Modus → KEIN Lernen (kein keyword_context Update)."""
|
|
state = ArchivarState()
|
|
state._loaded = True
|
|
|
|
# Simuliere: Strategie-Modus aktiv
|
|
state.set_ring_state("test_room_123", "jetzt", ttl_minutes=30)
|
|
|
|
# Versuche zu lernen → sollte blockiert werden
|
|
can_learn = not state.is_ring_state_active("test_room_123", "jetzt")
|
|
self.assertFalse(can_learn, "Strategie-Modus sollte Lernen blockieren")
|
|
|
|
def test_a2_anderer_grund_triggers_normal_learning(self):
|
|
"""'ANDERER GRUND' → normale Lernfunktion."""
|
|
from plugin.archivar.adapter_integration import ArchivarMessageHandler
|
|
|
|
with patch('archivar.adapter_integration.ROOM_MAP_PATH', str(self.temp_path)):
|
|
with patch('archivar.room_map.DEFAULT_MAP_PATH', str(self.temp_path)):
|
|
handler = ArchivarMessageHandler()
|
|
handler._state._state_file_path = str(self.state_path)
|
|
handler._state._loaded = True
|
|
|
|
# ANDERER GRUND → kein Strategie-Modus, normales Lernen
|
|
is_strategie = handler._is_strategie_topic("ANDERER GRUND")
|
|
self.assertFalse(is_strategie)
|
|
|
|
def test_a2_strategie_topic_detection(self):
|
|
"""Strategie-Themen werden korrekt erkannt."""
|
|
from plugin.archivar.adapter_integration import ArchivarMessageHandler
|
|
|
|
with patch('archivar.adapter_integration.ROOM_MAP_PATH', str(self.temp_path)):
|
|
with patch('archivar.room_map.DEFAULT_MAP_PATH', str(self.temp_path)):
|
|
handler = ArchivarMessageHandler()
|
|
handler._state._state_file_path = str(self.state_path)
|
|
handler._state._loaded = True
|
|
|
|
self.assertTrue(handler._is_strategie_topic("strategie"))
|
|
self.assertTrue(handler._is_strategie_topic("absprache"))
|
|
self.assertTrue(handler._is_strategie_topic("planung"))
|
|
self.assertTrue(handler._is_strategie_topic("entscheidung"))
|
|
self.assertFalse(handler._is_strategie_topic("dokumentation"))
|
|
self.assertFalse(handler._is_strategie_topic("plugin"))
|
|
|
|
|
|
# ═══════════════════════════════════════════════════════════════════════════════
|
|
# A3: Lernfunktion-Persistenz in room-map.json + Demotion
|
|
# ═══════════════════════════════════════════════════════════════════════════════
|
|
|
|
class TestA3LernPersistenz(unittest.TestCase):
|
|
"""A3: Gelernte Begriffe werden in room-map.json persistiert."""
|
|
|
|
def setUp(self):
|
|
self.temp_dir = tempfile.TemporaryDirectory()
|
|
self.temp_path = Path(self.temp_dir.name) / "room-map.json"
|
|
|
|
self.test_data = {
|
|
"version": 2,
|
|
"rooms": {
|
|
"catchall_id": {
|
|
"name": "#CatchAll",
|
|
"theme": "Catch-all room",
|
|
"keywords_100": [],
|
|
"keywords_context": [],
|
|
"catch_all": True,
|
|
},
|
|
"plugin_room_id": {
|
|
"name": "#Rocketchat-Plugin",
|
|
"theme": "Plugin-Entwicklung",
|
|
"keywords_100": ["plugin"],
|
|
"keywords_context": ["adapter"],
|
|
"catch_all": False,
|
|
},
|
|
},
|
|
"excluded_rooms": [],
|
|
"fact_store_path": str(self.temp_path.parent / "docs"),
|
|
}
|
|
with open(self.temp_path, "w") as f:
|
|
json.dump(self.test_data, f)
|
|
|
|
def tearDown(self):
|
|
self.temp_dir.cleanup()
|
|
|
|
def test_a3_add_keyword_context_persists(self):
|
|
"""1x 'Hier bleiben' → keyword_context wird in room-map.json geschrieben."""
|
|
room_map = RoomMap._from_dict(self.test_data)
|
|
room_map.add_keyword_context("plugin_room_id", "neues-keyword")
|
|
RoomMap.save(room_map, self.temp_path)
|
|
|
|
# Reload und prüfen
|
|
reloaded = RoomMap.load(self.temp_path)
|
|
entry = reloaded.get_room_by_id("plugin_room_id")
|
|
self.assertIn("neues-keyword", entry.keywords_context)
|
|
|
|
def test_a3_promote_to_keywords_100_after_3_stays(self):
|
|
"""3x 'Hier bleiben' → Keyword wird zu keywords_100 promoted."""
|
|
room_map = RoomMap._from_dict(self.test_data)
|
|
|
|
# Simuliere 3 stays für "dokumentation"
|
|
room_map.add_keyword_context("plugin_room_id", "dokumentation")
|
|
room_map.increment_stay_count("plugin_room_id", "dokumentation")
|
|
room_map.increment_stay_count("plugin_room_id", "dokumentation")
|
|
count = room_map.increment_stay_count("plugin_room_id", "dokumentation")
|
|
|
|
self.assertEqual(count, 3)
|
|
# Nach 3 stays → promote
|
|
promoted = room_map.promote_to_keywords_100("plugin_room_id", "dokumentation")
|
|
self.assertTrue(promoted)
|
|
entry = room_map.get_room_by_id("plugin_room_id")
|
|
self.assertIn("dokumentation", entry.keywords_100)
|
|
|
|
def test_a3_demotion_after_90_days_no_match(self):
|
|
"""90 Tage kein Match → auto-demote von keywords_100 zu keywords_context."""
|
|
room_map = RoomMap._from_dict(self.test_data)
|
|
|
|
# Setze last_matched auf 91 Tage in der Vergangenheit
|
|
old_date = (datetime.now() - timedelta(days=91)).isoformat()
|
|
room_map.set_keyword_last_matched("plugin_room_id", "plugin", old_date)
|
|
|
|
# Führe Demotion durch
|
|
demoted = room_map.demote_stale_keywords(max_age_days=90)
|
|
self.assertGreaterEqual(demoted, 1)
|
|
|
|
entry = room_map.get_room_by_id("plugin_room_id")
|
|
self.assertNotIn("plugin", entry.keywords_100)
|
|
self.assertIn("plugin", entry.keywords_context)
|
|
|
|
def test_a3_no_demotion_when_recently_matched(self):
|
|
"""Keyword mit kürzlichem Match wird NICHT demoted."""
|
|
room_map = RoomMap._from_dict(self.test_data)
|
|
|
|
# Setze last_matched auf heute
|
|
recent_date = datetime.now().isoformat()
|
|
room_map.set_keyword_last_matched("plugin_room_id", "plugin", recent_date)
|
|
|
|
demoted = room_map.demote_stale_keywords(max_age_days=90)
|
|
self.assertEqual(demoted, 0)
|
|
|
|
entry = room_map.get_room_by_id("plugin_room_id")
|
|
self.assertIn("plugin", entry.keywords_100)
|
|
|
|
def test_a3_faiss_stale_detection_on_keyword_change(self):
|
|
"""FAISS is_stale() erkennt Member-Set-Change nach Keyword-Änderung."""
|
|
room_map = RoomMap._from_dict(self.test_data)
|
|
old_version = room_map.version
|
|
|
|
room_map.add_keyword_context("plugin_room_id", "neues-keyword")
|
|
self.assertGreater(room_map.version, old_version,
|
|
"Version muss nach Keyword-Änderung inkrementiert sein")
|
|
|
|
|
|
# ═══════════════════════════════════════════════════════════════════════════════
|
|
# A4: Butler in Catch-All DM mit Strategie-Modus
|
|
# ═══════════════════════════════════════════════════════════════════════════════
|
|
|
|
class TestA4DMButlerStrategie(unittest.TestCase):
|
|
"""A4: Butler arbeitet in Catch-All DM, Strategie-Modus bei DM-Themen."""
|
|
|
|
def setUp(self):
|
|
self.temp_dir = tempfile.TemporaryDirectory()
|
|
self.temp_path = Path(self.temp_dir.name) / "room-map.json"
|
|
|
|
self.test_data = {
|
|
"version": 2,
|
|
"rooms": {
|
|
"catchall_id": {
|
|
"name": "DM Admin",
|
|
"theme": "Catch-all DM",
|
|
"keywords_100": [],
|
|
"keywords_context": [],
|
|
"catch_all": True,
|
|
},
|
|
"plugin_room_id": {
|
|
"name": "#Rocketchat-Plugin",
|
|
"theme": "Plugin-Entwicklung",
|
|
"keywords_100": ["plugin"],
|
|
"keywords_context": ["adapter"],
|
|
"catch_all": False,
|
|
},
|
|
},
|
|
"excluded_rooms": [],
|
|
"fact_store_path": str(self.temp_path.parent / "docs"),
|
|
}
|
|
with open(self.temp_path, "w") as f:
|
|
json.dump(self.test_data, f)
|
|
|
|
def tearDown(self):
|
|
self.temp_dir.cleanup()
|
|
|
|
def test_a4_dm_allows_butler_routing(self):
|
|
"""DM-Guard erlaubt jetzt Butler-Routing aus DMs (A4)."""
|
|
from plugin.archivar.router import BidirectionalRouter
|
|
from plugin.archivar.room_map import RoomMap
|
|
from plugin.archivar.archivar_state import ArchivarState
|
|
|
|
room_map = RoomMap._from_dict(self.test_data)
|
|
state = ArchivarState()
|
|
state._loaded = True
|
|
|
|
mock_classifier = MagicMock()
|
|
mock_classifier.is_initialized = True
|
|
mock_classifier.classify = AsyncMock(return_value=None)
|
|
mock_classifier._llm_raw = AsyncMock(return_value="plugin_room_id")
|
|
|
|
router = BidirectionalRouter(room_map, mock_classifier, state)
|
|
|
|
# DM (origin_room_id='') sollte jetzt Routing erlauben
|
|
import asyncio
|
|
decision = asyncio.run(
|
|
router.route("Bitte das Plugin dokumentieren", origin_room_id="dm_test_v8_2", is_dm=True)
|
|
)
|
|
|
|
# Sollte NICHT stay_in_room sein (A4 erlaubt DM-Routing)
|
|
# Der LLM schlägt plugin_room_id vor → Move-Request
|
|
self.assertFalse(decision.stay_in_room,
|
|
"A4: DM sollte Routing erlauben (nicht mehr stay_in_room)")
|
|
|
|
def test_a4_dm_strategie_topic_no_learning(self):
|
|
"""DM-Strategie-Themen (absprache, planung, strategie, entscheidung)
|
|
→ Strategie-Modus, KEIN Lernen."""
|
|
from plugin.archivar.adapter_integration import ArchivarMessageHandler
|
|
|
|
with patch('archivar.adapter_integration.ROOM_MAP_PATH', str(self.temp_path)):
|
|
with patch('archivar.room_map.DEFAULT_MAP_PATH', str(self.temp_path)):
|
|
handler = ArchivarMessageHandler()
|
|
handler._state._loaded = True
|
|
|
|
# DM-Strategie-Themen
|
|
for topic in ["absprache", "planung", "strategie", "entscheidung"]:
|
|
is_strategie = handler._is_strategie_topic(topic)
|
|
self.assertTrue(is_strategie,
|
|
f"'{topic}' sollte als Strategie-Thema erkannt werden")
|
|
|
|
def test_a4_dm_non_strategie_topic_allows_learning(self):
|
|
"""DM-Nicht-Strategie-Themen → normales Lernen."""
|
|
from plugin.archivar.adapter_integration import ArchivarMessageHandler
|
|
|
|
with patch('archivar.adapter_integration.ROOM_MAP_PATH', str(self.temp_path)):
|
|
with patch('archivar.room_map.DEFAULT_MAP_PATH', str(self.temp_path)):
|
|
handler = ArchivarMessageHandler()
|
|
handler._state._loaded = True
|
|
|
|
# Normales Thema → kein Strategie-Modus
|
|
self.assertFalse(handler._is_strategie_topic("dokumentation"))
|
|
self.assertFalse(handler._is_strategie_topic("plugin"))
|
|
|
|
|
|
# ═══════════════════════════════════════════════════════════════════════════════
|
|
# V8.2.1: Echte Integrationstests (Rainer Quality-Loop)
|
|
# ═══════════════════════════════════════════════════════════════════════════════
|
|
|
|
class TestV821A2Integration(unittest.TestCase):
|
|
"""A2: Echter Test dass stay_once → Strategie-Buttons gesendet werden."""
|
|
|
|
def setUp(self):
|
|
self.temp_dir = tempfile.TemporaryDirectory()
|
|
self.temp_path = Path(self.temp_dir.name) / "room-map.json"
|
|
|
|
self.test_data = {
|
|
"version": 2,
|
|
"rooms": {
|
|
"catchall_id": {
|
|
"name": "#CatchAll",
|
|
"theme": "Catch-all room",
|
|
"keywords_100": [],
|
|
"keywords_context": [],
|
|
"catch_all": True,
|
|
},
|
|
"plugin_room_id": {
|
|
"name": "#Rocketchat-Plugin",
|
|
"theme": "Plugin-Entwicklung",
|
|
"keywords_100": ["plugin"],
|
|
"keywords_context": ["adapter"],
|
|
"catch_all": False,
|
|
},
|
|
},
|
|
"excluded_rooms": [],
|
|
"fact_store_path": str(self.temp_path.parent / "docs"),
|
|
}
|
|
with open(self.temp_path, "w") as f:
|
|
json.dump(self.test_data, f)
|
|
|
|
def tearDown(self):
|
|
self.temp_dir.cleanup()
|
|
|
|
def test_a2_stay_once_sends_strategie_buttons(self):
|
|
"""stay_once → _should_trigger_nachklaerung() → _get_nachklaerung_buttons()
|
|
liefert korrekte Buttons mit allen 5 Optionen."""
|
|
from plugin.archivar.adapter_integration import ArchivarMessageHandler
|
|
|
|
with patch('archivar.adapter_integration.ROOM_MAP_PATH', str(self.temp_path)):
|
|
with patch('archivar.room_map.DEFAULT_MAP_PATH', str(self.temp_path)):
|
|
handler = ArchivarMessageHandler()
|
|
handler._state._loaded = True
|
|
|
|
# stay_once muss Nachklärung triggern
|
|
self.assertTrue(handler._should_trigger_nachklaerung("stay_once"))
|
|
|
|
# Nachklärungs-Buttons müssen alle 5 Optionen enthalten
|
|
buttons = handler._get_nachklaerung_buttons()
|
|
self.assertIn("actions", buttons)
|
|
action_types = [a["type"] for a in buttons["actions"]]
|
|
self.assertIn("absprache", action_types)
|
|
self.assertIn("planung", action_types)
|
|
self.assertIn("strategie", action_types)
|
|
self.assertIn("entscheidung", action_types)
|
|
self.assertIn("anderer_grund", action_types)
|
|
self.assertEqual(len(buttons["actions"]), 5)
|
|
|
|
def test_a2_strategie_topic_sets_ring_and_blocks_learning(self):
|
|
"""Strategie-Thema → set_ring_state('jetzt') → is_ring_state_active
|
|
blockiert Lernen."""
|
|
state = ArchivarState()
|
|
state._loaded = True
|
|
|
|
# Setze Ring-Zustand
|
|
state.set_ring_state("room_123", "jetzt", ttl_minutes=30)
|
|
ring = state.get_ring_state("room_123")
|
|
self.assertEqual(ring["state"], "jetzt")
|
|
self.assertIsNotNone(ring["expires_at"])
|
|
|
|
# Ring-Zustand 'jetzt' blockiert Lernen
|
|
self.assertTrue(state.is_ring_state_active("room_123", "jetzt"))
|
|
|
|
# Ring-Zustand 'aus' blockiert NICHT
|
|
self.assertFalse(state.is_ring_state_active("room_123", "aus"))
|
|
|
|
|
|
class TestV821A3Integration(unittest.TestCase):
|
|
"""A3: Echter Test dass _stay_counts nach save/load erhalten bleiben."""
|
|
|
|
def setUp(self):
|
|
self.temp_dir = tempfile.TemporaryDirectory()
|
|
self.temp_path = Path(self.temp_dir.name) / "room-map.json"
|
|
|
|
self.test_data = {
|
|
"version": 2,
|
|
"rooms": {
|
|
"catchall_id": {
|
|
"name": "#CatchAll",
|
|
"theme": "Catch-all room",
|
|
"keywords_100": [],
|
|
"keywords_context": [],
|
|
"catch_all": True,
|
|
},
|
|
"plugin_room_id": {
|
|
"name": "#Rocketchat-Plugin",
|
|
"theme": "Plugin-Entwicklung",
|
|
"keywords_100": ["plugin"],
|
|
"keywords_context": ["adapter"],
|
|
"catch_all": False,
|
|
},
|
|
},
|
|
"excluded_rooms": [],
|
|
"fact_store_path": str(self.temp_path.parent / "docs"),
|
|
}
|
|
with open(self.temp_path, "w") as f:
|
|
json.dump(self.test_data, f)
|
|
|
|
def tearDown(self):
|
|
self.temp_dir.cleanup()
|
|
|
|
def test_a3_stay_counts_survive_save_load_roundtrip(self):
|
|
"""_stay_counts und _keyword_last_matched überleben save/load."""
|
|
room_map = RoomMap._from_dict(self.test_data)
|
|
|
|
# Setze Tracking-Daten
|
|
room_map.increment_stay_count("plugin_room_id", "dokumentation")
|
|
room_map.increment_stay_count("plugin_room_id", "dokumentation")
|
|
room_map.increment_stay_count("plugin_room_id", "dokumentation")
|
|
room_map.set_keyword_last_matched(
|
|
"plugin_room_id", "plugin", "2026-07-14T10:00:00"
|
|
)
|
|
|
|
# Persistiere
|
|
RoomMap.save(room_map, self.temp_path)
|
|
|
|
# Lade neu
|
|
reloaded = RoomMap.load(self.temp_path)
|
|
entry = reloaded.get_room_by_id("plugin_room_id")
|
|
|
|
# _stay_counts muss erhalten sein
|
|
self.assertEqual(entry._stay_counts.get("dokumentation"), 3)
|
|
|
|
# _keyword_last_matched muss erhalten sein
|
|
self.assertEqual(
|
|
entry._keyword_last_matched.get("plugin"), "2026-07-14T10:00:00"
|
|
)
|
|
|
|
def test_a3_promote_resets_stay_count(self):
|
|
"""Promotion zu keywords_100 setzt _stay_counts zurück."""
|
|
room_map = RoomMap._from_dict(self.test_data)
|
|
|
|
# 3 stays → promote
|
|
room_map.add_keyword_context("plugin_room_id", "dokumentation")
|
|
for _ in range(3):
|
|
room_map.increment_stay_count("plugin_room_id", "dokumentation")
|
|
|
|
promoted = room_map.promote_to_keywords_100("plugin_room_id", "dokumentation")
|
|
self.assertTrue(promoted)
|
|
|
|
entry = room_map.get_room_by_id("plugin_room_id")
|
|
self.assertIn("dokumentation", entry.keywords_100)
|
|
# Stay-Count muss zurückgesetzt sein
|
|
self.assertNotIn("dokumentation", entry._stay_counts)
|
|
|
|
def test_a3_demotion_preserves_tracking_in_roundtrip(self):
|
|
"""Demotion + save/load: Tracking-Felder bleiben konsistent."""
|
|
room_map = RoomMap._from_dict(self.test_data)
|
|
|
|
# Setze altes last_matched
|
|
old_date = (datetime.now() - timedelta(days=100)).isoformat()
|
|
room_map.set_keyword_last_matched("plugin_room_id", "plugin", old_date)
|
|
|
|
# Demote
|
|
demoted = room_map.demote_stale_keywords(max_age_days=90)
|
|
self.assertGreaterEqual(demoted, 1)
|
|
|
|
# Persistiere
|
|
RoomMap.save(room_map, self.temp_path)
|
|
|
|
# Reload
|
|
reloaded = RoomMap.load(self.temp_path)
|
|
entry = reloaded.get_room_by_id("plugin_room_id")
|
|
|
|
# Nach Demotion: keyword in keywords_context, nicht in keywords_100
|
|
self.assertNotIn("plugin", entry.keywords_100)
|
|
self.assertIn("plugin", entry.keywords_context)
|
|
# _keyword_last_matched für demoted keyword muss gelöscht sein
|
|
self.assertNotIn("plugin", entry._keyword_last_matched)
|
|
|
|
|
|
class TestV821A4Integration(unittest.TestCase):
|
|
"""A4: Echter Test dass Strategie-Thema im DM → stay_in_room."""
|
|
|
|
def setUp(self):
|
|
self.temp_dir = tempfile.TemporaryDirectory()
|
|
self.temp_path = Path(self.temp_dir.name) / "room-map.json"
|
|
|
|
self.test_data = {
|
|
"version": 2,
|
|
"rooms": {
|
|
"catchall_id": {
|
|
"name": "DM Admin",
|
|
"theme": "Catch-all DM",
|
|
"keywords_100": [],
|
|
"keywords_context": [],
|
|
"catch_all": True,
|
|
},
|
|
"plugin_room_id": {
|
|
"name": "#Rocketchat-Plugin",
|
|
"theme": "Plugin-Entwicklung",
|
|
"keywords_100": ["plugin"],
|
|
"keywords_context": ["adapter"],
|
|
"catch_all": False,
|
|
},
|
|
},
|
|
"excluded_rooms": [],
|
|
"fact_store_path": str(self.temp_path.parent / "docs"),
|
|
}
|
|
with open(self.temp_path, "w") as f:
|
|
json.dump(self.test_data, f)
|
|
|
|
def tearDown(self):
|
|
self.temp_dir.cleanup()
|
|
|
|
def test_a4_dm_strategie_topic_stay_in_room(self):
|
|
"""Strategie-Thema im DM (origin_room_id='') → stay_in_room."""
|
|
from plugin.archivar.router import BidirectionalRouter
|
|
from plugin.archivar.room_map import RoomMap
|
|
from plugin.archivar.archivar_state import ArchivarState
|
|
|
|
room_map = RoomMap._from_dict(self.test_data)
|
|
state = ArchivarState()
|
|
state._loaded = True
|
|
|
|
mock_classifier = MagicMock()
|
|
mock_classifier.is_initialized = True
|
|
mock_classifier.classify = AsyncMock(return_value=None)
|
|
|
|
router = BidirectionalRouter(room_map, mock_classifier, state)
|
|
|
|
# Strategie-Thema im DM
|
|
import asyncio
|
|
decision = asyncio.run(
|
|
router.route("Lass uns eine Absprache zur Strategie treffen", origin_room_id="dm_test_v8_2_strat", is_dm=True)
|
|
)
|
|
|
|
# Muss stay_in_room sein (Strategie-Thema im DM)
|
|
self.assertTrue(decision.stay_in_room,
|
|
"A4: Strategie-Thema im DM muss stay_in_room sein")
|
|
|
|
def test_a4_dm_non_strategie_allows_routing(self):
|
|
"""Nicht-Strategie-Thema im DM → Routing erlaubt."""
|
|
from plugin.archivar.router import BidirectionalRouter
|
|
from plugin.archivar.room_map import RoomMap
|
|
from plugin.archivar.archivar_state import ArchivarState
|
|
|
|
room_map = RoomMap._from_dict(self.test_data)
|
|
state = ArchivarState()
|
|
state._loaded = True
|
|
|
|
mock_classifier = MagicMock()
|
|
mock_classifier.is_initialized = True
|
|
mock_classifier.classify = AsyncMock(return_value=None)
|
|
mock_classifier._llm_raw = AsyncMock(return_value="plugin_room_id")
|
|
|
|
router = BidirectionalRouter(room_map, mock_classifier, state)
|
|
|
|
# Normales Thema im DM
|
|
import asyncio
|
|
decision = asyncio.run(
|
|
router.route("Bitte das Plugin dokumentieren", origin_room_id="dm_test_v8_2_nonstrat", is_dm=True)
|
|
)
|
|
|
|
# Sollte NICHT stay_in_room sein (kein Strategie-Thema)
|
|
self.assertFalse(decision.stay_in_room,
|
|
"A4: Nicht-Strategie-Thema im DM muss Routing erlauben")
|
|
|
|
def test_a4_router_strategie_detection(self):
|
|
"""Router._is_strategie_topic() erkennt Strategie-Themen im Text."""
|
|
from plugin.archivar.router import BidirectionalRouter
|
|
|
|
# Strategie-Themen
|
|
self.assertTrue(BidirectionalRouter._is_strategie_topic("Wir brauchen eine Absprache"))
|
|
self.assertTrue(BidirectionalRouter._is_strategie_topic("Planung für Q3"))
|
|
self.assertTrue(BidirectionalRouter._is_strategie_topic("Strategie-Meeting morgen"))
|
|
self.assertTrue(BidirectionalRouter._is_strategie_topic("Entscheidung zum Budget"))
|
|
|
|
# Keine Strategie-Themen
|
|
self.assertFalse(BidirectionalRouter._is_strategie_topic("Dokumentation aktualisieren"))
|
|
self.assertFalse(BidirectionalRouter._is_strategie_topic("Plugin kompilieren"))
|
|
self.assertFalse(BidirectionalRouter._is_strategie_topic("Hallo Welt"))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|