"""Live Zenodo API integration tests.

Hit real network APIs — excluded from CI via `-m 'not integration'`.
Run locally with:

    pdm run pytest tests/performance/search_engines/test_search_engine_zenodo_live.py -v
"""

import pytest


@pytest.mark.integration
class TestZenodoIntegration:
    """Integration tests making real API calls to Zenodo.

    Note: These tests depend on external Zenodo API availability.
    They handle empty results gracefully since API may be rate-limited in CI.
    """

    def test_real_search_machine_learning(self):
        """Test real search for machine learning datasets."""
        from local_deep_research.web_search_engines.engines.search_engine_zenodo import (
            ZenodoSearchEngine,
        )

        engine = ZenodoSearchEngine(max_results=5)
        results = engine._get_previews("machine learning")

        # API may be rate-limited; verify structure if results returned
        if len(results) > 0:
            for r in results:
                assert "title" in r, "Each result should have a title"
                assert "link" in r, "Each result should have a link"
                assert r["source"] == "Zenodo", "Source should be Zenodo"
                assert "zenodo.org" in r["link"], "Link should be Zenodo URL"

    def test_real_search_returns_doi(self):
        """Test that real search returns DOIs."""
        from local_deep_research.web_search_engines.engines.search_engine_zenodo import (
            ZenodoSearchEngine,
        )

        engine = ZenodoSearchEngine(max_results=5)
        results = engine._get_previews("climate data")

        # API may be rate-limited; verify DOI if results returned
        if len(results) > 0:
            has_doi = any(r.get("doi") for r in results)
            assert has_doi, "At least one result should have a DOI"

    def test_real_search_datasets(self):
        """Test real search filtered to datasets."""
        from local_deep_research.web_search_engines.engines.search_engine_zenodo import (
            ZenodoSearchEngine,
        )

        engine = ZenodoSearchEngine(max_results=5, resource_type="dataset")
        results = engine._get_previews("genomics")

        # API may be rate-limited; verify type if results returned
        if len(results) > 0:
            for r in results:
                assert r.get("resource_type") in [
                    "Dataset",
                    "dataset",
                    "Other",
                ], f"Should be dataset type, got: {r.get('resource_type')}"

    def test_real_search_software(self):
        """Test real search filtered to software."""
        from local_deep_research.web_search_engines.engines.search_engine_zenodo import (
            ZenodoSearchEngine,
        )

        engine = ZenodoSearchEngine(max_results=5, resource_type="software")
        results = engine._get_previews("python")

        # API may be rate-limited; verify links if results returned
        if len(results) > 0:
            for r in results:
                assert r["link"].startswith("https://zenodo.org"), (
                    f"Link should be Zenodo URL: {r['link']}"
                )

    def test_real_search_returns_authors(self):
        """Test that real search returns authors."""
        from local_deep_research.web_search_engines.engines.search_engine_zenodo import (
            ZenodoSearchEngine,
        )

        engine = ZenodoSearchEngine(max_results=5)
        results = engine._get_previews("neural network")

        # API may be rate-limited; verify authors if results returned
        if len(results) > 0:
            has_authors = any(r.get("authors") for r in results)
            assert has_authors, (
                f"At least one should have authors, got: {[r.get('authors') for r in results]}"
            )

    def test_real_search_open_access(self):
        """Test real search for open access records."""
        from local_deep_research.web_search_engines.engines.search_engine_zenodo import (
            ZenodoSearchEngine,
        )

        engine = ZenodoSearchEngine(max_results=5, access_right="open")
        results = engine._get_previews("biology")

        # API may be rate-limited; verify access_right if results returned
        if len(results) > 0:
            for r in results:
                assert r.get("access_right") == "open", (
                    f"Should be open access, got: {r.get('access_right')}"
                )

    def test_real_search_returns_publication_date(self):
        """Test that real search returns publication dates."""
        from local_deep_research.web_search_engines.engines.search_engine_zenodo import (
            ZenodoSearchEngine,
        )

        engine = ZenodoSearchEngine(max_results=5)
        results = engine._get_previews("astronomy")

        # API may be rate-limited; verify publication_date if results returned
        if len(results) > 0:
            has_date = any(r.get("publication_date") for r in results)
            assert has_date, "At least one should have publication_date"
