#!/usr/bin/env python3 """Isolated browser tests of DOM reconciliation, preview alignment and owner labels. Run from the repository root. Does not emulate the server or OT protocol. """ from __future__ import annotations import base64 from pathlib import Path from playwright.sync_api import sync_playwright ROOT = Path(__file__).resolve().parents[1] def module_url(content: str) -> str: return "data:text/javascript;base64," + base64.b64encode(content.encode()).decode() def main(): reconcile = (ROOT / "static/js/dom-reconcile.js").read_text() editor_source = (ROOT / "static/js/note-editor.js").read_text() presence_source = editor_source.split(" function updatePresence(users) {", 1)[1].split(" function updateLatency", 1)[0] presence_source = "function updatePresence(users) {" + presence_source badges_source = editor_source.split(" let renderedAuthorshipOwners = [];", 1)[1].split(" function syncOwnerLabels()", 1)[0] badges_source = "let renderedAuthorshipOwners = [];" + badges_source geometry_source = "let previewPositionCache = null;" + editor_source.split(" let previewPositionCache = null;", 1)[1].split(" function previewScrollAnchor()", 1)[0] markdown = (ROOT / "static/js/markdown.js").read_text() markdown = "const EMOJI_SHORTCODES = {}; const t = (key, v, fallback) => fallback; const parseImageAlias = () => null;\n" + "\n".join( line for line in markdown.splitlines() if not line.startswith("import ") ) + "\nexport { firstRenderedTextRect };\n" html = '''
Alice
'''.replace("__RECONCILE__", module_url(reconcile)).replace("__MARKDOWN__", module_url(markdown)).replace("__BADGES_SOURCE__", badges_source).replace("__PRESENCE_SOURCE__", presence_source).replace("__GEOMETRY_SOURCE__", geometry_source) with sync_playwright() as playwright: browser = playwright.chromium.launch(executable_path="/usr/bin/chromium", args=["--no-sandbox", "--disable-dev-shm-usage"]) try: page = browser.new_page(viewport={"width": 1100, "height": 780}) errors = [] page.on("pageerror", lambda error: errors.append(str(error))) page.set_content(html) try: page.wait_for_function("window.frontendTestsPassed===true", timeout=15000) except Exception: raise AssertionError(f"Browser errors: {errors}") assert not errors, errors print("PASS: keyed DOM list updates preserve node identity; identical list has zero mutations") print("PASS: real presence update handles 1000 users with row/badge reuse, duplicates and XSS-safe text") print("PASS: owner-label canvas transforms; preview alignment parity for 150 lines in Chromium") print("PASS: preview geometry cache reuses measurements on scroll and invalidates on content change") finally: browser.close() if __name__ == "__main__": main()