import hashlib
import json
from pathlib import Path

ROOT = Path(__file__).parents[1]


def _sha256(path: Path) -> str:
    digest = hashlib.sha256()
    with path.open("rb") as stream:
        for chunk in iter(lambda: stream.read(1024 * 1024), b""):
            digest.update(chunk)
    return digest.hexdigest()


def test_reference_inputs_are_unchanged() -> None:
    expected = {
        "CODEX_K1_MAC_LINK_PREPROD_PROMPT.md": (
            "f24537119bd76f76d1914ca7f34a6736d8c5d3b6d19ead6d0ae8a0e5966c9770"
        ),
        "XGRIDS_Lixel_K1_PreProduction_Bible.md": (
            "22ea9d0dc6f5a623ba26d6f08108a905170db08112e0531a1d80bddd6101689c"
        ),
    }
    for filename, expected_hash in expected.items():
        assert _sha256(ROOT / "docs" / "reference" / filename) == expected_hash


def test_redacted_manifest_example_is_json() -> None:
    path = ROOT / "examples" / "session_manifest.redacted.json"
    payload = json.loads(path.read_text(encoding="utf-8"))
    assert payload["schema_version"] == 1
    assert payload["decision"]["status"] in {"GO", "PAUSE", "BLOCKED"}


def test_manifest_schema_is_json() -> None:
    path = ROOT / "schemas" / "session-manifest.schema.json"
    payload = json.loads(path.read_text(encoding="utf-8"))
    assert payload["$schema"] == "https://json-schema.org/draft/2020-12/schema"
    assert payload["properties"]["schema_version"]["const"] == 1
