"""Tests for citation handling."""


class TestCitationHandler:
    def test_init(self):
        from local_deep_research.citation_handling.citation_handler import (
            CitationHandler,
        )

        handler = CitationHandler()
        assert handler is not None

    def test_extract_citations(self):
        from local_deep_research.citation_handling.citation_handler import (
            CitationHandler,
        )

        handler = CitationHandler()
        text = "According to [1], this is true. Also see [2]."
        citations = handler.extract_citations(text)
        assert isinstance(citations, list)

    def test_format_citation(self):
        from local_deep_research.citation_handling.citation_handler import (
            CitationHandler,
        )

        handler = CitationHandler()
        citation = {
            "title": "Test",
            "url": "http://test.com",
            "author": "Author",
        }
        formatted = handler.format_citation(citation)
        assert isinstance(formatted, str)

    def test_generate_bibliography(self):
        from local_deep_research.citation_handling.citation_handler import (
            CitationHandler,
        )

        handler = CitationHandler()
        citations = [
            {"title": "A", "url": "http://a.com"},
            {"title": "B", "url": "http://b.com"},
        ]
        bib = handler.generate_bibliography(citations)
        assert isinstance(bib, str)

    def test_validate_citation(self):
        from local_deep_research.citation_handling.citation_handler import (
            CitationHandler,
        )

        handler = CitationHandler()
        valid = handler.validate_citation(
            {"title": "Test", "url": "http://test.com"}
        )
        assert isinstance(valid, bool)

    def test_merge_duplicate_citations(self):
        from local_deep_research.citation_handling.citation_handler import (
            CitationHandler,
        )

        handler = CitationHandler()
        citations = [
            {"url": "http://a.com", "title": "A"},
            {"url": "http://a.com", "title": "A duplicate"},
            {"url": "http://b.com", "title": "B"},
        ]
        merged = handler.merge_duplicates(citations)
        assert len(merged) <= len(citations)

    def test_sort_citations(self):
        from local_deep_research.citation_handling.citation_handler import (
            CitationHandler,
        )

        handler = CitationHandler()
        citations = [{"title": "B"}, {"title": "A"}, {"title": "C"}]
        sorted_cits = handler.sort_citations(citations, by="title")
        assert isinstance(sorted_cits, list)

    def test_export_to_bibtex(self):
        from local_deep_research.citation_handling.citation_handler import (
            CitationHandler,
        )

        handler = CitationHandler()
        citations = [{"title": "Test", "author": "Auth", "year": "2024"}]
        bibtex = handler.export_to_bibtex(citations)
        assert isinstance(bibtex, str) or bibtex is None

    def test_import_from_bibtex(self):
        from local_deep_research.citation_handling.citation_handler import (
            CitationHandler,
        )

        handler = CitationHandler()
        bibtex = "@article{test, title={Test}}"
        citations = handler.import_from_bibtex(bibtex)
        assert isinstance(citations, list)

    def test_number_citations(self):
        from local_deep_research.citation_handling.citation_handler import (
            CitationHandler,
        )

        handler = CitationHandler()
        text = "See [ref1] and [ref2]."
        citations = [{"id": "ref1"}, {"id": "ref2"}]
        numbered = handler.number_citations(text, citations)
        assert isinstance(numbered, str)
