Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): prevent accidental account menu switches (#4032)

WEIWEICurry77ccdc239c4c
diff --git a/apps/desktop/src/renderer/__tests__/userInfoSectionMobileDownload.test.tsx b/apps/desktop/src/renderer/__tests__/userInfoSectionMobileDownload.test.tsx
index fe844e7451..b03be70bdc 100644
--- a/apps/desktop/src/renderer/__tests__/userInfoSectionMobileDownload.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/userInfoSectionMobileDownload.test.tsx
@@ -1,22 +1,33 @@
// @vitest-environment jsdom
-import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
+import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
-const { authState, betaChannelState, listAccounts, navigate, syncAccounts, switchAccount } =
- vi.hoisted(() => ({
- authState: {
- user: { name: 'Cindy user', avatar: null } as { name: string; avatar: string | null } | null,
- mode: 'cloud' as 'cloud' | 'local',
- dataOwnerId: 'owner-a' as string | null,
- isCanary: false,
- },
- betaChannelState: { enableBeta: false, isCustomized: false, loading: false },
- listAccounts: vi.fn(),
- navigate: vi.fn(),
- syncAccounts: vi.fn(),
- switchAccount: vi.fn(),
- }));
+const {
+ authState,
+ betaChannelState,
+ confirm,
+ runningSnapshot,
+ listAccounts,
+ navigate,
+ syncAccounts,
+ switchAccount,
+} = vi.hoisted(() => ({
+ authState: {
+ user: { name: 'Cindy user', avatar: null } as { name: string; avatar: string | null } | null,
+ mode: 'cloud' as 'cloud' | 'local',
+ dataOwnerId: 'owner-a' as string | null,
+ isCanary: false,
+ },
+ betaChannelState: { enableBeta: false, isCustomized: false, loading: false },
+ confirm: vi.fn(),
+ runningSnapshot: new Map<string, { isRunning: boolean }>(),
+ listAccounts: vi.fn(),
+ navigate: vi.fn(),
+ syncAccounts: vi.fn(),
+ switchAccount: vi.fn(),
+}));
vi.mock('react-i18next', () => ({
useTranslation: () => ({
@@ -39,11 +50,11 @@ vi.mock('@/contexts/AuthContext', () => ({
}));
vi.mock('@/components/ui/confirm-dialog-provider', () => ({
- useOptionalConfirmDialog: () => ({ confirm: vi.fn().mockResolvedValue(true) }),
+ useOptionalConfirmDialog: () => ({ confirm }),
}));
vi.mock('@/lib/makerChatStore', () => ({
- makerChatStore: { getRunningSnapshot: () => new Map() },
+ makerChatStore: { getRunningSnapshot: () => runningSnapshot },
}));
vi.mock('@/hooks/useUpdateStatus', () => ({
@@ -90,6 +101,8 @@ vi.mock('@/components/sidebar/MobileDownloadDialog', () => ({
import { UserInfoSection } from '@/components/sidebar/UserInfoSection';
beforeEach(() => {
+ confirm.mockReset().mockResolvedValue(true);
+ runningSnapshot.clear();
navigate.mockClear();
authState.user = { name: 'Cindy user', avatar: null };
authState.mode = 'cloud';
@@ -214,10 +227,84 @@ describe('UserInfoSection mobile download entry', () => {
'true',
);
- fireEvent.click(screen.getByRole('menuitem', { name: /Other user/ }));
+ await userEvent.click(screen.getByRole('menuitem', { name: /Other user/ }));
+ await waitFor(() => expect(switchAccount).toHaveBeenCalledWith('other'));
+ });
+
+ it.each([false, true])(
+ 'does not switch on a release over an asynchronously loaded account (collapsed=%s)',
+ async (isCollapsed) => {
+ render(<UserInfoSection isCollapsed={isCollapsed} />);
+
+ // The opening press starts on the trigger. Account rows arrive before
+ // release; Radix must not turn that release into an account selection.
+ fireEvent.pointerDown(screen.getByRole('button', { name: 'sidebar.user.moreLabel' }), {
+ button: 0,
+ ctrlKey: false,
+ });
+ const account = await screen.findByRole('menuitem', { name: /Other user/ });
+ await act(async () => {
+ fireEvent.pointerUp(account, { button: 0 });
+ // Flush the asynchronous running-task check used by switchSavedAccount.
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ });
+ expect(switchAccount).not.toHaveBeenCalled();
+ expect(screen.getByRole('menu')).toBeTruthy();
+ },
+ );
+
+ it.each(['Enter', ' '])('still switches with keyboard selection (%s)', async (key) => {
+ render(<UserInfoSection isCollapsed={false} />);
+ fireEvent.keyDown(screen.getByRole('button', { name: 'sidebar.user.moreLabel' }), {
+ key: 'Enter',
+ });
+ const account = await screen.findByRole('menuitem', { name: /Other user/ });
+ act(() => account.focus());
+ fireEvent.keyDown(account, { key });
await waitFor(() => expect(switchAccount).toHaveBeenCalledWith('other'));
});
+ it.each([false, true])('keeps running-task confirmation (confirmed=%s)', async (confirmed) => {
+ runningSnapshot.set('running-session', { isRunning: true });
+ confirm.mockResolvedValue(confirmed);
+ render(<UserInfoSection isCollapsed={false} />);
+ fireEvent.pointerDown(screen.getByRole('button', { name: 'sidebar.user.moreLabel' }), {
+ button: 0,
+ ctrlKey: false,
+ });
+ await userEvent.click(await screen.findByRole('menuitem', { name: /Other user/ }));
+ await waitFor(() => expect(confirm).toHaveBeenCalledOnce());
+ if (confirmed) {
+ await waitFor(() => expect(switchAccount).toHaveBeenCalledWith('other'));
+ } else {
+ expect(switchAccount).not.toHaveBeenCalled();
+ }
+ });
+
+ it.each([false, true])(
+ 'does not switch during repeated open/dismiss (collapsed=%s)',
+ async (isCollapsed) => {
+ render(<UserInfoSection isCollapsed={isCollapsed} />);
+ const trigger = screen.getByRole('button', { name: 'sidebar.user.moreLabel' });
+ for (const closeWith of ['trigger', 'outside', 'escape', 'trigger', 'outside']) {
+ fireEvent.pointerDown(trigger, { button: 0, ctrlKey: false });
+ await screen.findByRole('menuitem', { name: /Other user/ });
+ if (closeWith === 'escape') {
+ fireEvent.keyDown(screen.getByRole('menu'), { key: 'Escape' });
+ } else {
+ fireEvent.pointerDown(closeWith === 'trigger' ? trigger : document.body, {
+ button: 0,
+ ctrlKey: false,
+ pointerType: 'mouse',
+ });
+ }
+ await waitFor(() => expect(screen.queryByRole('menu')).toBeNull());
+ }
+ expect(switchAccount).not.toHaveBeenCalled();
+ expect(confirm).not.toHaveBeenCalled();
+ },
+ );
+
it('drops the previous account menu snapshot as soon as the owner changes', async () => {
const view = render(<UserInfoSection isCollapsed={false} />);
diff --git a/apps/desktop/src/renderer/components/sidebar/UserInfoSection.tsx b/apps/desktop/src/renderer/components/sidebar/UserInfoSection.tsx
index c74468f492..007ef1fcfc 100644
--- a/apps/desktop/src/renderer/components/sidebar/UserInfoSection.tsx
+++ b/apps/desktop/src/renderer/components/sidebar/UserInfoSection.tsx
@@ -243,6 +243,10 @@ export function UserInfoSection({ isCollapsed, onOpenUpdateNotice }: UserInfoSec
account.isCurrent || switchingAccountKey !== null || !accountsMutationAllowed
}
onSelect={() => void switchSavedAccount(account)}
+ // Radix synthesizes a click when a press starts outside this item
+ // and releases over it. Async account rows can move under that
+ // release; require a normal click or keyboard selection instead.
+ onPointerUp={(event) => event.preventDefault()}
className="gap-2.5 py-2"
>
<AccountMenuAvatar account={account} />