Files
aigc-home/scripts/hel845/verify.py
T
2026-10-25 02:13:53 +08:00

292 lines
13 KiB
Python

"""Compare approved controls with an isolated build; no live services or paid calls."""
import argparse
import json
import os
import shutil
import subprocess
import time
import urllib.request
from pathlib import Path
from PIL import Image
from playwright.sync_api import expect, sync_playwright
ROOT = Path(__file__).resolve().parents[2]
p = argparse.ArgumentParser()
p.add_argument("--approved", type=Path, required=True)
p.add_argument("--output", type=Path, required=True)
p.add_argument(
"--python", required=True, help="Existing backend dependency environment"
)
a = p.parse_args()
out = a.output.resolve()
out.mkdir(parents=True, exist_ok=True)
data = out / "fixture"
assert not data.exists(), "Use a fresh isolated output directory"
env = {**os.environ, "PYTHONPATH": str(ROOT / "calliope-backend/src")}
metrics_js = """buttons => buttons.map(n => {
const c = getComputedStyle(n), svg = n.querySelector('svg');
return {background:c.backgroundColor, color:c.color, shadow:c.boxShadow,
icon:svg ? getComputedStyle(svg).color : null, radius:c.borderRadius,
width:n.getBoundingClientRect().width, height:n.getBoundingClientRect().height};
})"""
def contrast(bg, fg):
import re
def lum(s):
rgb = [float(x) / 255 for x in re.findall(r"[\d.]+", s)[:3]]
linear = [
v / 12.92 if v <= 0.04045 else ((v + 0.055) / 1.055) ** 2.4 for v in rgb
]
return sum(v * w for v, w in zip(linear, [0.2126, 0.7152, 0.0722]))
x, y = sorted([lum(bg), lum(fg)])
return round((y + 0.05) / (x + 0.05), 4)
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 []
log = (out / "server.log").open("w")
proc = subprocess.Popen(
[
a.python,
str(ROOT / "scripts/hel840/serve_fixture.py"),
"--data",
str(data),
"--port",
"18740",
],
cwd=ROOT,
env=env,
stdout=log,
stderr=subprocess.STDOUT,
)
(out / "owned-process.json").write_text(json.dumps({"pid": proc.pid, "port": 18740}))
rows, errors = [], []
try:
for _ in range(100):
if proc.poll() is not None:
raise RuntimeError((out / "server.log").read_text()[-3000:])
try:
urllib.request.urlopen(
"http://127.0.0.1:18740/api/health", timeout=1
).close()
break
except OSError:
time.sleep(0.1)
else:
raise RuntimeError("Fixture not ready")
with sync_playwright() as pw:
browser = pw.chromium.launch(headless=True, args=["--no-sandbox"])
try:
for theme in ["day", "night", "grade", "bluehour"]:
for w, h in [(1440, 900), (1366, 768), (1280, 720)]:
context = browser.new_context(viewport={"width": w, "height": h})
page, ref = context.new_page(), context.new_page()
page.on("pageerror", lambda e: errors.append(str(e)))
page.goto("http://127.0.0.1:18740/library")
page.evaluate("t => localStorage.setItem('aigc-theme',t)", theme)
page.reload()
page.locator(".lt").first.wait_for()
expect(page.locator("html")).to_have_attribute("data-theme", theme)
assert page.url.endswith("/library")
ref.goto(
(a.approved / "HEL-837-交互样本-v2.html").resolve().as_uri()
+ f"?theme={theme}#library"
)
ref.locator("#p-library.on").wait_for()
expect(ref.locator("html")).to_have_attribute("data-theme", theme)
for fill in [False, True]:
for dense in [False, True]:
fit = page.get_by_role(
"button", name="铺满" if fill else "完整", exact=True
)
fit.focus()
page.keyboard.press("Enter")
expect(fit).to_have_attribute("aria-pressed", "true")
density = page.get_by_role(
"button",
name="紧凑网格,仅缩略图" if dense else "标准网格",
exact=True,
)
density.focus()
page.keyboard.press("Space")
expect(density).to_have_attribute("aria-pressed", "true")
assert density.evaluate(
"n => n === document.activeElement && n.matches(':focus-visible')"
)
focus = density.evaluate(
"n => {const c=getComputedStyle(n);return {outline:c.outline,offset:c.outlineOffset,shadow:c.boxShadow}}"
)
assert (
"none" not in focus["outline"]
and "0px" not in focus["outline"]
)
grid = page.locator(".lgrid").first
classes = grid.get_attribute("class").split()
assert ("fill" in classes) == fill and (
"dense" in classes
) == dense
assert page.locator(".mat img").first.evaluate(
"n=>getComputedStyle(n).objectFit"
) == ("cover" if fill else "contain")
ref.locator(
f'[data-fit="{"fill" if fill else "contain"}"]'
).click()
ref.locator(
f'[data-dens="{"dense" if dense else "std"}"]'
).click()
page.wait_for_timeout(300)
ref.wait_for_timeout(300)
actual = page.locator(
".lib-segment button.on"
).evaluate_all(metrics_js)
original = ref.locator(
"#p-library .seg button.on"
).evaluate_all(metrics_js)
assert len(actual) == len(original) == 2
if theme == "day":
for x, y in zip(actual, original):
for key in [
"background",
"color",
"shadow",
"icon",
]:
assert x[key] == y[key], (theme, key, x, y)
assert contrast(x["background"], x["color"]) >= 4.5
# Reapply the exact pre-fix local tokens only inside the browser.
# This proves other themes are unchanged without changing source/build.
old = page.add_style_tag(
content='[data-theme="day"] .lib2 {--lib-option-bg:var(--hi);--lib-option-shadow:0 1px 2px rgb(var(--ink) / .14),0 0 0 1px rgb(var(--ink) / .06)}'
)
page.wait_for_timeout(300)
before = page.locator(
".lib-segment button.on"
).evaluate_all(metrics_js)
old.evaluate("n=>n.remove()")
page.wait_for_timeout(300)
if theme != "day":
assert before == actual
else:
assert before[0]["background"] == "rgb(20, 17, 9)"
assert (
contrast(
before[0]["background"], before[0]["color"]
)
< 1.2
)
assert not page.evaluate(
"document.documentElement.scrollWidth > innerWidth"
)
stem = f"{theme}-{w}x{h}-fill{int(fill)}-dense{int(dense)}"
if theme == "day" and w == 1440:
page.locator(".lib-tools").screenshot(
path=str(out / (stem + "-keyboard-focus.png"))
)
density.evaluate("n=>n.blur()")
ref.locator("#p-library .seg button").evaluate_all(
"buttons=>buttons.forEach(n=>n.blur())"
)
actual_path, ref_path = (
out / (stem + "-actual.png"),
out / (stem + "-approved.png"),
)
page.locator(".lib-tools").screenshot(path=str(actual_path))
ref.locator("#p-library .lib-tools").screenshot(
path=str(ref_path)
)
with (
Image.open(ref_path) as left,
Image.open(actual_path) as right,
):
pair = Image.new(
"RGB",
(
max(left.width, right.width),
left.height + right.height,
),
"white",
)
pair.paste(left, (0, 0))
pair.paste(right, (0, left.height))
pair.save(out / (stem + "-pair.png"))
rows.append(
{
"theme": theme,
"viewport": [w, h],
"fill": fill,
"dense": dense,
"url": page.url,
"actual": actual,
"approved": original,
"before_local_mapping": before,
"focus": focus,
"contrast": contrast(
actual[0]["background"], actual[0]["color"]
),
}
)
if theme == "day" and w == 1440:
page.screenshot(path=str(out / (stem + "-page.png")))
select = page.get_by_role("button", name="选择", exact=True)
select.focus()
page.keyboard.press("Enter")
expect(select).to_have_attribute("aria-pressed", "true")
page.get_by_role("button", name="图片", exact=False).first.click()
expect(page.locator(".lt video")).to_have_count(0)
page.get_by_role("combobox", name="排序").select_option("size")
page.get_by_role("textbox", name="文件名查找").fill("样本素材-01")
expect(page.locator(".lt")).to_have_count(1)
page.locator(".lt .ck").first.click()
expect(page.locator(".selbar")).to_be_visible()
context.close()
finally:
browser.close()
assert not errors, errors
(out / "values.json").write_text(json.dumps(rows, ensure_ascii=False, indent=2))
(out / "browser-errors.json").write_text(json.dumps(errors))
print(
json.dumps(
{
"states": len(rows),
"browser_errors": errors,
"day_contrast": rows[0]["contrast"],
}
)
)
finally:
raw = subprocess.check_output(
["multica", "daemon", "status", "--output", "json"], text=True
)
(out / "daemon-status.json").write_text(raw)
assert proc.pid not in pids(json.loads(raw)), "Refuse to terminate workspace daemon"
proc.terminate()
try:
proc.wait(timeout=10)
except subprocess.TimeoutExpired:
proc.kill()
proc.wait(timeout=5)
log.close()
shutil.rmtree(data, ignore_errors=True)
(out / "cleanup.json").write_text(
json.dumps(
{
"pid": proc.pid,
"returncode": proc.returncode,
"fixture_removed": not data.exists(),
}
)
)