"""Tests for the scaffold seam: ``list_scaffold`` / ``read_scaffold``.

The canonical standardized git/CI scaffold artifacts (the AG-03
``prepare-commit-msg`` hook + the ``check_commit_trailers.py`` CI checker) are
shipped as wheel data so ``mgf-common catch-up`` resolves the canonical text
rather than carrying a drift-prone copy. These pin the resolver behaviour AND
the single-source-of-truth invariant: the cookiecutter copies MUST match the
canonical (so a fleet scaffold can't silently diverge).
"""

from __future__ import annotations

from pathlib import Path

import pytest

import mgf.standard as ds

_REPO_ROOT = Path(__file__).resolve().parents[2]
_COOKIECUTTER = (
    _REPO_ROOT / "templates" / "cookiecutter-mgf-project" / "{{cookiecutter.project_slug}}"
)


class TestScaffoldSeam:
    def test_list_scaffold_is_the_two_artifacts(self) -> None:
        assert ds.list_scaffold() == ["check_commit_trailers.py", "prepare-commit-msg"]

    def test_read_hook_returns_the_canonical_text(self) -> None:
        text = ds.read_scaffold("prepare-commit-msg")
        assert "interpret-trailers" in text  # the load-bearing line
        assert text == (_REPO_ROOT / ".githooks" / "prepare-commit-msg").read_text(encoding="utf-8")

    def test_read_checker_returns_the_canonical_text(self) -> None:
        text = ds.read_scaffold("check_commit_trailers.py")
        assert "def missing_trailers" in text
        assert text == (_REPO_ROOT / "scripts" / "check_commit_trailers.py").read_text(
            encoding="utf-8"
        )

    def test_unknown_name_raises_with_available(self) -> None:
        with pytest.raises(FileNotFoundError) as exc:
            ds.read_scaffold("no-such-artifact")
        assert "prepare-commit-msg" in str(exc.value)


class TestCookiecutterCopiesArePinned:
    """DOC-18 — the cookiecutter ships physical copies; they MUST equal the
    canonical scaffold text so new repos never diverge from the source of truth."""

    def test_hook_copy_matches_canonical(self) -> None:
        copy = (_COOKIECUTTER / ".githooks" / "prepare-commit-msg").read_text(encoding="utf-8")
        assert copy == ds.read_scaffold("prepare-commit-msg")

    def test_checker_copy_matches_canonical(self) -> None:
        copy = (_COOKIECUTTER / "scripts" / "check_commit_trailers.py").read_text(encoding="utf-8")
        assert copy == ds.read_scaffold("check_commit_trailers.py")
