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.

Studio Playwright: condition waits and step budgets in thread_scoped_settings, mcp_arguments, chat_width (#11991)

Daniel Han2849e7c8b2d0
diff --git a/tests/studio/playwright_chat_width.py b/tests/studio/playwright_chat_width.py
index d2e8ceda691..dd7d8014856 100644
--- a/tests/studio/playwright_chat_width.py
+++ b/tests/studio/playwright_chat_width.py
@@ -15,14 +15,35 @@ Run with: python tests/studio/playwright_chat_width.py
"""
import os
+import sys
import time
import uuid
+from pathlib import Path
from playwright.sync_api import sync_playwright
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+from _playwright_robust import ( # noqa: E402
+ install_wall_clock_watchdog,
+ report_failing_step,
+ step_budget_s,
+ wait_for_settled,
+)
+
BASE = os.environ.get("BASE_URL", "http://127.0.0.1:8888")
PASSWORD = os.environ.get("STUDIO_NEW_PW") or os.environ.get("STUDIO_PW", "")
TIMEOUT_MS = 60_000
+# A step that runs past its budget stops the run with its name, instead of the next steps
+# waiting out their own timeouts. Sized on the waits each step chains at TIMEOUT_MS; a
+# hosted runner does each in seconds.
+STEP_BUDGET_S = step_budget_s(360)
+_watchdog = None
+
+
+def step(name):
+ print(f"[chat-width] STEP {name}", flush = True)
+ if _watchdog is not None:
+ _watchdog.begin_step(name, STEP_BUDGET_S)
def api(
@@ -148,6 +169,7 @@ def check_widths(page):
thread_url = page.url
measurements = {}
for preset in ("Standard", "Wide", "Full width"):
+ step(f"measure the {preset!r} preset across 7 viewports")
page.set_viewport_size({"width": 1440, "height": 900})
page.keyboard.press("Control+,")
page.get_by_role("dialog").get_by_role("button", name = "Appearance", exact = True).click()
@@ -157,7 +179,9 @@ def check_widths(page):
measurements[preset] = {}
for width in (390, 768, 900, 1280, 1536, 1920, 2560):
page.set_viewport_size({"width": width, "height": 900})
- page.wait_for_timeout(300)
+ # Measure once both surfaces have reflowed to the new viewport, not 300 ms after it.
+ wait_for_settled(page.locator(".aui-assistant-message-root"))
+ wait_for_settled(page.locator(".unsloth-composer-shell"))
measurements[preset][width] = page.evaluate(
"""() => {
const width = selector => document.querySelector(selector).getBoundingClientRect().width;
@@ -173,11 +197,12 @@ def check_widths(page):
page.goto(thread_url.split("?")[0] + "?new=width-transition")
shell = page.locator(".unsloth-composer-shell")
shell.wait_for()
- page.wait_for_timeout(300)
+ wait_for_settled(shell)
welcome_width = shell.bounding_box()["width"]
assert abs(welcome_width - measurements[preset][1920]["composer"]) <= 1
page.goto(thread_url)
page.locator(".aui-assistant-message-root").wait_for()
+ step("compare the presets")
for width, full in measurements["Full width"].items():
standard = measurements["Standard"][width]
wide = measurements["Wide"][width]
@@ -206,9 +231,14 @@ def check_widths(page):
if __name__ == "__main__":
+ _watchdog = install_wall_clock_watchdog(
+ float(os.environ.get("STUDIO_UI_WALL_TIMEOUT_S", "600")), label = "chat-width"
+ )
+ report_failing_step(_watchdog, label = "chat-width")
with sync_playwright() as playwright:
browser = playwright.chromium.launch()
page = browser.new_page(viewport = {"width": 1440, "height": 900}, reduced_motion = "reduce")
+ step("sign in and seed a thread")
token = sign_in(page)
thread_id = os.environ.get("CHAT_THREAD_ID") or seed_thread(page, token)
page.goto(
@@ -217,3 +247,4 @@ if __name__ == "__main__":
page.locator(".aui-assistant-message-root").wait_for(timeout = TIMEOUT_MS)
print(check_widths(page))
browser.close()
+ _watchdog.cancel()
diff --git a/tests/studio/playwright_mcp_arguments.py b/tests/studio/playwright_mcp_arguments.py
index 883d625b115..aac4201efda 100644
--- a/tests/studio/playwright_mcp_arguments.py
+++ b/tests/studio/playwright_mcp_arguments.py
@@ -19,7 +19,10 @@ from _playwright_robust import ( # noqa: E402
chromium_launch_args,
install_view_transition_killer,
install_wall_clock_watchdog,
+ report_failing_step,
+ step_budget_s,
wait_for_health,
+ wait_until,
)
@@ -39,12 +42,23 @@ SOURCE_FIXTURE = (
/ "mcp_argument_echo_server.py"
)
MCP_PYTHON = os.environ.get("STUDIO_MCP_PYTHON", sys.executable)
+# Each step below is a handful of 15 s actions and at most two 30 s connection probes; a
+# hosted runner does one in a few seconds. Past its budget the run stops, naming the step,
+# instead of the steps after it waiting out their own timeouts.
+STEP_BUDGET_S = step_budget_s(150)
+_watchdog = None
def info(message: str) -> None:
print(f"[mcp-ui] {message}", flush = True)
+def step(name: str) -> None:
+ print(f"[mcp-ui] STEP {name}", flush = True)
+ if _watchdog is not None:
+ _watchdog.begin_step(name, STEP_BUDGET_S)
+
+
def api(
path: str,
payload: dict | None = None,
@@ -153,6 +167,8 @@ def screenshot(dialog, name: str) -> None:
def delay_real_response(seconds: float):
def handler(route):
response = route.fetch()
+ # Kept: the delay is the fixture. It holds the decode response back so the loading
+ # state is on screen long enough to be asserted and captured.
time.sleep(seconds)
route.fulfill(response = response)
@@ -166,18 +182,29 @@ def hold_next_request(page, pattern: str) -> list:
def release_request(page, held: list) -> None:
- page.wait_for_timeout(50)
+ # Until the route has actually caught the request, instead of 50 ms. The assertion below
+ # still decides.
+ try:
+ wait_until(lambda: held, timeout_s = 10, what = "the held request", interval_s = 0.02, page = page)
+ except TimeoutError:
+ pass
assert len(held) == 1
held[0].continue_()
def run(page, launch_log: Path, fixture: Path) -> None:
+ step("open /chat with the seeded session")
page.goto(BASE + "/hub", wait_until = "domcontentloaded")
page.goto(BASE + "/chat", wait_until = "domcontentloaded")
- page.wait_for_timeout(1000)
+ # Until the app has either shown the composer's MCP control (signed in) or bounced to a
+ # sign-in form, instead of a fixed second; the URL check below decides which.
+ page.get_by_role("button", name = "MCP servers").or_(
+ page.locator("#password, #new-password")
+ ).first.wait_for(state = "visible", timeout = 30_000)
if page.url.startswith(BASE + "/login") or page.url.startswith(BASE + "/change-password"):
raise AssertionError(f"not authenticated: {page.url}")
+ step("create a stdio server with awkward arguments")
dialog = open_dialog(page)
dialog.get_by_role("button", name = "Add server").click()
dialog.locator("#mcp-display-name").fill("Playwright argument echo")
@@ -207,6 +234,7 @@ def run(page, launch_log: Path, fixture: Path) -> None:
row = row_for(dialog, "Playwright argument echo")
expect(row).to_contain_text("--flag")
+ step("edit hydrates the arguments, with a loading state")
page.route("**/api/mcp/servers/stdio/decode", delay_real_response(1.5))
row.get_by_role("button", name = "Edit server").click(no_wait_after = True)
loading = dialog.get_by_text("Reading local command…", exact = True)
@@ -219,8 +247,9 @@ def run(page, launch_log: Path, fixture: Path) -> None:
dialog.get_by_role("button", name = "Cancel").click()
dialog.get_by_role("button", name = "Close").click()
+ step("arguments persist across a reload and an edit")
page.reload(wait_until = "domcontentloaded")
- page.wait_for_timeout(500)
+ # open_dialog's first click waits for the control; the fixed 500 ms after the reload is gone.
dialog = open_dialog(page)
row = row_for(dialog, "Playwright argument echo")
row.get_by_role("button", name = "Edit server").click()
@@ -240,6 +269,7 @@ def run(page, launch_log: Path, fixture: Path) -> None:
assert_arguments(dialog, edited)
screenshot(dialog, "fixed-edit-persisted")
+ step("refresh relaunches once and locks the row while it runs")
dialog.get_by_role("button", name = "Cancel").click()
row = row_for(dialog, "Playwright argument echo")
launches_before_refresh = len(launch_log.read_text(encoding = "utf-8").splitlines())
@@ -258,6 +288,7 @@ def run(page, launch_log: Path, fixture: Path) -> None:
launches_after_refresh = len(launch_log.read_text(encoding = "utf-8").splitlines())
assert launches_after_refresh == launches_before_refresh + 1
+ step("import a config while the dialog and composer show it busy")
imported = {
"mcpServers": {
"Playwright imported echo": {
@@ -306,6 +337,7 @@ def run(page, launch_log: Path, fixture: Path) -> None:
timeout = 30_000
)
+ step("a failed decode keeps the form, and switching transport clears credentials")
row = row_for(dialog, "Playwright argument echo")
page.route(
"**/api/mcp/servers/stdio/decode",
@@ -359,12 +391,15 @@ def run(page, launch_log: Path, fixture: Path) -> None:
screenshot(dialog, "fixed-transport-credentials-cleared")
dialog.get_by_role("button", name = "Cancel").click()
+ step("the composer preset waits for a refresh and recovers from a list error")
dialog.get_by_role("button", name = "Close").click()
composer = page.get_by_role("button", name = "MCP servers")
expect(composer).to_be_visible()
composer.click()
preset = page.get_by_role("menuitem", name = "Unsloth Docs")
expect(preset).to_be_enabled()
+ # Kept: lets a list refresh the menu opening may have started go out before the route
+ # below starts holding the next GET, so the one it holds is the preset's own.
page.wait_for_timeout(250)
held_list: list = []
@@ -397,6 +432,7 @@ def run(page, launch_log: Path, fixture: Path) -> None:
page.unroute("**/api/mcp/servers/", hold_first_list)
expect(preset).to_be_enabled(timeout = 10_000)
preset.click()
+ # Kept: a "nothing more may happen" window; the second click must add one PUT and no POST.
page.wait_for_timeout(500)
assert writes.count("POST") == 1
assert writes.count("PUT") == 1
@@ -426,6 +462,7 @@ def run(page, launch_log: Path, fixture: Path) -> None:
expect(dialog.locator("li").filter(has_text = "Unsloth Docs")).to_have_count(1)
screenshot(dialog, "fixed-composer-preset-no-duplicate")
+ step("leaving the route closes a pending delete confirmation")
row = row_for(dialog, "Playwright argument echo")
row.get_by_role("button", name = "Delete server").click()
expect(page.get_by_role("alertdialog", name = "Delete MCP server")).to_be_visible()
@@ -465,7 +502,16 @@ def main() -> int:
"localStorage.setItem('unsloth_chat_mcp_enabled', 'true');"
"})();"
)
- install_wall_clock_watchdog(WALL_TIMEOUT_S, label = "mcp-arguments", info = info)
+ global _watchdog
+ # The whole run keeps the absolute WALL_TIMEOUT_S it always had (the total cap); steps
+ # starting do not extend it.
+ _watchdog = install_wall_clock_watchdog(
+ WALL_TIMEOUT_S,
+ label = "mcp-arguments",
+ info = info,
+ total_deadline_s = WALL_TIMEOUT_S,
+ )
+ report_failing_step(_watchdog, label = "mcp-arguments")
with sync_playwright() as playwright:
if BROWSER not in ("chromium", "firefox", "webkit"):
raise AssertionError(f"unsupported browser: {BROWSER}")
diff --git a/tests/studio/playwright_thread_scoped_settings.py b/tests/studio/playwright_thread_scoped_settings.py
index 1ae5fd8cc24..783ad7a3545 100644
--- a/tests/studio/playwright_thread_scoped_settings.py
+++ b/tests/studio/playwright_thread_scoped_settings.py
@@ -23,6 +23,15 @@ from pathlib import Path
from playwright.sync_api import expect, sync_playwright
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+from _playwright_robust import ( # noqa: E402
+ install_wall_clock_watchdog,
+ report_failing_step,
+ step_budget_s,
+ wait_for_settled,
+ wait_until,
+)
+
BASE = os.environ["BASE_URL"]
NEW = os.environ["STUDIO_NEW_PW"]
ART = Path(os.environ.get("PW_ART_DIR", "logs/playwright"))
@@ -39,12 +48,58 @@ GLOBAL_KEYS = (
"unsloth_chat_confirm_tool_calls",
)
+# Each step drives a few clicks and reads; a hosted runner does one in a few seconds. A step
+# that runs past its budget stops the run, named, instead of the next steps waiting out their
+# own timeouts. The inactivity budget is the old worst case of one step's waits.
+STEP_BUDGET_S = step_budget_s(max(180.0, 6 * TIMEOUT_MS / 1000))
+# Sign-in chains its own 60 s navigations and form waits, then the model unload.
+SIGN_IN_BUDGET_S = step_budget_s(max(360.0, 12 * TIMEOUT_MS / 1000))
+WALL_TIMEOUT_S = float(os.environ.get("STUDIO_UI_WALL_TIMEOUT_S", "600"))
+# The snapshot write and the settings mirror are both debounced 400 ms
+# (THREAD_SETTINGS_DEBOUNCE_MS / SETTINGS_DEBOUNCE_MS in chat-runtime-store.ts), so /api/chat
+# traffic has to stay quiet for longer than that before a thread counts as settled.
+CHAT_QUIET_S = 0.6
+
_step = [0]
+_watchdog = None
-def step(message):
+def step(message, budget_s = None):
_step[0] += 1
- print(f"[thread-settings] STEP {_step[0]}: {message}", flush = True)
+ name = f"{_step[0]}: {message}"
+ print(f"[thread-settings] STEP {name}", flush = True)
+ if _watchdog is not None:
+ _watchdog.begin_step(name, STEP_BUDGET_S if budget_s is None else budget_s)
+
+
+# Counts this document's in-flight fetches to the thread and settings endpoints, which carry
+# the snapshot GET and the debounced writes; see settle(). Counted in the page because the
+# Playwright request events left some of these open forever across the sign-in navigation.
+CHAT_TRAFFIC_JS = """
+(() => {
+ const traffic = (window.__chatTraffic = { inflight: 0, last: performance.now() });
+ const realFetch = window.fetch;
+ window.fetch = function (input, init) {
+ let path = "";
+ try {
+ path = new URL(String((input && input.url) || input), location.href).pathname;
+ } catch (_e) {}
+ if (!path.startsWith("/api/chat/threads") && !path.startsWith("/api/chat/settings")) {
+ return realFetch.apply(this, arguments);
+ }
+ traffic.inflight += 1;
+ traffic.last = performance.now();
+ const done = () => {
+ traffic.inflight -= 1;
+ traffic.last = performance.now();
+ };
+ return realFetch.apply(this, arguments).then(
+ (response) => { done(); return response; },
+ (error) => { done(); throw error; },
+ );
+ };
+})();
+"""
def fail(message):
@@ -89,7 +144,7 @@ def api(
def sign_in(page):
- step("sign in, then land on /chat")
+ step("sign in, then land on /chat", SIGN_IN_BUDGET_S)
page.goto(f"{BASE}/change-password", wait_until = "domcontentloaded", timeout = 60_000)
try:
page.locator("#new-password").wait_for(state = "visible", timeout = 15_000)
@@ -253,7 +308,35 @@ def unload_any_model(page, token):
}""",
{"base": BASE, "token": token, "modelPath": loaded},
)
- page.wait_for_timeout(1500)
+ # Until the unload has finished, instead of a fixed 1.5 s. Not fatal here, as the pause was
+ # not: a pill that stays disabled fails the step that clicks it.
+ try:
+ wait_until(
+ lambda: not _loaded_model(page, token),
+ timeout_s = 60,
+ what = f"{loaded!r} to unload",
+ interval_s = 0.25,
+ page = page,
+ )
+ except TimeoutError as exc:
+ print(f"[thread-settings] WARN {exc}", flush = True)
+
+
+def _loaded_model(page, token):
+ status = (
+ page.evaluate(
+ """async ({ base, token }) => {
+ const res = await fetch(base + "/api/inference/status", {
+ headers: { Authorization: "Bearer " + token },
+ });
+ if (!res.ok) return null;
+ return await res.json();
+ }""",
+ {"base": BASE, "token": token},
+ )
+ or {}
+ )
+ return status.get("model_identifier") or (status.get("loaded") or [None])[0]
def settle(page):
@@ -261,8 +344,18 @@ def settle(page):
page.locator('button[data-pill-label="Search"]:visible').first.wait_for(
state = "visible", timeout = TIMEOUT_MS
)
- # the snapshot arrives on a GET, and the pin write is debounced behind it.
- page.wait_for_timeout(1200)
+ # the snapshot arrives on a GET, and the pin write is debounced behind it. Instead of a fixed
+ # 1.2 s, wait until those endpoints have been quiet for longer than that debounce.
+ page.wait_for_function(
+ """([quietMs]) => {
+ const traffic = window.__chatTraffic;
+ return !!traffic && traffic.inflight === 0
+ && performance.now() - traffic.last >= quietMs;
+ }""",
+ arg = [CHAT_QUIET_S * 1000],
+ polling = 50,
+ timeout = TIMEOUT_MS,
+ )
def pill(page, label):
@@ -302,6 +395,23 @@ def read_globals(page):
)
+def _wait_globals(page, expected):
+ """Until the Search default in localStorage reads `expected`, instead of a fixed 600 ms.
+
+ Gives up quietly after 10 s: the caller's own check then reports the value it found.
+ """
+ try:
+ wait_until(
+ lambda: read_globals(page)["unsloth_chat_tools_enabled"] == expected,
+ timeout_s = 10,
+ what = f"the Search default to read {expected}",
+ interval_s = 0.1,
+ page = page,
+ )
+ except TimeoutError:
+ pass
+
+
def check_reasoning_transcript(page, token):
step("saved reasoning scrolls continuously with bounded content and full-source copy")
source = "\n\n".join(
@@ -363,6 +473,7 @@ def check_reasoning_transcript(page, token):
# nothing was going to move. A reader keeps scrolling; so does this, bounded.
for _ in range(12):
page.mouse.wheel(0, 100000)
+ # Kept: the pause between wheel turns of a bounded scroll loop, which checks its condition each turn.
page.wait_for_timeout(250)
if "Step 0399." in body.inner_text():
break
@@ -373,18 +484,22 @@ def check_reasoning_transcript(page, token):
trigger.click()
expect(transcript).to_have_count(0)
- page.wait_for_timeout(300)
+ wait_for_settled(trigger)
header_top = trigger.bounding_box()["y"]
trigger.click()
expect(body).to_contain_text("Step 0000.")
- page.wait_for_timeout(300)
+ wait_for_settled(trigger)
assert abs(trigger.bounding_box()["y"] - header_top) < 3, "reopening moved the header"
def main():
+ global _watchdog
+ _watchdog = install_wall_clock_watchdog(WALL_TIMEOUT_S, label = "thread-settings")
+ report_failing_step(_watchdog, label = "thread-settings")
with sync_playwright() as playwright:
browser = playwright.chromium.launch(args = ["--no-sandbox", "--disable-dev-shm-usage"])
context = browser.new_context(viewport = {"width": 1280, "height": 900})
+ context.add_init_script(CHAT_TRAFFIC_JS)
page = context.new_page()
page.set_default_timeout(TIMEOUT_MS)
page_errors = []
@@ -401,12 +516,12 @@ def main():
# chat would stop every toggle made before the first message from persisting at all.
settle(page)
pill(page, "Search").click()
- page.wait_for_timeout(600)
+ _wait_globals(page, "true")
enabled_globals = read_globals(page)
if enabled_globals["unsloth_chat_tools_enabled"] != "true":
fail(f"an unsaved chat's edit never reached the defaults: {enabled_globals!r}")
pill(page, "Search").click()
- page.wait_for_timeout(600)
+ _wait_globals(page, "false")
disabled_globals = read_globals(page)
if disabled_globals["unsloth_chat_tools_enabled"] != "false":
fail(f"toggling back never reached the defaults: {disabled_globals!r}")
@@ -503,7 +618,7 @@ def main():
step("a chat edited before it had modes of its own keeps the new defaults")
# no chat is open, so this moves the defaults every snapshot-less chat follows.
pill(page, "Search").click()
- page.wait_for_timeout(600)
+ _wait_globals(page, "true")
moved = read_globals(page)
if moved["unsloth_chat_tools_enabled"] != "true":
fail(f"a new-chat edit did not reach the defaults: {moved!r}")
@@ -545,6 +660,7 @@ def main():
context.close()
browser.close()
+ _watchdog.cancel()
print("[thread-settings] PASS", flush = True)