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.

13.7 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

"""The two autoscroll harnesses must compare every metric they record, or they go false-green."""

import ast
import types

import pytest
from pathlib import Path


ROOT = Path(__file__).resolve().parents[2]
STUDIO_TESTS = ROOT / "tests" / "studio"


def _require_playwright_page():
    """Test the symbol the harness imports, since a bare playwright package imports as a namespace."""
    sync_api = pytest.importorskip("playwright.sync_api")
    if not hasattr(sync_api, "Page"):
        pytest.skip(
            "playwright.sync_api resolved from "
            f"{getattr(sync_api, '__file__', None) or list(getattr(sync_api, '__path__', []))} "
            "but has no Page; playwright is not usably installed here"
        )


def source(name: str) -> str:
    return (STUDIO_TESTS / name).read_text(encoding = "utf-8")


def verdict(name: str) -> str:
    """Everything from `def main()` on: the only place a metric turns into an exit code."""
    text = source(name)
    start = text.index("def main()")
    return text[start:]


def test_chat_autoscroll_asserts_the_detached_stream_actually_grew() -> None:
    # `stillDetached` means nothing unless the stream added height (`grewWhileDetached`).
    main = verdict("playwright_chat_autoscroll.py")
    assert 'intent["stillDetached"]' in main
    assert 'intent["grewWhileDetached"]' in main


def test_research_freeze_asserts_the_dialog_took_the_modal_layer() -> None:
    # The stranded `pointer-events: none` checks only test something if the dialog went modal first.
    main = verdict("playwright_research_freeze.py")
    assert 'modal["body_pointer_events_after_approve"]' in main
    assert 'modal["body_pointer_events_while_open"]' in main


def test_research_freeze_asserts_the_stream_had_something_to_follow() -> None:
    # An empty activity list measures nothing; compare against the pre-stream baseline, not zero.
    main = verdict("playwright_research_freeze.py")
    assert 'stream["raf_per_second"]' in main
    assert 'stream["activities"]' in main
    assert 'stream["activities_before"]' in main


def test_research_freeze_asserts_the_report_stall_and_its_own_probe() -> None:
    # A stall of zero (no second sample) must fail too, or the budget passes on any tree.
    main = verdict("playwright_research_freeze.py")
    assert 'results["report"]["main_thread_stall_ms"] > MAIN_THREAD_STALL_BUDGET_MS' in main
    assert 'results["report"]["main_thread_stall_ms"] <= 0' in main


def test_research_freeze_keeps_a_hit_tested_click_in_the_report_phase() -> None:
    # A synthetic element.click() skips hit testing, so the verdict must read actionability.
    source_text = source("playwright_research_freeze.py")
    assert "page.click('[data-smoke=\"click-probe\"]'" in source_text
    main = verdict("playwright_research_freeze.py")
    assert 'results["report"]["click_landed"]' in main
    assert 'results["report"]["clicks_registered"]' in main


def test_harnesses_report_why_the_page_failed() -> None:
    # A thrown entry module and a slow one both look like a locator timeout with no output.
    for name in (
        "playwright_chat_autoscroll.py",
        "playwright_research_freeze.py",
        "playwright_strip_ansi_smoke.py",
    ):
        assert "echo_browser_errors(page, info)" in source(
            name
        ), f"{name} discards pageerror and console.error, so a crashed page reads as a timeout"


def test_ansi_smoke_keeps_the_failed_page_and_the_server_output() -> None:
    # Keep the screenshot, body excerpt and vite transform errors; the live log dies with the runner.
    text = source("playwright_strip_ansi_smoke.py")
    assert "dump(page, vite)" in text, "the assertions do not run under the dump"
    assert "dump_diagnostics(page, ART" in text
    assert 'getattr(vite, "vite_tail"' in text, "vite's output is dropped on failure"


def test_the_ansi_dump_survives_a_vite_server_that_is_still_talking(tmp_path, monkeypatch) -> None:
    # A daemon thread fills the tail deque while vite lives; the dump runs before the server stops.
    import importlib.util
    import threading
    from collections import deque

    import pytest

    _require_playwright_page()
    monkeypatch.setenv("PW_ART_DIR", str(tmp_path / "art"))
    spec = importlib.util.spec_from_file_location(
        "_ansi_smoke_under_test", STUDIO_TESTS / "playwright_strip_ansi_smoke.py"
    )
    assert spec is not None and spec.loader is not None
    smoke = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(smoke)

    tail: deque[str] = deque(maxlen = 20)
    for index in range(tail.maxlen or 20):
        tail.append(f"vite line {index}")
    vite = types.SimpleNamespace(vite_tail = tail)
    stop = threading.Event()

    def keep_talking() -> None:
        index = 0
        while not stop.is_set():
            tail.append(f"[vite] page reload {index}")
            index += 1

    talker = threading.Thread(target = keep_talking, daemon = True)
    talker.start()
    try:
        for _ in range(5):
            smoke.dump(types.SimpleNamespace(), vite)
    finally:
        stop.set()
        talker.join(timeout = 5)


