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.

3.3 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 reload must settle on the pill, never networkidle, which hangs after Cache-Control: no-store."""

from __future__ import annotations

import ast
from pathlib import Path


DRIVER = (Path(__file__).resolve().parent / "playwright_chat_ui.py").read_text(encoding = "utf-8")
STEP = "exercise_permission_mode_controls"
HELPER = "reload_and_wait_for_pill"


def _helper() -> ast.FunctionDef:
    for node in ast.walk(ast.parse(DRIVER)):
        if isinstance(node, ast.FunctionDef) and node.name == STEP:
            for inner in ast.walk(node):
                if isinstance(inner, ast.FunctionDef) and inner.name == HELPER:
                    return inner
    raise AssertionError(f"{HELPER} is no longer defined inside {STEP}")


def _calls(fn: ast.FunctionDef, name: str) -> list[ast.Call]:
    return [
        node
        for node in ast.walk(fn)
        if isinstance(node, ast.Call)
        and isinstance(node.func, ast.Attribute)
        and node.func.attr == name
    ]


def test_the_permission_reload_never_waits_for_networkidle():
    helper = _helper()
    idle = [
        call
        for call in _calls(helper, "wait_for_load_state")
        if any(
            isinstance(value, ast.Constant) and value.value == "networkidle"
            for value in [*call.args[:1], *(kw.value for kw in call.keywords if kw.arg == "state")]
        )
    ]
    assert not idle, (
        f"{HELPER} waits for networkidle again; with the step's page.route and no-store /api reads "
        "that wait never returns and the watchdog kills the job"
    )
    for call in _calls(helper, "reload"):
        for keyword in call.keywords:
            if keyword.arg == "wait_until":
                assert getattr(keyword.value, "value", None) != "networkidle", ast.unparse(call)


def test_the_permission_reload_settles_on_a_bounded_pill_wait():
    helper = _helper()
    reloads = _calls(helper, "reload")
    visible = _calls(helper, "to_be_visible")
    assert reloads and visible, ast.unparse(helper)
    assert reloads[0].lineno < visible[0].lineno, "the pill wait must follow the reload"
    timeout = next((kw.value for kw in visible[0].keywords if kw.arg == "timeout"), None)
    assert isinstance(timeout, ast.Constant) and 0 < timeout.value <= 60_000, ast.unparse(
        visible[0]
    )


def test_a_reload_that_booted_the_app_gets_no_second_chance():
    """The one extra reload is for an app shell that never started (no composer, no /api calls).
    A page that booted and still has no pill is the regression this step exists to catch."""
    source = (Path(__file__).resolve().parent / "playwright_chat_ui.py").read_text(encoding = "utf-8")
    start = source.index("def reload_and_wait_for_pill():")
    body = source[start : source.index("\n    # choose()", start)]
    assert body.count("page.reload(") == 2, "the step reloads more than once extra"
    guard = body.index('if state.get("composer")')
    assert (
        body[guard : body.index("\n", body.index("raise", guard))].rstrip().endswith("raise")
    ), "a booted page without the pill must fail, not retry"
    assert (
        body.index("_boot_state()") < body.index('shoot("04-permission-pill-missing")') < guard
    ), "record the page state and a screenshot before deciding"