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.

9.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

"""Checks width presets on a running Studio; seeds its own thread unless CHAT_THREAD_ID is given."""

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
    follow_os_reduced_motion,
    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
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(
    page,
    path,
    method = "GET",
    body = None,
    token = None,
):
    """Call the backend from the page, so the request carries the session cookie."""
    result = page.evaluate(
        """async ([url, method, body, token]) => {
            const headers = { "Content-Type": "application/json" };
            if (token) headers.Authorization = `Bearer ${token}`;
            const response = await fetch(url, {
                method,
                headers,
                body: body === null ? undefined : JSON.stringify(body),
            });
            const text = await response.text();
            let parsed = null;
            try { parsed = JSON.parse(text); } catch { parsed = text; }
            return { status: response.status, body: parsed };
        }""",
        [f"{BASE}{path}", method, body, token],
    )
    if result["status"] >= 400:
        raise AssertionError(f"{method} {path} returned {result['status']}: {result['body']!r}")
    return result["body"]


def sign_in(page):
    """Handles both starts: a fresh CI boot lands on /change-password, a re-run on /login."""
    page.goto(f"{BASE}/change-password", wait_until = "domcontentloaded", timeout = TIMEOUT_MS)
    try:
        page.locator("#new-password").wait_for(state = "visible", timeout = 15_000)
        rotating = True
    except Exception:  # noqa: BLE001 - already rotated, so the form is not there
        rotating = False
    if rotating:
        page.fill("#new-password", PASSWORD, timeout = TIMEOUT_MS)
        page.fill("#confirm-password", PASSWORD, timeout = TIMEOUT_MS)
        endpoint = "/api/auth/change-password"
    else:
        page.goto(f"{BASE}/login", wait_until = "domcontentloaded", timeout = TIMEOUT_MS)
        page.locator("#password").wait_for(state = "visible", timeout = TIMEOUT_MS)
        page.fill("#password", PASSWORD, timeout = TIMEOUT_MS)
        endpoint = "/api/auth/login"
    with page.expect_response(
        lambda r: endpoint in r.url and r.request.method == "POST", timeout = TIMEOUT_MS
    ) as response:
        page.locator('button[type="submit"]').click()
    if response.value.status >= 400:
        raise AssertionError(f"POST {endpoint} returned {response.value.status}")
    page.goto(f"{BASE}/chat", wait_until = "domcontentloaded", timeout = TIMEOUT_MS)
    page.locator('button[data-pill-label="Search"]:visible').first.wait_for(timeout = TIMEOUT_MS)
    return page.evaluate("() => localStorage.getItem('unsloth_auth_token')")


def seed_thread(page, token):
    """One user and one assistant turn; the long assistant text reaches the column cap at every viewport."""
    thread_id = str(uuid.uuid4())
    now = int(time.time() * 1000)
    api(
        page,
        "/api/chat/threads",
        method = "POST",
        token = token,
        body = {
            "id": thread_id,
            "title": "chat width presets",
            "modelType": "base",
            "modelId": "",
            "archived": False,
            "createdAt": now,
            "updatedAt": now,
        },
    )
    user_id = str(uuid.uuid4())
    api(
        page,
        f"/api/chat/threads/{thread_id}/messages",
        method = "PUT",
        token = token,
        body = {
            "messages": [
                {
                    "id": user_id,
                    "threadId": thread_id,
                    "parentId": None,
                    "role": "user",
                    "content": [{"type": "text", "text": "How wide does this column get?"}],
                    "createdAt": now,
                },
                {
                    "id": str(uuid.uuid4()),
                    "threadId": thread_id,
                    "parentId": user_id,
                    "role": "assistant",
                    "content": [
                        {"type": "text", "text": "Seeded reply for the width presets. " * 40}
                    ],
                    "createdAt": now + 1,
                },
            ]
        },
    )
    return thread_id


# The order appearance-custom-controls.tsx lists the chat width values in (standard, wide, full);
# test_chat_width_driver_presets.py keeps the two in step. The names are only this driver's labels.
CHAT_WIDTH_PRESETS = ("Standard", "Wide", "Full width")


def check_widths(page):
    """Full must preserve the space available in Wide, including narrow panes."""
    thread_url = page.url
    measurements = {}
    for index, preset in enumerate(CHAT_WIDTH_PRESETS):
        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()
        page.get_by_role("combobox", name = "Chat width").click()
        # By position, not label: the option text is copy (#16408 renamed Standard to Default).
        page.get_by_role("listbox").get_by_role("option").nth(index).click()
        page.keyboard.press("Escape")
        measurements[preset] = {}
        for width in (390, 768, 900, 1280, 1536, 1920, 2560):
            page.set_viewport_size({"width": width, "height": 900})
            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;
                    return {
                        message: width('.aui-assistant-message-root'),
                        composer: width('.unsloth-composer-shell'),
                        overflow: document.documentElement.scrollWidth > innerWidth,
                    };
                }"""
            )
        if preset == "Wide":
            page.set_viewport_size({"width": 1920, "height": 900})
            page.goto(thread_url.split("?")[0] + "?new=width-transition")
            shell = page.locator(".unsloth-composer-shell")
            shell.wait_for()
            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]
        for surface in ("message", "composer"):
            assert full[surface] >= wide[surface] - 1, (width, surface, full, wide)
            assert wide[surface] >= standard[surface] - 1, (width, surface, wide, standard)
        assert not any(measurements[preset][width]["overflow"] for preset in measurements)
    # The ordering above is >=, so also require the widest-viewport separation of the three presets.
    widest = max(measurements["Standard"])
    for surface in ("message", "composer"):
        standard = measurements["Standard"][widest][surface]
        wide = measurements["Wide"][widest][surface]
        full = measurements["Full width"][widest][surface]
        assert wide > standard, (
            f"at {widest}px Wide gives {surface} {wide}px, no more than Standard's "
            f"{standard}px, so the preset is doing nothing"
        )
        assert full > wide, (
            f"at {widest}px Full width gives {surface} {full}px, no more than Wide's "
            f"{wide}px, so the preset is doing nothing"
        )
    return measurements


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")
        follow_os_reduced_motion(page.context)
        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(
            f"{BASE}/chat?thread={thread_id}", wait_until = "domcontentloaded", timeout = TIMEOUT_MS
        )
        page.locator(".aui-assistant-message-root").wait_for(timeout = TIMEOUT_MS)
        print(check_widths(page))
        browser.close()
    _watchdog.cancel()