def test_stream_pacing_asserts_its_long_task_probe_measured_something() -> None:
    # longTaskMs is 0 both when the render is free and when the observer never ran (non-Chromium).
    main = verdict("playwright_stream_pacing.py")
    assert 'results.get("longTaskSupported")' in main
    assert 'results["longTasks"] <= 0' in main
    # An unthrottled renderer keeps up with any rate, so every budget would pass.
    assert 'results["cpu_throttle"] <= 1' in main


def test_stream_pacing_asserts_the_reply_was_actually_painted() -> None:
    # A page that rendered nothing scores zero on every budget, so assert the workload first.
    main = verdict("playwright_stream_pacing.py")
    assert 'results["paintedChars"] < floor' in main
    assert 'results["arrivals"]' in main
    # paintedChars only climbs, so check the length at settlement too.
    assert 'results["settledChars"] < floor' in main


def test_harnesses_own_their_dev_server() -> None:
    # A shared server outlives a killed wrapper and strands the port; each harness owns its own.
    for name in (
        "playwright_chat_autoscroll.py",
        "playwright_research_freeze.py",
        "playwright_strip_ansi_smoke.py",
        "playwright_thread_weight.py",
        "playwright_stream_pacing.py",
    ):
        text = source(name)
        assert "start_vite" in text, f"{name} does not start its own server"
        assert "stop_process" in text, f"{name} never tears its server down"
        # Vite's SPA fallback answers 200 for a page that no longer exists.
        assert "wait_for_smoke_page" in text, f"{name} gates on status rather than on content"


# The thread-weight harness asserts no budgets, so every metric it records must reach the
# printed table instead.
THREAD_WEIGHT = "playwright_thread_weight.py"


def thread_weight_verdict() -> str:
    """Everything from `def harness_failures(` on. Unlike the harnesses above, this one turns
    metrics into an exit code there rather than in `main()`."""
    text = source(THREAD_WEIGHT)
    return text[text.index("def harness_failures(") :]


def _dict_keys(node: ast.AST, helpers: dict[str, set[str]]) -> set[str]:
    """String keys of a dict literal, following `**helper(...)` into the helper's return dict."""
    keys: set[str] = set()
    if not isinstance(node, ast.Dict):
        return keys
    for key, value in zip(node.keys, node.values):
        if key is None:
            call = value
            if isinstance(call, ast.Call) and isinstance(call.func, ast.Name):
                keys |= helpers.get(call.func.id, set())
            continue
        if isinstance(key, ast.Constant) and isinstance(key.value, str):
            keys.add(key.value)
    return keys


def _thread_weight_recorded() -> dict[str, set[str]]:
    """{action: recorded metric names} for every `result["action"] = {...}` in the harness."""
    tree = ast.parse(source(THREAD_WEIGHT))
    helpers: dict[str, set[str]] = {}
    for node in ast.walk(tree):
        if isinstance(node, ast.FunctionDef):
            for statement in node.body:
                if isinstance(statement, ast.Return):
                    found = _dict_keys(statement.value, {})
                    if found:
                        helpers[node.name] = found
    recorded: dict[str, set[str]] = {}
    for node in ast.walk(tree):
        if not isinstance(node, ast.Assign) or len(node.targets) != 1:
            continue
        target = node.targets[0]
        if (
            isinstance(target, ast.Subscript)
            and isinstance(target.value, ast.Name)
            and target.value.id == "result"
            and isinstance(target.slice, ast.Constant)
            and isinstance(target.slice.value, str)
        ):
            keys = _dict_keys(node.value, helpers)
            if keys:
                recorded[target.slice.value] = keys
    return recorded


def test_thread_weight_records_the_four_actions() -> None:
    # An empty result set would let the coverage test below pass vacuously.
    recorded = _thread_weight_recorded()
    assert {"keystroke", "scroll", "menu", "delete"} <= set(recorded)
    # Each action records its timings plus five CDP counters and three long-task fields.
    for action, keys in recorded.items():
        assert len(keys) >= 10, f"{action} recorded only {len(keys)} metrics: {sorted(keys)}"


