"""Unit tests for search tool."""

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

from {{ package_name }}.models import Query, SearchResults
from {{ package_name }}.tools.search import search_documents

if TYPE_CHECKING:
    from conftest import DummyToolContext


@pytest.mark.asyncio
async def test_search_documents_returns_results(dummy_ctx: "DummyToolContext") -> None:
    """Verify search_documents returns valid SearchResults."""
    result = await search_documents(Query(question="What is PenguiFlow?"), dummy_ctx)

    assert isinstance(result, SearchResults)
    assert result.results, "Expected at least one search result"
    assert result.results[0].title, "Result should have a title"
    assert result.results[0].snippet, "Result should have a snippet"


@pytest.mark.asyncio
async def test_search_documents_with_different_query(dummy_ctx: "DummyToolContext") -> None:
    """Verify search handles different query types."""
    result = await search_documents(Query(question="How do I use tools?"), dummy_ctx)

    assert isinstance(result, SearchResults)
    assert result.results


{% if with_streaming %}
@pytest.mark.asyncio
async def test_search_documents_emits_chunks(dummy_ctx: "DummyToolContext") -> None:
    """Verify search emits streaming chunks during execution."""
    await search_documents(Query(question="Stream test"), dummy_ctx)

    assert dummy_ctx.chunks, "Expected streaming chunks to be emitted"
    # Verify chunks have valid structure
    for stream_id, seq, text, done in dummy_ctx.chunks:
        assert isinstance(stream_id, str)
        assert isinstance(seq, int)
        assert isinstance(text, str)
{% endif %}
