"""Replay synthetic text through the pinned, unchanged P9 padding detector.

Usage: python padding-probe.py /path/to/whitespace_padding.py
No scanner installation, LLM, external service, or skill execution is involved.
"""
from pathlib import Path
import hashlib
import importlib.util
import platform
import sys
import unicodedata

PIN = "ea2e457d571d422ea274c4c12159c6e1efc3f8db"
DIGEST = "d08b29f50e408b7a24b3ec9d872a474d8d2fc243ae8eb6192d35bd2a5d2a6b61"
source = Path(sys.argv[1]).resolve()
assert hashlib.sha256(source.read_bytes()).hexdigest() == DIGEST
spec = importlib.util.spec_from_file_location("padding_source", source)
detector = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = detector
spec.loader.exec_module(detector)

print(f"Python {platform.python_version()}; Unicode {unicodedata.unidata_version}; source {PIN}")
for ch in ("\u200b", "\u180e", "\u00a0", "\u2800"):
    print(f"U+{ord(ch):04X}: isspace={ch.isspace()}; padding={detector.is_padding_char(ch)}")
assert not "\u200b".isspace() and detector.is_padding_char("\u200b")
assert not "\u180e".isspace() and detector.is_padding_char("\u180e")
assert not detector.is_padding_char("\u2800")


def fence(pad):
    return "```\nA" + pad + "B\n```"


cases = [
    ("zero_width_79", "A" + "\u200b" * 79 + "B", "markdown", []),
    ("zero_width_80", "A" + "\u200b" * 80 + "B", "markdown", [("horizontal", 80)]),
    ("mongolian_80", "A" + "\u180e" * 80 + "B", "markdown", [("horizontal", 80)]),
    ("fenced_ascii_80", fence(" " * 80), "markdown", []),
    ("same_text_other_type", fence(" " * 80), "other", [("horizontal", 80)]),
    ("fenced_682", fence("\u3000" * 682), "markdown", []),
    ("fenced_683", fence("\u3000" * 683), "markdown", [("block", 683)]),
    ("unfenced_683", "A" + "\u3000" * 683 + "B", "markdown", [("horizontal", 683)]),
    ("unicode_vertical_20", "intro\u2028" + "\u2028" * 20 + "instruction", "markdown", [("vertical", 20)]),
    ("one_replacement", "\ufffdA" + " " * 80 + "B", "markdown", [("horizontal", 80)]),
    ("dense_replacement", "\ufffd" * 100 + "A" + " " * 80 + "B", "markdown", []),
    ("braille_80", "A" + "\u2800" * 80 + "B", "other", []),
    ("braille_512", "A" + "\u2800" * 512 + "B", "other", [("repetition", 512)]),
]
for name, content, file_type, expected in cases:
    runs = detector.detect_whitespace_padding(content, file_type=file_type)
    assert [(r.kind, r.length) for r in runs] == expected, name
    for run in runs:
        assert 0 <= run.start_offset <= run.end_offset <= len(content), name
        if run.kind in ("horizontal", "block"):
            assert run.end_offset - run.start_offset == run.length, name
    if not runs:
        print(f"{name}: no runs")
    else:
        run = runs[0]
        payload = content[run.start_offset:run.end_offset]
        unit = "blank_lines" if run.kind == "vertical" else "chars"
        print(f"{name}: {run.kind}; {unit}={run.length}; span_bytes={len(payload.encode('utf-8'))}; line={run.start_line}")
    if name == "fenced_683":
        assert run.start_offset == 5 and run.end_offset == 688
        assert len(payload.encode("utf-8")) == 2049
        assert run.summary == "U+3000 x200"
        print(f"fenced_683 summary: {run.summary} (bounded sample)")
    if name == "unicode_vertical_20":
        assert run.start_line == 2 and run.followed_by_content
print("PASS: 13 synthetic detector cases; no full-scanner or maliciousness verdict")
