""" Tests for intent router. """ import uuid import pytest from app.services.intent.router import IntentRouter, RuleMatcher from app.services.intent.models import ( FusionConfig, RuleMatchResult, SemanticMatchResult, LlmJudgeResult, FusionResult, ) from app.models.entities import IntentRule class TestRuleMatcher: """Test RuleMatcher basic functionality.""" def test_match_empty_message(self): matcher = RuleMatcher() result = matcher.match("", []) assert result.score == 0.0 assert result.rule is None assert result.duration_ms >= 0 def test_match_empty_rules(self): matcher = RuleMatcher() rule = IntentRule( id=uuid.uuid4(), tenant_id="test_tenant", name="Test Rule", keywords=["test", "demo"], is_enabled=True, ) result = matcher.match("test message", [rule]) assert result.score == 1.0 assert result.rule == rule assert result.match_type == "keyword" assert result.matched_text == "test" def test_match_regex(self): matcher = RuleMatcher() rule = IntentRule( id=uuid.uuid4(), tenant_id="test_tenant", name="Test Regex Rule", patterns=[r"test.*pattern"], is_enabled=True, ) result = matcher.match("this is a test regex pattern", [rule]) assert result.score == 1.0 assert result.rule == rule assert result.match_type == "regex" assert "pattern" in result.matched_text def test_no_match(self): matcher = RuleMatcher() rule = IntentRule( id=uuid.uuid4(), tenant_id="test_tenant", name="Test Rule", keywords=["specific", "keyword"], is_enabled=True, ) result = matcher.match("no match here", [rule]) assert result.score == 0.0 assert result.rule is None def test_priority_order(self): matcher = RuleMatcher() rule1 = IntentRule( id=uuid.uuid4(), tenant_id="test_tenant", name="High Priority", keywords=["high"], priority=10, is_enabled=True, ) rule2 = IntentRule( id=uuid.uuid4(), tenant_id="test_tenant", name="Low Priority", keywords=["low"], priority=1, is_enabled=True, ) result = matcher.match("high priority message", [rule1, rule2]) assert result.rule == rule1 assert result.rule.name == "High Priority" def test_disabled_rule(self): matcher = RuleMatcher() rule = IntentRule( id=uuid.uuid4(), tenant_id="test_tenant", name="Disabled Rule", keywords=["disabled"], is_enabled=False, ) result = matcher.match("disabled message", [rule]) assert result.score == 0.0 assert result.rule is None class TestIntentRouterBackwardCompatibility: """Test IntentRouter backward compatibility.""" def test_match_backward_compatible(self): router = IntentRouter() rule = IntentRule( id=uuid.uuid4(), tenant_id="test_tenant", name="Test Rule", keywords=["hello", "hi"], is_enabled=True, ) result = router.match("hello world", [rule]) assert result is not None assert result.rule.name == "Test Rule" assert result.match_type == "keyword" assert result.matched == "hello" def test_match_with_stats(self): router = IntentRouter() rule = IntentRule( id=uuid.uuid4(), tenant_id="test_tenant", name="Test Rule", keywords=["test"], is_enabled=True, ) result, rule_id = router.match_with_stats("test message", [rule]) assert result is not None assert rule_id == str(rule.id)