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.

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

"""Find-in-page in real browsers, plus degraded modes without highlight or visibility API."""

import json
import os
import sys
import time
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,
)

PORT = int(os.environ.get("SMOKE_PORT", "5419"))
ENGINES = [e for e in os.environ.get("SMOKE_ENGINES", "chromium").split(",") if e]
URL = f"http://127.0.0.1:{PORT}/smoke-find-in-page.html"

ENTRY_FAIL = os.environ.get("SMOKE_ENTRY_FAIL", "")

ENTRY_DELAY_MS = int(os.environ.get("SMOKE_ENTRY_DELAY_MS", "0"))
ENTRY_SCREENSHOT = os.environ.get("SMOKE_ENTRY_SCREENSHOT", "")
PLATFORMS = {
    "macOS": ("MacIntel", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) SmokeUA"),
    "Windows": ("Win32", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) SmokeUA"),
    "Linux": ("Linux x86_64", "Mozilla/5.0 (X11; Linux x86_64) SmokeUA"),
}
MOD = {"macOS": "Meta", "Windows": "Control", "Linux": "Control"}

NO_HIGHLIGHT_API = """
delete window.Highlight;
try { delete CSS.highlights; } catch (e) { /* getter-only on some engines */ }
"""

# Pre-rename engines answer only checkVisibilityCSS/checkOpacity and ignore modern option names.
LEGACY_CHECK_VISIBILITY = """
const real = Element.prototype.checkVisibility;
Element.prototype.checkVisibility = function (options) {
  const legacy = {};
  if (options && 'checkVisibilityCSS' in options) {
    legacy.checkVisibilityCSS = options.checkVisibilityCSS;
  }
  if (options && 'checkOpacity' in options) {
    legacy.checkOpacity = options.checkOpacity;
  }
  if (options && 'contentVisibilityAuto' in options) {
    legacy.contentVisibilityAuto = options.contentVisibilityAuto;
  }
  return real.call(this, legacy);
};
"""

# Safari < 17.4 and desktop WebKitGTK lack checkVisibility; an undefined answer must not read as visible.
NO_CHECK_VISIBILITY = """
delete Element.prototype.checkVisibility;
"""

failures: list[str] = []
passed = 0


def check(
    engine: str,
    mode: str,
    name: str,
    ok: bool,
    detail: str = "",
) -> None:
    global passed
    if ok:
        passed += 1
        return
    failures.append(f"{engine}/{mode}: {name}\n      {detail}")


def counter(page) -> str | None:
    return page.evaluate("() => window.__findSmoke.counter()")


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


def open_bar(page, mod: str) -> None:
    page.keyboard.press(f"{mod}+f")
    page.wait_for_function(
        "() => window.__findSmoke.state().open",
        timeout = 10000,
    )


def settle(
    page,
    condition: str,
    *,
    timeout: int = 10000,
    arg = None,
) -> None:
    """Waits on the condition rather than a flat sleep, so a slow runner is not failed by timing."""
    try:
        page.wait_for_function(condition, arg = arg, timeout = timeout)
    except PlaywrightTimeout:
        pass


def frames(page) -> None:
    """Let the commit that just changed state reach layout and paint (two animation frames)."""
    page.evaluate(
        "() => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)))"
    )


# The counter is empty while a query is pending and reads `active/count` (`0/0` for no match)
# once it has settled, so a non-empty counter is the bar's own "search done" signal.
COUNTED = "() => !!window.__findSmoke.counter()"
COUNTER_IS = "expected => window.__findSmoke.counter() === expected"
SEARCH_HAS = "text => (document.querySelector('[role=\"search\"]')?.innerText ?? '').includes(text)"
COUNTER_MOVED = "prev => { const c = window.__findSmoke.counter(); return !!c && c !== prev; }"


def settled_counter(page) -> str | None:
    """Wait for the query to settle and paint; the first settled render can show the previous
    results, so two more frames let the search effect land."""
    settle(page, COUNTED)
    frames(page)
    frames(page)
    return counter(page)


def step(page, key: str) -> None:
    """Press a walk key and wait for the active match to move (every query here has many)."""
    before = counter(page)
    page.keyboard.press(key)
    settle(page, COUNTER_MOVED, arg = before)
    frames(page)


def close_bar(page) -> None:
    page.keyboard.press("Escape")
    settle(page, "() => !window.__findSmoke.state().open")


def check_chord(page, engine: str, mode: str, mod: str) -> None:
    """The chord opens the bar, and does so from a text field too."""
    open_bar(page, mod)
    check(engine, mode, "the chord opens the bar", state(page).get("open") is True)
    check(
        engine,
        mode,
        "the bar is a search landmark",
        page.locator('[role="search"]').count() == 1,
    )

    page.evaluate("() => document.activeElement?.blur()")
    check(
        engine,
        mode,
        "the field can lose focus while the bar stays open",
        state(page).get("focused") is False,
    )
    page.keyboard.press(f"{mod}+f")
    settle(
        page,
        "() => { const s = window.__findSmoke.state(); return s.open && s.focused; }",
    )
    check(
        engine,
        mode,
        "the chord re-focuses the field instead of closing",
        state(page).get("open") is True and state(page).get("focused") is True,
    )
    close_bar(page)
    check(engine, mode, "Escape closes the bar", state(page).get("open") is False)


