"""PUBLIC_API pins: json vs __all__ (DOC-08/AP-10); md lists names (DOC-09); __version__ mirrors pyproject (AP-13)."""

from __future__ import annotations

import importlib
import json
import tomllib
from pathlib import Path


def _root() -> Path:
    for a in Path(__file__).resolve().parents:
        if (a / "pyproject.toml").exists():
            return a
    raise RuntimeError


def test_json_matches_generator() -> None:
    from tools.generate_public_api_json import build_payload  # type: ignore[import-not-found]

    assert json.loads((_root() / "PUBLIC_API.json").read_text(encoding="utf-8")) == build_payload()


def test_md_lists_every_name() -> None:
    md = (_root() / "PUBLIC_API.md").read_text(encoding="utf-8")
    mod = importlib.import_module("mgf.tele.core")
    assert not [n for n in mod.__all__ if f"`{n}`" not in md]


def test_version_mirrors_pyproject() -> None:
    pj = tomllib.loads((_root() / "pyproject.toml").read_text(encoding="utf-8"))["project"][
        "version"
    ]
    assert importlib.import_module("mgf.tele.core").__version__ == pj
