"""Tests for the contract + design seams: ``federation`` / ``design_dir`` /
``list_design`` / ``read_design``.

These ship the federation **contract** (``FEDERATION.md``, DOC-17) and the
foundation **design** papers (``docs/design/``) as wheel data so an agent
resolves the WHOLE federation picture — rules + contract + design — offline
from the version it pins, not only the standards corpus. (Gap A in the
federation-sync hardening, WU1.)
"""

from __future__ import annotations

import pytest

import mgf.standard as ds


class TestFederationContractSeam:
    def test_federation_returns_the_contract_text(self) -> None:
        text = ds.federation()
        assert isinstance(text, str)
        assert text.strip(), "FEDERATION.md resolved empty"
        # Pin a load-bearing contract marker so a gutted file is caught.
        assert "FEDERATION" in text

    def test_federation_is_substantial(self) -> None:
        # The contract is a real document, not a stub — guard against an
        # accidental pointer-stub shipping in the canonical owner.
        assert len(ds.federation()) > 500


class TestDesignSeam:
    def test_design_dir_resolves_to_a_dir_with_an_index(self) -> None:
        d = ds.design_dir()
        assert d.is_dir()
        assert (d / "README.md").is_file()

    def test_list_design_nonempty_and_sorted(self) -> None:
        papers = ds.list_design()
        assert papers, "design corpus resolved empty"
        assert papers == sorted(papers)
        assert all(isinstance(p, str) for p in papers)

    def test_federation_design_paper_is_reachable(self) -> None:
        assert "federation" in ds.list_design()
        assert ds.read_design("federation").strip()

    def test_read_design_unknown_name_raises_with_available(self) -> None:
        with pytest.raises(FileNotFoundError) as exc:
            ds.read_design("no-such-paper")
        assert "federation" in str(exc.value)

    def test_design_distinct_from_standards_and_recipes(self) -> None:
        assert ds.design_dir() != ds.standards_dir()
        assert ds.design_dir() != ds.recipes_dir()


class TestVersionCoherence:
    def test_dunder_version_matches_pyproject(self) -> None:
        # WU1 reconciled the historical __version__ drift; keep it pinned to
        # the packaged version so consumers reading mgf.standard.__version__
        # see the truth (WF-05 coherence).
        import tomllib
        from pathlib import Path

        # repo-root pyproject.toml (editable-install layout).
        pyproject = Path(__file__).resolve().parents[2] / "pyproject.toml"
        if not pyproject.is_file():  # pragma: no cover - only in a checkout
            pytest.skip("pyproject.toml not available (installed, not a checkout)")
        version = tomllib.loads(pyproject.read_text(encoding="utf-8"))["project"]["version"]
        assert ds.__version__ == version
