""" V8.1.2 R1: Integrationstest — Session-Sticky + Catch-All-Guard + Archivar-Pfad. Prüft: - _session_room_preferences werden gelöscht wenn target_room catch_all ist - classify_and_route wird aufgerufen (Archivar-Pfad aktiviert) - KEIN dispatch in catchall (kein _dispatch_agent, kein _post_message_with_retry) """ from __future__ import annotations import asyncio import os import sys import time import unittest from unittest.mock import AsyncMock, MagicMock, patch # Add the parent directory to the path sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) from plugin.archivar.room_map import RoomMap, RoomEntry class TestR1SessionStickyCatchAllGuard(unittest.TestCase): """V8.1.2 R1: Session-Sticky + Catch-All-Guard Integrationstest.""" def setUp(self): """Erstelle gemockten Adapter mit catch_all room_map und session preferences.""" # ── RoomMap mit catch_all=True für den target_room ────────────── self.catch_all_room_id = "catch-all-123" self.origin_room_id = "origin-room-456" self.target_room_id = self.catch_all_room_id # target ist catch_all # Erstelle RoomMap mit catch_all room from pathlib import Path self.room_map = RoomMap( rooms={ self.catch_all_room_id: RoomEntry( room_id=self.catch_all_room_id, name="Catch-All", theme="Allgemein", keywords_100=["allgemein"], catch_all=True, ), self.origin_room_id: RoomEntry( room_id=self.origin_room_id, name="Projekt A", theme="Entwicklung", keywords_100=["dev", "code"], catch_all=False, ), }, excluded_room_ids=set(), fact_store_path=Path("/tmp/test_fact_store"), ) # ── Mock ArchivarMessageHandler ──────────────────────────────── self.mock_archivar = MagicMock() self.mock_archivar._room_map = self.room_map self.mock_archivar._state = MagicMock() self.mock_archivar._state.should_stay_in_room.return_value = None self.mock_archivar.classify_and_route = AsyncMock() self.mock_archivar.classify_and_route.return_value = MagicMock( category="direct_question", target_room="", confidence=0.0, is_catch_all=True, stay_in_room=True, ) # ── Session-Sticky Preferences ───────────────────────────────── self._session_room_preferences = { self.origin_room_id: { "target_room": self.target_room_id, "target_name": "Catch-All", "expires_at": time.time() + 1800, # noch gültig } } # ── Mock _dispatch_agent und _post_message_with_retry ────────── self.mock_dispatch = AsyncMock() self.mock_post_message = AsyncMock() # ── Kern-Logik: Catch-All-Guard (aus adapter.py) ─────────────────── async def _simulate_session_sticky_check( self, room_id: str ) -> dict: """Simuliert den Session-Sticky-Check aus adapter.py _process_incoming. Returns: dict mit keys: 'preferences_deleted', 'archivar_called', 'dispatched' """ result = { "preferences_deleted": False, "archivar_called": False, "dispatched": False, } if room_id in self._session_room_preferences: pref = self._session_room_preferences[room_id] if time.time() < pref["expires_at"]: target_room_id = pref["target_room"] target_name = pref.get("target_name", "Zielraum") # V8.1.1 R1: Catch-All-Guard if self.mock_archivar is not None and self.mock_archivar._room_map is not None: target_entry = self.mock_archivar._room_map.get_room_by_id(target_room_id) if target_entry and target_entry.catch_all: # Sticky aufräumen — nicht mehr gültig del self._session_room_preferences[room_id] result["preferences_deleted"] = True # Fall through to Archivar classification decision = await self.mock_archivar.classify_and_route( "test message", origin_room_id=room_id ) result["archivar_called"] = True return result else: # Guards passed — auto-route (dispatch) result["dispatched"] = True return result else: # Kein Archivar/RoomMap — direkt routen (Fallback) result["dispatched"] = True return result return result # ── Tests ───────────────────────────────────────────────────────── def test_catch_all_guard_deletes_preferences(self): """Catch-All-Guard: _session_room_preferences werden gelöscht.""" result = asyncio.run( self._simulate_session_sticky_check(self.origin_room_id) ) self.assertTrue(result["preferences_deleted"]) self.assertNotIn(self.origin_room_id, self._session_room_preferences) def test_catch_all_guard_activates_archivar_path(self): """Catch-All-Guard: classify_and_route wird aufgerufen (Archivar-Pfad).""" result = asyncio.run( self._simulate_session_sticky_check(self.origin_room_id) ) self.assertTrue(result["archivar_called"]) self.mock_archivar.classify_and_route.assert_called_once() def test_catch_all_guard_no_dispatch(self): """Catch-All-Guard: KEIN dispatch in catchall.""" result = asyncio.run( self._simulate_session_sticky_check(self.origin_room_id) ) self.assertFalse(result["dispatched"]) def test_non_catch_all_target_dispatches(self): """Nicht-Catch-All-Ziel: dispatch wird ausgelöst.""" # Ändere target auf nicht-catch-all self._session_room_preferences[self.origin_room_id] = { "target_room": self.origin_room_id, # origin ist nicht catch_all "target_name": "Projekt A", "expires_at": time.time() + 1800, } result = asyncio.run( self._simulate_session_sticky_check(self.origin_room_id) ) self.assertTrue(result["dispatched"]) self.assertFalse(result["preferences_deleted"]) def test_expired_sticky_not_checked(self): """Abgelaufener Sticky wird nicht geprüft (kein Guard, kein Dispatch).""" self._session_room_preferences[self.origin_room_id] = { "target_room": self.target_room_id, "target_name": "Catch-All", "expires_at": time.time() - 10, # bereits abgelaufen } result = asyncio.run( self._simulate_session_sticky_check(self.origin_room_id) ) # Abgelaufen → kein Guard, kein Dispatch, kein Archivar self.assertFalse(result["preferences_deleted"]) self.assertFalse(result["archivar_called"]) self.assertFalse(result["dispatched"]) def test_no_preferences_no_action(self): """Kein Sticky-Eintrag → keine Aktion.""" self._session_room_preferences.clear() result = asyncio.run( self._simulate_session_sticky_check(self.origin_room_id) ) self.assertFalse(result["preferences_deleted"]) self.assertFalse(result["archivar_called"]) self.assertFalse(result["dispatched"]) def test_archivar_none_fallback_dispatches(self): """Ohne Archivar/RoomMap → direkt dispatch (Fallback).""" self.mock_archivar = None self._session_room_preferences[self.origin_room_id] = { "target_room": self.target_room_id, "target_name": "Catch-All", "expires_at": time.time() + 1800, } result = asyncio.run( self._simulate_session_sticky_check(self.origin_room_id) ) # Ohne Archivar → kein Guard, direkt dispatch self.assertTrue(result["dispatched"]) if __name__ == "__main__": unittest.main()