def check_counting(page, engine: str, mode: str, mod: str) -> None:
    open_bar(page, mod)
    page.keyboard.type("unsloth")
    shown = settled_counter(page)
    check(engine, mode, "a query is counted", bool(shown), f"counter={shown!r}")

    # An off-route workspace stays mounted under `inert`; its matches must not count.
    page.evaluate("() => window.__findSmoke.setWorkspace('other')")
    settle(page, COUNTER_MOVED, arg = shown)
    frames(page)
    other = counter(page)
    check(
        engine,
        mode,
        "an inert workspace is out of the count",
        other != shown,
        f"{shown!r} -> {other!r}",
    )
    page.evaluate("() => window.__findSmoke.setWorkspace('chat')")
    settle(page, COUNTER_IS, arg = shown)
    frames(page)
    check(
        engine,
        mode,
        "switching back restores the count",
        counter(page) == shown,
        f"{counter(page)!r} != {shown!r}",
    )
    close_bar(page)


def check_close_preserves_position(page, engine: str, mode: str, mod: str) -> None:
    search_page = new_page(page.context)
    try:
        for outer_scroll in (False, True):
            search_page.evaluate("outer => window.__findSmoke.setOuterScroll(outer)", outer_scroll)
            origin = search_page.locator("[data-find-scope] .mx-auto > .rounded-xl").first
            origin.evaluate("element => { element.tabIndex = 0; }")
            for dismiss in ("Escape", "button"):
                origin.focus()
                open_bar(search_page, mod)
                field = search_page.locator('[role="search"] input')
                field.fill("")
                search_page.wait_for_function("() => !window.__findSmoke.counter()")
                field.fill("unsloth")
                search_page.wait_for_function("() => !!window.__findSmoke.counter()")
                start = search_page.evaluate("() => window.__findSmoke.counter()")
                for _ in range(10):
                    field.press("Enter")
                # Wait for all ten steps to land: the active match ten places on, wrapping.
                settle(
                    search_page,
                    """start => {
                      const [a, n] = start.split('/').map(Number);
                      return window.__findSmoke.counter() === `${((a - 1 + 10) % n) + 1}/${n}`;
                    }""",
                    arg = start,
                )
                frames(search_page)
                before = search_page.evaluate("() => window.__findSmoke.scrollTop()")
                if dismiss == "Escape":
                    close_bar(search_page)
                else:
                    search_page.get_by_role("button", name = "Close find", exact = True).click()
                search_page.wait_for_function("() => !window.__findSmoke.state().open")
                search_page.evaluate(
                    "() => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)))"
                )
                after = search_page.evaluate("() => window.__findSmoke.scrollTop()")
                layout = "outer scroller" if outer_scroll else "chat scroller"
                check(
                    engine,
                    mode,
                    f"{dismiss} preserves the search position in the {layout}",
                    before > 500 and abs(after - before) <= 1,
                    f"scrollTop {before} -> {after}",
                )
                check(
                    engine,
                    mode,
                    f"{dismiss} restores focus in the {layout}",
                    origin.evaluate("element => document.activeElement === element"),
                )
    finally:
        search_page.close()


def check_walk(page, engine: str, mode: str, mod: str) -> None:
    open_bar(page, mod)
    page.keyboard.type("unsloth")
    settled_counter(page)
    start = page.evaluate("() => window.__findSmoke.scrollTop()")
    for _ in range(6):
        step(page, "Enter")
    moved = page.evaluate("() => window.__findSmoke.scrollTop()")
    check(
        engine,
        mode,
        "Enter walks the matches and scrolls to them",
        moved != start,
        f"scrollTop {start} -> {moved}",
    )

    # Shift+Enter walks back.
    step(page, "Shift+Enter")
    check(
        engine,
        mode,
        "Shift+Enter walks backwards",
        counter(page) is not None,
        f"counter={counter(page)!r}",
    )
    close_bar(page)


