from __future__ import annotations

from pathlib import Path
import tomllib


ROOT = Path(__file__).resolve().parents[1]
PYPROJECT = ROOT / "pyproject.toml"
PACKAGE_ROOT = ROOT / "src" / "fuggers_py"


def _configured_measured_packages() -> list[str]:
    with PYPROJECT.open("rb") as handle:
        data = tomllib.load(handle)
    return list(data["tool"]["fuggers_source_coverage"]["measured_packages"])


def _public_top_level_packages() -> list[str]:
    return sorted(
        path.name
        for path in PACKAGE_ROOT.iterdir()
        if path.is_dir()
        and not path.name.startswith("_")
        and (path / "__init__.py").exists()
    )


def test_source_coverage_config_tracks_all_public_top_level_packages() -> None:
    configured = sorted(_configured_measured_packages())
    discovered = _public_top_level_packages()
    missing = sorted(set(discovered) - set(configured))

    assert not missing, (
        "tool.fuggers_source_coverage.measured_packages is missing public package roots.\n"
        f"missing: {missing}\n"
        f"configured: {configured}\n"
        f"discovered: {discovered}"
    )
