"""Char-sheet Brick 1 grounding — confirms the live A1 surface is where this brick
edits. PRESENT-fact asserts only (idempotent across the later phases)."""
from pathlib import Path

REPO = Path(__file__).resolve().parents[3]  # core/tests -> repo root

CITATIONS = [
    ("recoil/core/ref_types.py", "class RefAsset"),
    ("recoil/core/ref_types.py", 'RefRole = Literal["identity", "composition", "board", "location", "prop", "sheet"]'),
    ("recoil/core/ref_types.py", "class ReferenceBundle"),
    ("recoil/core/ref_stem.py", "def ref_filename(subject"),
    ("recoil/core/ref_resolver.py", "def resolve_reference_bundle("),
    ("recoil/core/ref_resolver.py", "def _shelf_asset("),
    ("recoil/core/paths.py", 'VALID_REF_TYPES = frozenset({"identity", "turn", "closeup", "fullbody", "expr"})'),
    ("recoil/pipeline/_lib/dispatch_payload.py", "def _collect_reference_images("),
    ("recoil/core/ref_gate.py", "def assert_refs_complete("),
]


def test_all_cs1_citations_present():
    missing = []
    for rel, needle in CITATIONS:
        text = (REPO / rel).read_text(encoding="utf-8")
        if needle not in text:
            missing.append(f"{rel} :: {needle!r}")
    assert not missing, "Stale char-sheet citations (re-ground before building):\n" + "\n".join(missing)


def test_valid_ref_types_unchanged_no_new_kinds():
    """This brick adds NO new top-level kind — turn views are a `view` qualifier
    WITHIN the existing `turn` kind. Assert the frozen set still contains exactly the
    five A1 kinds (and `turn` is one of them)."""
    from recoil.core.paths import VALID_REF_TYPES
    assert VALID_REF_TYPES == frozenset({"identity", "turn", "closeup", "fullbody", "expr"})


def test_gate_keyed_on_identity_hero_only():
    """The fail-closed gate counts only role==identity + is_hero. Assert the source
    still expresses that — so turn/fullbody refs (is_hero=False) never satisfy it."""
    text = (REPO / "recoil/core/ref_gate.py").read_text(encoding="utf-8")
    assert "hero_subjects()" in text
