"""独立离线核验；Python 3.12 标准库，无网络或设备访问。"""
from pathlib import Path
import json
import struct
import datetime as dt
import math
import re
import uuid

def parse(d):
    n=d['raw']
    if type(n)is not int or not 0<=n<=255: raise ValueError('原始值超一字节')
    p=n*100/255
    return {'percent':p,'roundtrip_raw':round(p*255/100),'step_percent':100/255}


def main():
    root = Path(__file__).resolve().parent
    fixture = json.loads((root / "fixture.json").read_text(encoding="utf-8"))
    rows = []
    for item in fixture["cases"]:
        try:
            result = parse(item["input"])
        except ValueError as exc:
            assert "expected_error" in item, (item["case_id"], str(exc))
            assert str(exc) == item["expected_error"], (item["case_id"], str(exc))
            rows.append({"case_id": item["case_id"], "kind": item["kind"], "rejected": str(exc)})
        else:
            assert "expected_error" not in item, item["case_id"]
            assert result == item["expected"], (item["case_id"], result, item["expected"])
            rows.append({"case_id": item["case_id"], "kind": item["kind"], "result": result})
    report = {"fixture": fixture["topic"], "cases": rows, "assertions_passed": len(rows), "scope": "合成输入离线演算；未接真实设备"}
    (root / "report.json").write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
    print(json.dumps({"assertions_passed": len(rows), "all_passed": True}, ensure_ascii=True))

if __name__ == "__main__":
    main()