def check_streaming(page, engine: str, mode: str, mod: str) -> None:
    open_bar(page, mod)
    page.keyboard.type("unsloth")
    settled_counter(page)
    for _ in range(4):
        step(page, "Enter")
    before_scroll = page.evaluate("() => window.__findSmoke.scrollTop()")
    before_count = counter(page)
    reply = "a streamed unsloth reply"
    page.evaluate("text => window.__findSmoke.stream(text, 5)", reply)
    # Done streaming, and the reindex that counts the new match has landed.
    settle(
        page,
        """([text, prev]) => {
          const rows = document.querySelectorAll('[data-streamed]');
          const last = rows[rows.length - 1];
          const c = window.__findSmoke.counter();
          return last?.textContent === text && !!c && c !== prev;
        }""",
        arg = [reply, before_count],
    )
    # Observation window, not a readiness guess: one more reindex interval (300 ms) must pass
    # without the reader being scrolled, since a later reindex is where a jump would come from.
    page.wait_for_timeout(350)
    frames(page)
    check(
        engine,
        mode,
        "a streamed reply raises the count",
        counter(page) != before_count,
        f"{before_count!r} -> {counter(page)!r}",
    )
    check(
        engine,
        mode,
        "a streamed reply does not move the reader",
        page.evaluate("() => window.__findSmoke.scrollTop()") == before_scroll,
        f"scrollTop {before_scroll} -> {page.evaluate('() => window.__findSmoke.scrollTop()')}",
    )
    close_bar(page)


def check_paint_and_teardown(page, engine: str, mode: str, mod: str) -> None:
    """Whichever way this engine shows a match, opening paints and closing clears."""
    has_api = page.evaluate("() => typeof CSS !== 'undefined' && !!CSS.highlights")
    open_bar(page, mod)
    page.keyboard.type("unsloth")
    settled_counter(page)

    if has_api:
        painted = page.evaluate(
            "() => ({ all: CSS.highlights.has('unsloth-find'),"
            " active: CSS.highlights.has('unsloth-find-active') })"
        )
        check(
            engine,
            mode,
            "both highlight registries are painted",
            painted["all"] and painted["active"],
            str(painted),
        )
    else:
        # Engines differ on keeping the fallback selection; the invariant is that the field keeps accepting input.
        typed = page.evaluate("() => document.querySelector('[role=\"search\"] input')?.value")
        check(
            engine,
            mode,
            "the field is still typable once the fallback has selected",
            typed == "unsloth",
            f"input value={typed!r}, expected 'unsloth'",
        )
        check(
            engine,
            mode,
            "the fallback still counts every match",
            bool(counter(page)),
            f"counter={counter(page)!r}",
        )

    close_bar(page)
    if has_api:
        left = page.evaluate(
            "() => CSS.highlights.has('unsloth-find')"
            " || CSS.highlights.has('unsloth-find-active')"
        )
        check(engine, mode, "closing clears both registries", left is False)
    else:
        check(
            engine,
            mode,
            "closing drops the fallback selection",
            page.evaluate("() => (window.getSelection()?.toString() ?? '')") == "",
        )


def check_modal_gate(page, engine: str, mode: str, mod: str) -> None:
    """A modal backgrounds the scope, and the chord must go back to the browser."""
    page.evaluate("() => window.__findSmoke.setModal(true)")
    settle(
        page,
        "() => document.querySelector('[data-find-scope]')?.getAttribute('aria-hidden') === 'true'",
    )
    page.keyboard.press(f"{mod}+f")
    # Observation window, not a readiness guess: the bar must NOT open, so give it the time an
    # open would take (its chunk is already loaded by the earlier probes).
    page.wait_for_timeout(250)
    check(
        engine,
        mode,
        "the chord is declined while a modal backgrounds the scope",
        state(page).get("open") is False,
    )
    page.evaluate("() => window.__findSmoke.setModal(false)")
    settle(page, "() => !document.querySelector('[data-find-scope]')?.hasAttribute('aria-hidden')")
    open_bar(page, mod)
    check(
        engine,
        mode,
        "the chord works again once the modal is gone",
        state(page).get("open") is True,
    )
    close_bar(page)


def check_hidden_text(page, engine: str, mode: str, mod: str) -> None:
    """Verify display-none, hidden visibility, and hidden contents text are excluded."""
    page.evaluate(
        """() => {
      const scope = document.querySelector('[data-find-scope]') ?? document.body;
      for (const id of ['probe-vis', 'probe-contents', 'probe-none']) {
        document.getElementById(id)?.remove();
      }
      const gone = document.createElement('div');
      gone.id = 'probe-none';
      gone.style.display = 'none';
      gone.textContent = 'zqxjkvbrmp';
      scope.appendChild(gone);

      const plain = document.createElement('div');
      plain.id = 'probe-vis';
      plain.style.visibility = 'hidden';
      plain.textContent = 'zqxjkvbrmp';
      scope.appendChild(plain);

      const wrapper = document.createElement('span');
      wrapper.id = 'probe-contents';
      wrapper.style.display = 'contents';
      wrapper.style.visibility = 'hidden';
      wrapper.textContent = 'zqxjkvbrmp';
      scope.appendChild(wrapper);
    }"""
    )
    frames(page)
    open_bar(page, mod)
    page.keyboard.type("zqxjkvbrmp")
    shown = settled_counter(page)
    check(
        engine,
        mode,
        "hidden text is not findable",
        shown in (None, "", "0/0"),
        f"counter={shown!r} (a non-zero count means hidden text was indexed)",
    )
    close_bar(page)
    page.evaluate(
        """() => {
      document.getElementById('probe-vis')?.remove();
      document.getElementById('probe-contents')?.remove();
      document.getElementById('probe-none')?.remove();
    }"""
    )


