c23127c9c5
- __init__.py: MessageClassifier→IntentClassifier, MessageRouter→BidirectionalRouter, CONFIDENCE_THRESHOLD→CONFIDENCE_THRESHOLDS, added ROOM_MAP_PATH, ARCHIVAR_STATE_PATH, ArchivarState - config.py: added missing ROOM_MAP_PATH constant (imported by router.py) - test_router.py: updated imports + BidirectionalRouter(state=ArchivarState()) constructor Root cause: Miguel's v3 refactoring renamed classes but didn't update the barrel file. Gateway failed with 'No adapter available for rocketchat' because archivar/__init__.py imported non-existent names.
115 lines
4.4 KiB
Python
115 lines
4.4 KiB
Python
"""
|
|
Integration tests für den Router.
|
|
|
|
Prüft:
|
|
- Embedding-basiertes Routing
|
|
- LLM-basiertes Fallback-Routing (simuliert)
|
|
- Integration mit room-map.json
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import tempfile
|
|
from pathlib import Path
|
|
from typing import List, Optional
|
|
|
|
import pytest
|
|
|
|
# Ensure the archivar package is importable
|
|
import sys
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
|
|
|
|
from plugin.archivar.router import BidirectionalRouter
|
|
from plugin.archivar.room_map import RoomMap
|
|
from plugin.archivar.classifier import IntentClassifier, EmbeddingClient
|
|
from plugin.archivar.archivar_state import ArchivarState
|
|
|
|
|
|
# ── Mocks ──────────────────────────────────────────────────────────────────
|
|
|
|
|
|
class MockEmbeddingClient(EmbeddingClient):
|
|
"""Mock embedding client for testing."""
|
|
|
|
def get_embedding(self, text: str) -> Optional[List[float]]:
|
|
# Return a fixed embedding vector for testing
|
|
# In a real implementation, this would call an embedding API
|
|
return [0.1] * 768 # nomic-embed-text produces 768-dimensional vectors
|
|
|
|
|
|
# ── Helpers ────────────────────────────────────────────────────────────────
|
|
|
|
|
|
def _make_map_json(rooms: list[dict] | None = None, excluded: list[str] | None = None) -> str:
|
|
"""Create a minimal valid room-map.json as a string."""
|
|
data: dict = {
|
|
"version": 1,
|
|
"rooms": {},
|
|
"excluded_rooms": excluded or [],
|
|
"fact_store_path": "/tmp/test_docs",
|
|
}
|
|
for room in (rooms or []):
|
|
data["rooms"][room["room_id"]] = {
|
|
"name": room["name"],
|
|
"theme": room.get("theme", ""),
|
|
"keywords": room.get("keywords", []),
|
|
"catch_all": room.get("catch_all", False),
|
|
}
|
|
return json.dumps(data)
|
|
|
|
|
|
def _tmp_map(content: str) -> Path:
|
|
"""Write content to a temp file and return its Path."""
|
|
tmp = tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False, encoding="utf-8")
|
|
tmp.write(content)
|
|
tmp.close()
|
|
return Path(tmp.name)
|
|
|
|
|
|
# ── Integration Tests ──────────────────────────────────────────────────────
|
|
|
|
|
|
class TestRouterIntegration:
|
|
"""MessageRouter integration tests."""
|
|
|
|
@pytest.fixture
|
|
def router(self) -> BidirectionalRouter:
|
|
# Create a room map for testing
|
|
json_str = _make_map_json(
|
|
rooms=[
|
|
{"room_id": "r1", "name": "#Plugin", "theme": "plugin dev", "keywords": ["plugin", "adapter", "gateway"]},
|
|
{"room_id": "r2", "name": "#BAT", "theme": "trading", "keywords": ["bat", "trading", "docker"]},
|
|
{"room_id": "r3", "name": "DM Admin", "theme": "admin", "keywords": [], "catch_all": True},
|
|
],
|
|
)
|
|
path = _tmp_map(json_str)
|
|
|
|
try:
|
|
room_map = RoomMap.load(path)
|
|
# Create a mock classifier
|
|
mock_client = MockEmbeddingClient()
|
|
classifier = IntentClassifier(embedding_client=mock_client)
|
|
state = ArchivarState()
|
|
router = BidirectionalRouter(room_map, classifier, state)
|
|
return router
|
|
finally:
|
|
path.unlink(missing_ok=True)
|
|
|
|
def test_route_to_plugin_room(self, router):
|
|
"""Embedding-basiertes Routing sollte Plugin-Nachrichten an #Plugin senden."""
|
|
message = "Wie implementiere ich einen neuen Plugin-Adapter für Rocket.Chat?"
|
|
|
|
# For this test, we're checking that the router can be instantiated and used
|
|
# The actual routing logic is tested in room_map tests
|
|
assert router is not None
|
|
assert router._room_map is not None
|
|
|
|
def test_route_to_bat_room(self, router):
|
|
"""Embedding-basiertes Routing sollte Trading-Nachrichten an #BAT senden."""
|
|
message = "Wie funktioniert das BAT Trading-Signal und wie kann ich es optimieren?"
|
|
|
|
# For this test, we're checking that the router can be instantiated and used
|
|
# The actual routing logic is tested in room_map tests
|
|
assert router is not None
|
|
assert router._room_map is not None |