139 lines
5.1 KiB
Python
139 lines
5.1 KiB
Python
"""
|
|
Tests für das gewichtete Scoring im RoomMap-Modul.
|
|
"""
|
|
|
|
import unittest
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
import sys
|
|
import os
|
|
|
|
# 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 TestRoomMapWeightedScoring(unittest.TestCase):
|
|
"""Tests für das gewichtete Scoring im RoomMap-Modul."""
|
|
|
|
def setUp(self):
|
|
"""Set up test fixtures."""
|
|
# Create test rooms
|
|
self.rooms = {
|
|
"room1": RoomEntry(
|
|
room_id="room1",
|
|
name="#Plugin-Development",
|
|
theme="Plugin development and bug fixes",
|
|
keywords_100=["bug", "fix", "issue"],
|
|
keywords_context=["plugin", "feature", "development"]
|
|
),
|
|
"room2": RoomEntry(
|
|
room_id="room2",
|
|
name="#Documentation",
|
|
theme="Documentation and user guides",
|
|
keywords_100=["doc", "document", "guide"],
|
|
keywords_context=["documentation", "manual", "tutorial"]
|
|
),
|
|
"room3": RoomEntry(
|
|
room_id="room3",
|
|
name="#General",
|
|
theme="General discussion",
|
|
keywords_100=[],
|
|
keywords_context=["general", "discussion", "chat"],
|
|
catch_all=True
|
|
)
|
|
}
|
|
|
|
# Create a RoomMap instance
|
|
self.room_map = RoomMap(
|
|
rooms=self.rooms,
|
|
excluded_room_ids=set(),
|
|
fact_store_path=Path("/tmp/test_docs"),
|
|
version=2
|
|
)
|
|
|
|
def test_calculate_weighted_score(self):
|
|
"""Test calculating weighted scores."""
|
|
# Get a room entry
|
|
room = self.rooms["room1"]
|
|
|
|
# Test with text that matches some keywords
|
|
text = "I need help with plugin development"
|
|
score = self.room_map.calculate_weighted_score(text, room)
|
|
|
|
# Should match "plugin" and "development" -> 2 out of 3 keywords = 0.6667
|
|
self.assertAlmostEqual(score, 2/3, places=2)
|
|
|
|
def test_find_room_by_keywords_with_100_percent_match(self):
|
|
"""Test finding room with 100% keyword match."""
|
|
# Text with a 100% match keyword
|
|
text = "There is a bug in the plugin"
|
|
room: Optional[RoomEntry] = self.room_map.find_room_by_keywords(text)
|
|
|
|
# Should find room1 because it has "bug" in keywords_100
|
|
self.assertIsNotNone(room)
|
|
if room is not None:
|
|
self.assertEqual(room.room_id, "room1")
|
|
|
|
def test_find_room_by_keywords_with_context_match(self):
|
|
"""Test finding room with context keyword match."""
|
|
# Text with context keyword match
|
|
text = "I'm working on plugin development"
|
|
room: Optional[RoomEntry] = self.room_map.find_room_by_keywords(text)
|
|
|
|
# Should find room1 because it has "plugin" and "development" in keywords_context
|
|
self.assertIsNotNone(room)
|
|
if room is not None:
|
|
self.assertEqual(room.room_id, "room1")
|
|
|
|
def test_find_room_by_keywords_with_origin_priority(self):
|
|
"""Test finding room with origin room priority."""
|
|
# Text with context keyword match
|
|
text = "I'm working on plugin development"
|
|
|
|
# When origin_room_id is set to room2, it should still find room1
|
|
# because 100% keywords take precedence
|
|
room: Optional[RoomEntry] = self.room_map.find_room_by_keywords(text, origin_room_id="room2")
|
|
self.assertIsNotNone(room)
|
|
if room is not None:
|
|
self.assertEqual(room.room_id, "room1")
|
|
|
|
# Text without 100% match but with context match
|
|
text2 = "plugin development discussion"
|
|
room = self.room_map.find_room_by_keywords(text2, origin_room_id="room2")
|
|
|
|
# Should find room1 because it has better context match
|
|
self.assertIsNotNone(room)
|
|
if room is not None:
|
|
self.assertEqual(room.room_id, "room1")
|
|
|
|
def test_find_room_by_keywords_no_match(self):
|
|
"""Test finding room with no keyword matches."""
|
|
# Text with no matching keywords
|
|
text = "completely unrelated topic"
|
|
room: Optional[RoomEntry] = self.room_map.find_room_by_keywords(text)
|
|
|
|
# Should not find any room
|
|
self.assertIsNone(room)
|
|
|
|
def test_find_room_by_keywords_min_score(self):
|
|
"""Test finding room with minimum score requirement."""
|
|
# Text with only one context keyword match
|
|
text = "plugin unrelated stuff"
|
|
room: Optional[RoomEntry] = self.room_map.find_room_by_keywords(text, min_score=2)
|
|
|
|
# Should not find any room because score is only 1 (below min_score of 2)
|
|
self.assertIsNone(room)
|
|
|
|
# Same text with lower min_score
|
|
room = self.room_map.find_room_by_keywords(text, min_score=1)
|
|
|
|
# Should find room1 because score is 1 (meets min_score of 1)
|
|
self.assertIsNotNone(room)
|
|
if room is not None:
|
|
self.assertEqual(room.room_id, "room1")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main() |