"""P-G regression gate: no raw control bytes in the standards corpus (DOC-12).

A stray control byte (e.g. a literal NUL) keeps a file valid UTF-8 yet flips it to
"binary" for ``file(1)`` and GNU grep's heuristic, silently degrading every agent's
text tooling on the document. Reject control bytes outside the standard whitespace set
(tab / LF / CR). Catches the P-G class in milliseconds.
"""

from __future__ import annotations

from pathlib import Path

_ALLOWED = {0x09, 0x0A, 0x0D}  # tab, newline, carriage return
_ROOT = Path(__file__).resolve().parents[2]
_STANDARDS = _ROOT / "docs" / "standards"


def test_no_control_bytes_in_standards_corpus() -> None:
    offenders: list[str] = []
    for md in sorted(_STANDARDS.rglob("*.md")):
        data = md.read_bytes()
        for i, byte in enumerate(data):
            if byte < 0x20 and byte not in _ALLOWED:
                line = data[: i + 1].count(b"\n") + 1
                offenders.append(f"{md.relative_to(_ROOT)}:{line} control byte 0x{byte:02x}")
    assert not offenders, "P-G: control byte(s) in standards corpus — " + "; ".join(offenders[:10])