def check_content_visibility_reveal(page, engine: str, mode: str, mod: str) -> None:
    """Verify a match below a `content-visibility: auto` placeholder is revealed."""
    planted = page.evaluate(
        """() => {
      const conversation = document.querySelector('[data-find-scope] .mx-auto');
      document.getElementById('cv-probe')?.remove();
      const box = document.createElement('div');
      box.id = 'cv-probe';
      box.style.contentVisibility = 'auto';
      box.style.containIntrinsicSize = 'auto 140px';
      const lines = [];
      for (let i = 0; i < 120; i++) lines.push('filler line ' + i);
      lines.push('zqxjcvneedle at the very bottom');
      box.innerHTML = lines.map((t) => '<p>' + t + '</p>').join('');
      conversation.appendChild(box);
      return Math.round(box.getBoundingClientRect().height);
    }"""
    )
    check(
        engine,
        mode,
        "the placeholder really is standing in for the block",
        planted == 140,
        f"placeholder height={planted}",
    )
    frames(page)
    open_bar(page, mod)
    # Insert whole like a paste: typing reveals on every keystroke and would hide the bug.
    page.keyboard.insert_text("zqxjcvneedle")
    where_js = """() => {
      const p = [...document.querySelectorAll('#cv-probe p')]
        .find((n) => n.textContent.includes('zqxjcvneedle'));
      const r = p.getBoundingClientRect();
      return { top: Math.round(r.top), bottom: Math.round(r.bottom),
               height: window.innerHeight,
               real: Math.round(document.getElementById('cv-probe').getBoundingClientRect().height),
               inViewport: r.top >= 0 && r.bottom <= window.innerHeight };
    }"""
    # Wait for exactly what is asserted below: the block rendered and its match on screen.
    settle(page, f"() => {{ const w = ({where_js})(); return w.real > 1000 && w.inViewport; }}")
    frames(page)
    where = page.evaluate(where_js)
    check(
        engine,
        mode,
        "the block behind the placeholder rendered",
        where["real"] > 1000,
        f"height={where['real']} (still the placeholder)",
    )
    check(
        engine,
        mode,
        "typing the query leaves the match on screen",
        where["inViewport"] is True,
        f"{where} (aimed at the placeholder and never looked again)",
    )
    close_bar(page)
    page.evaluate("() => document.getElementById('cv-probe')?.remove()")


def check_spelling_variants(page, engine: str, mode: str, mod: str) -> None:
    """Verify composed and decomposed spellings share a correctly mapped range."""
    planted = page.evaluate(
        """() => {
      const scope = document.querySelector('[data-find-scope]') ?? document.body;
      document.getElementById('probe-nfd')?.remove();
      const row = document.createElement('div');
      row.id = 'probe-nfd';
      // cafe + combining acute, then " vbnmqz" to keep the query out of the prose.
      row.textContent = 'cafe\u0301 vbnmqz';
      scope.appendChild(row);
      return row.textContent.normalize('NFC') !== row.textContent;
    }"""
    )
    check(engine, mode, "the planted text really is decomposed", planted is True)
    frames(page)
    open_bar(page, mod)
    page.keyboard.type("caf\u00e9 vbnmqz")
    settle(page, COUNTER_IS, arg = "1/1")
    frames(page)
    shown = counter(page)
    check(
        engine,
        mode,
        "a composed query finds decomposed text",
        shown == "1/1",
        f"counter={shown!r}",
    )
    if page.evaluate("() => typeof CSS !== 'undefined' && !!CSS.highlights"):
        covers = page.evaluate(
            """() => {
      const set = CSS.highlights.get('unsloth-find-active');
      const range = set ? [...set][0] : null;
      return range ? range.toString() : null;
    }"""
        )
        check(
            engine,
            mode,
            "the painted range covers what the document wrote",
            covers == "cafe\u0301 vbnmqz",
            f"painted={covers!r} (a drifted offset paints the wrong characters)",
        )
    close_bar(page)
    page.evaluate("() => document.getElementById('probe-nfd')?.remove()")


def check_typing_burst(page, engine: str, mode: str, mod: str) -> None:
    """A word typed quickly searches once, without stalling each following key."""
    open_bar(page, mod)
    field = page.locator('[role="search"] input')
    field.fill("")
    settle(page, COUNTER_IS, arg = "")
    frames(page)
    page.evaluate(
        """() => {
      window.__findSmokePaints = 0;
      if (CSS.highlights) {
        const registry = CSS.highlights;
        const set = registry.set.bind(registry);
        registry.set = (...args) => {
          window.__findSmokePaints += 1;
          return set(...args);
        };
      } else {
        const addRange = Selection.prototype.addRange;
        Selection.prototype.addRange = function (...args) {
          window.__findSmokePaints += 1;
          return addRange.apply(this, args);
        };
      }
    }"""
    )
    page.keyboard.type("doing", delay = 10)
    immediate = field.input_value()
    shown = settled_counter(page)
    paints = page.evaluate("() => window.__findSmokePaints")
    check(
        engine,
        mode,
        "a typing burst leaves every character responsive",
        immediate == "doing",
        f"value={immediate!r}",
    )
    check(
        engine,
        mode,
        "a typing burst coalesces to one search paint",
        paints <= 2 and bool(shown),
        f"paint operations={paints}, counter={shown!r}",
    )
    close_bar(page)


