Files
leleka/scripts/e2e/codex/tasks/ttt/accept.py
T

48 lines
3.0 KiB
Python

#!/usr/bin/env python3
"""Static acceptance for the Three.js tic-tac-toe task (no browser on the server).
accept.py WORKSPACE exit 0 = every check passed
The six real-mouse browser checks (scripts/e2e/codex/check_ttt.mjs) need Node + Playwright and run afterwards over the stored workspaces.
This script only reads the workspace.
"""
import hashlib
import os
import re
import sys
ws = os.path.abspath(sys.argv[1])
results = []
def check(name, ok, detail=""):
results.append(bool(ok))
print(("PASS " if ok else "FAIL ") + name + ("" if ok else " :: " + str(detail)[:300]), flush=True)
THREE_SHA = "08fd7545d13d2c7fb65ab691530a802dafefd638596501854f267d0fb13c39e7"
path = os.path.join(ws, "index.html")
check("index.html exists", os.path.isfile(path))
html = open(path, encoding="utf-8", errors="replace").read() if os.path.isfile(path) else ""
check("index.html is not trivial (>= 1500 bytes)", len(html.encode()) >= 1500, len(html))
three = os.path.join(ws, "three.module.min.js")
check("vendored three.module.min.js still present and unmodified",
os.path.isfile(three) and hashlib.sha256(open(three, "rb").read()).hexdigest() == THREE_SHA)
check("imports the vendored build by a relative path", re.search(r"""from\s+['"](\./)?three\.module\.min\.js['"]""", html) or
re.search(r"""import\(\s*['"](\./)?three\.module\.min\.js['"]""", html))
check("loads nothing from the network", not re.search(r"""(src|href)\s*=\s*['"]https?://""", html) and not re.search(r"""(from|import)\s*\(?\s*['"]https?://""", html))
check("uses a WebGL renderer", "WebGLRenderer" in html)
check("creates at least two kinds of 3D meshes (X and O)", len(re.findall(r"\bMesh\s*\(", html)) >= 2)
check("raycasting for clicks", "Raycaster" in html and "setFromCamera" in html)
check("click or pointer listener", re.search(r"""addEventListener\(\s*['"](click|pointerdown|mousedown|pointerup|mouseup)['"]""", html) or re.search(r"\bon(click|pointerdown|mousedown)\s*=", html))
check('element with id="status"', re.search(r"""id\s*=\s*['"]status['"]""", html) or "getElementById('status')" in html or 'getElementById("status")' in html)
check('button with id="restart"', re.search(r"""<button[^>]*id\s*=\s*['"]restart['"]""", html) or re.search(r"""id\s*=\s*['"]restart['"]""", html))
check("win and draw logic present", re.search(r"win", html, re.I) and re.search(r"draw", html, re.I) and
(len(re.findall(r"\[\s*\d\s*,\s*\d\s*,\s*\d\s*\]", html)) >= 8 or re.search(r"for\s*\(", html)))
check("status strings", re.search(r"turn", html, re.I) and re.search(r"wins", html, re.I))
script = "\n".join(re.findall(r"<script[^>]*>(.*?)</script>", html, re.S))
check("inline script present, brackets balanced", script.strip() != "" and all(script.count(a) == script.count(b) for a, b in ("()", "[]", "{}")),
"%d/%d %d/%d %d/%d" % (script.count("("), script.count(")"), script.count("["), script.count("]"), script.count("{"), script.count("}")))
print("SUMMARY %d/%d" % (sum(results), len(results)))
sys.exit(0 if all(results) else 1)