8591933d8d
Implements the RC-Skill's promised room-map completeness check after Adapter.connect(). Previously the adapter subscribed to rooms but never verified they were all present in room-map.json. New rooms were subscribed but invisible to the Archivar router. Scope (~450 lines, 4 files): - adapter_integration.py: check_room_map_completeness() compares subscribed rooms (filtered to c/p types) against RoomMap. Single-Room-Regel: 1 relevant room → auto catch_all, no interview. onboarding_active flag in _stats prevents re-triggering. - room_map.py: add_or_update_room() for safe in-memory mutations, reload() for disk-to-memory sync without gateway restart, static save() for atomic file writes. - adapter.py: connect() calls check after Archivar init, triggers _start_onboarding() if rooms missing. _process_incoming() intercepts 'Raum: #Name = Thema' responses during active onboarding. - test_auto_onboarding.py: 4 unit tests covering single-room auto-assign, multi-room detection, DM filtering, and already- complete map (no action). Deployed to production: adapter.py + archivar/*.py copied to Auslieferungspfad. Gateway restarted — no onboarding triggered (map already complete).
175 lines
7.1 KiB
Python
175 lines
7.1 KiB
Python
"""Tests für die Auto-Onboarding-Funktion des Archivar-Moduls."""
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
# Füge das plugin-Verzeichnis zum Python-Pfad hinzu
|
|
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
|
|
|
|
from archivar.adapter_integration import ArchivarMessageHandler
|
|
from archivar.room_map import RoomMap, RoomEntry
|
|
|
|
|
|
class TestAutoOnboarding(unittest.TestCase):
|
|
"""Testfälle für die Auto-Onboarding-Funktion."""
|
|
|
|
def setUp(self):
|
|
"""Set up test fixtures before each test method."""
|
|
# Erstelle eine temporäre room-map.json für Tests
|
|
self.temp_dir = tempfile.TemporaryDirectory()
|
|
self.temp_path = Path(self.temp_dir.name) / "room-map.json"
|
|
|
|
# Mock-Daten für die RoomMap
|
|
self.test_data = {
|
|
"version": 1,
|
|
"rooms": {
|
|
"catchall_room_id": {
|
|
"name": "#CatchAll",
|
|
"theme": "Catch-all room for everything else",
|
|
"keywords": [],
|
|
"catch_all": True
|
|
}
|
|
},
|
|
"excluded_rooms": [],
|
|
"fact_store_path": str(self.temp_path.parent / "docs")
|
|
}
|
|
|
|
# Schreibe die Testdaten in die temporäre Datei
|
|
with open(self.temp_path, 'w') as f:
|
|
json.dump(self.test_data, f)
|
|
|
|
def tearDown(self):
|
|
"""Tear down test fixtures after each test method."""
|
|
self.temp_dir.cleanup()
|
|
|
|
@patch('archivar.adapter_integration.ROOM_MAP_PATH')
|
|
def test_single_room_auto_catch_all(self, mock_room_map_path):
|
|
"""Testet die Single-Room-Regel: 1 relevanter Raum → automatisch catch_all."""
|
|
mock_room_map_path.return_value = str(self.temp_path)
|
|
|
|
# Erstelle einen ArchivarMessageHandler
|
|
handler = ArchivarMessageHandler()
|
|
|
|
# Mock die _load_room_map Methode, um unsere Testdaten zu verwenden
|
|
with patch.object(handler, '_load_room_map', return_value=RoomMap._from_dict(self.test_data)):
|
|
handler._room_map = handler._load_room_map()
|
|
|
|
# Simuliere subscribed_rooms mit nur einem relevanten Raum
|
|
subscribed_rooms = {
|
|
"single_room_id": {"name": "#SingleRoom", "type": "c"}
|
|
}
|
|
|
|
# Führe die Prüfung aus
|
|
missing = handler.check_room_map_completeness(subscribed_rooms)
|
|
|
|
# Es sollten keine fehlenden Räume gemeldet werden (Single-Room-Regel)
|
|
self.assertEqual(len(missing), 0)
|
|
|
|
# Der Raum sollte automatisch als catch_all hinzugefügt worden sein
|
|
self.assertIn("single_room_id", handler._room_map.rooms)
|
|
self.assertTrue(handler._room_map.rooms["single_room_id"].catch_all)
|
|
|
|
@patch('archivar.adapter_integration.ROOM_MAP_PATH')
|
|
def test_multi_room_onboarding_triggered(self, mock_room_map_path):
|
|
"""Testet dass Onboarding bei mehreren fehlenden Räumen ausgelöst wird."""
|
|
mock_room_map_path.return_value = str(self.temp_path)
|
|
|
|
# Erstelle einen ArchivarMessageHandler
|
|
handler = ArchivarMessageHandler()
|
|
|
|
# Mock die _load_room_map Methode, um unsere Testdaten zu verwenden
|
|
with patch.object(handler, '_load_room_map', return_value=RoomMap._from_dict(self.test_data)):
|
|
handler._room_map = handler._load_room_map()
|
|
|
|
# Simuliere subscribed_rooms mit mehreren relevanten Räumen
|
|
subscribed_rooms = {
|
|
"room1_id": {"name": "#Room1", "type": "c"},
|
|
"room2_id": {"name": "#Room2", "type": "p"}
|
|
}
|
|
|
|
# Führe die Prüfung aus
|
|
missing = handler.check_room_map_completeness(subscribed_rooms)
|
|
|
|
# Es sollten 2 fehlende Räume gemeldet werden
|
|
self.assertEqual(len(missing), 2)
|
|
self.assertEqual(missing[0]["room_id"], "room1_id")
|
|
self.assertEqual(missing[1]["room_id"], "room2_id")
|
|
|
|
@patch('archivar.adapter_integration.ROOM_MAP_PATH')
|
|
def test_dm_rooms_ignored(self, mock_room_map_path):
|
|
"""Testet dass DM-Räume ignoriert werden (außer Operator-DM)."""
|
|
mock_room_map_path.return_value = str(self.temp_path)
|
|
|
|
# Erstelle einen ArchivarMessageHandler
|
|
handler = ArchivarMessageHandler()
|
|
|
|
# Mock die _load_room_map Methode, um unsere Testdaten zu verwenden
|
|
with patch.object(handler, '_load_room_map', return_value=RoomMap._from_dict(self.test_data)):
|
|
handler._room_map = handler._load_room_map()
|
|
|
|
# Simuliere subscribed_rooms mit DM-Räumen
|
|
subscribed_rooms = {
|
|
"dm1_id": {"name": "DM mit Bernd", "type": "d"},
|
|
"dm2_id": {"name": "DM mit Bernd, Thomas", "type": "d"},
|
|
"channel_id": {"name": "#Channel", "type": "c"}
|
|
}
|
|
|
|
# Führe die Prüfung aus
|
|
missing = handler.check_room_map_completeness(subscribed_rooms)
|
|
|
|
# Nur der Channel sollte als fehlend gemeldet werden, nicht die DMs
|
|
self.assertEqual(len(missing), 1)
|
|
self.assertEqual(missing[0]["room_id"], "channel_id")
|
|
|
|
@patch('archivar.adapter_integration.ROOM_MAP_PATH')
|
|
def test_map_already_complete_no_action(self, mock_room_map_path):
|
|
"""Testet dass nichts passiert wenn die Map bereits vollständig ist."""
|
|
mock_room_map_path.return_value = str(self.temp_path)
|
|
|
|
# Erstelle Testdaten mit bereits vorhandenen Räumen
|
|
test_data_with_rooms = {
|
|
"version": 1,
|
|
"rooms": {
|
|
"catchall_room_id": {
|
|
"name": "#CatchAll",
|
|
"theme": "Catch-all room for everything else",
|
|
"keywords": [],
|
|
"catch_all": True
|
|
},
|
|
"room1_id": {
|
|
"name": "#Room1",
|
|
"theme": "First test room",
|
|
"keywords": ["test"],
|
|
"catch_all": False
|
|
}
|
|
},
|
|
"excluded_rooms": [],
|
|
"fact_store_path": str(self.temp_path.parent / "docs")
|
|
}
|
|
|
|
# Erstelle einen ArchivarMessageHandler
|
|
handler = ArchivarMessageHandler()
|
|
|
|
# Mock die _load_room_map Methode, um unsere Testdaten zu verwenden
|
|
with patch.object(handler, '_load_room_map', return_value=RoomMap._from_dict(test_data_with_rooms)):
|
|
handler._room_map = handler._load_room_map()
|
|
|
|
# Simuliere subscribed_rooms mit Räumen, die bereits in der Map sind
|
|
subscribed_rooms = {
|
|
"room1_id": {"name": "#Room1", "type": "c"}
|
|
}
|
|
|
|
# Führe die Prüfung aus
|
|
missing = handler.check_room_map_completeness(subscribed_rooms)
|
|
|
|
# Es sollten keine fehlenden Räume gemeldet werden
|
|
self.assertEqual(len(missing), 0)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main() |