"""
Unit tests for SearchService
============================

Tests search functionality including synonyms, FTS, and fuzzy matching.
"""

import pytest

# Mark all tests in this module as unit tests
pytestmark = pytest.mark.unit


class TestSearchService:
    """Tests for SearchService class."""

    def test_search_returns_list(self, app, db):
        """Search should always return a list."""
        from search_service import SearchService

        with app.app_context():
            service = SearchService(db.session)
            results = service.search("IT")

            assert isinstance(results, list)

    def test_search_empty_query(self, app, db):
        """Empty query should return empty list or all results."""
        from search_service import SearchService

        with app.app_context():
            service = SearchService(db.session)
            results = service.search("")

            assert isinstance(results, list)

    def test_search_special_characters(self, app, db):
        """Search should handle special characters safely."""
        from search_service import SearchService

        with app.app_context():
            service = SearchService(db.session)

            # SQL injection attempt
            results = service.search("'; DROP TABLE companies; --")
            assert isinstance(results, list)

            # XSS attempt
            results = service.search("<script>alert('xss')</script>")
            assert isinstance(results, list)

    def test_search_polish_characters(self, app, db):
        """Search should handle Polish characters."""
        from search_service import SearchService

        with app.app_context():
            service = SearchService(db.session)

            results = service.search("usługi")
            assert isinstance(results, list)

            results = service.search("żółć")
            assert isinstance(results, list)

    def test_search_case_insensitive(self, app, db):
        """Search should be case insensitive."""
        from search_service import SearchService

        with app.app_context():
            service = SearchService(db.session)

            results_lower = service.search("it")
            results_upper = service.search("IT")
            results_mixed = service.search("It")

            # All should return results (may not be identical due to ranking)
            assert isinstance(results_lower, list)
            assert isinstance(results_upper, list)
            assert isinstance(results_mixed, list)


class TestSearchSynonyms:
    """Tests for search synonym handling."""

    def test_synonym_expansion(self, app, db):
        """Search should expand synonyms."""
        from search_service import SearchService

        with app.app_context():
            service = SearchService(db.session)

            # "informatyka" should match "IT" companies
            results = service.search("informatyka")
            assert isinstance(results, list)
