"""Tests for generic Distiller using LLMClient."""
import pytest
from unittest.mock import AsyncMock, MagicMock
from neo_cortex.distiller import Distiller, KnowledgeEntry
from neo_cortex.llm import LLMClient


class TestDistillerGeneric:
    def test_accepts_any_llm_client(self):
        mock_llm = MagicMock(spec=LLMClient)
        d = Distiller(mock_llm)
        assert d._llm is mock_llm

    @pytest.mark.asyncio
    async def test_distill_calls_llm(self):
        mock_llm = AsyncMock(spec=LLMClient)
        mock_llm.call.return_value = '[{"topic":"test","content":"test content","type":"feature","project":"general","concepts":["foo"]}]'
        d = Distiller(mock_llm)
        entries = await d.distill(["turn 1", "turn 2"])
        assert len(entries) == 1
        assert entries[0].topic == "test"
        mock_llm.call.assert_called_once()

    @pytest.mark.asyncio
    async def test_distill_empty_turns(self):
        mock_llm = AsyncMock(spec=LLMClient)
        d = Distiller(mock_llm)
        assert await d.distill([]) == []
        mock_llm.call.assert_not_called()

    @pytest.mark.asyncio
    async def test_distill_handles_llm_error(self):
        mock_llm = AsyncMock(spec=LLMClient)
        mock_llm.call.side_effect = Exception("API down")
        d = Distiller(mock_llm)
        entries = await d.distill(["turn 1"])
        assert entries == []

    @pytest.mark.asyncio
    async def test_distill_handles_bad_json(self):
        mock_llm = AsyncMock(spec=LLMClient)
        mock_llm.call.return_value = "not json at all"
        d = Distiller(mock_llm)
        entries = await d.distill(["turn 1"])
        assert entries == []

    @pytest.mark.asyncio
    async def test_distill_uses_high_max_tokens(self):
        """Distiller needs 16k+ output for large batches."""
        mock_llm = AsyncMock(spec=LLMClient)
        mock_llm.call.return_value = "[]"
        d = Distiller(mock_llm)
        await d.distill(["turn 1"])
        _, kwargs = mock_llm.call.call_args
        assert kwargs.get("max_tokens", 0) >= 8000

    def test_gemini_distiller_alias_exists(self):
        """Backward compat: GeminiDistiller should still be importable."""
        from neo_cortex.distiller import GeminiDistiller
        assert GeminiDistiller is not None
