Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { act } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { I18nProvider } from '../../i18n';
import { WebShellPortalRootContext } from '../../portalRoot';

Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });

Expand Down Expand Up @@ -306,11 +307,13 @@ describe('AddWorkspaceDialog', () => {

it('opens the system picker and fills the selected absolute path', async () => {
const onPick = vi.fn().mockResolvedValue('/Users/me/code');
const onSuggest = vi.fn().mockResolvedValue(SUGGESTIONS);
mount(
<AddWorkspaceDialog
onClose={vi.fn()}
onAdd={vi.fn()}
onPick={onPick}
onSuggest={onSuggest}
/>,
);

Expand All @@ -321,6 +324,75 @@ describe('AddWorkspaceDialog', () => {

expect(onPick).toHaveBeenCalledTimes(1);
expect(input().value).toBe('/Users/me/code');
expect(document.activeElement).not.toBe(input());
await settle();
expect(listbox()).toBeNull();
});

it('keeps suggestions closed when a lookup finishes after blur', async () => {
let resolveSuggestions!: (value: typeof SUGGESTIONS) => void;
const onSuggest = vi.fn(
() =>
new Promise<typeof SUGGESTIONS>((resolve) => {
resolveSuggestions = resolve;
}),
);
mount(
<AddWorkspaceDialog
onClose={vi.fn()}
onAdd={vi.fn()}
onSuggest={onSuggest}
/>,
);

type('/home/me/co');
await settle();
act(() => input().blur());
await act(async () => {
await vi.advanceTimersByTimeAsync(100);
resolveSuggestions(SUGGESTIONS);
await Promise.resolve();
});

expect(listbox()).toBeNull();
});

it('opens suggestions for a focused input in a shadow-DOM portal root', async () => {
const onSuggest = vi.fn().mockResolvedValue(SUGGESTIONS);
const host = document.createElement('div');
document.body.append(host);
container = host;
const shadowRoot = host.attachShadow({ mode: 'open' });
const portalRoot = document.createElement('div');
shadowRoot.append(portalRoot);
const dialogContainer = document.createElement('div');
shadowRoot.append(dialogContainer);
root = createRoot(dialogContainer);
act(() => {
root!.render(
<WebShellPortalRootContext.Provider value={portalRoot}>
<I18nProvider language="en">
<AddWorkspaceDialog
onClose={vi.fn()}
onAdd={vi.fn()}
onSuggest={onSuggest}
/>
</I18nProvider>
</WebShellPortalRootContext.Provider>,
);
});

const shadowInput = shadowRoot.querySelector<HTMLInputElement>(
'#add-workspace-path',
)!;
// document.activeElement retargets to the shadow host in this mode.
expect(shadowRoot.activeElement).toBe(shadowInput);
expect(document.activeElement).toBe(host);

typeInto(shadowInput, '/home/me/co');
await settle();

expect(shadowRoot.querySelector('[role="listbox"]')).not.toBeNull();
});

it('leaves the path unchanged when the system picker is cancelled', async () => {
Expand All @@ -341,6 +413,87 @@ describe('AddWorkspaceDialog', () => {
expect(input().value).toBe('');
});

it('opens suggestions on the first edit after a cancelled picker', async () => {
let resolvePick!: (value: string | undefined) => void;
const onPick = vi.fn(
() =>
new Promise<string | undefined>((resolve) => {
resolvePick = resolve;
}),
);
const onSuggest = vi.fn().mockResolvedValue(SUGGESTIONS);
mount(
<AddWorkspaceDialog
onClose={vi.fn()}
onAdd={vi.fn()}
onPick={onPick}
onSuggest={onSuggest}
/>,
);

act(() => {
browseButton().click();
});
// While the picker is open, the blur timer sets the suppress flag.
await act(async () => {
await vi.advanceTimersByTimeAsync(100);
});
await act(async () => {
resolvePick(undefined);
await Promise.resolve();
await Promise.resolve();
});

act(() => input().focus());
type('/home/me/co');
await settle();

expect(onSuggest).toHaveBeenCalledWith('/home/me/co');
expect(listbox()).not.toBeNull();
});

it('opens suggestions on the first edit after picking the typed path', async () => {
let resolvePick!: (value: string | undefined) => void;
const onPick = vi.fn(
() =>
new Promise<string | undefined>((resolve) => {
resolvePick = resolve;
}),
);
const onSuggest = vi.fn().mockResolvedValue(SUGGESTIONS);
mount(
<AddWorkspaceDialog
onClose={vi.fn()}
onAdd={vi.fn()}
onPick={onPick}
onSuggest={onSuggest}
/>,
);

type('/home/me/co');
await settle();
expect(listbox()).not.toBeNull();

act(() => {
browseButton().click();
});
await act(async () => {
await vi.advanceTimersByTimeAsync(100);
});
await act(async () => {
// Same value as typed: setPath bails out, so no path-change effect.
resolvePick('/home/me/co');
await Promise.resolve();
await Promise.resolve();
});

act(() => input().focus());
type('/home/me/cod');
await settle();

expect(listbox()).not.toBeNull();
});

it('shows an error when the system picker fails', async () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] Error path for pickDirectory not tested for suppressNextFetchOpenRef clearing — The finally block in pickDirectory runs suppressNextFetchOpenRef.current = false for all three outcomes (success, cancelled, error). The cancelled and success paths are tested by the new tests; the error path is not. If a future refactor moves the flag-clearing out of the finally block and into the success-only path, the error test would pass while the error path would silently suppress the next suggestion fetch.

Suggested change
it('shows an error when the system picker fails', async () => {
it('shows an error when the system picker fails', async () => {
const onPick = vi.fn().mockRejectedValue(new Error('boom'));
const onSuggest = vi.fn().mockResolvedValue(SUGGESTIONS);
mount(
<AddWorkspaceDialog
onClose={vi.fn()}
onAdd={vi.fn()}
onPick={onPick}
onSuggest={onSuggest}
/>,
);
act(() => browseButton().click());
await act(async () => { await vi.advanceTimersByTimeAsync(100); });
await act(async () => { await Promise.resolve(); await Promise.resolve(); });
expect(screen.getByText('Failed to open folder picker')).toBeTruthy();
act(() => input().focus());
type('/home/me/co');
await settle();
expect(listbox()).not.toBeNull();
中文说明

pickDirectory 的 finally 块在所有三种结果(成功、取消、错误)下都会执行 suppressNextFetchOpenRef.current = false。取消和成功路径已由新测试覆盖;错误路径尚未覆盖。如果未来重构将标志清除移出 finally 块并仅放入成功路径,该错误测试仍会通过,但错误路径将静默抑制下一次建议查询。建议在错误测试中添加 onSuggest 并在错误解决后输入路径断言建议列表打开。

— deepseek-v4-flash via Qwen Code /review (v0.21.8)

const onPick = vi.fn().mockRejectedValue(new Error('boom'));
mount(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,17 @@ export function AddWorkspaceDialog({
setSuggestions(result.suggestions);
setHostSep(result.sep || '/');
setHighlight(-1);
if (openOnResult || listOpenRef.current) {
// document.activeElement retargets to the shadow host in
// shadow-DOM portal mode; read focus from the input's own root.
if (
(
inputRef.current?.getRootNode() as
| Document
| ShadowRoot
| undefined
)?.activeElement === inputRef.current &&
(openOnResult || listOpenRef.current)
) {
setListOpen(result.suggestions.length > 0);
}
},
Expand Down Expand Up @@ -149,6 +159,7 @@ export function AddWorkspaceDialog({

const pickDirectory = useCallback(async () => {
if (!onPick) return;
inputRef.current?.blur();
setBrowsing(true);
setError(null);
try {
Expand All @@ -163,6 +174,9 @@ export function AddWorkspaceDialog({
setError(t('sidebar.addWorkspaceBrowseError'));
} finally {
setBrowsing(false);
// The blur before the picker set suppressNextFetchOpenRef; clear it so
// the first edit after a cancelled or no-op pick still opens the list.
suppressNextFetchOpenRef.current = false;
}
}, [onPick, closeList, t]);

Expand Down Expand Up @@ -304,7 +318,6 @@ export function AddWorkspaceDialog({
<Button
type="button"
variant="outline"
onMouseDown={(event) => event.preventDefault()}
onClick={() => void pickDirectory()}
disabled={submitting || browsing}
>
Expand Down
Loading