def check_reverted_query_repaints(page, engine: str, mode: str, mod: str) -> None:
    """Restoring the settled query inside the debounce window must restore its paint."""
    if not page.evaluate("() => typeof CSS !== 'undefined' && !!CSS.highlights"):
        return
    open_bar(page, mod)
    field = page.locator('[role="search"] input')
    field.fill("unsloth")
    page.wait_for_function(
        "() => !!window.__findSmoke.counter()",
        timeout = 10000,
    )
    frames(page)
    before = page.evaluate(
        """() => CSS.highlights.has('unsloth-find') &&
          CSS.highlights.has('unsloth-find-active')"""
    )
    field.press("End")
    field.type("x")
    # Deliberate: the Backspace has to land inside the 100 ms settle window.
    page.wait_for_timeout(20)
    field.press("Backspace")
    settle(
        page,
        """() => CSS.highlights.has('unsloth-find') && CSS.highlights.has('unsloth-find-active')
          && !!window.__findSmoke.counter()""",
    )
    frames(page)
    after = page.evaluate(
        """() => ({
          painted: CSS.highlights.has('unsloth-find') &&
            CSS.highlights.has('unsloth-find-active'),
          counter: window.__findSmoke.counter(),
        })"""
    )
    check(
        engine,
        mode,
        "returning to the settled query repaints its unchanged matches",
        before is True and field.input_value() == "unsloth" and after["painted"] is True,
        f"painted before={before}, after={after}, value={field.input_value()!r}",
    )
    close_bar(page)


def check_pending_query_stays_unpainted(page, engine: str, mode: str, mod: str) -> None:
    """Keeps old highlights from repainting during a typing burst."""
    if not page.evaluate("() => typeof CSS !== 'undefined' && !!CSS.highlights"):
        return
    open_bar(page, mod)
    field = page.locator('[role="search"] input')
    field.fill("unsloth")
    page.wait_for_function("() => !!window.__findSmoke.counter()", timeout = 10000)
    frames(page)
    field.press("End")
    field.type("x")
    page.evaluate(
        """() => {
      document.getElementById('pending-repaint-probe')?.remove();
      const row = document.createElement('p');
      row.id = 'pending-repaint-probe';
      row.textContent = 'mutation while the query remains unsettled';
      document.querySelector('[data-find-scope]').appendChild(row);
    }"""
    )
    field.type("abcdefghi", delay = 55)
    pending = {
        "value": field.input_value(),
        "counter": counter(page),
        "painted": page.evaluate(
            """() => CSS.highlights.has('unsloth-find') ||
              CSS.highlights.has('unsloth-find-active')"""
        ),
    }
    check(
        engine,
        mode,
        "an asynchronous reindex stays unpainted while the query is pending",
        pending["value"] != "unsloth"
        and pending["counter"] in (None, "")
        and not pending["painted"],
        f"pending state={pending}",
    )
    settled_counter(page)
    close_bar(page)
    page.evaluate("() => document.getElementById('pending-repaint-probe')?.remove()")


def check_katex_mutation_reindexes(page, engine: str, mode: str, mod: str) -> None:
    """A text mutation in KaTeX's painted aria-hidden tree invalidates the index."""
    page.evaluate(
        """() => {
      const scope = document.querySelector('[data-find-scope]') ?? document.body;
      document.getElementById('probe-katex-mutation')?.remove();
      const host = document.createElement('span');
      host.id = 'probe-katex-mutation';
      host.className = 'katex';
      host.innerHTML = '<span class="katex-mathml">ignored mirror</span>' +
        '<span class="katex-html" aria-hidden="true">mutablekatex</span>';
      scope.appendChild(host);
    }"""
    )
    frames(page)
    open_bar(page, mod)
    page.locator('[role="search"] input').fill("mutablekatex")
    page.wait_for_function(
        "() => window.__findSmoke.counter() === '1/1'",
        timeout = 10000,
    )
    page.evaluate(
        "() => { document.querySelector('#probe-katex-mutation .katex-html').textContent = 'changedkatex'; }"
    )
    settle(page, COUNTER_IS, arg = "0/0")
    frames(page)
    after = counter(page)
    check(
        engine,
        mode,
        "a KaTeX painted-text mutation refreshes the count",
        after in (None, "", "0/0"),
        f"counter={after!r} (1/1 means aria-hidden mutation filtering stayed stale)",
    )
    close_bar(page)
    page.evaluate("() => document.getElementById('probe-katex-mutation')?.remove()")


