# tests/test_models.py
"""Tests for response model parsing."""
from __future__ import annotations

from ihyee.models import (
    ContentItem,
    ExtractedLink,
    FetchResponse,
    ImageContent,
    ResponseMeta,
    SearchResponse,
    TextContent,
)


class TestSearchResponse:
    def test_parses_minimal_response(self):
        data = {
            "query": "test",
            "results_count": 0,
            "content": [],
            "meta": {"pages_fetched": 0, "pages_rendered": 0, "content_mode": "both"},
        }
        result = SearchResponse.model_validate(data)
        assert result.query == "test"
        assert result.results_count == 0
        assert result.content == []
        assert result.meta.degraded is False

    def test_parses_full_response(self):
        data = {
            "query": "transformer",
            "results_count": 1,
            "content": [
                {
                    "type": "text",
                    "url": "https://example.com",
                    "text": {
                        "full_text": "Full content here",
                        "summary": "Short summary",
                        "author": "Jane",
                        "date_published": "2025-01-01",
                        "links": [
                            {"url": "https://arxiv.org", "text": "Paper", "context": "See:"}
                        ],
                    },
                },
                {
                    "type": "image_url",
                    "url": "https://example.com",
                    "image_url": {"url": "https://example.com/img.jpg", "alt": "Diagram"},
                },
            ],
            "meta": {
                "search_time_ms": 1200,
                "pages_fetched": 1,
                "pages_rendered": 0,
                "content_mode": "both",
            },
        }
        result = SearchResponse.model_validate(data)
        assert result.results_count == 1
        assert len(result.content) == 2
        assert result.content[0].text.summary == "Short summary"
        assert result.content[0].text.links[0].url == "https://arxiv.org"
        assert result.content[1].image_url.alt == "Diagram"
        assert result.meta.search_time_ms == 1200


class TestFetchResponse:
    def test_parses_with_error_item(self):
        data = {
            "content": [
                {
                    "type": "text",
                    "url": "https://example.com",
                    "text": {"error": "Timeout after 60s"},
                }
            ],
            "meta": {"pages_fetched": 0, "pages_rendered": 0, "content_mode": "both"},
        }
        result = FetchResponse.model_validate(data)
        assert result.content[0].text.error == "Timeout after 60s"

    def test_parses_document_item(self):
        data = {
            "content": [
                {
                    "type": "document",
                    "url": "https://example.com/paper.pdf",
                    "document": {
                        "s3_url": "https://s3.example.com/doc.pdf",
                        "original_url": "https://example.com/paper.pdf",
                        "content_type": "application/pdf",
                        "size_bytes": 240000,
                    },
                }
            ],
            "meta": {"pages_fetched": 1, "pages_rendered": 0, "content_mode": "both"},
        }
        result = FetchResponse.model_validate(data)
        assert result.content[0].document.size_bytes == 240000


class TestDefaults:
    def test_text_content_defaults(self):
        tc = TextContent.model_validate({})
        assert tc.author == "Unknown"
        assert tc.links == []
        assert tc.full_text is None

    def test_response_meta_defaults(self):
        meta = ResponseMeta.model_validate({})
        assert meta.degraded is False
        assert meta.content_mode == "both"
