"""Boundary smoke test: Ratatoskr must not import from a Worldtree checkout.

The boundary rule (docs/design-brief.md §2): Ratatoskr depends on the
*published Conversation API spec* — vendored at `docs/conversation-api-spec.md`
at a pinned SHA — and NOT on any Worldtree source code. This test fails
loud if any file under `src/ratatoskr/` imports from `core.*`, `worldtree.*`,
or any other identifiable Worldtree module path.

If you find yourself wanting to import from Worldtree: stop. The right
move is one of:
  - Re-derive the type from the vendored spec.
  - File a question to worldtree-dev via althing (the spec is unclear).
  - Bump the spec pin (`docs/SPEC-PIN.md`) and re-vendor if the spec moved.
"""
from __future__ import annotations

import pathlib
import re

_FORBIDDEN_IMPORT_PATTERNS = [
    re.compile(r"^\s*import\s+core(\.|\s|$)", re.MULTILINE),
    re.compile(r"^\s*from\s+core(\.|\s+import)", re.MULTILINE),
    re.compile(r"^\s*import\s+worldtree(\.|\s|$)", re.MULTILINE),
    re.compile(r"^\s*from\s+worldtree(\.|\s+import)", re.MULTILINE),
]

_SRC_ROOT = pathlib.Path(__file__).parent.parent / "src" / "ratatoskr"


def test_no_worldtree_imports() -> None:
    violations: list[tuple[pathlib.Path, str]] = []
    for path in _SRC_ROOT.rglob("*.py"):
        text = path.read_text(encoding="utf-8")
        for pattern in _FORBIDDEN_IMPORT_PATTERNS:
            for match in pattern.finditer(text):
                line = text[: match.start()].count("\n") + 1
                violations.append((path, f"line {line}: {match.group(0).strip()}"))
    if violations:
        report = "\n".join(
            f"  {p.relative_to(_SRC_ROOT.parent.parent)}: {v}" for p, v in violations
        )
        raise AssertionError(
            "Ratatoskr must not import from Worldtree source. Violations:\n"
            f"{report}\n\n"
            "See docs/design-brief.md §2 (boundary rule) and "
            "docs/SPEC-PIN.md (spec-pin discipline)."
        )
