"""Regression checks for the supported Wagtail 7/Oscar search contract."""

from pathlib import Path
import unittest

PACKAGE_ROOT = (
    Path(__file__).resolve().parents[1] / "ocyan" / "plugin" / "oscar_elasticsearch"
)


class ModernSearchContractTests(unittest.TestCase):
    def test_removed_wagtail_search_contract_is_not_reintroduced(self):
        source = "\n".join(
            path.read_text()
            for path in PACKAGE_ROOT.rglob("*.py")
            if "tests" not in path.parts
        )

        self.assertNotRegex(source, r"(?:from|import)\s+wagtail_extendedsearch")
        self.assertNotRegex(
            source,
            r"from\s+wagtail\.search\.backends\.elasticsearch6\s+import",
        )

    def test_oscar_elasticsearch_three_is_the_runtime_contract(self):
        setup_source = (PACKAGE_ROOT.parents[2] / "setup.py").read_text()
        views_source = (PACKAGE_ROOT / "search" / "views.py").read_text()

        self.assertIn('"django-oscar-elasticsearch>=3,<4"', setup_source)
        self.assertIn('"elasticsearch>=8,<9"', setup_source)
        self.assertIn('"search.views.search"', views_source)
        self.assertIn('"CatalogueAutoCompleteView"', views_source)