def test_thread_weight_row_shapes_stay_distinct() -> None:
    """TABLE_ROWS is (label, pick) and GROWTH_AXES is (label, pick, floored); a bulk edit that lands
    in the wrong one makes print_table die after a forty-minute run, before anything is shown."""
    import importlib
    import sys

    sys.path.insert(0, str(STUDIO_TESTS))
    pytest = importlib.import_module("pytest")
    _require_playwright_page()
    module = importlib.import_module("playwright_thread_weight")
    assert all(len(row) == 2 for row in module.TABLE_ROWS)
    assert all(len(row) == 3 for row in module.GROWTH_AXES)


def test_thread_weight_prints_every_metric_it_records() -> None:
    text = source(THREAD_WEIGHT)
    table = text[text.index("TABLE_ROWS = (") : text.index("def print_table")]
    missing = [
        f'{action}["{metric}"]'
        for action, metrics in _thread_weight_recorded().items()
        for metric in sorted(metrics)
        if f'r["{action}"]["{metric}"]' not in table
    ]
    assert not missing, (
        "recorded but never printed, so the harness would report a curve with these missing: "
        + ", ".join(missing)
    )


def test_thread_weight_proves_it_discriminates_rather_than_gating_on_a_budget() -> None:
    # It must fail when nothing was clicked. Asserted against the verdict region only, since the
    # verdict text also appears elsewhere in the source.
    main = thread_weight_verdict()
    assert "GROWTH_AXES" in main
    assert "no measured axis rose with N" in main
    # The menu opens non-modally; the verdict must reject a run mixing modes across N.
    assert 'menu["body_pointer_events_while_open"]' in main
    assert 'menu["items_while_open"]' in main
    assert 'deleted["messages_after"]' in main
    # A keystroke that never reached the runtime still reports the double-rAF paint floor.
    assert 'row["paint_floor_ms"]' in main
    assert 'keystroke["runtime_text"] != keystroke["dom_text"]' in main
    # Network requests and console warnings each cost a CDP round trip that grows with N.
    assert 'row["stray_api_requests"]' in main
    assert 'row["console_warnings"]' in main


def _thread_weight_row(deleted: bool = True, layer: str = "auto") -> dict:
    """One healthy column of the thread-weight table, optionally with a delete that did nothing."""
    return {
        "counts": {
            "messages": 10_000,
            "codeBlocks": 4,
            "katexNodes": 4,
            "actionBars": 3,
            "tooltipTriggers": 8,
        },
        "stray_api_requests": 0,
        "console_warnings": 0,
        "first_console_warning": None,
        "viewport": {"scrollHeight": 9000, "clientHeight": 800, "scrollTop": 0},
        "paint_floor_ms": 33,
        "keystroke": {"median_ms": 120, "runtime_text": "x" * 40, "dom_text": "x" * 40},
        "scroll": {"wall_ms": 100, "scrolled_px": 10_000},
        "menu": {
            "open_ms": 10,
            "close_ms": 10,
            "body_pointer_events_after_close": "auto",
            "body_pointer_events_while_open": layer,
            "items_while_open": 5,
            "triggers_while_hovered": 8,
        },
        "delete": {
            "ms": 5 if deleted else None,
            "messages_before": 10,
            "messages_after": 9 if deleted else 10,
        },
    }


def test_thread_weight_rejects_a_dead_delete_at_every_size() -> None:
    """The delete checks once sat under the whole-run modal-layer `if`, so a table whose delete
    never removed anything still passed while another axis grew. They belong to the per-size
    loop: every measured column has to prove its own delete, not just the last one."""
    import importlib
    import sys

    sys.path.insert(0, str(STUDIO_TESTS))
    pytest = importlib.import_module("pytest")
    _require_playwright_page()
    module = importlib.import_module("playwright_thread_weight")

    sizes = [10, 50]
    for dead in sizes:
        results = {
            "sizes": sizes,
            "by_size": {str(size): _thread_weight_row(deleted = size != dead) for size in sizes},
        }
        assert f"N={dead} never deleted a message" in module.harness_failures(results)
    healthy = {
        "sizes": sizes,
        "by_size": {str(size): _thread_weight_row() for size in sizes},
    }
    assert not [f for f in module.harness_failures(healthy) if "delete" in f]