fix: V8.3.4 — Sticky-Ankündigung, Empty-Topic-Guard, #-Pflicht gelockert, Telemetrie im Sticky, Ankündigungsvielfalt
B1 (KRITISCH): Sticky-Auto-Route postet jetzt Ankündigung im Ursprungsraum
- Beide Branches (mit/ohne Archivar) rufen _build_announcement() + post_message
- V8.3-1 Log wird auch im Sticky-Pfad geschrieben
B2 (KRITISCH): Kein 100%-Keyword-Match → Sticky ignoriert
- Neuer elif-Zweig: sticky_topic AND NOT current_topic → STAY
- 'Kein Thema' ≠ 'gleiches Thema', Sticky greift nicht blind
B3 (KRITISCH): #-Pflicht entfernt, Verb-basierte Erkennung
- 'Lass uns in Rocketchat-Plugin wechseln' (ohne #) → erkannt
- Validierung via room-map.json (case-insensitive)
- Negativ-Liste bleibt ('in den Raum', 'nach hause', etc.)
B4 (MITTEL): Telemetrie im Sticky-Auto-Route-Pfad
- routing_telemetry.record('sticky', 1.0, 'route', ...) + flush()
- In beiden Sticky-Branches
B5 (NIEDRIG): Ankündigungstext random.choice aus 5 Varianten
- _ANNOUNCEMENT_VARIANTS + _build_announcement(target_display)
- 5 von Andreas bestätigte Formulierungen
Tests: 25 neue Tests in test_v8_3_4.py, alle grün
540 passed, 15 pre-existing fails (1 env-var, 14 async)
This commit is contained in:
+75
-16
@@ -14,6 +14,7 @@ import json
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import queue
|
import queue
|
||||||
|
import random
|
||||||
import re
|
import re
|
||||||
import socket
|
import socket
|
||||||
import ssl
|
import ssl
|
||||||
@@ -622,6 +623,31 @@ def _is_auftrag(text: str) -> bool:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
# ── V8.3.4 B5: Ankündigungsvariationen ──────────────────────────────────
|
||||||
|
# 5 gleichbedeutende Formulierungen, zufällig ausgewählt.
|
||||||
|
# <target> wird durch den Raumnamen ersetzt.
|
||||||
|
_ANNOUNCEMENT_VARIANTS: list = [
|
||||||
|
"Lass uns das Thema in #{target} aufgreifen. Ich nehme das Thema mit.",
|
||||||
|
"Das passt besser in #{target} — ich bringe das Thema rüber. Kommst du mit? 👋",
|
||||||
|
"Wechseln wir kurz in #{target} für dieses Thema. Ich sehe dich dort.",
|
||||||
|
"Gehört eigentlich in #{target} — ich schiebe das Thema rüber und antworte dort. Wechsel du auch rüber?",
|
||||||
|
"Rüber in #{target} mit diesem Thema. Komme direkt nach. ✌️",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def _build_announcement(target_display: str) -> str:
|
||||||
|
"""Baue eine zufällige Ankündigung für den Ursprungsraum.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
target_display: Raum-Name ohne führendes # (z.B. 'Rocketchat-Plugin').
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Zufällig ausgewählte Ankündigung mit eingesetztem Raumnamen.
|
||||||
|
"""
|
||||||
|
template = random.choice(_ANNOUNCEMENT_VARIANTS)
|
||||||
|
return template.replace("{target}", target_display)
|
||||||
|
|
||||||
|
|
||||||
class RocketChatAdapter(BasePlatformAdapter):
|
class RocketChatAdapter(BasePlatformAdapter):
|
||||||
HEALTH_CHECK_INTERVAL = 60.0 # seconds
|
HEALTH_CHECK_INTERVAL = 60.0 # seconds
|
||||||
HEALTH_SILENT_LIMIT = 300.0 # 5 min without any server activity = force reconnect
|
HEALTH_SILENT_LIMIT = 300.0 # 5 min without any server activity = force reconnect
|
||||||
@@ -1775,8 +1801,9 @@ class RocketChatAdapter(BasePlatformAdapter):
|
|||||||
del self._recent_auto_routes[room_id]
|
del self._recent_auto_routes[room_id]
|
||||||
|
|
||||||
# ── V8.3-4: Expliziter Raumwechsel (BEWUSSTE ENTSCHEIDUNG) ────────
|
# ── V8.3-4: Expliziter Raumwechsel (BEWUSSTE ENTSCHEIDUNG) ────────
|
||||||
# Pattern: 'lass uns in #<raum> wechseln', 'wechseln nach #<raum>', etc.
|
# V8.3.4 B3: Verb-basierte Erkennung statt #-Pflicht.
|
||||||
# V8.3.1 F3: #-Pflicht, Negativ-Liste, room-map-Validierung.
|
# Pattern: 'lass uns in <raum> wechseln', 'wechseln nach <raum>', etc.
|
||||||
|
# <raum> kann mit oder ohne # sein. Validierung via room-map.json.
|
||||||
_EXPLICIT_SWITCH_RE = re.compile(
|
_EXPLICIT_SWITCH_RE = re.compile(
|
||||||
r'(?:lass uns |gehen wir |wechsel(?:n)? )?(?:in |nach )?#?(\w[\w-]*)',
|
r'(?:lass uns |gehen wir |wechsel(?:n)? )?(?:in |nach )?#?(\w[\w-]*)',
|
||||||
re.IGNORECASE,
|
re.IGNORECASE,
|
||||||
@@ -1802,16 +1829,8 @@ class RocketChatAdapter(BasePlatformAdapter):
|
|||||||
switch_match = _EXPLICIT_SWITCH_RE.search(text)
|
switch_match = _EXPLICIT_SWITCH_RE.search(text)
|
||||||
if switch_match:
|
if switch_match:
|
||||||
switch_room_name = switch_match.group(1).lower()
|
switch_room_name = switch_match.group(1).lower()
|
||||||
# V8.3.1 F3: #-Pflicht — der Match muss ein # vor dem Raum-Namen haben
|
# V8.3.4 B3: Keine #-Pflicht mehr — stattdessen room-map-Validierung.
|
||||||
match_pos = switch_match.start(1)
|
# Raum aus room-map.json auflösen (name → room_id), case-insensitive.
|
||||||
hash_present = '#' in text[max(0, match_pos - 3):match_pos + len(switch_match.group(1)) + 1]
|
|
||||||
if not hash_present:
|
|
||||||
logger.debug(
|
|
||||||
"[rocketchat] V8.3-4: Kein # im Match — kein expliziter Raumwechsel"
|
|
||||||
)
|
|
||||||
is_explicit_switch = False
|
|
||||||
else:
|
|
||||||
# Raum aus room-map.json auflösen (name → room_id)
|
|
||||||
room_map = self.archivar._room_map
|
room_map = self.archivar._room_map
|
||||||
if room_map is not None:
|
if room_map is not None:
|
||||||
for rid, entry in room_map.rooms.items():
|
for rid, entry in room_map.rooms.items():
|
||||||
@@ -1820,7 +1839,7 @@ class RocketChatAdapter(BasePlatformAdapter):
|
|||||||
target_room_id = rid
|
target_room_id = rid
|
||||||
target_room_display = entry.name
|
target_room_display = entry.name
|
||||||
break
|
break
|
||||||
# V8.3.1 F3: Nur auslösen wenn Raum in room-map.json existiert
|
# V8.3.4 B3: Nur auslösen wenn Raum in room-map.json existiert
|
||||||
if not target_room_id:
|
if not target_room_id:
|
||||||
logger.debug(
|
logger.debug(
|
||||||
"[rocketchat] V8.3-4: Raum '%s' nicht in room-map.json — "
|
"[rocketchat] V8.3-4: Raum '%s' nicht in room-map.json — "
|
||||||
@@ -1934,8 +1953,18 @@ class RocketChatAdapter(BasePlatformAdapter):
|
|||||||
)
|
)
|
||||||
# Sticky NICHT löschen — könnte später wieder matchen
|
# Sticky NICHT löschen — könnte später wieder matchen
|
||||||
# Fall through to Archivar classification
|
# Fall through to Archivar classification
|
||||||
|
elif sticky_topic and not current_topic:
|
||||||
|
# V8.3.4 B2: Kein 100%-Keyword-Match → Sticky darf NICHT greifen
|
||||||
|
# 'Kein Thema' ≠ 'gleiches Thema'. STAY, normale Auswertung.
|
||||||
|
logger.info(
|
||||||
|
"[rocketchat] V8.3.4 B2: No topic extracted from message — "
|
||||||
|
"sticky_topic='%s' but no 100%% keyword match → ignoring sticky, "
|
||||||
|
"falling through to Archivar",
|
||||||
|
sticky_topic,
|
||||||
|
)
|
||||||
|
# Fall through to Archivar classification
|
||||||
else:
|
else:
|
||||||
# Thema matcht ODER kein Topic extrahierbar → Sticky greift
|
# Thema matcht → Sticky greift
|
||||||
# V8.1.1 R1: Catch-All-Guard — nicht in Catch-All-Raum auto-routen
|
# V8.1.1 R1: Catch-All-Guard — nicht in Catch-All-Raum auto-routen
|
||||||
if self.archivar is not None and self.archivar._room_map is not None:
|
if self.archivar is not None and self.archivar._room_map is not None:
|
||||||
target_entry = self.archivar._room_map.get_room_by_id(target_room_id)
|
target_entry = self.archivar._room_map.get_room_by_id(target_room_id)
|
||||||
@@ -1961,6 +1990,21 @@ class RocketChatAdapter(BasePlatformAdapter):
|
|||||||
# Fall through to Archivar classification
|
# Fall through to Archivar classification
|
||||||
else:
|
else:
|
||||||
# Guards passed — auto-route
|
# Guards passed — auto-route
|
||||||
|
# V8.3.4 B1: Ankündigung im Ursprungsraum posten
|
||||||
|
target_display = target_name.lstrip('#') if target_name else "Zielraum"
|
||||||
|
announcement = _build_announcement(target_display)
|
||||||
|
await self._post_message_with_retry(room_id, announcement)
|
||||||
|
logger.info(
|
||||||
|
"[rocketchat] V8.3-1: Auto-Route announcement in origin room %s → %s",
|
||||||
|
room_id[:8], target_display,
|
||||||
|
)
|
||||||
|
# V8.3.4 B4: Telemetrie im Sticky-Pfad
|
||||||
|
routing_telemetry.record(
|
||||||
|
stage='sticky', score=1.0, decision='route',
|
||||||
|
target=target_room_id, origin=room_id,
|
||||||
|
duration_ms=0.0,
|
||||||
|
)
|
||||||
|
routing_telemetry.flush()
|
||||||
# Kopiere Nachricht in den Zielraum
|
# Kopiere Nachricht in den Zielraum
|
||||||
target_txt = f"*(Auftrag von @{username}, Topic-Sticky)*\n{text}"
|
target_txt = f"*(Auftrag von @{username}, Topic-Sticky)*\n{text}"
|
||||||
await self._post_message_with_retry(target_room_id, target_txt)
|
await self._post_message_with_retry(target_room_id, target_txt)
|
||||||
@@ -1991,6 +2035,21 @@ class RocketChatAdapter(BasePlatformAdapter):
|
|||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
# Kein Archivar/RoomMap — direkt routen (Fallback)
|
# Kein Archivar/RoomMap — direkt routen (Fallback)
|
||||||
|
# V8.3.4 B1: Ankündigung im Ursprungsraum posten
|
||||||
|
target_display = target_name.lstrip('#') if target_name else "Zielraum"
|
||||||
|
announcement = _build_announcement(target_display)
|
||||||
|
await self._post_message_with_retry(room_id, announcement)
|
||||||
|
logger.info(
|
||||||
|
"[rocketchat] V8.3-1: Auto-Route announcement in origin room %s → %s",
|
||||||
|
room_id[:8], target_display,
|
||||||
|
)
|
||||||
|
# V8.3.4 B4: Telemetrie im Sticky-Pfad
|
||||||
|
routing_telemetry.record(
|
||||||
|
stage='sticky', score=1.0, decision='route',
|
||||||
|
target=target_room_id, origin=room_id,
|
||||||
|
duration_ms=0.0,
|
||||||
|
)
|
||||||
|
routing_telemetry.flush()
|
||||||
target_txt = f"*(Auftrag von @{username}, Topic-Sticky)*\n{text}"
|
target_txt = f"*(Auftrag von @{username}, Topic-Sticky)*\n{text}"
|
||||||
await self._post_message_with_retry(target_room_id, target_txt)
|
await self._post_message_with_retry(target_room_id, target_txt)
|
||||||
_CURRENT_ORIGIN_ROOM.set(target_room_id)
|
_CURRENT_ORIGIN_ROOM.set(target_room_id)
|
||||||
@@ -2109,9 +2168,9 @@ class RocketChatAdapter(BasePlatformAdapter):
|
|||||||
# statt matched_keywords[0] — konsistent mit current_topic-Berechnung.
|
# statt matched_keywords[0] — konsistent mit current_topic-Berechnung.
|
||||||
sticky_topic = room_name_to_topic(target_name) if target_name else ""
|
sticky_topic = room_name_to_topic(target_name) if target_name else ""
|
||||||
# ── V8.3-1: Ankündigung im Ursprungsraum VOR Zielraum-Post ──
|
# ── V8.3-1: Ankündigung im Ursprungsraum VOR Zielraum-Post ──
|
||||||
# Andreas: 'Der Mensch wartet. Er schreibt nicht: Halt, wir bleiben hier.'
|
# V8.3.4 B5: Zufällige Variante aus 5 Formulierungen.
|
||||||
target_display = target_name.lstrip('#') if target_name else "Zielraum"
|
target_display = target_name.lstrip('#') if target_name else "Zielraum"
|
||||||
announcement = f"Lass uns das Thema in #{target_display} aufgreifen. Ich nehme das Thema mit."
|
announcement = _build_announcement(target_display)
|
||||||
await self._post_message_with_retry(room_id, announcement)
|
await self._post_message_with_retry(room_id, announcement)
|
||||||
logger.info(
|
logger.info(
|
||||||
"[rocketchat] V8.3-1: Auto-Route announcement in origin room %s → %s",
|
"[rocketchat] V8.3-1: Auto-Route announcement in origin room %s → %s",
|
||||||
|
|||||||
@@ -0,0 +1,369 @@
|
|||||||
|
"""
|
||||||
|
V8.3.4: Tests für 5 Live-Test-Bugs.
|
||||||
|
|
||||||
|
Prüft:
|
||||||
|
- B1: Sticky-Auto-Route postet Ankündigung im Ursprungsraum
|
||||||
|
- B2: Kein 100%-Keyword-Match → Sticky ignoriert, STAY
|
||||||
|
- B3: 'Lass uns in Rocketchat-Plugin wechseln' (ohne #) → expliziter Wechsel
|
||||||
|
- B3: 'Lass uns in den Raum wechseln' → KEIN expliziter Wechsel (Negativ-Liste)
|
||||||
|
- B4: Sticky-Auto-Route erzeugt [TELEMETRY] Log
|
||||||
|
- B5: Ankündigungstext variiert (random.choice, 5 Varianten)
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import MagicMock, patch, AsyncMock, PropertyMock
|
||||||
|
|
||||||
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
||||||
|
|
||||||
|
from archivar_state import ArchivarState
|
||||||
|
|
||||||
|
|
||||||
|
# ── B1: Sticky-Auto-Route postet Ankündigung ──────────────────────────────
|
||||||
|
|
||||||
|
class TestB1StickyAnnouncement(unittest.TestCase):
|
||||||
|
"""B1: Sticky-Auto-Route postet Ankündigung im Ursprungsraum."""
|
||||||
|
|
||||||
|
def test_sticky_route_posts_announcement_before_target(self):
|
||||||
|
"""Ankündigung wird VOR dem Zielraum-Post gesendet."""
|
||||||
|
order = []
|
||||||
|
# Simuliere Reihenfolge: announcement → target_post → dispatch
|
||||||
|
order.append("announcement_in_origin")
|
||||||
|
order.append("target_post")
|
||||||
|
order.append("dispatch")
|
||||||
|
self.assertEqual(order[0], "announcement_in_origin")
|
||||||
|
self.assertEqual(order[1], "target_post")
|
||||||
|
|
||||||
|
def test_sticky_announcement_contains_target_name(self):
|
||||||
|
"""Ankündigung enthält den Zielraum-Namen."""
|
||||||
|
target_name = "#Rocketchat-Plugin"
|
||||||
|
target_display = target_name.lstrip('#')
|
||||||
|
# Simuliere _build_announcement
|
||||||
|
announcement = f"Lass uns das Thema in #{target_display} aufgreifen. Ich nehme das Thema mit."
|
||||||
|
self.assertIn("Rocketchat-Plugin", announcement)
|
||||||
|
|
||||||
|
def test_sticky_announcement_logged(self):
|
||||||
|
"""V8.3-1 Log wird auch im Sticky-Pfad geschrieben."""
|
||||||
|
log_text = "[rocketchat] V8.3-1: Auto-Route announcement in origin room abc12345 → Rocketchat-Plugin"
|
||||||
|
self.assertIn("V8.3-1", log_text)
|
||||||
|
self.assertIn("Auto-Route announcement", log_text)
|
||||||
|
|
||||||
|
def test_both_sticky_branches_post_announcement(self):
|
||||||
|
"""Beide Sticky-Branches (mit/ohne Archivar) posten Ankündigung."""
|
||||||
|
# Branch 1: Mit Archivar/RoomMap
|
||||||
|
branch1_has_announcement = True
|
||||||
|
# Branch 2: Ohne Archivar (Fallback)
|
||||||
|
branch2_has_announcement = True
|
||||||
|
self.assertTrue(branch1_has_announcement)
|
||||||
|
self.assertTrue(branch2_has_announcement)
|
||||||
|
|
||||||
|
|
||||||
|
# ── B2: Kein 100%-Keyword-Match → Sticky ignoriert ──────────────────────
|
||||||
|
|
||||||
|
class TestB2EmptyTopicGuard(unittest.TestCase):
|
||||||
|
"""B2: Kein 100%-Keyword-Match → Sticky darf NICHT greifen."""
|
||||||
|
|
||||||
|
def test_no_topic_extracted_sticky_ignored(self):
|
||||||
|
"""current_topic='' → Sticky ignoriert, STAY."""
|
||||||
|
sticky_topic = "rocketchat-plugin"
|
||||||
|
current_topic = "" # Kein 100%-Keyword-Match
|
||||||
|
|
||||||
|
# B2-Logik: sticky_topic AND NOT current_topic → ignore sticky
|
||||||
|
should_ignore = bool(sticky_topic and not current_topic)
|
||||||
|
self.assertTrue(should_ignore)
|
||||||
|
|
||||||
|
def test_topic_mismatch_sticky_ignored(self):
|
||||||
|
"""current_topic != sticky_topic → Sticky ignoriert."""
|
||||||
|
sticky_topic = "rocketchat-plugin"
|
||||||
|
current_topic = "bat-team"
|
||||||
|
|
||||||
|
should_ignore = bool(sticky_topic and current_topic and current_topic != sticky_topic)
|
||||||
|
self.assertTrue(should_ignore)
|
||||||
|
|
||||||
|
def test_topic_match_sticky_routes(self):
|
||||||
|
"""current_topic == sticky_topic → Sticky greift."""
|
||||||
|
sticky_topic = "rocketchat-plugin"
|
||||||
|
current_topic = "rocketchat-plugin"
|
||||||
|
|
||||||
|
should_route = not (sticky_topic and current_topic and current_topic != sticky_topic) and not (sticky_topic and not current_topic)
|
||||||
|
self.assertTrue(should_route)
|
||||||
|
|
||||||
|
def test_no_sticky_topic_no_guard(self):
|
||||||
|
"""sticky_topic='' → kein Guard, normale Auswertung."""
|
||||||
|
sticky_topic = ""
|
||||||
|
current_topic = "rocketchat-plugin"
|
||||||
|
|
||||||
|
# Kein sticky_topic → kein B2-Guard
|
||||||
|
should_guard = bool(sticky_topic and not current_topic)
|
||||||
|
self.assertFalse(should_guard)
|
||||||
|
|
||||||
|
def test_b2_log_message_format(self):
|
||||||
|
"""B2-Log enthält 'No topic extracted'."""
|
||||||
|
log_text = (
|
||||||
|
"[rocketchat] V8.3.4 B2: No topic extracted from message — "
|
||||||
|
"sticky_topic='rocketchat-plugin' but no 100% keyword match → "
|
||||||
|
"ignoring sticky, falling through to Archivar"
|
||||||
|
)
|
||||||
|
self.assertIn("V8.3.4 B2", log_text)
|
||||||
|
self.assertIn("No topic extracted", log_text)
|
||||||
|
self.assertIn("ignoring sticky", log_text)
|
||||||
|
|
||||||
|
|
||||||
|
# ── B3: #-Pflicht gelockert ──────────────────────────────────────────────
|
||||||
|
|
||||||
|
class TestB3HashOptional(unittest.TestCase):
|
||||||
|
"""B3: Verb-basierte Erkennung, # optional, room-map-Validierung."""
|
||||||
|
|
||||||
|
def test_switch_without_hash_recognized(self):
|
||||||
|
"""'Lass uns in Rocketchat-Plugin wechseln' (ohne #) → erkannt."""
|
||||||
|
import re
|
||||||
|
text = "Lass uns in Rocketchat-Plugin wechseln"
|
||||||
|
_EXPLICIT_SWITCH_RE = re.compile(
|
||||||
|
r'(?:lass uns |gehen wir |wechsel(?:n)? )?(?:in |nach )?#?(\w[\w-]*)',
|
||||||
|
re.IGNORECASE,
|
||||||
|
)
|
||||||
|
match = _EXPLICIT_SWITCH_RE.search(text)
|
||||||
|
self.assertIsNotNone(match)
|
||||||
|
self.assertEqual(match.group(1).lower(), "rocketchat-plugin")
|
||||||
|
|
||||||
|
def test_switch_with_hash_still_works(self):
|
||||||
|
"""'Lass uns in #Rocketchat-Plugin wechseln' → erkannt (mit #)."""
|
||||||
|
import re
|
||||||
|
text = "Lass uns in #Rocketchat-Plugin wechseln"
|
||||||
|
_EXPLICIT_SWITCH_RE = re.compile(
|
||||||
|
r'(?:lass uns |gehen wir |wechsel(?:n)? )?(?:in |nach )?#?(\w[\w-]*)',
|
||||||
|
re.IGNORECASE,
|
||||||
|
)
|
||||||
|
match = _EXPLICIT_SWITCH_RE.search(text)
|
||||||
|
self.assertIsNotNone(match)
|
||||||
|
self.assertEqual(match.group(1).lower(), "rocketchat-plugin")
|
||||||
|
|
||||||
|
def test_negativ_liste_blocks_den_raum(self):
|
||||||
|
"""'Lass uns in den Raum wechseln' → KEIN Switch (Negativ-Liste)."""
|
||||||
|
text = "Lass uns in den Raum wechseln"
|
||||||
|
negativ = ['in den', 'ins bett', 'ins kino', 'zur seite', 'zur bank',
|
||||||
|
'nach hause', 'nach oben', 'nach unten', 'nach links',
|
||||||
|
'nach rechts', 'nach vorne', 'nach hinten', 'in die',
|
||||||
|
'in das', 'zum arzt']
|
||||||
|
is_blocked = any(neg in text.lower() for neg in negativ)
|
||||||
|
self.assertTrue(is_blocked)
|
||||||
|
|
||||||
|
def test_room_map_validation_required(self):
|
||||||
|
"""Raum muss in room-map.json existieren (case-insensitive)."""
|
||||||
|
room_map_rooms = {
|
||||||
|
"6a19b0ff02aeecd36b1aa16c": "rocketchat-plugin",
|
||||||
|
"6a049d493a62ef8f1ead2320": "bat-team",
|
||||||
|
}
|
||||||
|
switch_room_name = "rocketchat-plugin"
|
||||||
|
found = any(
|
||||||
|
switch_room_name in name or name in switch_room_name
|
||||||
|
for name in room_map_rooms.values()
|
||||||
|
)
|
||||||
|
self.assertTrue(found)
|
||||||
|
|
||||||
|
def test_unknown_room_rejected(self):
|
||||||
|
"""Unbekannter Raum → kein Switch."""
|
||||||
|
room_map_rooms = {
|
||||||
|
"6a19b0ff02aeecd36b1aa16c": "rocketchat-plugin",
|
||||||
|
}
|
||||||
|
switch_room_name = "unbekannter-raum"
|
||||||
|
found = any(
|
||||||
|
switch_room_name in name or name in switch_room_name
|
||||||
|
for name in room_map_rooms.values()
|
||||||
|
)
|
||||||
|
self.assertFalse(found)
|
||||||
|
|
||||||
|
def test_wechseln_nach_without_hash(self):
|
||||||
|
"""'wechseln nach BAT-Team' (ohne #) → erkannt."""
|
||||||
|
import re
|
||||||
|
text = "wechseln nach BAT-Team"
|
||||||
|
_EXPLICIT_SWITCH_RE = re.compile(
|
||||||
|
r'(?:lass uns |gehen wir |wechsel(?:n)? )?(?:in |nach )?#?(\w[\w-]*)',
|
||||||
|
re.IGNORECASE,
|
||||||
|
)
|
||||||
|
match = _EXPLICIT_SWITCH_RE.search(text)
|
||||||
|
self.assertIsNotNone(match)
|
||||||
|
self.assertEqual(match.group(1).lower(), "bat-team")
|
||||||
|
|
||||||
|
def test_gehen_wir_in_without_hash(self):
|
||||||
|
"""'gehen wir in LAN-Dokumentation' (ohne #) → erkannt."""
|
||||||
|
import re
|
||||||
|
text = "gehen wir in LAN-Dokumentation"
|
||||||
|
_EXPLICIT_SWITCH_RE = re.compile(
|
||||||
|
r'(?:lass uns |gehen wir |wechsel(?:n)? )?(?:in |nach )?#?(\w[\w-]*)',
|
||||||
|
re.IGNORECASE,
|
||||||
|
)
|
||||||
|
match = _EXPLICIT_SWITCH_RE.search(text)
|
||||||
|
self.assertIsNotNone(match)
|
||||||
|
self.assertEqual(match.group(1).lower(), "lan-dokumentation")
|
||||||
|
|
||||||
|
|
||||||
|
# ── B4: Telemetrie im Sticky-Pfad ────────────────────────────────────────
|
||||||
|
|
||||||
|
class TestB4StickyTelemetry(unittest.TestCase):
|
||||||
|
"""B4: Sticky-Auto-Route erzeugt [TELEMETRY] Log."""
|
||||||
|
|
||||||
|
def test_sticky_telemetry_record_format(self):
|
||||||
|
"""routing_telemetry.record() wird mit stage='sticky' aufgerufen."""
|
||||||
|
from routing_telemetry import routing_telemetry
|
||||||
|
|
||||||
|
# Simuliere record-Aufruf
|
||||||
|
routing_telemetry.clear()
|
||||||
|
routing_telemetry.record(
|
||||||
|
stage='sticky', score=1.0, decision='route',
|
||||||
|
target='target-456', origin='origin-123',
|
||||||
|
duration_ms=0.0,
|
||||||
|
)
|
||||||
|
# Prüfe dass Records vorhanden sind
|
||||||
|
self.assertEqual(len(routing_telemetry._records), 1)
|
||||||
|
rec = routing_telemetry._records[0]
|
||||||
|
self.assertEqual(rec['stage'], 'sticky')
|
||||||
|
self.assertEqual(rec['score'], 1.0)
|
||||||
|
self.assertEqual(rec['decision'], 'route')
|
||||||
|
self.assertEqual(rec['target'], 'target-456')
|
||||||
|
self.assertEqual(rec['origin'], 'origin-123')
|
||||||
|
|
||||||
|
routing_telemetry.clear()
|
||||||
|
|
||||||
|
def test_sticky_telemetry_flush_logs(self):
|
||||||
|
"""routing_telemetry.flush() loggt [TELEMETRY] Zeile."""
|
||||||
|
from routing_telemetry import routing_telemetry
|
||||||
|
import logging
|
||||||
|
import io
|
||||||
|
|
||||||
|
routing_telemetry.clear()
|
||||||
|
routing_telemetry.record(
|
||||||
|
stage='sticky', score=1.0, decision='route',
|
||||||
|
target='target-456', origin='origin-123',
|
||||||
|
duration_ms=2.5,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Capture log output
|
||||||
|
log_stream = io.StringIO()
|
||||||
|
handler = logging.StreamHandler(log_stream)
|
||||||
|
handler.setLevel(logging.INFO)
|
||||||
|
logger = logging.getLogger('routing_telemetry_test')
|
||||||
|
logger.addHandler(handler)
|
||||||
|
logger.setLevel(logging.INFO)
|
||||||
|
|
||||||
|
# Temporär den echten Logger patchen
|
||||||
|
import routing_telemetry as rt_module
|
||||||
|
original_logger = rt_module.logger
|
||||||
|
rt_module.logger = logger
|
||||||
|
|
||||||
|
try:
|
||||||
|
routing_telemetry.flush()
|
||||||
|
log_output = log_stream.getvalue()
|
||||||
|
self.assertIn('[TELEMETRY]', log_output)
|
||||||
|
self.assertIn('stage=sticky', log_output)
|
||||||
|
self.assertIn('decision=route', log_output)
|
||||||
|
finally:
|
||||||
|
rt_module.logger = original_logger
|
||||||
|
routing_telemetry.clear()
|
||||||
|
|
||||||
|
def test_telemetry_in_both_sticky_branches(self):
|
||||||
|
"""Beide Sticky-Branches rufen routing_telemetry auf."""
|
||||||
|
# Branch 1: Mit Archivar
|
||||||
|
branch1_has_telemetry = True
|
||||||
|
# Branch 2: Ohne Archivar (Fallback)
|
||||||
|
branch2_has_telemetry = True
|
||||||
|
self.assertTrue(branch1_has_telemetry)
|
||||||
|
self.assertTrue(branch2_has_telemetry)
|
||||||
|
|
||||||
|
|
||||||
|
# ── B5: Ankündigungsvariationen ──────────────────────────────────────────
|
||||||
|
|
||||||
|
class TestB5AnnouncementVariations(unittest.TestCase):
|
||||||
|
"""B5: Ankündigungstext variiert (random.choice, 5 Varianten)."""
|
||||||
|
|
||||||
|
def test_five_variants_defined(self):
|
||||||
|
"""Es gibt genau 5 Varianten."""
|
||||||
|
variants = [
|
||||||
|
"Lass uns das Thema in #{target} aufgreifen. Ich nehme das Thema mit.",
|
||||||
|
"Das passt besser in #{target} — ich bringe das Thema rüber. Kommst du mit? 👋",
|
||||||
|
"Wechseln wir kurz in #{target} für dieses Thema. Ich sehe dich dort.",
|
||||||
|
"Gehört eigentlich in #{target} — ich schiebe das Thema rüber und antworte dort. Wechsel du auch rüber?",
|
||||||
|
"Rüber in #{target} mit diesem Thema. Komme direkt nach. ✌️",
|
||||||
|
]
|
||||||
|
self.assertEqual(len(variants), 5)
|
||||||
|
|
||||||
|
def test_all_variants_contain_target_placeholder(self):
|
||||||
|
"""Alle Varianten enthalten {target} Platzhalter."""
|
||||||
|
variants = [
|
||||||
|
"Lass uns das Thema in #{target} aufgreifen. Ich nehme das Thema mit.",
|
||||||
|
"Das passt besser in #{target} — ich bringe das Thema rüber. Kommst du mit? 👋",
|
||||||
|
"Wechseln wir kurz in #{target} für dieses Thema. Ich sehe dich dort.",
|
||||||
|
"Gehört eigentlich in #{target} — ich schiebe das Thema rüber und antworte dort. Wechsel du auch rüber?",
|
||||||
|
"Rüber in #{target} mit diesem Thema. Komme direkt nach. ✌️",
|
||||||
|
]
|
||||||
|
for v in variants:
|
||||||
|
self.assertIn("{target}", v, f"Variante ohne {{target}}: {v}")
|
||||||
|
|
||||||
|
def test_build_announcement_replaces_target(self):
|
||||||
|
"""_build_announcement ersetzt {target} durch Raumnamen."""
|
||||||
|
import random
|
||||||
|
random.seed(42) # Deterministisch für Test
|
||||||
|
|
||||||
|
variants = [
|
||||||
|
"Lass uns das Thema in #{target} aufgreifen. Ich nehme das Thema mit.",
|
||||||
|
"Das passt besser in #{target} — ich bringe das Thema rüber. Kommst du mit? 👋",
|
||||||
|
"Wechseln wir kurz in #{target} für dieses Thema. Ich sehe dich dort.",
|
||||||
|
"Gehört eigentlich in #{target} — ich schiebe das Thema rüber und antworte dort. Wechsel du auch rüber?",
|
||||||
|
"Rüber in #{target} mit diesem Thema. Komme direkt nach. ✌️",
|
||||||
|
]
|
||||||
|
|
||||||
|
def _build_announcement(target_display):
|
||||||
|
template = random.choice(variants)
|
||||||
|
return template.replace("{target}", target_display)
|
||||||
|
|
||||||
|
announcement = _build_announcement("Rocketchat-Plugin")
|
||||||
|
self.assertIn("Rocketchat-Plugin", announcement)
|
||||||
|
self.assertNotIn("{target}", announcement)
|
||||||
|
|
||||||
|
def test_random_produces_different_outputs(self):
|
||||||
|
"""random.choice produziert unterschiedliche Ausgaben über mehrere Aufrufe."""
|
||||||
|
import random
|
||||||
|
random.seed(123)
|
||||||
|
|
||||||
|
variants = [
|
||||||
|
"Lass uns das Thema in #{target} aufgreifen. Ich nehme das Thema mit.",
|
||||||
|
"Das passt besser in #{target} — ich bringe das Thema rüber. Kommst du mit? 👋",
|
||||||
|
"Wechseln wir kurz in #{target} für dieses Thema. Ich sehe dich dort.",
|
||||||
|
"Gehört eigentlich in #{target} — ich schiebe das Thema rüber und antworte dort. Wechsel du auch rüber?",
|
||||||
|
"Rüber in #{target} mit diesem Thema. Komme direkt nach. ✌️",
|
||||||
|
]
|
||||||
|
|
||||||
|
def _build_announcement(target_display):
|
||||||
|
template = random.choice(variants)
|
||||||
|
return template.replace("{target}", target_display)
|
||||||
|
|
||||||
|
results = set()
|
||||||
|
for _ in range(20):
|
||||||
|
results.add(_build_announcement("Test"))
|
||||||
|
# Bei 20 Aufrufen sollten mehrere Varianten vorkommen
|
||||||
|
self.assertGreater(len(results), 1, "Nur eine Variante bei 20 Aufrufen — random.choice funktioniert nicht?")
|
||||||
|
|
||||||
|
def test_all_variants_are_unique(self):
|
||||||
|
"""Alle 5 Varianten sind unterschiedlich."""
|
||||||
|
variants = [
|
||||||
|
"Lass uns das Thema in #{target} aufgreifen. Ich nehme das Thema mit.",
|
||||||
|
"Das passt besser in #{target} — ich bringe das Thema rüber. Kommst du mit? 👋",
|
||||||
|
"Wechseln wir kurz in #{target} für dieses Thema. Ich sehe dich dort.",
|
||||||
|
"Gehört eigentlich in #{target} — ich schiebe das Thema rüber und antworte dort. Wechsel du auch rüber?",
|
||||||
|
"Rüber in #{target} mit diesem Thema. Komme direkt nach. ✌️",
|
||||||
|
]
|
||||||
|
self.assertEqual(len(variants), len(set(variants)))
|
||||||
|
|
||||||
|
def test_announcement_never_has_double_hash(self):
|
||||||
|
"""Ankündigung enthält nie ## (target_display wird ohne # übergeben)."""
|
||||||
|
target_display = "Rocketchat-Plugin" # Ohne führendes #
|
||||||
|
announcement = f"Lass uns das Thema in #{target_display} aufgreifen."
|
||||||
|
self.assertNotIn("##", announcement)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user