"""TS-01 — every source module has a mirror in the test tree.

Walks ``src/mgf/cloud`` and asserts each module is mirrored by at least one test
file, against the documented :data:`MIRRORS` allow-list. The allow-list encodes
the deliberate coarse-grouping convention (e.g. the CLI's argparse modules are
exercised as one surface by ``test_cli``, the two providers + factory by
``test_providers`` / ``test_infomaniak``). A NEW source module with no entry here
fails this test — forcing an explicit mirroring decision rather than a silent gap.
"""

from __future__ import annotations

from pathlib import Path

_REPO_ROOT = Path(__file__).resolve().parents[1]
_SRC = _REPO_ROOT / "src" / "mgf" / "cloud"
_TESTS = _REPO_ROOT / "tests"

# source module (path relative to src/mgf/cloud, no .py) -> mirroring test files
# (paths relative to tests/). Coarse grouping is intentional and documented here.
MIRRORS: dict[str, list[str]] = {
    "__init__": ["unit/test_version_in_sync.py", "unit/test_public_api_pin.py"],
    "__main__": ["unit/test_cli.py", "unit/test_remote.py"],
    "config": ["unit/test_config.py"],
    "cost": ["unit/test_cost.py"],
    "defaults": ["unit/test_defaults.py"],
    "exceptions": ["unit/test_exceptions.py"],
    "models": ["unit/test_models.py"],
    "remote": ["unit/test_remote.py"],
    "cli/__init__": ["unit/test_cli.py"],
    "cli/_app": ["unit/test_cli.py"],
    "cli/_output": ["unit/test_cli.py"],
    "cli/_version": ["unit/test_ops_verbs.py"],
    "cli/_doctor": ["unit/test_ops_verbs.py"],
    "cli/_explain": ["unit/test_ops_verbs.py"],
    "cli/_bundle": ["unit/test_ops_verbs.py"],
    "providers/__init__": ["unit/test_providers.py"],
    "providers/base": ["unit/test_providers.py"],
    "providers/fake": ["unit/test_providers.py"],
    "providers/infomaniak": ["unit/test_infomaniak.py"],
    "web/__init__": ["unit/test_web.py"],
    "web/_app": ["unit/test_web.py"],
}


def _source_modules() -> set[str]:
    return {py.relative_to(_SRC).with_suffix("").as_posix() for py in _SRC.rglob("*.py")}


def test_every_source_module_is_in_the_allow_list() -> None:
    discovered = _source_modules()
    documented = set(MIRRORS)
    missing = discovered - documented
    stale = documented - discovered
    assert not missing, f"source modules with no mirror entry (add to MIRRORS): {sorted(missing)}"
    assert not stale, f"MIRRORS entries for modules that no longer exist: {sorted(stale)}"


def test_every_mirror_file_exists() -> None:
    for module, mirrors in MIRRORS.items():
        for mirror in mirrors:
            assert (_TESTS / mirror).is_file(), f"{module}: mirror test {mirror} is missing"
