"""Tests for the standards seam: ``standards_dir`` / ``list_standards`` /
``read_standard``.

The standards corpus (``docs/standards/``) is the federation's source of
truth, shipped as wheel data so ``mgf-common standards`` resolves the canonical
rules from the version a consumer pins — without vendoring or path-hacking.
These pin the resolver behaviour and the invariant that a known topic is
reachable and an unknown one fails loud (mirroring the recipes seam). Until
now the standards seam — the package's whole reason to exist — had no direct
test; this closes that gap.
"""

from __future__ import annotations

import pytest

import mgf.standard as ds


class TestStandardsSeam:
    def test_standards_dir_resolves_to_a_dir_with_an_index(self) -> None:
        d = ds.standards_dir()
        assert d.is_dir()
        assert (d / "README.md").is_file()

    def test_list_standards_nonempty_and_sorted(self) -> None:
        topics = ds.list_standards()
        assert topics, "standards corpus resolved empty"
        assert topics == sorted(topics)
        assert all(isinstance(t, str) for t in topics)
        # A few load-bearing topics the federation always ships.
        assert {"README", "LOGGING", "ERROR_HANDLING"} <= set(topics)

    def test_read_standard_returns_the_topic_text(self) -> None:
        text = ds.read_standard("LOGGING")
        assert isinstance(text, str)
        assert text.strip(), "LOGGING resolved empty"
        # A stable anchor present in the LOGGING rules box.
        assert "LG-01" in text

    def test_read_standard_unknown_name_raises_with_available(self) -> None:
        with pytest.raises(FileNotFoundError) as exc:
            ds.read_standard("no-such-topic")
        # The error lists what *is* available, mirroring read_recipe.
        assert "LOGGING" in str(exc.value)
