"""
Behavioral tests for loader_registry module.

Tests the document loader registry functions for file type detection and loader selection.
"""


class TestGetSupportedExtensions:
    """Tests for get_supported_extensions function."""

    def test_returns_list(self):
        """Returns a list of extensions."""
        from local_deep_research.document_loaders.loader_registry import (
            get_supported_extensions,
        )

        result = get_supported_extensions()
        assert isinstance(result, list)

    def test_returns_non_empty_list(self):
        """Returns non-empty list of extensions."""
        from local_deep_research.document_loaders.loader_registry import (
            get_supported_extensions,
        )

        result = get_supported_extensions()
        assert len(result) > 0

    def test_all_extensions_start_with_dot(self):
        """All extensions start with a dot."""
        from local_deep_research.document_loaders.loader_registry import (
            get_supported_extensions,
        )

        result = get_supported_extensions()
        for ext in result:
            assert ext.startswith("."), f"{ext} does not start with dot"

    def test_includes_pdf(self):
        """Includes PDF extension."""
        from local_deep_research.document_loaders.loader_registry import (
            get_supported_extensions,
        )

        result = get_supported_extensions()
        assert ".pdf" in result

    def test_includes_txt(self):
        """Includes TXT extension."""
        from local_deep_research.document_loaders.loader_registry import (
            get_supported_extensions,
        )

        result = get_supported_extensions()
        assert ".txt" in result

    def test_includes_markdown(self):
        """Includes markdown extensions."""
        from local_deep_research.document_loaders.loader_registry import (
            get_supported_extensions,
        )

        result = get_supported_extensions()
        assert ".md" in result

    def test_includes_docx(self):
        """Includes DOCX extension."""
        from local_deep_research.document_loaders.loader_registry import (
            get_supported_extensions,
        )

        result = get_supported_extensions()
        assert ".docx" in result

    def test_includes_csv(self):
        """Includes CSV extension."""
        from local_deep_research.document_loaders.loader_registry import (
            get_supported_extensions,
        )

        result = get_supported_extensions()
        assert ".csv" in result

    def test_includes_json(self):
        """Includes JSON extension."""
        from local_deep_research.document_loaders.loader_registry import (
            get_supported_extensions,
        )

        result = get_supported_extensions()
        assert ".json" in result

    def test_includes_yaml(self):
        """Includes YAML extensions."""
        from local_deep_research.document_loaders.loader_registry import (
            get_supported_extensions,
        )

        result = get_supported_extensions()
        assert ".yaml" in result
        assert ".yml" in result

    def test_includes_html(self):
        """Includes HTML extensions."""
        from local_deep_research.document_loaders.loader_registry import (
            get_supported_extensions,
        )

        result = get_supported_extensions()
        assert ".html" in result
        assert ".htm" in result

    def test_includes_xml(self):
        """Includes XML extension."""
        from local_deep_research.document_loaders.loader_registry import (
            get_supported_extensions,
        )

        result = get_supported_extensions()
        assert ".xml" in result

    def test_includes_jupyter(self):
        """Includes Jupyter notebook extension."""
        from local_deep_research.document_loaders.loader_registry import (
            get_supported_extensions,
        )

        result = get_supported_extensions()
        assert ".ipynb" in result

    def test_includes_toml(self):
        """Includes TOML extension."""
        from local_deep_research.document_loaders.loader_registry import (
            get_supported_extensions,
        )

        result = get_supported_extensions()
        assert ".toml" in result


class TestIsExtensionSupported:
    """Tests for is_extension_supported function."""

    def test_pdf_is_supported(self):
        """PDF extension is supported."""
        from local_deep_research.document_loaders.loader_registry import (
            is_extension_supported,
        )

        assert is_extension_supported(".pdf") is True

    def test_txt_is_supported(self):
        """TXT extension is supported."""
        from local_deep_research.document_loaders.loader_registry import (
            is_extension_supported,
        )

        assert is_extension_supported(".txt") is True

    def test_md_is_supported(self):
        """Markdown extension is supported."""
        from local_deep_research.document_loaders.loader_registry import (
            is_extension_supported,
        )

        assert is_extension_supported(".md") is True

    def test_json_is_supported(self):
        """JSON extension is supported."""
        from local_deep_research.document_loaders.loader_registry import (
            is_extension_supported,
        )

        assert is_extension_supported(".json") is True

    def test_yaml_is_supported(self):
        """YAML extension is supported."""
        from local_deep_research.document_loaders.loader_registry import (
            is_extension_supported,
        )

        assert is_extension_supported(".yaml") is True
        assert is_extension_supported(".yml") is True

    def test_unknown_extension_not_supported(self):
        """Unknown extension is not supported."""
        from local_deep_research.document_loaders.loader_registry import (
            is_extension_supported,
        )

        assert is_extension_supported(".xyz123") is False

    def test_handles_extension_without_dot(self):
        """Handles extension without leading dot."""
        from local_deep_research.document_loaders.loader_registry import (
            is_extension_supported,
        )

        assert is_extension_supported("pdf") is True
        assert is_extension_supported("txt") is True

    def test_case_insensitive(self):
        """Extension check is case insensitive."""
        from local_deep_research.document_loaders.loader_registry import (
            is_extension_supported,
        )

        assert is_extension_supported(".PDF") is True
        assert is_extension_supported(".Pdf") is True
        assert is_extension_supported(".pDf") is True

    def test_empty_extension_not_supported(self):
        """Empty extension is not supported."""
        from local_deep_research.document_loaders.loader_registry import (
            is_extension_supported,
        )

        assert is_extension_supported("") is False

    def test_dot_only_not_supported(self):
        """Dot only is not supported."""
        from local_deep_research.document_loaders.loader_registry import (
            is_extension_supported,
        )

        assert is_extension_supported(".") is False


