# #region Test.CleanRelease.SourceIsolation [C:2] [TYPE Module] [SEMANTICS test,clean-release,source,isolation]
# @BRIEF Tests for validate_internal_sources — endpoint validation against registry.
# @RELATION BINDS_TO -> [SourceIsolation]

from datetime import UTC, datetime
import pytest

from src.models.clean_release import ResourceSourceEntry, ResourceSourceRegistry
from src.services.clean_release.source_isolation import validate_internal_sources


def _make_registry(hosts: list[str] | None = None) -> ResourceSourceRegistry:
    """Create a ResourceSourceRegistry with entries."""
    if hosts is None:
        hosts = ["internal.local", "repo.internal.local"]
    now = datetime.now(UTC)
    entries = [
        ResourceSourceEntry(
            source_id=f"src-{i}", host=h, protocol="https",
            purpose="internal", enabled=True,
        )
        for i, h in enumerate(hosts)
    ]
    return ResourceSourceRegistry(
        registry_id="reg-1", name="Test Registry", entries=entries,
        updated_at=now, updated_by="system", status="ACTIVE",
    )


class TestValidateInternalSources:
    """validate_internal_sources — endpoint validation."""

    # #region test_all_internal [C:2] [TYPE Function]
    def test_all_internal(self):
        """All endpoints in allowed list → ok=True."""
        registry = _make_registry()
        result = validate_internal_sources(registry, ["internal.local", "repo.internal.local"])
        assert result["ok"] is True
        assert result["violations"] == []
    # #endregion test_all_internal

    # #region test_some_external [C:2] [TYPE Function]
    def test_some_external(self):
        """External endpoint → violation reported."""
        registry = _make_registry()
        result = validate_internal_sources(registry, ["internal.local", "external.bad.com"])
        assert result["ok"] is False
        assert len(result["violations"]) == 1
        assert result["violations"][0]["location"] == "external.bad.com"
        assert result["violations"][0]["blocked_release"] is True
    # #endregion test_some_external

    # #region test_empty_endpoint [C:2] [TYPE Function]
    def test_empty_endpoint(self):
        """Empty endpoint → violation with empty-endpoint placeholder."""
        registry = _make_registry()
        result = validate_internal_sources(registry, [""])
        assert result["ok"] is False
        assert result["violations"][0]["location"] == "<empty-endpoint>"
    # #endregion test_empty_endpoint

    # #region test_disabled_entries_ignored [C:2] [TYPE Function]
    def test_disabled_entries_ignored(self):
        """Disabled registry entries are not included in allowed hosts."""
        entries = [
            ResourceSourceEntry(source_id="s1", host="internal.local", protocol="https", purpose="internal", enabled=False),
        ]
        registry = ResourceSourceRegistry(
            registry_id="reg-1", name="Test", entries=entries,
            updated_at=datetime.now(UTC), updated_by="system",
        )
        result = validate_internal_sources(registry, ["internal.local"])
        assert result["ok"] is False
        assert len(result["violations"]) == 1
    # #endregion test_disabled_entries_ignored

    # #region test_case_insensitive_matching [C:2] [TYPE Function]
    def test_case_insensitive_matching(self):
        """Endpoint matching is case-insensitive."""
        registry = _make_registry(hosts=["Internal.Local"])
        result = validate_internal_sources(registry, ["INTERNAL.LOCAL"])
        assert result["ok"] is True
    # #endregion test_case_insensitive_matching

    # #region test_no_endpoints [C:2] [TYPE Function]
    def test_no_endpoints(self):
        """No endpoints provided → ok=True (nothing to check)."""
        registry = _make_registry()
        result = validate_internal_sources(registry, [])
        assert result["ok"] is True
    # #endregion test_no_endpoints
