# region TestSourceIsolation [TYPE Module]
# @RELATION DEPENDS_ON ->[SourceIsolation]
# @SEMANTICS: tests, clean-release, source-isolation, internal-only
# @PURPOSE: Verify internal source registry validation behavior.
# @LAYER Domain
# @INVARIANT  External endpoints always produce blocking violations.

from datetime import UTC, datetime

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


# region _registry [TYPE Function]
# @RELATION  BINDS_TO -> TestSourceIsolation
def _registry() -> ResourceSourceRegistry:
    return ResourceSourceRegistry(
        registry_id="registry-internal-v1",
        name="Internal Sources",
        entries=[
            ResourceSourceEntry(
                source_id="src-1",
                host="repo.intra.company.local",
                protocol="https",
                purpose="artifact-repo",
                enabled=True,
            ),
            ResourceSourceEntry(
                source_id="src-2",
                host="pypi.intra.company.local",
                protocol="https",
                purpose="package-mirror",
                enabled=True,
            ),
        ],
        updated_at=datetime.now(UTC),
        updated_by="tester",
        status="active",
    )


# endregion _registry


# region test_validate_internal_sources_all_internal_ok [TYPE Function]
# @RELATION  BINDS_TO -> TestSourceIsolation
# @PURPOSE: Verify validate_internal_sources passes when all sources are internal and allowed.
def test_validate_internal_sources_all_internal_ok():
    result = validate_internal_sources(
        registry=_registry(),
        endpoints=["repo.intra.company.local", "pypi.intra.company.local"],
    )
    assert result["ok"] is True
    assert result["violations"] == []


# endregion test_validate_internal_sources_all_internal_ok


# region test_validate_internal_sources_external_blocked [TYPE Function]
# @RELATION  BINDS_TO -> TestSourceIsolation
# @PURPOSE: Verify validate_internal_sources blocks external sources when policy requires internal-only.
def test_validate_internal_sources_external_blocked():
    result = validate_internal_sources(
        registry=_registry(),
        endpoints=["repo.intra.company.local", "pypi.org"],
    )
    assert result["ok"] is False
    assert len(result["violations"]) == 1
    assert result["violations"][0]["category"] == "external-source"
    assert result["violations"][0]["blocked_release"] is True


# endregion test_validate_internal_sources_external_blocked
# endregion TestSourceIsolation
