fix(webui): polish mobile settings and context interactions

This commit is contained in:
Xubin Ren committed 2026-09-13 23:35:16 +08:00
1 parent 827dbe137b
commit f5c6be2608
5 files changed
+553 -80

No files matched your search

@@ -0,0 +1,163 @@
import { createEvent, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it } from "vitest";
import { ComposerUsagePopover } from "@/components/thread/ComposerUsagePopover";
function pointer(
target: HTMLElement,
type: "pointerDown" | "pointerMove" | "pointerUp" | "pointerCancel" | "lostPointerCapture",
{ y, x = 160, time = 1000, id = 1 }: { y: number; x?: number; time?: number; id?: number },
) {
const event = createEvent[type](target, {
pointerId: id, pointerType: "touch", isPrimary: true, button: 0, clientX: x, clientY: y,
});
Object.defineProperty(event, "timeStamp", { value: time });
fireEvent(target, event);
}
async function openSheet() {
const user = userEvent.setup();
render(
<ComposerUsagePopover
bottomSheet
showLabel
context={{ contextTokens: 10000, contextWindowTokens: 200000 }}
rounds={[
{ id: "round", timestamp: 0, inputTokens: 10000, cachedTokens: 8000 },
{ id: "round-2", timestamp: 60000, inputTokens: 14000, outputTokens: 280 },
]}
/>,
);
const trigger = screen.getByTestId("composer-context-usage");
await user.click(trigger);
const sheet = screen.getByRole("dialog", { name: "Context usage" });
const handle = within(sheet).getByRole("button", { name: "Close" });
return { user, trigger, sheet, handle };
}
describe("Mobile context usage sheet", () => {
it("replaces the X with a keyboard-accessible drag handle", async () => {
const { user, handle, sheet, trigger } = await openSheet();
expect(within(sheet).getAllByRole("button", { name: "Close" })).toEqual([handle]);
expect(handle.querySelector("svg")).toBeNull();
expect(handle).toHaveClass("touch-none", "h-11");
handle.focus();
await user.keyboard("{Enter}");
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
expect(trigger).toHaveFocus();
});
it("keeps tapped round details open, switches rounds, and toggles the same bar closed", async () => {
const { user, sheet } = await openSheet();
const [first, second] = within(sheet).getAllByTestId("round-usage-bar");
const tap = (target: HTMLElement) => user.pointer([
{ keys: "[TouchA>]", target },
{ keys: "[/TouchA]", target },
]);
expect(first.tagName).toBe("BUTTON");
await tap(first);
const details = within(sheet).getByRole("dialog");
expect(details).toHaveTextContent("10,000");
expect(details).toHaveTextContent("80%");
expect(first).toHaveAttribute("aria-expanded", "true");
expect(screen.queryByRole("tooltip")).not.toBeInTheDocument();
fireEvent.pointerLeave(first, { pointerType: "touch" });
expect(details).toBeVisible();
await tap(second);
await waitFor(() => expect(within(sheet).getAllByRole("dialog")).toHaveLength(1));
expect(within(sheet).getByRole("dialog")).toHaveTextContent("14,000");
expect(within(sheet).getByRole("dialog")).toHaveTextContent("280");
expect(first).toHaveAttribute("aria-expanded", "false");
expect(second).toHaveAttribute("aria-expanded", "true");
await tap(second);
await waitFor(() => expect(within(sheet).queryByRole("dialog")).not.toBeInTheDocument());
expect(sheet).toBeVisible();
await tap(first);
await tap(within(sheet).getByRole("heading", { name: "Context usage" }));
await waitFor(() => expect(within(sheet).queryByRole("dialog")).not.toBeInTheDocument());
expect(sheet).toBeVisible();
});
it("dismisses nested round details with Escape before the sheet, and supports dragging with details open", async () => {
const { user, sheet, handle, trigger } = await openSheet();
const first = within(sheet).getAllByTestId("round-usage-bar")[0];
first.focus();
await user.keyboard("{Enter}");
expect(within(sheet).getByRole("dialog")).toBeVisible();
await user.keyboard("{Escape}");
await waitFor(() => expect(within(sheet).queryByRole("dialog")).not.toBeInTheDocument());
expect(sheet).toBeVisible();
expect(first).toHaveFocus();
await user.click(first);
pointer(handle, "pointerDown", { y: 400 });
pointer(handle, "pointerMove", { y: 520, time: 1400 });
pointer(handle, "pointerUp", { y: 520, time: 1500 });
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
expect(document.body).not.toHaveStyle({ pointerEvents: "none" });
expect(trigger).toHaveFocus();
});
it.each([
[100, 500], // Deliberate pull.
[32, 40], // Short, quick downward flick.
])("dismisses after dragging %dpx in %dms and opens at the original position", async (distance, duration) => {
const { user, handle, sheet, trigger } = await openSheet();
pointer(handle, "pointerDown", { y: 400 });
pointer(handle, "pointerMove", { y: 400 + distance, time: 1000 + duration });
// happy-dom doesn't expose individual transforms through getComputedStyle.
expect(sheet.style.translate).toBe(`0 ${distance}px`);
expect(sheet).toHaveStyle({ transition: "none" });
pointer(handle, "pointerUp", { y: 400 + distance, time: 1000 + duration });
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
expect(document.body).not.toHaveStyle({ pointerEvents: "none" });
expect(trigger).toHaveFocus();
await user.click(trigger);
expect(screen.getByRole("dialog").style.translate).toBe("0 0px");
});
it.each([
{ end: "pointerUp" as const, y: 432, x: 160 },
{ end: "pointerUp" as const, y: 370, x: 160 },
{ end: "pointerUp" as const, y: 460, x: 260 },
{ end: "pointerCancel" as const, y: 500, x: 160 },
{ end: "lostPointerCapture" as const, y: 500, x: 160 },
])("snaps back without a click-through on $end at ($x, $y)", async ({ end, y, x }) => {
const { user, handle, sheet } = await openSheet();
pointer(handle, "pointerDown", { y: 400 });
pointer(handle, "pointerMove", { y, x, time: 1400 });
pointer(handle, end, { y, x, time: 1500 });
expect(sheet.style.translate).toBe("0 0px");
expect(sheet.style.transition).toBe("");
fireEvent.click(handle, { detail: 1 });
expect(screen.getByRole("dialog")).toBeVisible();
// A real subsequent tap must still close the sheet.
await user.click(handle);
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
expect(document.body).not.toHaveStyle({ pointerEvents: "none" });
});
it("does not capture content scrolling or let another pointer finish the drag", async () => {
const { user, handle, sheet } = await openSheet();
const content = within(sheet).getByRole("group", { name: "Input tokens" });
pointer(content, "pointerDown", { y: 480 });
pointer(content, "pointerMove", { y: 580 });
pointer(content, "pointerUp", { y: 580, time: 1500 });
expect(sheet.style.translate).toBe("0 0px");
pointer(handle, "pointerDown", { y: 400 });
pointer(handle, "pointerMove", { y: 412 });
pointer(handle, "pointerDown", { y: 412, id: 2 });
pointer(handle, "pointerUp", { y: 600, id: 2 });
expect(sheet.style.translate).toBe("0 12px");
pointer(handle, "pointerUp", { y: 412, time: 1500 });
expect(sheet.style.translate).toBe("0 0px");
await user.keyboard("{Escape}");
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
});
});
+160
View File
@@ -0,0 +1,160 @@
import { useState, type ComponentProps } from "react";
import { act, render, screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";
import { SettingsSidebar } from "@/components/settings/SettingsSidebar";
import {
installSettingsViewTestHooks,
renderSettingsView,
settingsPayload,
} from "@/tests/settings-test-utils";
function mockMobileMedia(initial = true) {
const target = new EventTarget();
const media = {
matches: initial,
media: "(max-width: 1023px)",
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: target.addEventListener.bind(target),
removeEventListener: target.removeEventListener.bind(target),
dispatchEvent: target.dispatchEvent.bind(target),
};
vi.stubGlobal("matchMedia", vi.fn(() => media));
return (matches: boolean) => act(() => {
media.matches = matches;
target.dispatchEvent(new Event("change"));
});
}
function Sidebar(props: Partial<ComponentProps<typeof SettingsSidebar>>) {
const [section, setSection] = useState(props.activeSection ?? "models");
return <SettingsSidebar activeSection={section} onSelectSection={setSection}
onBackToChat={() => {}} {...props} />;
}
describe("Settings navigation on mobile", () => {
installSettingsViewTestHooks();
it("keeps only back and the current section in the header; selects sections in a sheet", async () => {
mockMobileMedia();
const user = userEvent.setup();
const restart = vi.fn();
const back = vi.fn();
render(<Sidebar onRestart={restart} onBackToChat={back} />);
const header = screen.getByRole("complementary");
expect(within(header).getAllByRole("button")).toHaveLength(2);
const trigger = screen.getByRole("button", { name: "Settings: Models" });
await user.click(trigger);
const sheet = screen.getByRole("dialog", { name: "Settings" });
expect(within(sheet).getByRole("button", { name: "Models", exact: true }))
.toHaveAttribute("aria-current", "page");
expect(within(sheet).getByRole("navigation", { name: "Settings sections" })).toBeVisible();
expect(within(sheet).getByRole("button", { name: "Restart" })).toBeVisible();
await user.click(within(sheet).getByRole("button", { name: "Appearance" }));
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
expect(screen.getByRole("button", { name: "Settings: Appearance" })).toHaveFocus();
expect(document.body.style.pointerEvents).not.toBe("none");
expect(restart).not.toHaveBeenCalled();
await user.click(screen.getByRole("button", { name: "Back to chat" }));
expect(back).toHaveBeenCalledOnce();
});
it("treats capability subpages as the same navigation section", async () => {
mockMobileMedia();
const user = userEvent.setup();
render(<Sidebar activeSection="image" />);
await user.click(screen.getByRole("button", { name: "Settings: Capabilities" }));
expect(screen.getByRole("button", { name: "Capabilities", exact: true }))
.toHaveAttribute("aria-current", "page");
expect(screen.queryByRole("button", { name: "Restart" })).not.toBeInTheDocument();
});
it.each(["close", "escape", "outside"])("dismisses with %s and restores focus and pointer events", async (method) => {
mockMobileMedia();
const user = userEvent.setup();
render(<Sidebar />);
const trigger = screen.getByRole("button", { name: "Settings: Models" });
await user.click(trigger);
if (method === "close") await user.click(screen.getByRole("button", { name: "Close" }));
else if (method === "escape") await user.keyboard("{Escape}");
else {
// The real overlay is outside the modal content; ignore the locked page behind it.
const overlay = document.querySelector<HTMLElement>('[data-state="open"][aria-hidden="true"]');
expect(overlay).not.toBeNull();
await user.click(overlay!);
}
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
expect(trigger).toHaveFocus();
expect(document.body.style.pointerEvents).not.toBe("none");
});
it("keeps manual restart in the sheet and closes it before handing off", async () => {
mockMobileMedia();
const user = userEvent.setup();
const restart = vi.fn();
render(<Sidebar onRestart={restart} />);
expect(screen.queryByRole("button", { name: "Restart" })).not.toBeInTheDocument();
await user.click(screen.getByRole("button", { name: "Settings: Models" }));
await user.click(screen.getByRole("button", { name: "Restart" }));
expect(restart).toHaveBeenCalledOnce();
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
expect(document.body.style.pointerEvents).not.toBe("none");
});
it("shows pending changes inline, disables restart while running, and clears the prompt after restart", async () => {
mockMobileMedia();
const user = userEvent.setup();
const restart = vi.fn();
const { rerender } = render(<Sidebar onRestart={restart} restartPending />);
expect(screen.getByRole("status")).toHaveTextContent("Saved. Restart to apply changes.");
await user.click(screen.getByRole("button", { name: "Restart", exact: true }));
expect(restart).toHaveBeenCalledOnce();
rerender(<Sidebar onRestart={restart} restartPending isRestarting />);
const runningLabel = screen.getByRole("status").textContent!;
expect(screen.getByRole("button", { name: runningLabel, exact: true })).toBeDisabled();
await user.click(screen.getByRole("button", { name: "Settings: Models" }));
expect(within(screen.getByRole("dialog")).getByRole("button", { name: runningLabel })).toBeDisabled();
await user.keyboard("{Escape}");
rerender(<Sidebar onRestart={restart} restartPending={false} />);
expect(screen.queryByRole("status")).not.toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Restart" })).not.toBeInTheDocument();
});
it("releases the open sheet when crossing to desktop and does not reopen on return", async () => {
const resize = mockMobileMedia();
const user = userEvent.setup();
const restart = vi.fn();
render(<Sidebar onRestart={restart} />);
await user.click(screen.getByRole("button", { name: "Settings: Models" }));
resize(false);
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
expect(document.body.style.pointerEvents).not.toBe("none");
expect(screen.queryByRole("button", { name: "Settings: Models" })).not.toBeInTheDocument();
expect(screen.getByRole("button", { name: "Models", exact: true })).toBeVisible();
await user.click(screen.getByRole("button", { name: "Restart" }));
expect(restart).toHaveBeenCalledOnce();
resize(true);
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
expect(screen.getByRole("button", { name: "Settings: Models" })).toBeVisible();
});
it("still asks before leaving saved changes that need a restart", async () => {
mockMobileMedia();
const user = userEvent.setup();
const back = vi.fn();
renderSettingsView({ initialSection: "models", initialSettings: {
...settingsPayload(), requires_restart: true,
}, onBackToChat: back });
await user.click(screen.getByRole("button", { name: "Settings: Models" }));
await user.click(screen.getByRole("button", { name: "Appearance", exact: true }));
await user.click(screen.getByRole("button", { name: "Back to chat" }));
const dialog = screen.getByRole("dialog", { name: "Restart before leaving?" });
expect(back).not.toHaveBeenCalled();
await user.click(within(dialog).getByRole("button", { name: "Restart later" }));
expect(back).toHaveBeenCalledOnce();
await waitFor(() => expect(document.body.style.pointerEvents).not.toBe("none"));
});
});
+1 -1
View File
@@ -1190,7 +1190,7 @@ describe("ThreadComposer", () => {
expect(onWorkspaceScopeChange).not.toHaveBeenCalled();
});
it.each(["close button", "Escape"])("opens narrow context usage as a bottom sheet and restores interaction after %s", async (dismissal) => {
it.each(["handle tap", "Escape"])("opens narrow context usage as a bottom sheet and restores interaction after %s", async (dismissal) => {
vi.spyOn(HTMLFormElement.prototype, "getBoundingClientRect").mockReturnValue(
rect({ width: 390, height: 160 }),
);