Files
aigc-home/scripts/hel842/capture.py
T

201 lines
7.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Reproducible screenshots of built product against isolated real backend/fake HTTP LLM."""
from __future__ import annotations
import argparse
import json
import os
import subprocess
import sys
import time
import urllib.request
from pathlib import Path
from playwright.sync_api import sync_playwright
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "scripts/hel840"))
sys.path.insert(0, str(ROOT / "scripts/hel842"))
p = argparse.ArgumentParser()
p.add_argument("--output", type=Path, required=True)
p.add_argument("--quick", action="store_true")
p.add_argument("--states-only", action="store_true")
p.add_argument("--motion-only", action="store_true")
args = p.parse_args()
out = args.output.resolve()
out.mkdir(parents=True, exist_ok=True)
data = out / "fixture"
data.mkdir(exist_ok=True)
log = (out / "server.log").open("w")
proc = subprocess.Popen(
[
str(ROOT / "calliope-backend/.venv/bin/python"),
str(ROOT / "scripts/hel840/serve_fixture.py"),
"--data",
str(data),
"--port",
"18740",
],
stdout=log,
stderr=subprocess.STDOUT,
cwd=ROOT,
)
(out / "owned-process.json").write_text(
json.dumps(
{
"pid": proc.pid,
"purpose": "isolated fixture backend",
"cleanup": "foreground capture.py finally terminates exact child",
}
)
)
try:
for _ in range(100):
if proc.poll() is not None:
raise RuntimeError((out / "server.log").read_text()[-4000:])
try:
with urllib.request.urlopen(
"http://127.0.0.1:18740/api/health", timeout=1
) as response:
if response.status == 200:
break
except OSError:
time.sleep(0.1)
else:
raise RuntimeError("backend not ready")
with sync_playwright() as pw:
browser = pw.chromium.launch(
headless=True,
args=["--no-sandbox"],
env={
**os.environ,
"LD_LIBRARY_PATH": os.environ.get(
"HEL840_BROWSER_LIBS",
str(
ROOT
/ "evidence/hel840/browser-runtime/libs/usr/lib/x86_64-linux-gnu"
),
),
},
)
context = browser.new_context(
viewport={"width": 1440, "height": 900},
record_video_dir=str(out / "recordings"),
record_video_size={"width": 1440, "height": 900},
)
page = context.new_page()
errors = []
page.on("pageerror", lambda e: errors.append(str(e)))
themes = (
[]
if args.states_only or args.motion_only
else ["night"]
if args.quick
else ["night", "day", "grade", "bluehour"]
)
sizes = [(1440, 900)] if args.quick else [(1440, 900), (1366, 768), (1280, 720)]
metrics = []
for theme in themes:
for width, height in sizes:
page.set_viewport_size({"width": width, "height": height})
page.goto("http://127.0.0.1:18740/settings")
page.evaluate(
'(theme)=>localStorage.setItem("aigc-theme",theme)', theme
)
for label, route in [
("02-素材库", "/library"),
("03-模型连接", "/connections?tab=llm"),
]:
page.goto("http://127.0.0.1:18740" + route)
page.wait_for_timeout(700)
if label.startswith("03"):
for n in range(2):
page.locator(".mrow").nth(n).get_by_role(
"button", name="测试", exact=True
).click()
page.locator(".mrow").nth(n).locator(".spill.ok").wait_for()
page.get_by_role("button", name="编辑", exact=True).nth(
1
).click()
page.wait_for_timeout(150)
assert page.locator(".mlist").bounding_box()["height"] >= 212
assert page.locator(".mrow").first.bounding_box()["y"] >= 44
if label.startswith("04"):
page.locator("#desk-prompt").wait_for()
page.locator("#desk-prompt").fill(
"一只猫,柔和侧光,<lora:cat:0.7>,(neon:1.2)"
)
page.locator(".pt-buttons").first.get_by_role(
"button"
).first.click()
page.locator(".pt-res").wait_for()
folder = out / "matrix" / theme
folder.mkdir(parents=True, exist_ok=True)
page.screenshot(path=str(folder / f"{label}-{width}x{height}.png"))
metrics.append(
{
"theme": theme,
"width": width,
"height": height,
"page": label,
"dom": page.evaluate(
"""()=>{const rect=s=>{const n=document.querySelector(s);if(!n)return null;const r=n.getBoundingClientRect(),c=getComputedStyle(n);return {x:r.x,y:r.y,width:r.width,height:r.height,bottom:r.bottom,font:c.font,fontFamily:c.fontFamily,color:c.color,background:c.backgroundColor}};return {overflow:document.documentElement.scrollWidth>innerWidth,theme:document.documentElement.dataset.theme,main:rect('main'),resume:rect('.resume'),library:rect('.lib2'),filter:rect('.mg-side'),detail:rect('.lib-detail'),models:rect('.mdl'),modelList:rect('.mlist'),modelRow:rect('.mrow'),modelDetail:rect('.mdl-detail'),band:rect('.ptband'),dock:rect('.studio-form'),media:rect('.stage-media .print'),generate:rect('.gen')}}"""
),
}
)
(out / "geometry.json").write_text(
json.dumps(metrics, ensure_ascii=False, indent=2)
)
from controls import verify_controls
verify_controls(page, data, out)
if args.motion_only:
from motion import record_motion
record_motion(page, data, out)
elif not args.quick:
from exercise import exercise
exercise(page, data, out)
(out / "browser-errors.json").write_text(
json.dumps(errors, ensure_ascii=False, indent=2)
)
print(
json.dumps(
{
"screenshots": len(metrics),
"browser_errors": errors,
"output": str(out),
},
ensure_ascii=False,
),
flush=True,
)
context.close()
browser.close()
finally:
# Compare against the workspace daemon before terminating only our own child.
raw = subprocess.check_output(
["multica", "daemon", "status", "--output", "json"], text=True
)
(out / "daemon-status.json").write_text(raw)
daemon = json.loads(raw)
def pids(obj):
if isinstance(obj, dict):
return [v for k, v in obj.items() if k in ("pid", "process_id")] + [
n for v in obj.values() for n in pids(v)
]
if isinstance(obj, list):
return [n for v in obj for n in pids(v)]
return []
if proc.pid not in pids(daemon):
proc.terminate()
try:
proc.wait(timeout=10)
except subprocess.TimeoutExpired:
proc.kill()
proc.wait(timeout=5)
log.close()