"""Tests for the 3-Layer Cookie-Lookup-Service."""

from compliance.services.cookie_library_lookup import (
    _is_specific_enough,
    _name_matches,
    _strip_wildcards,
)


class TestStripWildcards:
    def test_lowercase(self):
        assert _strip_wildcards("_GA") == "_ga"

    def test_strip_star(self):
        assert _strip_wildcards("_ga*") == "_ga"

    def test_strip_dotstar(self):
        assert _strip_wildcards("_pk_id.*") == "_pk_id"

    def test_strip_trailing_underscore(self):
        # OCD-Pattern: trailing _ is implicit wildcard
        assert _strip_wildcards("guest_uuid_essential_") == "guest_uuid_essential"

    def test_strip_trailing_dot(self):
        assert _strip_wildcards("_pk_id.") == "_pk_id"


class TestIsSpecificEnough:
    def test_long_name(self):
        assert _is_specific_enough("OptanonConsent")

    def test_short_with_separator(self):
        assert _is_specific_enough("_ga")

    def test_short_no_separator_rejected(self):
        assert not _is_specific_enough("c")
        assert not _is_specific_enough("ID")
        assert not _is_specific_enough("abc")


class TestNameMatches:
    def test_exact(self):
        assert _name_matches("OptanonConsent", "OptanonConsent")

    def test_prefix_with_separator(self):
        # _ga library + browser _ga_K8YL3M9T
        assert _name_matches("_ga", "_ga_K8YL3M9T")
        # __cf_bm library + browser __cf_bm_hash
        assert _name_matches("__cf_bm", "__cf_bm_hash")

    def test_short_unspecific_rejected(self):
        # 1-char library entries must not match arbitrary queries
        assert not _name_matches("c", "completely_unknown")
        assert not _name_matches("ID", "IDcharger")

    def test_prefix_no_separator_rejected(self):
        # Even with longer library, must have separator after prefix
        assert not _name_matches("Compa", "Completely_unknown")

    def test_wildcard_match(self):
        # _pk_id.* matches _pk_id.5.7d8
        assert _name_matches("_pk_id.*", "_pk_id.5.7d8")

    def test_trailing_underscore_match(self):
        # guest_uuid_essential_ matches guest_uuid_essential_xyz
        assert _name_matches("guest_uuid_essential_", "guest_uuid_essential_xyz")

    def test_unrelated(self):
        assert not _name_matches("_ga", "intercom-session")
