#!/usr/bin/env python3
"""Validate interactive publication structure, provenance, and hashes."""
from pathlib import Path
from html.parser import HTMLParser
from urllib.parse import urlsplit, unquote
import hashlib, json, re
ROOT=Path(__file__).resolve().parents[1]
SOURCE=ROOT.parent/"world_lobby_four_trajectory_20260929"
METHODS=("M1","M2","M3","M4"); FRAMES=(0,36,72,108,144)
GT_SHA="6c28ab067c63c5b1fce19c6a972f3d66c7ac4d9bd9ce3b5a57e2e18f5641fe8f"
GT_BYTES=24816108
def sha(path): return hashlib.sha256(path.read_bytes()).hexdigest()
def mm(a,b): return [[sum(a[i][k]*b[k][j] for k in range(4)) for j in range(4)] for i in range(4)]
class Doc(HTMLParser):
def __init__(self,path):
super().__init__(); self.path=path; self.ids=set(); self.links=[]; self.tags=[]; self.meta=[]; self.canon=[]
self.feed(path.read_text(encoding="utf-8"))
def handle_starttag(self,tag,attrs):
a=dict(attrs); self.tags.append(tag)
if "id" in a:
assert a["id"] not in self.ids, f"duplicate id {a['id']}"; self.ids.add(a["id"])
for key in ("href","src"):
if key in a: self.links.append(a[key])
if tag=="meta" and a.get("property","").startswith("og:"): self.meta.append(a["property"])
if tag=="link" and a.get("rel")=="canonical": self.canon.append(a.get("href"))
tables=json.loads((ROOT/"data/tables_1_7.json").read_text())
assert list(tables["tables"])==[f"table{i}" for i in range(1,8)]
for key,rows in tables["tables"].items(): assert [r["method"] for r in rows]==list(METHODS),key
m4_table2=next(row for row in tables["tables"]["table2"] if row["method"]=="M4")
assert m4_table2["ate_m"]=="-" and m4_table2["rotation_deg"]=="-"
docs={name:Doc(ROOT/name) for name in ("index.html","en.html")}
required={"results","motivation","workflow","more-results","related-work","limitations","citation","references","office-cafe",
"model-select","scene-mode","scene-reset","scene-split","scene-retry","compare-method","compare-frame",
*{f"table-{i}" for i in range(1,8)},*{f"figure-{i}" for i in range(1,7)}}
for name,doc in docs.items():
assert required<=doc.ids,f"{name}: missing {required-doc.ids}"
assert "model-viewer" not in doc.tags and "model-viewer" not in (ROOT/name).read_text().lower()
assert len(doc.canon)==1 and doc.canon[0].startswith("https://")
assert {"og:title","og:description","og:image","og:url"}<=set(doc.meta)
for link in doc.links:
u=urlsplit(link)
if u.scheme or u.netloc: continue
target=(ROOT/unquote(u.path)).resolve() if u.path else ROOT/name
assert target.is_relative_to(ROOT) and target.exists(),f"broken link {name}: {link}"
if u.fragment and target.suffix==".html":
target_doc=docs.get(target.name) or Doc(target)
assert unquote(u.fragment) in target_doc.ids,f"broken anchor {link}"
html=(ROOT/name).read_text()
assert all(f"Figure {i}." in html for i in range(1,7))
assert html.index('id="table-2"') < html.index('id="figure-2"')
assert html.index('id="figure-4"') < html.index('id="figure-5"')
assert 'assets/fixed_views/GT/000.png' in html and 'assets/fixed_views/M1/000.png' in html
assert 'src="https://office-cafe-vipe.hiwtishere.chatgpt.site/"' in html
assert 'href="https://office-cafe-vipe.hiwtishere.chatgpt.site/shake.html"' in html
assert 'type="importmap"' in html and 'scene-compare.js' in html
assert 'name="twitter:card" content="summary_large_image"' in html
assert 'name="twitter:image" content="https://wentingw.github.io/agentic-world-blog/assets/teaser_originals.png"' in html
assert '' in html
section_order=[html.index(f'