qwen-code/packages/web-shell/client/components/dialogs/ModelDialog.test.tsx
carffuca 5c9e73f371
feat(web-shell): overhaul list-dialog interaction, keyboard nav & a11y (#6128)
* feat(web-shell): add keyboard-nav and IME-safe filter hooks

Two reusable hooks for list-style dialogs:

- useListboxKeyboard: Arrow/Home/End/Enter navigation driven by an active
  index, with a "keyboard mode" flag to suppress hover. Yields modified-key
  combos (Cmd/Ctrl/Alt/Shift), Home/End in text inputs, and Enter on focused
  buttons/links to native handling.
- useFilterInput: IME-composition-safe search state so a filtered list does
  not refire on every intermediate pinyin character (commits on compositionend).

* feat(web-shell): DialogShell Escape/backdrop close and focus management

Give every dialog shared, accessible dismissal and focus behaviour:
- Escape closes (guarded during IME composition so it cancels the
  composition, not the dialog)
- click on the backdrop closes
- Tab is trapped within the panel, wrapping at both ends
- focus moves into the dialog on open and is restored to the opener on close

* feat(web-shell): overhaul list-dialog interaction and accessibility

Unify interaction across the model, theme, approval, resume, tools, delete,
release and rewind dialogs:

- keyboard navigation via useListboxKeyboard, with a roving highlight that
  opens on the current value and does not fight the mouse
- consistent selection visuals: a single roving highlight plus a persistent
  "current" accent bar + checkmark; options are role=option divs (no stray
  focus ring)
- IME-safe search via useFilterInput; fix Chinese-input jitter in the
  resume/delete/release search boxes
- accessibility: role=listbox/option, aria-activedescendant, and aria-selected
  bound to the current value rather than the roving highlight
- destructive dialogs keep Enter non-destructive where a confirm button is the
  commit (delete/release); rewind confirms on Enter like a single-select picker

Refactors:
- extract shared SessionRow used by resume/delete/release
- rename resume-picker-* CSS primitives to picker-* (they are shared by all
  list dialogs, not resume-specific)

Adds regression tests for the model duplicate-current fix, release hover
selection, rewind Enter, the listbox/aria wiring, and the shared hooks.

* fix(web-shell): address dialog interaction review feedback

Follow-up fixes from upstream review:
- DialogShell closes on completed backdrop clicks instead of mousedown, and its
  Tab trap now also catches the panel-focused fallback case
- ToolsDialog now has full listbox semantics (ids, aria-activedescendant,
  aria-expanded)
- Rewind keeps the roving cursor separate from the confirmed target; Enter only
  confirms, and the danger button executes the rewind
- Model/Approval aria-selected now reflects the actual current value; model
  highlights stay in bounds when the model list shrinks
- Home/End and modified arrow-key combos yield to native text navigation in
  search inputs; Escape yields to IME composition in DialogShell
- Dead picker CSS and duplicate declarations removed; extra regression tests
  added for reviewer-raised edge cases

* fix(web-shell): harden shared dialog keyboard and IME handling

* fix(web-shell): tighten dialog shell focus, stacking, and backdrop behavior

* fix(web-shell): align list dialog selection semantics and add coverage
2026-07-02 12:19:17 +00:00

158 lines
5.1 KiB
TypeScript

// @vitest-environment jsdom
import { afterEach, describe, expect, it, vi } from 'vitest';
import { act } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { I18nProvider } from '../../i18n';
import { dp } from './dialogStyles';
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
if (!Element.prototype.scrollIntoView) {
Element.prototype.scrollIntoView = () => {};
}
// ModelDialog only reads `useConnection()`; models/current come in via props here.
vi.mock('@qwen-code/webui/daemon-react-sdk', () => ({
useConnection: () => ({}),
}));
const { ModelDialog } = await import('./ModelDialog');
let container: HTMLDivElement | null = null;
let root: Root | null = null;
function mount(node: React.ReactNode) {
container = document.createElement('div');
document.body.appendChild(container);
root = createRoot(container);
act(() => {
root!.render(<I18nProvider language="en">{node}</I18nProvider>);
});
}
afterEach(() => {
act(() => root?.unmount());
container?.remove();
root = null;
container = null;
});
describe('ModelDialog current marker', () => {
it('marks exactly one row current when two models share an id', () => {
// Two providers expose the same model id "qwen"; `currentModel` is only an
// id, so both used to be flagged. Only the first match should be current.
const models = [
{ id: 'qwen', authType: 'a', baseUrl: 'https://a' },
{ id: 'qwen', authType: 'b', baseUrl: 'https://b' },
{ id: 'other', authType: 'c' },
];
mount(
<ModelDialog onSelect={vi.fn()} models={models} currentModelId="qwen" />,
);
const options = Array.from(container!.querySelectorAll('[role="option"]'));
expect(options).toHaveLength(3);
const currentClass = dp('dialog-current');
const marked = options.filter((el) => el.className.includes(currentClass));
expect(marked).toHaveLength(1);
expect(marked[0].textContent).toContain('1.');
});
it('binds aria-selected to the current model, not the roving highlight', () => {
const models = [{ id: 'a' }, { id: 'b' }, { id: 'c' }];
mount(
<ModelDialog onSelect={vi.fn()} models={models} currentModelId="b" />,
);
const selected = () =>
Array.from(container!.querySelectorAll('[aria-selected="true"]'));
// Only the current model (b) is aria-selected on open.
expect(selected()).toHaveLength(1);
expect(selected()[0].textContent).toContain('b');
// Moving the keyboard highlight must not change which row is aria-selected.
act(() => {
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' }));
});
expect(selected()).toHaveLength(1);
expect(selected()[0].textContent).toContain('b');
});
});
describe('ModelDialog keyboard confirmation', () => {
it('confirms the highlighted model on Enter', () => {
const onSelect = vi.fn();
const models = [{ id: 'a' }, { id: 'b' }, { id: 'c' }];
mount(
<ModelDialog onSelect={onSelect} models={models} currentModelId="b" />,
);
// Enter with no navigation confirms the current model's row.
act(() => {
window.dispatchEvent(
new KeyboardEvent('keydown', { key: 'Enter', cancelable: true }),
);
});
expect(onSelect).toHaveBeenCalledWith('b');
// After arrowing, Enter confirms the newly highlighted row.
act(() => {
window.dispatchEvent(
new KeyboardEvent('keydown', { key: 'ArrowDown', cancelable: true }),
);
});
act(() => {
window.dispatchEvent(
new KeyboardEvent('keydown', { key: 'Enter', cancelable: true }),
);
});
expect(onSelect).toHaveBeenLastCalledWith('c');
});
});
describe('ModelDialog highlight follows the current model', () => {
const models = [{ id: 'a' }, { id: 'b' }, { id: 'c' }];
const activeDescendant = () =>
container!
.querySelector('[role="listbox"]')!
.getAttribute('aria-activedescendant');
it('re-syncs the highlight when the current model changes while open', () => {
mount(
<ModelDialog onSelect={vi.fn()} models={models} currentModelId="a" />,
);
expect(activeDescendant()).toBe('model-opt-0');
// Another client sharing the session switches the model while the dialog
// is open: the highlight (and thus detail panel / Enter) must follow.
act(() => {
root!.render(
<I18nProvider language="en">
<ModelDialog onSelect={vi.fn()} models={models} currentModelId="c" />
</I18nProvider>,
);
});
expect(activeDescendant()).toBe('model-opt-2');
});
it('stops following once the user has navigated', () => {
mount(
<ModelDialog onSelect={vi.fn()} models={models} currentModelId="a" />,
);
act(() => {
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' }));
});
expect(activeDescendant()).toBe('model-opt-1');
// The user owns the highlight now — a current-model change must not steal it.
act(() => {
root!.render(
<I18nProvider language="en">
<ModelDialog onSelect={vi.fn()} models={models} currentModelId="c" />
</I18nProvider>,
);
});
expect(activeDescendant()).toBe('model-opt-1');
});
});