"""PUBLIC_API pins: json+md vs __all__ (DOC-08/09/AP-10); __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.vm.libvirt")
    missing = [n for n in mod.__all__ if f"`{n}`" not in md]
    assert not missing, f"PUBLIC_API.md missing: {missing}"


def test_version_mirrors_pyproject() -> None:
    pj = tomllib.loads((_root() / "pyproject.toml").read_text(encoding="utf-8"))["project"][
        "version"
    ]
    mod = importlib.import_module("mgf.vm.libvirt")
    assert mod.__version__ == pj, f"AP-13: __version__ {mod.__version__} != pyproject {pj}"
