""" V8.1.1 R3: Tests für Auto-Route — 100%-Match → Zielraum, self-match → Stay, Catch-All → kein Route. Prüft: - 100%-Keyword-Match routet in Zielraum - Self-Match (Zielraum = Ursprungsraum) → stay_in_room - Catch-All-Raum als Ziel → kein Auto-Route - Auto-Route setzt _CURRENT_ORIGIN_ROOM auf Zielraum - Auto-Route setzt Session-Sticky """ from __future__ import annotations import os import sys import tempfile import unittest from unittest.mock import MagicMock, patch # Add the parent directory to the path sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) from archivar_state import ArchivarState class TestAutoRoute(unittest.TestCase): """Tests für die Auto-Route-Logik (V8.1 P1).""" def setUp(self): self.temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.json') self.temp_file.close() self.state = ArchivarState(self.temp_file.name) def tearDown(self): if os.path.exists(self.temp_file.name): os.unlink(self.temp_file.name) # ── 100%-Match Tests ──────────────────────────────────────────────── def test_100_percent_match_routes_to_target(self): """100%-Keyword-Match routet in den Zielraum.""" target_room = "target-456" origin_room = "origin-123" # 100%-Match: target_room != origin_room, kein move_request_sent should_route = ( target_room and target_room != origin_room ) self.assertTrue(should_route) def test_100_percent_match_sets_target_room(self): """100%-Match setzt _CURRENT_ORIGIN_ROOM auf Zielraum.""" target_room = "target-456" origin_room = "origin-123" # Bei Auto-Route wird _CURRENT_ORIGIN_ROOM auf target_room gesetzt current_origin = target_room # Simuliere .set() self.assertEqual(current_origin, target_room) self.assertNotEqual(current_origin, origin_room) # ── Self-Match Tests ──────────────────────────────────────────────── def test_self_match_stays_in_room(self): """Self-Match (Zielraum = Ursprungsraum) → stay_in_room.""" target_room = "room-123" origin_room = "room-123" is_self_match = target_room == origin_room self.assertTrue(is_self_match) # Bei self-match: stay_in_room=True, move_request_sent=False stay_in_room = is_self_match self.assertTrue(stay_in_room) def test_self_match_no_auto_route(self): """Self-Match löst KEIN Auto-Route aus.""" target_room = "room-123" origin_room = "room-123" should_auto_route = ( target_room and target_room != origin_room ) self.assertFalse(should_auto_route) # ── Catch-All Tests ───────────────────────────────────────────────── def test_catch_all_target_no_auto_route(self): """Catch-All-Raum als Ziel → kein Auto-Route.""" target_is_catch_all = True should_auto_route = not target_is_catch_all self.assertFalse(should_auto_route) def test_non_catch_all_target_auto_routes(self): """Nicht-Catch-All-Raum als Ziel → Auto-Route.""" target_is_catch_all = False should_auto_route = not target_is_catch_all self.assertTrue(should_auto_route) # ── Sticky-Setzung Tests ──────────────────────────────────────────── def test_auto_route_sets_session_sticky(self): """Auto-Route setzt Session-Sticky mit 30 Min TTL.""" import time target_room = "target-456" target_name = "#DevOps" sticky = { "target_room": target_room, "target_name": target_name, "expires_at": time.time() + 1800, } self.assertEqual(sticky["target_room"], target_room) self.assertGreater(sticky["expires_at"], time.time()) self.assertLess(sticky["expires_at"], time.time() + 1801) # ── Forwarded Flag Tests ──────────────────────────────────────────── def test_auto_route_sets_forwarded_true(self): """Auto-Route setzt forwarded=True.""" target_room = "target-456" origin_room = "origin-123" forwarded = ( target_room and target_room != origin_room ) self.assertTrue(forwarded) def test_no_auto_route_forwarded_false(self): """Kein Auto-Route → forwarded=False.""" target_room = "" origin_room = "origin-123" forwarded = ( target_room and target_room != origin_room ) self.assertFalse(forwarded) # ── Routing Decision Tests ────────────────────────────────────────── def test_routing_decision_has_target_room(self): """RoutingDecision hat target_room gesetzt.""" decision = { "target_room": "target-456", "category": "work_order", "move_request_sent": False, "stay_in_room": False, } self.assertTrue(decision["target_room"]) self.assertFalse(decision["move_request_sent"]) self.assertFalse(decision["stay_in_room"]) def test_routing_decision_move_request_blocks_auto_route(self): """move_request_sent=True blockiert Auto-Route (Butler-Prinzip).""" decision = { "target_room": "target-456", "move_request_sent": True, } should_auto_route = ( decision["target_room"] and not decision["move_request_sent"] ) self.assertFalse(should_auto_route) if __name__ == "__main__": unittest.main()