def check_skip_attribute(page, engine: str, mode: str, mod: str) -> None:
    """Marking a region skippable has to take it out of the count."""
    page.evaluate(
        """() => {
      const scope = document.querySelector('[data-find-scope]') ?? document.body;
      document.getElementById('probe-skip')?.remove();
      const panel = document.createElement('div');
      panel.id = 'probe-skip';
      panel.textContent = 'wqzlmxtvbn wqzlmxtvbn';
      scope.appendChild(panel);
    }"""
    )
    frames(page)
    open_bar(page, mod)
    page.keyboard.type("wqzlmxtvbn")
    before = settled_counter(page)
    check(engine, mode, "the planted panel is counted", bool(before), f"{before!r}")

    # Adding the attribute used to be filtered out of the observer's own batch.
    page.evaluate("() => document.getElementById('probe-skip')?.setAttribute('data-find-skip', '')")
    settle(page, COUNTER_MOVED, arg = before)
    frames(page)
    after = counter(page)
    check(
        engine,
        mode,
        "marking a region data-find-skip reindexes and drops it",
        after != before,
        f"{before!r} -> {after!r} (unchanged means the mutation was filtered out)",
    )
    close_bar(page)
    page.evaluate("() => document.getElementById('probe-skip')?.remove()")


def run_page(page, engine: str, mode: str, mod: str) -> None:
    for probe in (
        check_close_preserves_position,
        check_chord,
        check_counting,
        check_walk,
        check_streaming,
        check_paint_and_teardown,
        check_modal_gate,
        check_hidden_text,
        check_content_visibility_reveal,
        check_spelling_variants,
        check_typing_burst,
        check_reverted_query_repaints,
        check_pending_query_stays_unpainted,
        check_katex_mutation_reindexes,
        check_skip_attribute,
    ):
        try:
            probe(page, engine, mode, mod)
        except Exception as exc:  # noqa: BLE001
            check(
                engine,
                mode,
                f"{probe.__name__} ran to completion",
                False,
                f"{type(exc).__name__}: {str(exc)[:300]}",
            )


def new_page(context):
    page = context.new_page()
    for attempt in range(30):
        try:
            page.goto(URL, wait_until = "domcontentloaded", timeout = 30000)
            break
        except Exception:
            if attempt == 29:
                raise
            time.sleep(2)
    page.wait_for_function("() => !!window.__findSmoke", timeout = 120000)
    return page


def run_entry_chunk_failure(browser, engine: str) -> None:
    """A first-use find chunk failure degrades locally instead of unmounting the shell."""
    context = browser.new_context(user_agent = PLATFORMS["Linux"][1])

    def block_find_entry(route):
        if "find-bar-loader" in route.request.url:
            return route.abort("failed")
        return route.fallback()

    context.route("**/*", block_find_entry)
    page = new_page(context)
    page.keyboard.press("Control+f")
    failure = page.locator('[data-testid="find-in-page-load-failure"]')
    failure.wait_for(state = "visible", timeout = 15000)
    root_text = page.locator("#root").inner_text()
    mode = "entry-chunk-failure"
    check(engine, mode, "the local failure notice is visible", failure.count() == 1)
    check(
        engine,
        mode,
        "the failure offers reload recovery",
        failure.get_by_role("button").count() == 2,
    )

    check(
        engine,
        mode,
        "the failure recovery receives keyboard focus",
        failure.get_by_role("button", name = "Reload").evaluate(
            "button => button === document.activeElement",
        ),
    )
    check(
        engine,
        mode,
        "the conversation shell survives",
        "Message 1" in root_text and "Message 40" in root_text,
        f"root text length={len(root_text)}",
    )
    check(
        engine,
        mode,
        "the failed bar does not leave a search landmark",
        page.locator('[role="search"]').count() == 0,
    )

    if ENTRY_SCREENSHOT:
        Path(ENTRY_SCREENSHOT.format(engine = engine)).parent.mkdir(parents = True, exist_ok = True)
        page.screenshot(path = ENTRY_SCREENSHOT.format(engine = engine), full_page = False)

    failure.get_by_role("button", name = "Close").click()
    check(
        engine,
        mode,
        "the failed bar can be dismissed without reloading",
        failure.count() == 0,
    )
    context.close()


