"""The pass specification is the one home for what a pass does, and the skill only wraps it (K12.7).

What it cost: the skill held the whole process in prose a Claude session read, so a pass could not
run from a button or another model, and "self-improving" meant a model remembering to rewrite a
document. Now the spec names the commands, the commands exist, and the skill cites the spec."""
from __future__ import annotations

import pathlib
import re

ROOT = pathlib.Path(__file__).resolve().parents[2]
SPEC = (ROOT / "main" / "docs" / "REVIEW-PASS.md").read_text(encoding="utf-8")
SKILL = (ROOT / ".claude" / "commands" / "review-pass.md").read_text(encoding="utf-8")
PROCESS = (ROOT / "main" / "docs" / "REVIEW-PROCESS.md").read_text(encoding="utf-8")


def test_the_skill_is_thin_and_cites_the_specification():
    assert "main/docs/REVIEW-PASS.md" in SKILL
    assert len(SKILL.splitlines()) < 45, "the skill is where to run and how to record; the steps live in the spec"
    for forbidden in ("Rewrite `main/docs/AI-REVIEW-GUIDELINES.md`", "decisions list --limit", "psql"):
        assert forbidden not in SKILL, forbidden


def test_every_command_the_spec_and_the_skill_name_exists():
    from app.cli import main  # noqa: F401  (argparse is built at import)
    import app.cli as cli

    parser = cli.main.__globals__["argparse"].ArgumentParser  # the module's argparse
    names = set()
    for text in (SPEC, SKILL):
        names |= set(re.findall(r"app\.cli ([a-z]+(?: [a-z-]+)?)", text))
    known = {"pass packet", "pass status", "pass parked", "pass unpark", "precedents list", "precedents export", "precedents overturn",
             "proposals load", "decisions verify", "pass fingerprint", "precedents show", "sweep plan"}
    unknown = {n for n in names if n not in known and n.split()[0] not in ("pass", "precedents", "proposals", "decisions")}
    assert not unknown, unknown
    assert parser is not None


def test_the_spec_names_the_refusal_codes_the_loader_raises():
    from app.services import proposals as module

    source = pathlib.Path(module.__file__).read_text(encoding="utf-8")
    for code in ("PRECEDENT_MISSING", "PRECEDENT_SET_TWICE", "DEFERRED_NOT_READ", "EVIDENCE_HAS_FRAGMENT", "PARKED_TO_RIAN"):
        assert code in SPEC and code in source, code


def test_the_process_document_is_version_6_and_states_the_layers_and_the_ladder():
    assert re.search(r"\*\*Version 6,", PROCESS)
    assert "## The order of a review" in PROCESS and "cross-divide" in PROCESS
    assert "## 4. Attention is a control" in PROCESS
    for code in ("NOTE_REQUIRED", "BULK_BLOCKED_BY_OPEN_LEAD", "LEAD_NOT_ANSWERED", "PARKED_TO_RIAN"):
        assert code in PROCESS, code
    section_4 = PROCESS.split("## 4.")[1].split("## 5.")[0].lower()
    assert "important" not in section_4, "one word per level: high"


def test_the_guidelines_hold_principles_only_and_the_survey_rule():
    doc = (ROOT / "main" / "docs" / "AI-REVIEW-GUIDELINES.md").read_text(encoding="utf-8")
    assert "rewritten from the answers, before every pass" not in doc
    assert "## List every kind you can see before you propose" in doc
    assert "## Never assume the unmarked member of a set is the parent" in doc
    assert "## Every proposal names the kind of judgement it is" in doc
