"""Import validation tests.

Ensures all lazy imports across packages resolve correctly,
catching cross-package import errors that mocks would hide.
"""
import importlib
import pkgutil

import pytest


def _collect_modules(package_name: str) -> list[str]:
    """Recursively collect all module names under a package."""
    try:
        package = importlib.import_module(package_name)
    except Exception:
        return [package_name]

    modules = [package_name]
    if hasattr(package, "__path__"):
        for _importer, modname, _ispkg in pkgutil.walk_packages(
            package.__path__, prefix=package_name + "."
        ):
            modules.append(modname)
    return modules


SHARED_MODULES = _collect_modules("shared")
BACKEND_MODULES = _collect_modules("backend")
TRAINING_MODULES = _collect_modules("training")


@pytest.mark.parametrize("module_name", SHARED_MODULES)
def test_shared_module_imports(module_name: str) -> None:
    """Every module in the shared package should import without error."""
    importlib.import_module(module_name)


@pytest.mark.parametrize("module_name", BACKEND_MODULES)
def test_backend_module_imports(module_name: str) -> None:
    """Every module in the backend package should import without error."""
    importlib.import_module(module_name)


@pytest.mark.parametrize("module_name", TRAINING_MODULES)
def test_training_module_imports(module_name: str) -> None:
    """Every module in the training package should import without error."""
    importlib.import_module(module_name)
