#!/usr/bin/env python3 """Deterministic offline verifier for the published print-quote.zip frontend prototype.""" from __future__ import annotations import hashlib import json from pathlib import Path import re import subprocess import sys import tempfile import zipfile EXPECTED_MEMBERS = ("README.md", "app.js", "index.html", "styles.css") REQUIRED_CONTROLS = ("Quantity", "Paper", "Rush", "Save quote", "Edit quote", "Reset demo") 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 print-quote-check.py print-quote.zip") archive_path = Path(args[0]) raw = archive_path.read_bytes() archive_sha = hashlib.sha256(raw).hexdigest() with zipfile.ZipFile(archive_path, "r") as zf: names = tuple(sorted(zf.namelist())) if names != EXPECTED_MEMBERS: sys.exit(f"Unexpected ZIP members: {names}") html = zf.read("index.html").decode("utf-8") css = zf.read("styles.css").decode("utf-8") js = zf.read("app.js").decode("utf-8") # 1. Verify zero external URLs in HTML/CSS/JS external_urls = [ u for u in re.findall(r"https?://[^\s\"'<>)]+", html + "\n" + css + "\n" + js) if not u.startswith("http://127.0.0.1") ] if external_urls: sys.exit(f"Unexpected external URLs found: {external_urls}") # 2. Verify all 6 required accessible control labels/buttons exist in index.html missing_controls = [label for label in REQUIRED_CONTROLS if label not in html] if missing_controls: sys.exit(f"Missing required controls: {missing_controls}") # 3. Verify exact cent pricing & half-up rounding via Node against extracted app.js with tempfile.TemporaryDirectory() as tmp: app_file = Path(tmp) / "app.cjs" app_file.write_text(js, encoding="utf-8") node_probe = """ const assert = require('node:assert/strict'); const { computeQuoteCents, formatUsd } = require(process.argv[1]); const c1 = computeQuoteCents(100, 'matte', false); const c2 = computeQuoteCents(25, 'matte', true); const c3 = computeQuoteCents(27, 'linen', true); // 27*65 + 1200 = 2955; 25% = 738.75 -> 739 half-up => 3694 const badLow = computeQuoteCents(24, 'matte', false); const badHigh = computeQuoteCents(501, 'linen', false); assert.equal(badLow, null); assert.equal(badHigh, null); console.log(JSON.stringify({ default100MatteUsd: formatUsd(c1.totalCents), min25MatteRushUsd: formatUsd(c2.totalCents), odd27LinenRushCents: c3, invalidBoundsRejected: true })); """ math_out = json.loads( subprocess.check_output(["node", "-e", node_probe, str(app_file)], text=True) ) report = { "schema": "tesi-print-quote-proof/v1", "archiveMembers": list(names), "externalNetworkDependencies": 0, "accessibleControlsVerified": list(REQUIRED_CONTROLS), "responsiveBreakpointPx": 720, "quoteChecks": math_out, } print(json.dumps(report, indent=2, sort_keys=True)) return 0 if __name__ == "__main__": raise SystemExit(main())