"""Regression tests to ensure the CLAUDE document builds correctly and
matches the checked-in reference version.

Run with:
    uv run pytest -q
"""

import subprocess
from pathlib import Path

PROJECT_ROOT = Path(__file__).resolve().parents[1]
OUTPUT_PATH = PROJECT_ROOT / "output" / "CLAUDE.md"
REFERENCE_PATH = PROJECT_ROOT / "output" / "CLAUDE-Original.md"


def run_cmd(cmd: list[str]) -> None:
    """Run a shell command and assert it exits with 0."""
    result = subprocess.run(cmd, cwd=PROJECT_ROOT, capture_output=True, text=True)
    if result.returncode != 0:
        raise AssertionError(
            f"Command {' '.join(cmd)} failed with code {result.returncode}\n"
            f"STDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}"
        )


def test_build_succeeds_and_validates() -> None:
    """End-to-end: build the document and validate it."""
    # 1. Build
    run_cmd(["just", "output"])

    # 2. Validate (will raise on failure)
    run_cmd(["just", "validate"])

    # 3. Verify the output file exists and has content
    assert OUTPUT_PATH.exists(), "Output file was not created"
    content = OUTPUT_PATH.read_text(encoding="utf-8")
    assert len(content) > 100, "Output file is too short"
    assert "# Claude Collective" in content, "Output file missing expected header"