def run_entry_chunk_delay(browser, engine: str) -> None:
    """Keeps query, focus, selection, and commands through a delayed entry handoff."""
    context = browser.new_context(user_agent = PLATFORMS["Linux"][1])
    page = new_page(context)
    composer = page.locator('textarea[placeholder="Message"]')
    composer.focus()
    started = time.monotonic()
    page.keyboard.press("Control+f")
    loading = page.get_by_test_id("find-in-page-loading")
    loading.wait_for(state = "visible", timeout = 5000)
    field = loading.locator("input")
    page.keyboard.type("unsloth", delay = 15)
    field.press("End")
    for _ in range(3):
        field.press("Shift+ArrowLeft")

    field.press("Enter")
    loading_state = field.evaluate(
        """input => ({
          value: input.value,
          start: input.selectionStart,
          end: input.selectionEnd,
          direction: input.selectionDirection,
        })"""
    )
    composer_value = composer.input_value()

    field.press("Enter")
    loading.wait_for(
        state = "detached",
        timeout = max(15000, ENTRY_DELAY_MS + 10000),
    )
    elapsed_ms = (time.monotonic() - started) * 1000
    loaded = page.locator('[role="search"] input')
    loaded_state = loaded.evaluate(
        """input => ({
          value: input.value,
          start: input.selectionStart,
          end: input.selectionEnd,
          direction: input.selectionDirection,
        })"""
    )
    mode = "entry-chunk-delay"
    check(
        engine,
        mode,
        "the controlled loading shell receives early typing",
        loading_state["value"] == "unsloth" and composer_value == "",
        f"loading={loading_state}, composer={composer_value!r}",
    )
    check(
        engine,
        mode,
        "the configured delay holds the real entry chunk",
        elapsed_ms >= ENTRY_DELAY_MS * 0.8,
        f"delay={ENTRY_DELAY_MS}ms, observed={elapsed_ms:.1f}ms",
    )
    check(
        engine,
        mode,
        "the loaded bar retains the query and focus",
        loaded_state["value"] == "unsloth" and state(page).get("focused") is True,
        f"loaded={loaded_state}, state={state(page)}",
    )
    check(
        engine,
        mode,
        "the loaded bar retains the loading input selection",
        loaded_state == loading_state,
        f"loading={loading_state}, loaded={loaded_state}",
    )
    check(
        engine,
        mode,
        "every Enter queued by the loading shell advances after handoff",
        "3/28" in page.locator('[role="search"]').inner_text(),
        page.locator('[role="search"]').inner_text(),
    )
    loaded.fill("studio")
    loaded.evaluate(
        """input => {
          input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));
          input.dispatchEvent(new KeyboardEvent('keydown', {
            key: 'f',
            code: 'KeyF',
            ctrlKey: true,
            bubbles: true,
          }));
        }""",
    )
    settle(page, SEARCH_HAS, arg = "1/8")
    frames(page)
    check(
        engine,
        mode,
        "a repeated find chord clears pending navigation",
        "1/8" in page.locator('[role="search"]').inner_text(),
        page.locator('[role="search"]').inner_text(),
    )

    context.close()

    context = browser.new_context(user_agent = PLATFORMS["Linux"][1])
    page = new_page(context)
    page.locator('textarea[placeholder="Message"]').focus()
    page.keyboard.press("Control+f")
    loading = page.get_by_test_id("find-in-page-loading")
    loading.wait_for(state = "visible", timeout = 5000)
    field = loading.locator("input")
    field.fill("definitely-no-such-match")
    field.press("Enter")
    loading.wait_for(
        state = "detached",
        timeout = max(15000, ENTRY_DELAY_MS + 10000),
    )
    page.evaluate(
        "() => window.__findSmoke.stream("
        "'definitely-no-such-match definitely-no-such-match', 1)",
    )
    settle(page, SEARCH_HAS, arg = "1/2")
    frames(page)
    check(
        engine,
        mode,
        "a zero-result search consumes queued navigation",
        "1/2" in page.locator('[role="search"]').inner_text(),
        page.locator('[role="search"]').inner_text(),
    )
    loaded = page.locator('[role="search"] input')
    loaded.fill("unsloth")
    settle(page, SEARCH_HAS, arg = "1/28")
    frames(page)
    check(
        engine,
        mode,
        "a queued step for an empty result does not advance a later query",
        "1/28" in page.locator('[role="search"]').inner_text(),
        page.locator('[role="search"]').inner_text(),
    )
    context.close()

    context = browser.new_context(user_agent = PLATFORMS["Linux"][1])
    page = new_page(context)
    page.locator('textarea[placeholder="Message"]').focus()
    page.keyboard.press("Control+f")
    loading = page.get_by_test_id("find-in-page-loading")
    loading.wait_for(state = "visible", timeout = 5000)
    field = loading.locator("input")
    field.fill("definitely-no-such-match")
    field.press("Enter")
    field.fill("unsloth")
    loading.wait_for(
        state = "detached",
        timeout = max(15000, ENTRY_DELAY_MS + 10000),
    )
    settle(page, SEARCH_HAS, arg = "1/28")
    frames(page)
    check(
        engine,
        mode,
        "a loading-shell step does not advance a query typed after it",
        "1/28" in page.locator('[role="search"]').inner_text(),
        page.locator('[role="search"]').inner_text(),
    )
    context.close()

    context = browser.new_context(user_agent = PLATFORMS["Linux"][1])
    page = new_page(context)
    page.locator('textarea[placeholder="Message"]').focus()
    page.keyboard.press("Control+f")
    loading = page.get_by_test_id("find-in-page-loading")
    loading.wait_for(state = "visible", timeout = 5000)
    field = loading.locator("input")
    field.dispatch_event("compositionstart", {"data": "u"})
    # Observation window, not readiness: the input must stay mounted across the entry delay.
    page.wait_for_timeout(ENTRY_DELAY_MS + 500)
    check(
        engine,
        mode,
        "an active IME composition keeps the loading input mounted",
        loading.count() == 1 and field.evaluate("input => input.isConnected"),
    )
    field.evaluate(
        """input => {
          input.dispatchEvent(new CompositionEvent('compositionend', { data: '語', bubbles: true }));
          const setValue = Object.getOwnPropertyDescriptor(
            HTMLInputElement.prototype,
            'value',
          ).set;
          setValue.call(input, '語');
          input.dispatchEvent(new InputEvent('input', {
            data: '語',
            inputType: 'insertCompositionText',
            bubbles: true,
          }));
        }""",
    )
    loading.wait_for(
        state = "detached",
        timeout = max(15000, ENTRY_DELAY_MS + 10000),
    )
    check(
        engine,
        mode,
        "the loaded bar takes over after composition ends",
        page.locator('[role="search"] input').count() == 1
        and page.locator('[role="search"] input').input_value() == "語",
    )
    context.close()

    context = browser.new_context(user_agent = PLATFORMS["Linux"][1])
    page = new_page(context)
    page.locator('textarea[placeholder="Message"]').focus()
    page.keyboard.press("Control+f")
    loading = page.get_by_test_id("find-in-page-loading")
    loading.wait_for(state = "visible", timeout = 5000)
    loading.locator("input").dispatch_event(
        "keydown",
        {
            "key": "Escape",
            "code": "Escape",
            "keyCode": 229,
            "isComposing": True,
            "bubbles": True,
            "cancelable": True,
        },
    )
    # Observation window: a composing Escape must NOT close the session.
    page.wait_for_timeout(100)
    check(
        engine,
        mode,
        "a composing Escape leaves the loading find session open",
        loading.count() == 1,
    )
    context.close()

    context = browser.new_context(user_agent = PLATFORMS["Linux"][1])
    page = new_page(context)
    page.locator('textarea[placeholder="Message"]').focus()
    page.keyboard.press("Control+f")
    loading = page.get_by_test_id("find-in-page-loading")
    loading.wait_for(state = "visible", timeout = 5000)
    page.locator("[data-find-scope]").click(position = {"x": 20, "y": 200})
    page.keyboard.press("Escape")
    settle(page, "() => !document.querySelector('[role=\"search\"]')")
    check(
        engine,
        mode,
        "Escape closes the loading find session after focus leaves it",
        page.locator('[role="search"]').count() == 0,
    )
    context.close()


