#!/usr/bin/env python3 """Deterministic zero-dependency offline verifier for the published visual-qa-pack.zip 3-viewport browser QA deliverable.""" from __future__ import annotations import hashlib import json from pathlib import Path import struct import sys import zipfile import zlib EXPECTED_MEMBERS = ("README.md", "qa-1440.png", "qa-360.png", "qa-768.png", "visual-qa-report.json") PNG_SIG = b"\x89PNG\r\n\x1a\n" EXPECTED_VIEWPORTS = ((360, 800, "qa-360.png"), (768, 1024, "qa-768.png"), (1440, 900, "qa-1440.png")) def verify_png(raw: bytes) -> tuple[int, int]: if not raw.startswith(PNG_SIG): sys.exit("Invalid PNG header signature") pos = len(PNG_SIG) width = height = 0 idat_parts: list[bytes] = [] saw_iend = False while pos < len(raw): if pos + 8 > len(raw): sys.exit("Truncated PNG chunk header") length, ctype = struct.unpack(">I4s", raw[pos : pos + 8]) pos += 8 if pos + length + 4 > len(raw): sys.exit(f"Truncated PNG chunk body for {ctype!r}") cdata = raw[pos : pos + length] expected_crc = struct.unpack(">I", raw[pos + length : pos + length + 4])[0] actual_crc = zlib.crc32(ctype + cdata) & 0xFFFFFFFF if actual_crc != expected_crc: sys.exit(f"PNG CRC32 mismatch on chunk {ctype!r}") pos += length + 4 if ctype == b"IHDR": w, h, bit_depth, color_type, comp, filt, interlace = struct.unpack(">IIBBBBB", cdata) if bit_depth != 8 or color_type != 2 or comp != 0 or filt != 0 or interlace != 0: sys.exit("Unexpected PNG IHDR encoding parameters") width, height = w, h elif ctype == b"IDAT": idat_parts.append(cdata) elif ctype == b"IEND": saw_iend = True break if not saw_iend or width <= 0 or height <= 0 or not idat_parts: sys.exit("Incomplete PNG chunks") decompressed = zlib.decompress(b"".join(idat_parts)) expected_raw_len = height * (1 + width * 3) if len(decompressed) != expected_raw_len: sys.exit(f"Decompressed PNG scanline byte count {len(decompressed)} != {expected_raw_len}") return width, height def main(argv: list[str] | None = None) -> int: args = argv if argv is not None else sys.argv[1:] if len(args) != 1: sys.exit("Usage: python3 -I -B visual-qa-check.py visual-qa-pack.zip") archive_path = Path(args[0]) with zipfile.ZipFile(archive_path, "r") as zf: names = tuple(sorted(zf.namelist())) if names != EXPECTED_MEMBERS: sys.exit(f"Unexpected visual-qa-pack.zip members: {names}") report = json.loads(zf.read("visual-qa-report.json").decode("utf-8")) png_map = {name: zf.read(name) for _, _, name in EXPECTED_VIEWPORTS} viewports = report.get("viewports") if not isinstance(viewports, list) or len(viewports) != 3: sys.exit("visual-qa-report.json must contain exactly 3 viewport entries") verified_viewports: list[dict[str, object]] = [] for idx, (exp_w, exp_h, exp_file) in enumerate(EXPECTED_VIEWPORTS): vp = viewports[idx] raw_png = png_map[exp_file] actual_w, actual_h = verify_png(raw_png) if (actual_w, actual_h) != (exp_w, exp_h): sys.exit(f"{exp_file} dimensions {(actual_w, actual_h)} != {(exp_w, exp_h)}") actual_sha = hashlib.sha256(raw_png).hexdigest() if vp.get("screenshotFile") != exp_file or vp.get("width") != exp_w or vp.get("height") != exp_h: sys.exit(f"Viewport metadata mismatch on {exp_file}") if vp.get("bytes") != len(raw_png) or vp.get("sha256") != actual_sha: sys.exit(f"Screenshot digest or byte-count mismatch on {exp_file}") if vp.get("overflowPx") != 0 or vp.get("scrollWidth") != exp_w: sys.exit(f"Horizontal overflow detected on {exp_file}: overflowPx={vp.get('overflowPx')}") if vp.get("consoleErrors") != 0 or vp.get("pageErrors") != 0: sys.exit(f"Console or page errors recorded on {exp_file}") verified_viewports.append( { "viewport": f"{exp_w}x{exp_h}", "screenshotFile": exp_file, "bytes": len(raw_png), "sha256": actual_sha, "overflowPx": 0, "consoleErrors": 0, "pageErrors": 0, } ) proof = { "schema": "tesi-visual-qa-proof/v1", "targetUrl": report["targetUrl"], "archiveMembers": list(names), "allViewportsOverflowZero": True, "totalConsoleErrors": 0, "totalPageErrors": 0, "verifiedViewports": verified_viewports, } print(json.dumps(proof, indent=2, sort_keys=True)) return 0 if __name__ == "__main__": raise SystemExit(main())