"""Original Content design versus real FastAPI: geometry, flows and both preview origins.""" 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 idle, reserve from starlette.applications import Starlette from starlette.routing import Mount from starlette.staticfiles import StaticFiles from site_engine.build.pipeline import build_site 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_CONTENT_OUT", "/tmp/site-engine-content-001")) def metrics(page, selectors): return page.evaluate( """selectors=>Object.fromEntries(selectors.map(s=>{ const e=document.querySelector(s),c=getComputedStyle(e),r=e.getBoundingClientRect(); return [s,{width:r.width,height:r.height,font:c.fontSize,padding:c.padding, color:c.color,background:c.backgroundColor,columns:c.gridTemplateColumns}]; }))""", selectors, ) def main(): OUT.mkdir(parents=True, exist_ok=True) report = { "runs": [], "comparisons": [], "page_errors": [], "native_dialogs": [], "console_errors": [], "expected_errors": [], } with sync_playwright() as pw: browser = pw.chromium.launch( channel="chromium", headless=True, args=["--no-sandbox", "--no-proxy-server"] ) for interactive in (False, True): run = {"interactive": interactive, "checks": []} report["runs"].append(run) with ( tempfile.TemporaryDirectory(prefix="content-design-qa-") as tmp, ExitStack() as stack, ): work, site = Path(tmp), Path(tmp) / "site" shutil.copytree( ROOT / "examples/site-studio", site, ignore=shutil.ignore_patterns(".engine") ) original_file = (site / "content/services/bots.md").read_bytes() build_site(site, work / "dist") admin_socket, preview_socket = reserve(), reserve() admin = "http://127.0.0.1:" + str(admin_socket.getsockname()[1]) preview = "http://127.0.0.1:" + str(preview_socket.getsockname()[1]) stack.enter_context( patch.dict( os.environ, { "SITE_ROOT": str(site), "SITE_DIST": str(work / "dist"), "SITE_STATE_DIR": str(work / "state"), "SITE_ADMIN_LOGIN": "admin", "SITE_ADMIN_PASSWORD_HASH": hash_password(PASSWORD, n=2**10), "SITE_SECRET_KEY": "content-browser-secret-" * 4, "SITE_COOKIE_SECURE": "0", "SITE_ADMIN_ORIGIN": admin if interactive else "", "SITE_PREVIEW_ORIGIN": preview if interactive else "", }, ) ) app, gate = create_app(), {"delay": 0} @app.middleware("http") async def delay(request, call_next, gate=gate): if ( request.url.path.endswith("/update") and request.url.path.startswith("/admin/editor/") and gate["delay"] ): delay, gate["delay"] = gate["delay"], 0 await asyncio.sleep(delay) 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() design_origin = 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(10000) 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, ) 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.goto(admin + "/admin/content/services/bots") idle(page) ref.goto(design_origin + "/content-antirion.html") if not interactive: selectors = [ ".entry-workspace", ".collection-pane .pane-title", ".entry-pane .pane-title", ".entry-tabs button", ".field-label", ".preview-pane .pane-title", ".workspace-footer", ] for skin in ("light", "dark"): page.evaluate("(skin)=>{document.body.dataset.skin=skin}", skin) # Approved dark Antirion tokens; the source has no Antirion dark palette. 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}) actual, original = metrics(page, selectors), metrics(ref, selectors) assert not page.evaluate( "document.documentElement.scrollWidth>innerWidth+1" ), (skin, width) for selector in selectors[1:]: assert actual[selector]["font"] == original[selector]["font"], ( selector, width, ) assert ( actual[selector]["padding"] == original[selector]["padding"] ), (selector, width) if width >= 1280: a, b = ( actual[".entry-workspace"]["columns"].split(), original[".entry-workspace"]["columns"].split(), ) for i in (0, 1, 3): assert a[i] == b[i], (width, a, b) if skin == "light": for selector in selectors[1:]: assert ( actual[selector]["background"] == original[selector]["background"] ), (selector, width) report["comparisons"].append( { "skin": skin, "width": width, "runtime": actual, "reference": original, } ) page.screenshot( path=str(OUT / f"content-{skin}-{width}.png"), full_page=True ) if skin == "light": ref.screenshot( path=str(OUT / f"reference-{width}.png"), full_page=True ) page.evaluate("document.body.dataset.skin='light'") page.set_viewport_size({"width": 1440, "height": 1050}) print("PASS", interactive, len(run["checks"]) + 1, flush=True) run["checks"].append("original_geometry_fonts_padding_palette_and_responsive") # Local filters/tabs send no requests, retain keyboard focus. before = len(posts) page.locator("#entry-query").fill("nothing-matches") expect(page.locator("[data-content-list-empty]")).to_be_visible() assert page.locator("#entry-query").evaluate("e=>e===document.activeElement") page.locator("#entry-query").fill("") page.locator("[data-content-filter=draft]").click() expect(page.locator("[data-entry=automation]")).to_be_visible() expect(page.locator("[data-entry=bots]")).to_be_hidden() page.locator("[data-content-filter=all]").click() for tab in ("relations", "usage", "schema", "fields"): page.locator("[data-content-tab=" + tab + "]").click() expect(page.locator("[data-content-panel=" + tab + "]")).to_be_visible() assert len(posts) == before print("PASS", interactive, len(run["checks"]) + 1, flush=True) run["checks"].append("local_filter_tabs_and_real_metadata") field = page.locator('[name="p.title"]') field.evaluate("e=>e.__qaInput=true") page.locator("#preview-result iframe").evaluate("e=>e.__qaFrame=true") before = len(posts) field.fill("One background content update") idle(page) expect(page.frame_locator("#preview-result iframe").locator("strong")).to_have_text( "One background content update" ) assert posts[before:] == [admin + "/admin/editor/content/services/bots/update"], ( posts[before:] ) assert field.evaluate("e=>e.__qaInput===true&&document.activeElement===e") assert page.locator("#preview-result iframe").evaluate("e=>e.__qaFrame===true") assert (site / "content/services/bots.md").read_bytes() == original_file print("PASS", interactive, len(run["checks"]) + 1, flush=True) run["checks"].append("one_post_keeps_field_frame_and_no_autosave") gate["delay"] = 1 field.fill("Delayed candidate") page.wait_for_function( "()=>document.getElementById('document-editor').dataset.updating==='true'" ) field.fill("Latest candidate during pending POST") page.locator("[data-content-tab=relations]").click() expect(page.locator("[data-content-panel=relations]")).to_be_visible() idle(page) page.locator("[data-content-tab=fields]").click() expect(field).to_have_value("Latest candidate during pending POST") print("PASS", interactive, len(run["checks"]) + 1, flush=True) run["checks"].append("typing_and_tabs_do_not_wait_for_delayed_post") # Asset/ref dialogs have real options and restore focus with Escape. picker = page.locator('[data-content-picker="p.hero_asset"]') picker.click() expect(page.locator("dialog[open] .asset-option").first).to_be_visible() page.keyboard.press("Escape") assert picker.evaluate("e=>e===document.activeElement") page.locator('[data-content-picker="p.related_scenarios"]').click() expect(page.locator("dialog[open] .ref-choice")).to_have_count(3) page.locator("dialog[open] input[type=checkbox]").first.uncheck() page.locator("dialog[open]").get_by_role( "button", name="Применить", exact=True ).click() idle(page) page.locator('[data-content-bool="p.featured"]').uncheck() idle(page) assert page.evaluate("window.siteDocument.state.model.meta.featured") is False print("PASS", interactive, len(run["checks"]) + 1, flush=True) run["checks"].append("real_asset_ref_picker_and_boolean") # Explicit add and remove list rows preserve names and field focus. page.locator('[data-content-add="p.benefits"]').click() row = page.locator('[data-list-row="p.benefits.2"] textarea') row.fill("New list item") idle(page) expect(page.locator('[data-list-row="p.benefits.3"]')).to_be_hidden() page.locator('[data-content-remove="p.benefits.0"]').click() idle(page) assert page.evaluate("window.siteDocument.state.model.meta.benefits.length") == 2 expect(page.locator('[name="p.benefits.1"]')).to_have_value("New list item") print("PASS", interactive, len(run["checks"]) + 1, flush=True) run["checks"].append("explicit_list_add_delete_reindexes_controls") page.locator("[data-content-preview=page]").click() idle(page) expect(page.frame_locator("#preview-result iframe").locator("h1")).to_have_text( "Latest candidate during pending POST" ) page.locator("[data-content-version=live]").click() idle(page) expect(page.frame_locator("#preview-result iframe").locator("h1")).to_have_text( "Telegram-боты" ) page.locator("[data-content-preview=card]").click() idle(page) expect(page.locator("#preview-result .preview-empty")).to_contain_text("карточка") page.locator("[data-content-version=workspace]").click() idle(page) print("PASS", interactive, len(run["checks"]) + 1, flush=True) run["checks"].append("real_workspace_card_page_and_immutable_live_page") page.locator("[data-content-validate]").first.click() expect(page.locator("dialog[open] .review-summary")).to_be_visible() page.screenshot(path=str(OUT / f"review-{interactive}.png")) page.keyboard.press("Escape") page.locator("[data-editor-mode=expert]").click() expect(page.locator("#expert-pane")).to_be_visible() page.locator("[data-content-diff]").click() expect(page.locator("dialog[open] .diff-row.changed").first).to_be_visible() page.keyboard.press("Escape") source = page.locator("#document-source").input_value() page.locator("#document-source").fill("---\ntitle: [\n---\nKeep this source") page.wait_for_timeout(450) page.wait_for_function("()=>!window.siteDocument.state.busy") expect(page.locator("#document-source")).to_have_value( "---\ntitle: [\n---\nKeep this source" ) expect(page.locator("[data-content-source-errors]")).not_to_be_empty() page.locator("#document-source").fill(source) idle(page) page.locator("[data-content-find]").click() page.locator("[data-source-query]").fill("Latest candidate") page.locator("dialog[open]").get_by_role("button", name="Найти", exact=True).click() assert page.locator("#document-source").evaluate( "e=>e.selectionEnd>e.selectionStart" ) page.locator("[data-editor-mode=constructor]").click() print("PASS", interactive, len(run["checks"]) + 1, flush=True) run["checks"].append("review_expert_diff_find_invalid_yaml_recovery") # Navigation confirmation uses the original dialog, Escape retains edits. page.locator("[data-entry=ai]").click() expect(page.locator("dialog[open]")).to_contain_text("несохранённые") page.keyboard.press("Escape") assert page.url.endswith("/services/bots") page.locator("[data-save-document]").click() page.wait_for_function( "()=>!window.siteDocument.state.dirty&&!window.siteDocument.state.busy" ) assert b"Latest candidate" in (site / "content/services/bots.md").read_bytes() assert (work / "dist/current").exists() print("PASS", interactive, len(run["checks"]) + 1, flush=True) run["checks"].append("leave_confirmation_explicit_save_no_publish") # Real CAS conflict; local unsaved text survives. field.fill("My conflict candidate") idle(page) server_source = ( (site / "content/services/bots.md") .read_text() .replace("Latest candidate during pending POST", "Server concurrent title") ) (site / "content/services/bots.md").write_text(server_source) page.locator("[data-save-document]").click() expect(page.locator("dialog[open] .conflict-grid")).to_be_visible() expect(field).to_have_value("My conflict candidate") page.screenshot(path=str(OUT / f"conflict-{interactive}.png")) page.keyboard.press("Escape") print("PASS", interactive, len(run["checks"]) + 1, flush=True) run["checks"].append("real_409_preserves_unsaved_source") # Reload is intentional and has no system dialog. page.reload() idle(page) field = page.locator('[name="p.title"]') field.fill("Copied current editor") idle(page) page.locator("[data-content-duplicate]").click() page.wait_for_url("**/bots-copy") idle(page) expect(page.locator('[name="p.title"]')).to_have_value( "Copied current editor (копия)" ) assert "status: draft" in (site / "content/services/bots-copy.md").read_text() page.locator("[data-content-delete]").click() expect(page.locator("dialog[open]")).to_contain_text("Удалить запись?") page.keyboard.press("Escape") assert (site / "content/services/bots-copy.md").exists() page.locator("[data-content-delete]").click() page.locator("dialog[open]").get_by_role( "button", name="Удалить из workspace", exact=True ).click() page.wait_for_url("**/services/ai") assert not (site / "content/services/bots-copy.md").exists() idle(page) page.locator("[data-content-delete]").click() expect(page.locator("#request-error")).to_contain_text("архивируйте") page.locator("[data-close-request-error]").click() print("PASS", interactive, len(run["checks"]) + 1, flush=True) run["checks"].append( "duplicate_current_candidate_delete_confirmation_and_published_guard" ) # Empty/off collections and a real new draft in an enabled collection. page.goto(admin + "/admin/content/posts") expect(page.locator("[data-content-list-empty]")).to_contain_text( "Записей пока нет" ) expect( page.get_by_role("button", name="Создать запись", exact=True) ).to_be_disabled() page.goto(admin + "/admin/content/services") idle(page) page.get_by_role("button", name="Создать запись", exact=True).click() page.wait_for_url("**/services/entry-*") idle(page) assert page.locator("#entry-slug").input_value().startswith("entry-") print("PASS", interactive, len(run["checks"]) + 1, flush=True) run["checks"].append("empty_disabled_collection_and_create_real_draft") # HTTP and network errors keep the active fields and show a message. field = page.locator('[name="p.title"]') page.route( "**/admin/editor/**/update", lambda route: route.fulfill( status=500, content_type="application/json", body=json.dumps( { "error": { "code": "qa_failure", "message": "Проверка HTTP 500", "request_id": "content-qa", } } ), ), ) field.fill("Keep text after error") expect(page.locator("#request-error")).to_contain_text("Проверка HTTP 500") expect(field).to_have_value("Keep text after error") page.unroute("**/admin/editor/**/update") field.fill("Recovery after 500") idle(page) page.route("**/admin/editor/**/update", lambda route: route.abort()) field.fill("Keep text offline") expect(page.locator("#request-error")).to_contain_text("Нет связи") expect(field).to_have_value("Keep text offline") page.unroute("**/admin/editor/**/update") field.fill("Recovered connection") idle(page) print("PASS", interactive, len(run["checks"]) + 1, flush=True) run["checks"].append("500_and_network_errors_visible_and_recoverable") ref.close() assert not report["page_errors"], report["page_errors"] assert not report["native_dialogs"], report["native_dialogs"] assert not report["console_errors"], report["console_errors"] browser.close() (OUT / "report.json").write_text(json.dumps(report, ensure_ascii=False, indent=2)) print( json.dumps( { "comparisons": len(report["comparisons"]), "runs": report["runs"], "errors": report["page_errors"], }, ensure_ascii=False, indent=2, ) ) if __name__ == "__main__": main()