def run_engine(pw, engine: str) -> None:
    launch = {"args": chromium_launch_args()} if engine == "chromium" else {}
    browser = getattr(pw, engine).launch(**launch)

    if ENTRY_FAIL:
        try:
            run_entry_chunk_failure(browser, engine)
        finally:
            browser.close()
        return

    if ENTRY_DELAY_MS:
        try:
            run_entry_chunk_delay(browser, engine)
        finally:
            browser.close()
        return
    try:
        for platform, (nav_platform, agent) in PLATFORMS.items():
            context = browser.new_context(user_agent = agent)
            context.add_init_script(
                "Object.defineProperty(navigator, 'platform', "
                f"{{ get: () => {json.dumps(nav_platform)} }});"
            )
            page = new_page(context)
            run_page(page, engine, platform, MOD[platform])
            context.close()

        for mode, script in (
            ("no-highlight-api", NO_HIGHLIGHT_API),
            ("legacy-checkVisibility", LEGACY_CHECK_VISIBILITY),
            ("no-checkVisibility", NO_CHECK_VISIBILITY),
        ):
            context = browser.new_context(user_agent = PLATFORMS["Linux"][1])
            context.add_init_script(
                "Object.defineProperty(navigator, 'platform', { get: () => 'Linux x86_64' });"
            )
            context.add_init_script(script)
            page = new_page(context)
            run_page(page, engine, mode, "Control")
            context.close()
    finally:
        browser.close()


def main() -> int:
    if ENTRY_DELAY_MS:
        os.environ["SMOKE_MODULE_DELAY_MATCH"] = "find-bar-loader"
        os.environ["SMOKE_MODULE_DELAY_MS"] = str(ENTRY_DELAY_MS)

    server = start_vite(PORT)
    try:
        wait_for_smoke_page(URL, "smoke-find-in-page-main.tsx", proc = server)
        with sync_playwright() as pw:
            for engine in ENGINES:
                run_engine(pw, engine)
    finally:
        stop_process(server)

    total = passed + len(failures)
    print(f"[find-in-page] {passed}/{total} checks passed across {', '.join(ENGINES)}")
    for failure in failures:
        print(f"[find-in-page] FAIL {failure}")
    return 1 if failures else 0


if __name__ == "__main__":
    raise SystemExit(main())