class TestGetLoaderClassForExtension:
    """Tests for get_loader_class_for_extension function."""

    def test_returns_tuple_for_supported_extension(self):
        """Returns tuple for supported extension."""
        from local_deep_research.document_loaders.loader_registry import (
            get_loader_class_for_extension,
        )

        result = get_loader_class_for_extension(".pdf")
        assert result is not None
        assert isinstance(result, tuple)
        assert len(result) == 2

    def test_returns_none_for_unsupported_extension(self):
        """Returns None for unsupported extension."""
        from local_deep_research.document_loaders.loader_registry import (
            get_loader_class_for_extension,
        )

        result = get_loader_class_for_extension(".xyz123")
        assert result is None

    def test_first_element_is_class(self):
        """First element is a class."""
        from local_deep_research.document_loaders.loader_registry import (
            get_loader_class_for_extension,
        )

        result = get_loader_class_for_extension(".pdf")
        assert result is not None
        loader_class = result[0]
        assert isinstance(loader_class, type)

    def test_second_element_is_dict(self):
        """Second element is a dict of kwargs."""
        from local_deep_research.document_loaders.loader_registry import (
            get_loader_class_for_extension,
        )

        result = get_loader_class_for_extension(".pdf")
        assert result is not None
        kwargs = result[1]
        assert isinstance(kwargs, dict)

    def test_handles_extension_without_dot(self):
        """Handles extension without leading dot."""
        from local_deep_research.document_loaders.loader_registry import (
            get_loader_class_for_extension,
        )

        result = get_loader_class_for_extension("pdf")
        assert result is not None

    def test_case_insensitive(self):
        """Extension lookup is case insensitive."""
        from local_deep_research.document_loaders.loader_registry import (
            get_loader_class_for_extension,
        )

        result_lower = get_loader_class_for_extension(".pdf")
        result_upper = get_loader_class_for_extension(".PDF")
        assert result_lower is not None
        assert result_upper is not None
        assert result_lower[0] == result_upper[0]

    def test_json_returns_simple_json_loader(self):
        """JSON extension returns SimpleJSONLoader."""
        from local_deep_research.document_loaders.json_loader import (
            SimpleJSONLoader,
        )
        from local_deep_research.document_loaders.loader_registry import (
            get_loader_class_for_extension,
        )

        result = get_loader_class_for_extension(".json")
        assert result is not None
        assert result[0] == SimpleJSONLoader

    def test_yaml_returns_yaml_loader(self):
        """YAML extension returns YAMLLoader."""
        from local_deep_research.document_loaders.loader_registry import (
            get_loader_class_for_extension,
        )
        from local_deep_research.document_loaders.yaml_loader import YAMLLoader

        result = get_loader_class_for_extension(".yaml")
        assert result is not None
        assert result[0] == YAMLLoader

    def test_txt_has_encoding_kwargs(self):
        """TXT loader has encoding kwargs."""
        from local_deep_research.document_loaders.loader_registry import (
            get_loader_class_for_extension,
        )

        result = get_loader_class_for_extension(".txt")
        assert result is not None
        kwargs = result[1]
        assert "encoding" in kwargs


class TestLoaderRegistry:
    """Tests for LOADER_REGISTRY constant."""

    def test_registry_is_dict(self):
        """LOADER_REGISTRY is a dictionary."""
        from local_deep_research.document_loaders.loader_registry import (
            LOADER_REGISTRY,
        )

        assert isinstance(LOADER_REGISTRY, dict)

    def test_registry_has_loader_class_key(self):
        """Each registry entry has loader_class key."""
        from local_deep_research.document_loaders.loader_registry import (
            LOADER_REGISTRY,
        )

        for ext, entry in LOADER_REGISTRY.items():
            assert "loader_class" in entry, f"{ext} missing loader_class"

    def test_registry_keys_are_lowercase(self):
        """Registry keys are lowercase extensions."""
        from local_deep_research.document_loaders.loader_registry import (
            LOADER_REGISTRY,
        )

        for ext in LOADER_REGISTRY.keys():
            assert ext.startswith(".")
            assert ext == ext.lower()
