#!/usr/bin/env python3
import hashlib,json,collections,sys
from pathlib import Path
b=Path(sys.argv[1]).read_bytes()
sha=lambda x:hashlib.sha256(x).hexdigest()
assert sha(b)=="19b39154b7452809260bb3fb248971f2b871b6a38be9fc3c669ba4a7872f308c", "raw-file SHA-256 mismatch"
last="0"*64;counts=collections.Counter();passed=0
for line in b.splitlines():
 r=json.loads(line);digest=r.pop("entry_sha256")
 assert r["prev_sha256"]==last
 assert sha(json.dumps(r,sort_keys=True,separators=(",",":")).encode())==digest
 last=digest;counts[r["module"],r["scenario"]]+=1;passed+=(r["ok"] is True)
assert len(counts)==24 and all(n==30 for n in counts.values()) and passed==720
print("PASS reference-only 720/720; file and chained record integrity")
