# retoor <retoor@molodetz.nl>

from pathlib import Path

PACKAGE_ROOT = Path(__file__).resolve().parents[4] / "devplacepy"
PACKAGE_DIR = PACKAGE_ROOT / "services" / "acceptance"
NEEDLES = (
    "services.acceptance",
    "devplacepy/services/acceptance",
    "AcceptanceService",
)


def source_files(suffixes):
    return [
        path
        for path in PACKAGE_ROOT.rglob("*")
        if path.suffix in suffixes
        and path.is_file()
        and PACKAGE_DIR not in path.parents
        and "__pycache__" not in path.parts
    ]


def references(path):
    body = path.read_text()
    return any(needle in body for needle in NEEDLES)


def test_only_main_imports_the_acceptance_package():
    importers = [
        path.relative_to(PACKAGE_ROOT).as_posix()
        for path in source_files({".py"})
        if references(path)
    ]
    assert importers == ["main.py"]


def test_no_template_or_static_asset_mentions_the_package():
    mentions = [
        path.relative_to(PACKAGE_ROOT).as_posix()
        for path in source_files({".html", ".js", ".css"})
        if references(path)
    ]
    assert mentions == []


def test_the_feature_has_no_route_schema_or_agent_tool():
    watched = ("routers", "schemas", "docs_api")
    surfaced = [
        path.relative_to(PACKAGE_ROOT).as_posix()
        for path in source_files({".py"})
        if path.parts[len(PACKAGE_ROOT.parts)] in watched and references(path)
    ]
    assert surfaced == []


def test_the_devii_catalog_does_not_expose_the_service():
    catalog = PACKAGE_ROOT / "services" / "devii" / "actions"
    mentions = [path.name for path in catalog.rglob("*.py") if references(path)]
    assert mentions == []


def test_the_package_imports_no_router_and_no_template_global():
    for path in PACKAGE_DIR.glob("*.py"):
        body = path.read_text()
        assert "devplacepy.routers" not in body
        assert "devplacepy.templating" not in body
