NeuralOS-Hypervisor / test_zerodom_chrome.py
AEUPH's picture
v3: Text2OS image OS - layer compositor, zero-DOM canvas client, dirty-tile binary protocol, generative sprite atlas
a31e333 verified
Raw History Blame Contribute Delete
3.83 kB
"""ZERO-DOM acceptance test via headless Chrome.
1. asserts the served page has NO DOM UI (no div/img/button/input/span), only
the single framebuffer <canvas>;
2. screenshots the running OS and asserts real pixels were produced (the page
is not black, and a Luna-blue taskbar band exists at the bottom).
"""
import os
import re
import subprocess
import sys
import time
import urllib.request
import json
CHROME = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
PORT = 7866
BASE = f"http://127.0.0.1:{PORT}"
HERE = os.path.dirname(os.path.abspath(__file__))
def wait_up():
for _ in range(80):
try:
with urllib.request.urlopen(f"{BASE}/healthz", timeout=2) as r:
json.loads(r.read())
return True
except Exception:
time.sleep(0.25)
return False
def chrome(args, timeout=60):
cmd = [CHROME, "--headless=new", "--no-first-run", "--no-default-browser-check",
"--disable-gpu", "--hide-scrollbars",
"--user-data-dir=" + os.path.join(HERE, ".chrome-test"), *args]
return subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
def main():
env = dict(os.environ, NEURALOS_ENGINE="mock", PORT=str(PORT),
NEURALOS_CACHE="/tmp/neuralos_dom_cache")
proc = subprocess.Popen([sys.executable, "app.py"], env=env,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
try:
assert wait_up(), "server did not start"
# ---- 1. DOM audit ----
dom = chrome(["--dump-dom", "--virtual-time-budget=6000", BASE]).stdout
# strip <script>/<style> bodies so JS comments/strings can't be miscounted
markup = re.sub(r"<style.*?</style>", "", dom, flags=re.S | re.I)
markup = re.sub(r"<script.*?</script>", "", markup, flags=re.S | re.I)
counts = {tag: len(re.findall(rf"<\s*{tag}\b", markup, re.I))
for tag in ("canvas", "div", "img", "button", "input", "span", "svg", "iframe", "a")}
print("DOM tag counts (markup only):", counts)
assert counts["canvas"] == 1, f"expected exactly one canvas element, got {counts['canvas']}"
for tag in ("div", "img", "button", "input", "span", "svg", "iframe"):
assert counts[tag] == 0, f"ZERO-DOM violated: found <{tag}>"
body = re.search(r"<body[^>]*>(.*)</body>", markup, re.S | re.I)
inner = body.group(1) if body else markup
inner = re.sub(r"<canvas[^>]*>\s*</canvas>", "", inner, flags=re.S | re.I)
assert inner.strip() == "", f"unexpected DOM content: {inner[:200]!r}"
print("ZERO-DOM: body contains only one canvas and no other elements")
# ---- 2. render audit ----
shot = os.path.join(HERE, "zerodom_shot.png")
if os.path.exists(shot):
os.remove(shot)
chrome([f"--screenshot={shot}", "--window-size=1000,1000",
"--virtual-time-budget=8000", BASE])
assert os.path.exists(shot), "no screenshot produced"
from PIL import Image
import numpy as np
img = Image.open(shot).convert("RGB")
a = np.asarray(img)
nonblack = (a.sum(axis=2) > 30).mean()
bottom = a[int(a.shape[0] * 0.93):, :, :]
blueish = ((bottom[:, :, 2].astype(int) > bottom[:, :, 0].astype(int) + 25)).mean()
print(f"screenshot {img.size}, non-black {nonblack:.1%}, bottom-blue {blueish:.1%}")
assert nonblack > 0.5, "framebuffer looks blank"
assert blueish > 0.3, "no Luna-blue taskbar band rendered"
print("RENDER OK: pixels produced, taskbar present")
print("ZERO-DOM E2E OK")
finally:
proc.terminate()
try:
proc.wait(10)
except Exception:
proc.kill()
if __name__ == "__main__":
main()