from pathlib import Path
import argparse
import copy
import csv
import hashlib
import io
import json
import re

parser = argparse.ArgumentParser()
parser.add_argument("--actual")
parser.add_argument("--local-http", action="store_true")
args = parser.parse_args()
checks = []
def check(condition, label):
    if not condition:
        raise ValueError(label)
    checks.append(label)
def load(name): return json.loads(Path(name).read_text())
def rows(name): return list(csv.DictReader(io.StringIO(Path(name).read_text(encoding="utf-8-sig"))))
def negative(fn, value, label):
    try: fn(value)
    except (ValueError, AssertionError, KeyError, TypeError, IndexError): checks.append(label)
    else: raise AssertionError("Negative input accepted: " + label)
v1=Path("handbook.md").read_text();v2=Path("handbook-v2-reference.md").read_text()
check("QR-SYNC-V1" in v1 or "QR-SYNC-V2" in v1,"source file has known revision")
def validate(v):
    if "QR-SYNC-V2" not in v or "周四" not in v or "设备数量：12台" not in v or "QR-SYNC-V1" in v: raise ValueError("new revision with protected fact required")
validate(v2);checks.append("manual revised reference preserves 12 devices")
negative(validate,v2.replace("12台","18台"),"unrelated device mutation rejected")
if args.actual: validate(Path(args.actual).read_text());checks.append("local source changed; background sync not proved by this check")
report={"layer":"offline/material checks, not proof of unrun product or model", "checks":checks, "count":len(checks)}
Path("verification.json").write_text(json.dumps(report,ensure_ascii=False,indent=2))
print(json.dumps(report,ensure_ascii=False))
