"""TS-01 — every src module has a test mirror OR a MIRROR_ALLOWLIST entry."""

from __future__ import annotations

from pathlib import Path

_TESTS = Path(__file__).resolve().parent
_PKG = _TESTS.parent / "src" / "mgf" / "http"
_ALLOW = _TESTS / "MIRROR_ALLOWLIST.txt"


def _allow() -> set[str]:
    if not _ALLOW.is_file():
        return set()
    out = set()
    for ln in _ALLOW.read_text(encoding="utf-8").splitlines():
        name = ln.split("#")[0].strip()
        if name:
            out.add(name)
    return out


def _has_mirror(stem: str) -> bool:
    return any((_TESTS / "unit" / f"test_{s}.py").exists() for s in {stem, stem.lstrip("_")})


def test_every_source_module_has_mirror_or_allowlist() -> None:
    allow = _allow()
    missing = [
        p.name
        for p in _PKG.rglob("*.py")
        if "__pycache__" not in p.parts
        and p.name != "__init__.py"
        and p.name not in allow
        and not _has_mirror(p.stem)
    ]
    assert not missing, f"TS-01: modules with no test mirror and no allow-list entry: {missing}"
