Add Site Studio themes and Theme Manager
Support categorized fragments, theme presets, block widths, and server-backed theme editing with onboarding and preview. Include Site Studio, design sources, specification and verification reports; remove the legacy studio-site example.
This commit is contained in:
@@ -0,0 +1,247 @@
|
||||
"""Compare production Site Studio with the seed in Chromium, using isolated FastAPI servers."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from playwright.sync_api import expect, sync_playwright
|
||||
from qa_001 import PASSWORD, ROOT, serve
|
||||
from qa_ai_studio import LocalURLs
|
||||
|
||||
from site_engine.build.pipeline import build_site
|
||||
from site_engine.core.registry import load_site
|
||||
from site_engine.services.auth import hash_password
|
||||
from site_engine.web.app import create_app
|
||||
|
||||
|
||||
def geometry(page, selector):
|
||||
return page.locator(selector).evaluate_all("""nodes => nodes.map(n => {
|
||||
const r=n.getBoundingClientRect(), s=getComputedStyle(n);
|
||||
return {x:r.x,y:r.y,width:r.width,height:r.height,paddingTop:s.paddingTop,
|
||||
paddingBottom:s.paddingBottom,font:s.font,background:s.backgroundImage,backgroundColor:s.backgroundColor};
|
||||
})""")
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--out", type=Path, default=Path("/tmp/site-engine-studio-002"))
|
||||
args = parser.parse_args()
|
||||
args.out.mkdir(parents=True, exist_ok=True)
|
||||
report = {"checks": [], "geometry": [], "page_errors": [], "dialogs": [], "http_errors": []}
|
||||
with tempfile.TemporaryDirectory(prefix="site-studio-002-") as tmp:
|
||||
work = Path(tmp)
|
||||
site, dist = work / "site", work / "dist"
|
||||
shutil.copytree(ROOT / "examples/site-studio", site)
|
||||
result = build_site(site, dist)
|
||||
assert result.pages == 13, result.pages
|
||||
current = dist / "current"
|
||||
# Every generated local link and asset must resolve. Redirect HTML is included.
|
||||
for html in current.rglob("*.html"):
|
||||
parsed = LocalURLs()
|
||||
parsed.feed(html.read_text())
|
||||
for url in parsed.urls:
|
||||
local = url.split("#", 1)[0].split("?", 1)[0]
|
||||
if not local:
|
||||
continue
|
||||
target = (
|
||||
current / local.lstrip("/") if local.startswith("/") else html.parent / local
|
||||
)
|
||||
assert target.is_file() or (target / "index.html").is_file(), (html, url)
|
||||
assert not (current / "cases/booking").exists()
|
||||
assert not (current / "services/automation").exists()
|
||||
assert not (current / "scenarios/real-estate").exists()
|
||||
assert not (current / "blog").exists() and not (current / "courses").exists()
|
||||
report["checks"].append(
|
||||
"13 pages, all local links/assets/redirects, draft and feature exclusion"
|
||||
)
|
||||
reference = FastAPI()
|
||||
reference.mount(
|
||||
"/", StaticFiles(directory=ROOT / "design/examples/site-studio/preview-dist", html=True)
|
||||
)
|
||||
env = {
|
||||
"SITE_ROOT": str(site),
|
||||
"SITE_DIST": str(dist),
|
||||
"SITE_STATE_DIR": str(work / "state"),
|
||||
"SITE_ADMIN_LOGIN": "admin",
|
||||
"SITE_ADMIN_PASSWORD_HASH": hash_password(PASSWORD, n=2**10),
|
||||
"SITE_SECRET_KEY": "site-studio-browser-secret-" * 3,
|
||||
"SITE_COOKIE_SECURE": "0",
|
||||
"SITE_ADMIN_ORIGIN": "",
|
||||
"SITE_PREVIEW_ORIGIN": "",
|
||||
}
|
||||
with (
|
||||
patch.dict(os.environ, env),
|
||||
serve(create_app()) as origin,
|
||||
serve(reference) as ref_origin,
|
||||
sync_playwright() as pw,
|
||||
):
|
||||
browser = pw.chromium.launch(channel="chromium", headless=True, args=["--no-sandbox"])
|
||||
page = browser.new_page(viewport={"width": 1440, "height": 1000})
|
||||
source = browser.new_page(viewport={"width": 1440, "height": 1000})
|
||||
page.on("pageerror", lambda e: report["page_errors"].append(str(e)))
|
||||
page.on("dialog", lambda d: (report["dialogs"].append(d.message), d.dismiss()))
|
||||
page.on(
|
||||
"response",
|
||||
lambda r: (
|
||||
report["http_errors"].append([r.status, r.url])
|
||||
if r.status >= 400 and "/forms/" not in r.url
|
||||
else None
|
||||
),
|
||||
)
|
||||
for width in (1440, 1280, 1024, 768, 390, 360):
|
||||
for p in (page, source):
|
||||
p.set_viewport_size({"width": width, "height": 1000})
|
||||
for route in (
|
||||
"/",
|
||||
"/about/",
|
||||
"/services/",
|
||||
"/services/bots/",
|
||||
"/request/",
|
||||
"/privacy/",
|
||||
):
|
||||
page.goto(origin + route)
|
||||
source.goto(ref_origin + route)
|
||||
page.locator("img").evaluate_all(
|
||||
"(nodes)=>Promise.all(nodes.map(n=>n.decode().catch(()=>{})))"
|
||||
)
|
||||
source.locator("img").evaluate_all(
|
||||
"(nodes)=>Promise.all(nodes.map(n=>n.decode().catch(()=>{})))"
|
||||
)
|
||||
page.evaluate("window.scrollTo(0,0)")
|
||||
source.evaluate("window.scrollTo(0,0)")
|
||||
assert page.evaluate("document.documentElement.scrollWidth <= innerWidth"), (
|
||||
width,
|
||||
route,
|
||||
)
|
||||
actual = geometry(page, "[data-engine-block]")
|
||||
expected = geometry(source, "[data-block] > :first-child")
|
||||
report["geometry"].append(
|
||||
{"width": width, "route": route, "actual": actual, "source": expected}
|
||||
)
|
||||
assert len(actual) == len(expected), (width, route, len(actual), len(expected))
|
||||
for a, b in zip(actual, expected):
|
||||
assert abs(a["x"] - b["x"]) < 1 and abs(a["width"] - b["width"]) < 1, (
|
||||
width,
|
||||
route,
|
||||
a,
|
||||
b,
|
||||
)
|
||||
if route != "/request/":
|
||||
assert (
|
||||
abs(a["y"] - b["y"]) < 1 and abs(a["height"] - b["height"]) < 1
|
||||
), (width, route, a, b)
|
||||
assert a["background"] == b["background"], (width, route, a, b)
|
||||
# Header alpha uses the theme color and may serialize as color(srgb …).
|
||||
if a != actual[0]:
|
||||
assert a["backgroundColor"] == b["backgroundColor"], (
|
||||
width,
|
||||
route,
|
||||
a,
|
||||
b,
|
||||
)
|
||||
if width in (1440, 390):
|
||||
name = route.strip("/").replace("/", "-") or "home"
|
||||
page.screenshot(
|
||||
path=str(args.out / f"public-{name}-{width}.png"), full_page=True
|
||||
)
|
||||
source.screenshot(
|
||||
path=str(args.out / f"source-{name}-{width}.png"), full_page=True
|
||||
)
|
||||
report["checks"].append(
|
||||
f"{width}px: six source/public pages, width geometry and no overflow"
|
||||
)
|
||||
page.set_viewport_size({"width": 1440, "height": 1000})
|
||||
page.goto(origin + "/services/")
|
||||
page.locator("details summary").focus()
|
||||
page.keyboard.press("Enter")
|
||||
assert page.locator("details").evaluate("(n)=>n.open")
|
||||
page.goto(origin + "/request/")
|
||||
page.locator('[name="name"]').fill("Демонстрация")
|
||||
page.locator('[name="email"]').fill("test@example.org")
|
||||
page.locator('[name="consent"]').check()
|
||||
with page.expect_response(lambda r: "/forms/form_contact" in r.url) as sent:
|
||||
page.locator(".demo-form button").click()
|
||||
assert sent.value.status == 501
|
||||
page.get_by_role("status").filter(has_text="HTTP 501").wait_for()
|
||||
assert page.locator('[name="email"]').input_value() == "test@example.org"
|
||||
assert page.url == origin + "/request/"
|
||||
report["checks"].append(
|
||||
"FAQ keyboard/native details; actual form POST=501 with visible feedback and preserved input"
|
||||
)
|
||||
page.goto(origin + "/admin/login")
|
||||
page.locator('[name="login"]').fill("admin")
|
||||
page.locator('[name="password"]').fill(PASSWORD)
|
||||
page.get_by_role("button", name="Войти", exact=True).click()
|
||||
page.wait_for_url(origin + "/admin/")
|
||||
page.goto(origin + "/admin/pages/index")
|
||||
page.bring_to_front()
|
||||
assert page.locator("#document-editor").count() or "document-editor" in page.content()
|
||||
frame = page.frame_locator("#preview-result iframe")
|
||||
expect(frame.locator(".f-hero h1")).to_have_text("Автоматизируем бизнес с помощью AI")
|
||||
expect(frame.locator(".f-hero")).to_have_attribute("data-preview-selected", "true")
|
||||
expect(frame.locator(".f-hero")).to_have_css(
|
||||
"background-image", re.compile("linear-gradient")
|
||||
)
|
||||
expect(frame.locator(".f-hero h1")).to_be_visible()
|
||||
page.wait_for_load_state("networkidle")
|
||||
page.screenshot(path=str(args.out / "constructor-1440.png"), full_page=True)
|
||||
reg = load_site(site)
|
||||
assert reg.fragments["steps"].builtin and reg.fragments["entry_card"].builtin
|
||||
report["checks"].append(
|
||||
"Real admin opens the independent example with shared steps/entry_card"
|
||||
)
|
||||
original = (site / "pages/index.md").read_bytes()
|
||||
page.locator("[data-catalog-open]").first.click()
|
||||
dialog = page.locator("#builder-catalog-dialog")
|
||||
dialog.locator("[data-catalog-search]").fill("entry_card")
|
||||
dialog.locator('[data-fragment="entry_card"]').click()
|
||||
field = page.locator('[data-inspector] [name="p.entry"]')
|
||||
expect(field).to_be_visible()
|
||||
options = page.locator(
|
||||
'[data-inspector] datalist[id="dl-p.entry"] option'
|
||||
).evaluate_all("(nodes)=>nodes.map(n=>n.value)")
|
||||
assert set(options) == {
|
||||
"services/bots",
|
||||
"services/ai",
|
||||
"scenarios/legal",
|
||||
"cases/ai-reception",
|
||||
}, options
|
||||
field.fill("services/bots")
|
||||
expect(page.locator("#document-source")).to_have_value(
|
||||
re.compile("entry: services/bots")
|
||||
)
|
||||
assert (site / "pages/index.md").read_bytes() == original
|
||||
expect(frame.locator('.f-entry_card a[href="/services/bots/"]')).to_contain_text(
|
||||
"Telegram-боты"
|
||||
)
|
||||
page.locator("[data-save-document]").click()
|
||||
expect(page.locator("#editor-result")).to_have_attribute("data-saved", "1")
|
||||
assert "entry: services/bots" in (site / "pages/index.md").read_text()
|
||||
page.reload()
|
||||
assert "entry: services/bots" in page.locator("#document-source").input_value()
|
||||
report["checks"].append(
|
||||
"entry_card: only published collection/slug choices, unsaved buffer, explicit Save and reopen"
|
||||
)
|
||||
browser.close()
|
||||
assert not report["page_errors"] and not report["dialogs"] and not report["http_errors"], report
|
||||
(args.out / "browser.json").write_text(json.dumps(report, ensure_ascii=False, indent=2))
|
||||
print(
|
||||
json.dumps(
|
||||
{"checks": report["checks"], "comparisons": len(report["geometry"])},
|
||||
ensure_ascii=False,
|
||||
indent=2,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user