Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

17.8 KB
Raw Blame
#!/usr/bin/env python3
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Browser checks for NonModalDropdownMenu scroll, dismiss and focus, which AST tests cannot reach."""

from __future__ import annotations

import json
import os
import sys
from pathlib import Path

from playwright.sync_api import TimeoutError as PlaywrightTimeout
from playwright.sync_api import sync_playwright

sys.path.insert(0, str(Path(__file__).resolve().parent))
from _playwright_robust import (  # noqa: E402
    chromium_launch_args,
    start_vite,
    stop_process,
    wait_for_smoke_page,
)

ENGINE = os.environ.get("PW_ENGINE", "chromium")
PORT = int(os.environ.get("PW_PORT", "5401"))
OUT = Path(os.environ.get("PW_OUT", "logs/nonmodal_menus_report.json"))
ENTRY = "/smoke-nonmodal-menus-main.tsx"
PAGE = "/smoke-nonmodal-menus.html"

# Both sides of the `duration-100` exit window: a guard outliving the close swallows the next click.
EXIT_WINDOW_MS = (30, 75, 125)


class Checks:
    def __init__(self) -> None:
        self.results: list[dict[str, object]] = []

    def record(
        self,
        name: str,
        ok: bool,
        detail: object = "",
    ) -> None:
        self.results.append({"case": name, "ok": bool(ok), "detail": detail})
        print(f"{'PASS' if ok else 'FAIL'}  {name}  {detail}", flush = True)

    @property
    def failed(self) -> list[dict[str, object]]:
        return [r for r in self.results if not r["ok"]]


def menus_open(page) -> int:
    return page.locator("[role=menu]").count()


def state(page) -> dict:
    return page.evaluate("() => window.probe.documentState()")


def open_row(page, row: int) -> None:
    page.get_by_label(f"Row options {row}", exact = True).click()
    page.wait_for_selector("[role=menu]", state = "visible")


def open_control(page) -> None:
    page.get_by_label("Control options", exact = True).click()
    page.wait_for_selector("[role=menu]", state = "visible")


def until(
    page,
    condition: str,
    arg = None,
    timeout: int = 5000,
) -> None:
    """Wait until `condition` holds without failing here, since a broken build then fails the same
    check below; replaces flat sleeps that also asserted runner speed."""
    try:
        page.wait_for_function(condition, arg = arg, timeout = timeout)
    except PlaywrightTimeout:
        pass


MENUS = "document.querySelectorAll('[role=menu]').length"


def until_menus(page, expr: str) -> None:
    """Wait for the open-menu count to satisfy `expr`, e.g. `== 0` or `>= 2`."""
    until(page, f"() => {MENUS} {expr}")


def frames(page) -> None:
    """Let a scroll or focus change reach its listeners (two animation frames)."""
    page.evaluate("() => new Promise(r => requestAnimationFrame(() => requestAnimationFrame(r)))")


def clicks_at_least(page, n: int) -> None:
    until(page, "n => window.probe.documentClicks() >= n", arg = n)
    frames(page)


def focused_label(page, label: str) -> None:
    until(page, "l => document.activeElement?.getAttribute('aria-label') === l", arg = label)


def raw_click(page, selector: str) -> None:
    """Mouse press at the centre; Playwright's click refuses under a modal body's pointer-events: none."""
    box = page.locator(selector).bounding_box()
    if box is None:
        raise RuntimeError(f"{selector} has no box")
    page.mouse.click(box["x"] + box["width"] / 2, box["y"] + box["height"] / 2)


def close_all(page) -> None:
    for _ in range(6):
        before = menus_open(page)
        if before == 0:
            return
        page.keyboard.press("Escape")
        until_menus(page, f"< {before}")


def reset_list(page) -> None:
    page.evaluate("() => { document.getElementById('list').scrollTop = 0; }")
    frames(page)


def check_document_is_untouched(page, checks: Checks) -> None:
    baseline = state(page)
    open_row(page, 0)
    opened = state(page)
    checks.record(
        "opening a converted menu writes nothing to <body>",
        opened["bodyPointerEvents"] == "" and not opened["scrollLocked"],
        opened,
    )
    checks.record(
        "opening a converted menu aria-hides nothing new",
        opened["ariaHidden"] <= baseline["ariaHidden"],
        f"{baseline['ariaHidden']} -> {opened['ariaHidden']}",
    )
    close_all(page)

    open_control(page)
    control = state(page)
    checks.record(
        "the unconverted menu still locks the document, so the probe can see a lock",
        control["bodyPointerEvents"] == "none"
        and control["scrollLocked"]
        and control["ariaHidden"] > 0,
        control,
    )
    close_all(page)


def check_scroll_closes(page, checks: Checks) -> None:
    reset_list(page)
    open_row(page, 1)
    page.mouse.move(120, 300)
    page.mouse.wheel(0, 400)
    until_menus(page, "== 0")
    frames(page)
    scrolled = page.evaluate("() => document.getElementById('list').scrollTop")
    checks.record("a wheel behind an open menu closes it", menus_open(page) == 0)
    checks.record("the list keeps the scroll the wheel gave it", scrolled > 0, scrolled)
    # Observation window: a snap-back would arrive after the close, so give it time to.
    page.wait_for_timeout(300)
    settled = page.evaluate("() => document.getElementById('list').scrollTop")
    checks.record(
        "closing does not snap the list back to the trigger",
        settled == scrolled,
        f"{scrolled} -> {settled}",
    )

    reset_list(page)
    open_row(page, 2)
    page.evaluate("() => { document.getElementById('list').scrollTop = 500; }")
    until_menus(page, "== 0")
    checks.record("a programmatic scroll closes the menu", menus_open(page) == 0)

    reset_list(page)
    open_row(page, 3)
    page.evaluate("() => document.getElementById('list').scrollBy({top: 200, behavior: 'smooth'})")
    until_menus(page, "== 0")
    checks.record("a smooth scroll closes the menu", menus_open(page) == 0)

    reset_list(page)
    open_row(page, 6)
    page.evaluate("() => window.scrollTo(0, 200)")
    until_menus(page, "== 0")
    checks.record("a window scroll closes the menu", menus_open(page) == 0)
    page.evaluate("() => window.scrollTo(0, 0)")


def check_scrolls_that_must_not_close(page, checks: Checks) -> None:
    reset_list(page)
    page.get_by_label("Tall menu", exact = True).click()
    page.wait_for_selector("[role=menu]", state = "visible")
    page.evaluate(
        "() => { const v = document.querySelector('[data-slot=dropdown-menu-viewport]');"
        " v.scrollTop = 120; }"
    )
    # Observation window: the menu must NOT close, so give a close the time it would take.
    page.wait_for_timeout(400)
    checks.record("scrolling the menu's own viewport leaves it open", menus_open(page) >= 1)
    close_all(page)

    open_row(page, 4)
    page.evaluate("() => { document.getElementById('other').scrollTop = 300; }")
    # Observation window: the menu must NOT close.
    page.wait_for_timeout(400)
    checks.record("scrolling an unrelated list leaves the menu open", menus_open(page) >= 1)
    close_all(page)

    open_row(page, 5)
    page.get_by_test_id("export-5").click()
    until_menus(page, ">= 2")
    frames(page)
    with_sub = menus_open(page)
    page.evaluate(
        "() => { const vs = document.querySelectorAll('[data-slot=dropdown-menu-viewport]');"
        " vs[vs.length - 1].scrollTop = 100; }"
    )
    # Observation window: neither menu may close.
    page.wait_for_timeout(400)
    checks.record(
        "scrolling an open submenu leaves both menus open",
        with_sub >= 2 and menus_open(page) == with_sub,
        f"{with_sub} -> {menus_open(page)}",
    )
    close_all(page)


def check_dismiss_guard(page, checks: Checks) -> None:
    # Reset AFTER opening: the opening click reaches document too.
    open_row(page, 7)
    page.evaluate("() => window.probe.resetClicks()")
    raw_click(page, "#outside-button")
    until_menus(page, "== 0")
    frames(page)
    swallowed = page.evaluate("() => window.probe.documentClicks()")
    checks.record(
        "the click that dismisses a menu does not reach what is under it",
        menus_open(page) == 0 and swallowed == 0,
        f"clicks={swallowed}",
    )
    page.evaluate("() => window.probe.resetClicks()")
    raw_click(page, "#outside-button")
    clicks_at_least(page, 1)
    delivered = page.evaluate("() => window.probe.documentClicks()")
    checks.record("the click after that one is delivered", delivered == 1, delivered)

    for label, opener in (
        ("a converted menu", lambda: open_row(page, 8)),
        ("the unconverted control", lambda: open_control(page)),
    ):
        for delay in EXIT_WINDOW_MS:
            opener()
            page.keyboard.press("Escape")
            # Deliberate: the click has to land this far into the close's exit window.
            page.wait_for_timeout(delay)
            page.evaluate("() => window.probe.resetClicks()")
            raw_click(page, "#outside-button")
            clicks_at_least(page, 1)
            landed = page.evaluate("() => window.probe.documentClicks()")
            checks.record(
                f"a click {delay}ms after closing {label} is delivered", landed == 1, landed
            )
            close_all(page)

    # Firefox and WebKit keep the caret, Chromium does not; either is fine, <body> is not.
    focus_after = {}
    for label, opener in (
        ("converted", lambda: open_row(page, 9)),
        ("control", lambda: open_control(page)),
    ):
        opener()
        raw_click(page, "#outside-input")
        # Focus is restored (or not) when the closed menu unmounts after its exit animation.
        until_menus(page, "== 0")
        frames(page)
        focus_after[label] = page.evaluate(
            "() => document.activeElement?.getAttribute('data-slot')"
            " ?? document.activeElement?.id ?? null"
        )
        close_all(page)
    checks.record(
        "dismissing into an input never drops focus onto the document",
        focus_after["converted"] in {"outside-input", "dropdown-menu-trigger"},
        focus_after,
    )


def check_focus_return(page, checks: Checks) -> None:
    active = "() => document.activeElement?.getAttribute('aria-label')"
    close_all(page)
    reset_list(page)
    open_row(page, 10)
    page.keyboard.press("Escape")
    focused_label(page, "Row options 10")
    checks.record(
        "Escape returns focus to the trigger that opened the menu",
        page.evaluate(active) == "Row options 10",
        page.evaluate(active),
    )

    # The scroll-close flag must not survive into the next close.
    reset_list(page)
    open_row(page, 11)
    page.evaluate("() => { document.getElementById('list').scrollTop = 300; }")
    until_menus(page, "== 0")
    reset_list(page)
    open_row(page, 12)
    page.keyboard.press("Escape")
    focused_label(page, "Row options 12")
    checks.record(
        "a scroll-close does not change how the next Escape restores focus",
        page.evaluate(active) == "Row options 12",
        page.evaluate(active),
    )

    # The scroll-close path prevents Radix's restore, so it must itself skip stealing focus back.
    reset_list(page)
    open_row(page, 14)
    page.evaluate("() => { document.getElementById('list').scrollTop = 40; }")
    # Deliberate: the click has to land while the scroll-closed menu is still animating out.
    page.wait_for_timeout(20)
    raw_click(page, "#outside-input")
    # The close-autofocus this guards against runs when the menu unmounts.
    until_menus(page, "== 0")
    frames(page)
    landed = page.evaluate(
        "() => document.activeElement?.getAttribute('data-slot')"
        " ?? document.activeElement?.id ?? null"
    )
    checks.record(
        "an input clicked while a scroll-closed menu animates out keeps the caret",
        landed == "outside-input",
        landed,
    )

    reset_list(page)
    open_row(page, 13)
    page.evaluate("() => { document.getElementById('list').scrollTop = 600; }")
    until_menus(page, "== 0")
    frames(page)
    checks.record(
        "a scroll-close keeps the scroll it was closed by",
        page.evaluate("() => document.getElementById('list').scrollTop") == 600,
        page.evaluate("() => document.getElementById('list').scrollTop"),
    )


def check_keyboard(page, checks: Checks) -> None:
    # Focusing an off-screen trigger scrolls it a frame later, racing the open.
    reset_list(page)
    page.get_by_label("Row options 2", exact = True).focus()
    frames(page)
    page.keyboard.press("Enter")
    # Flat wait on purpose: a keyboard-opened menu drops an ArrowDown pressed right after opening,
    # even when focused and animated; no DOM state marks readiness.
    page.wait_for_timeout(300)
    checks.record("Enter opens the menu", menus_open(page) >= 1)
    page.keyboard.press("ArrowDown")
    page.keyboard.press("ArrowRight")
    until_menus(page, ">= 2")
    checks.record("ArrowRight opens the submenu", menus_open(page) >= 2, menus_open(page))
    page.keyboard.press("ArrowLeft")
    until_menus(page, "== 1")
    checks.record("ArrowLeft closes only the submenu", menus_open(page) == 1, menus_open(page))
    close_all(page)

    page.get_by_label("Row options 3", exact = True).focus()
    frames(page)
    page.keyboard.press("Space")
    until_menus(page, ">= 1")
    checks.record("Space opens the menu", menus_open(page) >= 1)
    close_all(page)


def check_lifetime(page, checks: Checks) -> None:
    reset_list(page)
    open_row(page, 16)
    page.evaluate("() => window.probe.removeRow(16)")
    until_menus(page, "== 0")
    checks.record(
        "deleting the row while its menu is open strands no menu",
        menus_open(page) == 0,
        menus_open(page),
    )
    page.evaluate("() => window.probe.reset()")
    frames(page)

    reopened: dict[str, int] = {}
    reset_list(page)
    for label, opener, name in (
        ("a converted menu", lambda: open_row(page, 17), "Row options 17"),
        ("the unconverted control", lambda: open_control(page), "Control options"),
    ):
        opener()
        page.keyboard.press("Escape")
        # Deliberate: reopen inside the exit animation.
        page.wait_for_timeout(30)
        page.get_by_label(name, exact = True).click()
        # Observation window: a second menu would show up in this time.
        page.wait_for_timeout(500)
        reopened[label] = menus_open(page)
        close_all(page)
    # The reopening click is a race either way; the invariant is never two menus open.
    checks.record(
        "reopening during the exit animation never leaves two menus open",
        all(count <= 1 for count in reopened.values()),
        reopened,
    )

    before = page.evaluate("() => window.probe.scrollListeners()")
    for _ in range(30):
        page.get_by_label("Row options 18", exact = True).click()
        page.wait_for_timeout(60)
        page.keyboard.press("Escape")
        page.wait_for_timeout(60)
    close_all(page)
    until_menus(page, "== 0")
    frames(page)
    after = page.evaluate("() => window.probe.scrollListeners()")
    checks.record(
        "30 open and close cycles leak no scroll listener", after == before, f"{before} -> {after}"
    )

    page.evaluate(
        "() => { const l = document.getElementById('list');"
        " l.scrollTop = 8 * 40 - l.clientHeight + 20; }"
    )
    frames(page)
    # Press the visible sliver; not click_forced, which scrolls the trigger into view.
    trigger = page.get_by_label("Row options 8", exact = True).bounding_box()
    clip = page.locator("#list").bounding_box()
    top = max(trigger["y"], clip["y"])
    bottom = min(trigger["y"] + trigger["height"], clip["y"] + clip["height"])
    page.mouse.click(trigger["x"] + trigger["width"] / 2, (top + bottom) / 2)
    until_menus(page, ">= 1")
    # Observation window: the menu must also STAY open (a scroll-into-view would close it).
    page.wait_for_timeout(300)
    checks.record(
        "a partly visible trigger opens a menu that stays open",
        menus_open(page) >= 1,
        menus_open(page),
    )
    close_all(page)

    reset_list(page)
    open_row(page, 19)
    page.get_by_label("Row options 20", exact = True).click()
    # Observation window: a second menu would show up in this time.
    page.wait_for_timeout(400)
    checks.record(
        "clicking another row's trigger does not leave two menus open",
        menus_open(page) <= 1,
        menus_open(page),
    )
    close_all(page)


def main() -> int:
    vite = start_vite(PORT)
    checks = Checks()
    try:
        url = f"http://127.0.0.1:{PORT}{PAGE}"
        wait_for_smoke_page(url, ENTRY, proc = vite, info = lambda m: print(m, flush = True))
        with sync_playwright() as pw:
            kwargs: dict = {"headless": True}
            if ENGINE == "chromium":
                kwargs["args"] = chromium_launch_args()
            browser = getattr(pw, ENGINE).launch(**kwargs)
            page = browser.new_context(viewport = {"width": 1280, "height": 800}).new_page()
            page.goto(url, wait_until = "domcontentloaded")
            page.wait_for_function("() => Boolean(window.probe)")
            checks.record(
                "the engine honours focus({preventScroll})",
                page.evaluate("() => window.probe.supportsPreventScroll()"),
                ENGINE,
            )
            check_document_is_untouched(page, checks)
            check_scroll_closes(page, checks)
            check_scrolls_that_must_not_close(page, checks)
            check_dismiss_guard(page, checks)
            check_focus_return(page, checks)
            check_keyboard(page, checks)
            check_lifetime(page, checks)
            browser.close()
    finally:
        stop_process(vite)

    passed = len(checks.results) - len(checks.failed)
    print(f"\n{ENGINE}: {passed}/{len(checks.results)} passed", flush = True)
    OUT.parent.mkdir(parents = True, exist_ok = True)
    OUT.write_text(
        json.dumps({"engine": ENGINE, "results": checks.results}, indent = 2), encoding = "utf8"
    )
    return 1 if checks.failed else 0


if __name__ == "__main__":
    sys.exit(main())