"""Real publication workflow and original design in Chromium; isolated temporary sites.""" from __future__ import annotations import asyncio import json import os import shutil import tempfile from contextlib import ExitStack from pathlib import Path from unittest.mock import patch from playwright.sync_api import expect, sync_playwright from qa_001 import PASSWORD, ROOT, serve from qa_editor_updates import reserve from starlette.applications import Starlette from starlette.routing import Mount from starlette.staticfiles import StaticFiles from site_engine.build.pipeline import hash_tree from site_engine.services.auth import hash_password from site_engine.web.app import create_app from site_engine.web.preview_app import create_preview_app OUT = Path(os.environ.get("QA_PUBLICATION_OUT", "/tmp/site-engine-publication-001")) def metrics(page, selectors): return page.evaluate( """selectors=>Object.fromEntries(selectors.map(s=>{ const e=document.querySelector(s),r=e.getBoundingClientRect(),c=getComputedStyle(e); return [s,{width:r.width,font:c.fontSize,padding:c.padding,background:c.backgroundColor,columns:c.gridTemplateColumns}]; }))""", selectors, ) def main(): OUT.mkdir(parents=True, exist_ok=True) report = { "comparisons": [], "runs": [], "page_errors": [], "console_errors": [], "expected_errors": [], "native_dialogs": [], } with sync_playwright() as pw: browser = pw.chromium.launch( channel="chromium", headless=True, args=["--no-sandbox", "--no-proxy-server"] ) for interactive in (False, True): with tempfile.TemporaryDirectory(prefix="publication-qa-") as tmp, ExitStack() as stack: work = Path(tmp) site = work / "site" dist = work / "dist" shutil.copytree( ROOT / "examples/site-studio", site, ignore=shutil.ignore_patterns(".engine") ) admin_socket, preview_socket = reserve(), reserve() admin = f"http://127.0.0.1:{admin_socket.getsockname()[1]}" preview = f"http://127.0.0.1:{preview_socket.getsockname()[1]}" stack.enter_context( patch.dict( os.environ, { "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": "publication-browser-secret-" * 3, "SITE_COOKIE_SECURE": "0", "SITE_ADMIN_ORIGIN": admin if interactive else "", "SITE_PREVIEW_ORIGIN": preview if interactive else "", }, ) ) app = create_app() gate = {"delay": 0} @app.middleware("http") async def delay(request, call_next, gate=gate): if request.url.path == "/admin/publication/build" and gate["delay"]: value, gate["delay"] = gate["delay"], 0 await asyncio.sleep(value) return await call_next(request) stack.enter_context(serve(app, admin_socket)) if interactive: stack.enter_context(serve(create_preview_app(), preview_socket)) else: preview_socket.close() reference = stack.enter_context( serve( Starlette( routes=[Mount("/", StaticFiles(directory=ROOT / "design", html=True))] ) ) ) context = browser.new_context( viewport={"width": 1440, "height": 1050}, reduced_motion="reduce" ) stack.callback(context.close) page, ref = context.new_page(), context.new_page() page.set_default_timeout(12000) page.on("pageerror", lambda e: report["page_errors"].append(str(e))) def console(message): if message.type != "error": return expected = ( any(str(code) in message.text for code in (401, 403, 409, 422, 500)) or "net::ERR_FAILED" in message.text ) report["expected_errors" if expected else "console_errors"].append( {"message": message.text, "url": message.location.get("url")} ) page.on("console", console) page.on( "dialog", lambda d: (report["native_dialogs"].append(d.message), d.dismiss()) ) posts = [] page.on( "request", lambda r, posts=posts: posts.append(r.url) if r.method == "POST" else None, ) checks = [] report["runs"].append({"interactive": interactive, "checks": checks}) def done(name, checks=checks, interactive=interactive): checks.append(name) print("PASS", interactive, len(checks), name, flush=True) def ready(page=page): expect(page.locator("[data-publication-root]")).to_have_attribute( "aria-busy", "false" ) def tab(name, page=page): page.locator(f'[data-pub-tab="{name}"]').click() def action(name, page=page): page.locator(f'[data-pub-action="{name}"]').first.click() def build(page=page, action=action, ready=ready): action("candidate") ready() expect(page.locator("[data-pub-action=publish]")).to_be_visible() def publish(comment, page=page, action=action, ready=ready): action("publish") page.locator("#release-comment").fill(comment) action("confirm-publish") ready() expect(page.locator("dialog[open]")).to_have_count(0) page.goto(admin + "/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(admin + "/admin/") with page.expect_response(lambda r, admin=admin: r.url == admin + "/admin/publish") as opened: page.locator('#sidebar a[href="/admin/publish"]').click() assert opened.value.request.headers.get("hx-boosted") == "true" ready() expect(page.locator(".livebar")).to_contain_text("Публичного релиза пока нет") assert not (dist / "current").exists() done("real_empty_first_release") ref.goto(reference + "/publication-antirion.html") if not interactive: selectors = [ ".heading h1", ".livebar", ".pipe", ".panel-header", ".table th", ".table td", ".button.primary", ".tabs", ] for skin in ("light", "dark"): page.evaluate("(skin)=>{document.body.dataset.skin=skin}", skin) for width in (1440, 1280, 1024, 768, 390, 360): page.set_viewport_size({"width": width, "height": 1050}) ref.set_viewport_size({"width": width, "height": 1050}) a, b = metrics(page, selectors), metrics(ref, selectors) assert not page.evaluate( "document.documentElement.scrollWidth>innerWidth+1" ), (skin, width) for s in selectors: assert a[s]["font"] == b[s]["font"], (s, width, a[s], b[s]) assert a[s]["padding"] == b[s]["padding"], (s, width, a[s], b[s]) if skin == "light": assert a[s]["background"] == b[s]["background"], ( s, width, a[s], b[s], ) report["comparisons"].append( {"skin": skin, "width": width, "runtime": a, "reference": b} ) page.screenshot( path=str(OUT / f"publication-{skin}-{width}.png"), full_page=True ) if skin == "light": ref.screenshot( path=str(OUT / f"reference-{width}.png"), full_page=True ) page.set_viewport_size({"width": 1440, "height": 1050}) page.evaluate("document.body.dataset.skin='light'") done("original_geometry_fonts_padding_palette_responsive_12_sets") before = len(posts) page.locator("#pub-search").fill("no-such-document") expect(page.locator(".table tbody")).to_contain_text("Ничего не найдено") expect(page.locator("#pub-search")).to_be_focused() page.locator("#pub-search").fill("pages/index.md") page.locator('[data-pub-select="pages/index.md"]').click() expect(page.locator(".split>.stack").last).to_contain_text("pages/index.md") tab("checks") tab("history") tab("changes") assert len(posts) == before done("local_filters_selection_tabs_no_post") action("compare-doc") expect(page.locator("dialog[open]")).to_contain_text("Workspace") page.keyboard.press("Escape") expect(page.locator("dialog[open]")).to_have_count(0) action("doc-history") expect(page.locator("dialog[open]")).to_contain_text("предыдущих версий пока нет") action("close-modal") done("real_compare_and_empty_document_history_designer_dialog") action("validate") ready() expect(page.locator(".summary")).to_contain_text("Проверено") assert not (dist / "current").exists() done("real_validate_does_not_publish") gate["delay"] = 1.5 action("candidate") tab("activity") expect(page.locator("[data-pub-tab=activity]")).to_have_attribute( "aria-selected", "true" ) ready() tab("candidate") expect(page.locator("[data-pub-action=publish]")).to_be_enabled() candidate = json.loads((work / "state/publication/candidate.json").read_text()) assert not (dist / "current").exists() original_output = hash_tree(dist / candidate["id"]) done("background_candidate_does_not_freeze_tabs_or_activate") action("candidate-diff") expect(page.locator("dialog[open]")).to_contain_text("pages/index.md") action("close-modal") action("preview") ready() frame = page.frame_locator(".release-preview") expect(frame.locator("body")).to_contain_text("Автоматизируем") assert not page.frames[-1].locator("script").count() page.screenshot( path=str(OUT / f"candidate-preview-{interactive}.png"), full_page=True ) action("close-modal") done("immutable_candidate_diff_and_real_passive_preview") action("publish") page.locator("#release-comment").fill("Не публиковать") action("close-modal") assert not (dist / "current").exists() publish("Первый проверенный релиз") first = (dist / "current").resolve().name assert first == candidate["id"] and hash_tree(dist / first) == original_output expect(page.locator(".livebar")).to_contain_text(first) done("cancel_and_explicit_publish_exact_candidate") path = site / "pages/index.md" source = path.read_text() path.write_text(source + "\nИзменение для второго релиза.\n") tab("changes") action("refresh-workspace") ready() build() candidate = json.loads((work / "state/publication/candidate.json").read_text()) action("publish") page.locator("#release-comment").fill("Устаревший кандидат") path.write_text(path.read_text() + "\nНовая правка после просмотра.\n") action("confirm-publish") ready() expect(page.locator("#request-error")).to_contain_text("Кандидат устарел") assert (dist / "current").resolve().name == first action("close-modal") page.locator("[data-close-request-error]").click() tab("candidate") expect(page.locator("[data-pub-action=publish]")).to_be_disabled() done("real_stale_candidate_409_visible_and_live_unchanged") build() publish("Второй релиз") second = (dist / "current").resolve().name assert second != first source_hash = hash_tree(site) page.locator(f'[data-pub-action=rollback][data-release="{first}"]').click() page.screenshot(path=str(OUT / f"rollback-{interactive}.png"), full_page=True) action("close-modal") assert (dist / "current").resolve().name == second page.locator(f'[data-pub-action=rollback][data-release="{first}"]').click() action("rollback-confirm") ready() assert (dist / "current").resolve().name == first and hash_tree(site) == source_hash done("rollback_cancel_confirm_preserves_workspace") page.goto(admin + "/admin/publish") ready() expect(page.locator(".livebar")).to_contain_text(first) tab("activity") expect(page.locator(".timeline")).to_contain_text("release_rollback") tab("history") page.locator(f'[data-pub-action=release-details][data-release="{second}"]').click() expect(page.locator("dialog[open]")).to_contain_text("Второй релиз") action("close-modal") done("reload_real_history_comments_and_audit") with patch( "site_engine.core.render.Renderer.render", side_effect=RuntimeError("QA render failure"), ): action("candidate") ready() expect(page.locator("#request-error")).to_contain_text("Внутренняя ошибка сервера") expect(page.locator("#publication-panel")).to_contain_text("Ошибка сборки") action("build-log") expect(page.locator("dialog[open]")).to_contain_text("ID запроса") action("close-modal") assert (dist / "current").resolve().name == first page.locator("[data-close-request-error]").click() page.route("**/admin/publication/build", lambda route: route.abort()) action("candidate") ready() expect(page.locator("#request-error")).to_contain_text("Нет связи с сервером") page.unroute("**/admin/publication/build") page.locator("[data-close-request-error]").click() build() done("500_network_visible_logged_and_recoverable") page.goto(admin + "/admin/pages/index") expect(page.locator(".builder")).to_be_visible() page.goto(admin + "/admin/content/services/bots") expect(page.locator(".entry-workspace")).to_be_visible() page.goto(admin + "/admin/theme") expect(page.locator(".theme-content")).to_be_visible() page.goto(admin + "/admin/publish") ready() tab("checks") page.locator("[data-pub-tab=checks]").focus() page.keyboard.press("ArrowRight") expect(page.locator("[data-pub-tab=candidate]")).to_be_focused() page.locator("[data-action=profile]").click() page.locator("#profile-dialog button[type=submit]").click() expect(page.locator("input[name=login]")).to_be_visible() done("accepted_screens_keyboard_and_logout") browser.close() (OUT / "report.json").write_text(json.dumps(report, ensure_ascii=False, indent=2)) assert not report["page_errors"], report["page_errors"] assert not report["console_errors"], report["console_errors"] assert not report["native_dialogs"], report["native_dialogs"] print( json.dumps( { "comparisons": len(report["comparisons"]), "runs": report["runs"], "errors": report["page_errors"], }, ensure_ascii=False, indent=2, ) ) if __name__ == "__main__": main()