test_fuse_intent.py 2.56 KB
"""End-to-end tests for fuse_intent with mock LLM."""

import pytest

from intent_fusion import IntentRouter, fuse_intent, FusionVerdict, invalidate_cache
from tests.conftest import MockLLM, SAMPLE_ENTRIES


@pytest.fixture(autouse=True)
def _clean_singleton():
    invalidate_cache()
    yield
    invalidate_cache()


class TestIntentRouter:
    """Tests using the class-based IntentRouter API."""

    async def test_clear_direct_query(self, sample_entries, mock_llm):
        router = IntentRouter(sample_entries)
        result = await router.fuse("where is my order", mock_llm)

        assert result.verdict == FusionVerdict.CLEAR
        assert result.top is not None
        assert result.top.name == "query_order_status"
        assert result.total_ms > 0

    async def test_small_talk_verdict(self, sample_entries):
        """Small talk should not get a confident CLEAR on a real intent."""
        llm = MockLLM()
        router = IntentRouter(sample_entries)
        result = await router.fuse("hello there", llm)

        assert result.verdict is not None
        assert result.total_ms > 0

    async def test_active_channels_both(self, sample_entries, mock_llm):
        router = IntentRouter(sample_entries)
        result = await router.fuse("where is my order", mock_llm)

        assert result.degraded is False
        assert "embedding" in result.active_channels
        assert "tree" in result.active_channels

    async def test_timing_fields_populated(self, sample_entries, mock_llm):
        router = IntentRouter(sample_entries)
        result = await router.fuse("cancel my order", mock_llm)

        assert result.emb_ms >= 0
        assert result.tree_ms >= 0
        assert result.total_ms >= 0

    async def test_candidates_list_populated(self, sample_entries, mock_llm):
        router = IntentRouter(sample_entries)
        result = await router.fuse("where is my order", mock_llm)

        assert len(result.candidates) > 0


class TestFuseIntentFunction:
    """Tests using the convenience function API."""

    async def test_function_api_works(self, sample_entries, mock_llm):
        result = await fuse_intent("where is my order", sample_entries, mock_llm)

        assert result.verdict in (FusionVerdict.CLEAR, FusionVerdict.AMBIGUOUS)
        assert result.top is not None

    async def test_function_api_reuses_router(self, sample_entries, mock_llm):
        r1 = await fuse_intent("where is my order", sample_entries, mock_llm)
        r2 = await fuse_intent("cancel my order", sample_entries, mock_llm)

        assert r1.top is not None
        assert r2.top is not None