Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
// @vitest-environment jsdom
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,
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: () => ({
t: (key: string) => key,
}),
}));
vi.mock('react-router-dom', () => ({
useLocation: () => ({ pathname: '/', search: '' }),
useNavigate: () => navigate,
}));
vi.mock('@/contexts/AuthContext', () => ({
useAuth: () => ({
...authState,
listAccounts,
syncAccounts,
switchAccount,
}),
}));
vi.mock('@/components/ui/confirm-dialog-provider', () => ({
useOptionalConfirmDialog: () => ({ confirm }),
}));
vi.mock('@/lib/makerChatStore', () => ({
makerChatStore: { getRunningSnapshot: () => runningSnapshot },
}));
vi.mock('@/hooks/useUpdateStatus', () => ({
useUpdateStatus: () => ({ status: 'idle' }),
}));
vi.mock('@/hooks/useUpdateBannerDismiss', () => ({
useUpdateBannerDismiss: () => ({ dismissed: false, restore: vi.fn() }),
}));
vi.mock('@/hooks/useBetaChannelSettings', () => ({
useBetaChannelSettings: () => ({ state: betaChannelState }),
}));
vi.mock('@/hooks/useLogout', () => ({
useLogout: () => ({ handleLogout: vi.fn() }),
}));
vi.mock('@/components/sidebar/MobileDownloadDialog', () => ({
MobileDownloadDialog: ({
open,
remoteAvailable,
onOpenRemoteSettings,
onOpenDevices,
}: {
open: boolean;
remoteAvailable: boolean;
onOpenRemoteSettings: () => void;
onOpenDevices: () => void;
}) =>
open ? (
<div role="dialog">
<span>{remoteAvailable ? 'remote available' : 'remote unavailable'}</span>
<button type="button" onClick={onOpenRemoteSettings}>
open remote settings
</button>
<button type="button" onClick={onOpenDevices}>
open linked devices
</button>
</div>
) : null,
}));
import { UserInfoSection } from '@/components/sidebar/UserInfoSection';
import { toast } from '@/lib/toast';
vi.mock('@/lib/toast', () => ({
toast: { loading: vi.fn(() => 'progress-toast'), dismiss: vi.fn(), error: vi.fn() },
}));
beforeEach(() => {
vi.mocked(toast.loading).mockClear();
vi.mocked(toast.dismiss).mockClear();
vi.mocked(toast.error).mockClear();
confirm.mockReset().mockResolvedValue(true);
runningSnapshot.clear();
navigate.mockReset();
authState.user = { name: 'Cindy user', avatar: null };
authState.mode = 'cloud';
authState.dataOwnerId = 'owner-a';
listAccounts.mockReset().mockResolvedValue({
mutationAllowed: true,
accounts: [
{
accountKey: 'current',
displayName: 'Cindy user',
email: '[email protected]',
avatarUrl: null,
kind: 'personal',
orgName: null,
orgLogoUrl: null,
isCurrent: true,
},
{
accountKey: 'other',
displayName: 'Other user',
email: '[email protected]',
avatarUrl: null,
kind: 'personal',
orgName: null,
orgLogoUrl: null,
isCurrent: false,
},
],
});
syncAccounts.mockReset().mockResolvedValue({
mutationAllowed: true,
accounts: [
{
accountKey: 'current',
displayName: 'Cindy user',
email: '[email protected]',
avatarUrl: null,
kind: 'personal',
orgName: null,
orgLogoUrl: null,
isCurrent: true,
},
{
accountKey: 'other',
displayName: 'Other user',
email: '[email protected]',
avatarUrl: null,
kind: 'personal',
orgName: null,
orgLogoUrl: null,
isCurrent: false,
},
],
});
switchAccount.mockReset().mockResolvedValue(undefined);
betaChannelState.enableBeta = false;
betaChannelState.loading = false;
Object.defineProperty(window, 'electronAPI', {
configurable: true,
value: {
appDisplayVersion: '1.0.0',
appDisplayVersionDetail: '1.0.0-test',
},
});
});
afterEach(cleanup);
describe('UserInfoSection mobile download entry', () => {
it('shows the Beta label beside the expanded app version when the channel is enabled', () => {
betaChannelState.enableBeta = true;
render(<UserInfoSection isCollapsed={false} />);
expect(screen.getByTestId('sidebar-beta-channel-label').textContent).toBe(
'settings.betaChannel.badge',
);
expect(screen.getByRole('button', { name: 'sidebar.user.moreLabel' })).toBeTruthy();
});
it.each([
['expanded', false],
['collapsed', true],
])('opens the dialog from the %s sidebar', (_label, isCollapsed) => {
render(<UserInfoSection isCollapsed={isCollapsed} />);
fireEvent.click(
screen.getByRole('button', {
name: 'sidebar.user.downloadMobile',
}),
);
expect(screen.getByRole('dialog')).toBeTruthy();
expect(screen.getByText('remote available')).toBeTruthy();
fireEvent.click(screen.getByRole('button', { name: 'open remote settings' }));
expect(navigate).toHaveBeenCalledWith('/settings?tab=remote-control');
});
it('opens the expanded device list from the dialog', () => {
render(<UserInfoSection isCollapsed={false} />);
fireEvent.click(
screen.getByRole('button', {
name: 'sidebar.user.downloadMobile',
}),
);
fireEvent.click(screen.getByRole('button', { name: 'open linked devices' }));
expect(navigate).toHaveBeenCalledWith('/settings?tab=remote-control§ion=devices');
});
it('shows all saved accounts only in the multi-account menu and switches directly', async () => {
render(<UserInfoSection isCollapsed={false} />);
fireEvent.pointerDown(screen.getByRole('button', { name: 'sidebar.user.moreLabel' }), {
button: 0,
ctrlKey: false,
});
expect(await screen.findByRole('menuitem', { name: /Other user/ })).toBeTruthy();
expect(screen.getByText('sidebar.accountSwitcher.current')).toBeTruthy();
expect(screen.getByRole('menuitem', { name: /Cindy user/ }).getAttribute('aria-disabled')).toBe(
'true',
);
await userEvent.click(screen.getByRole('menuitem', { name: /Other user/ }));
await waitFor(() => expect(switchAccount).toHaveBeenCalledWith('other'));
});
it('explains a loaded shared-data restriction without mislabeling the loading state', async () => {
const saved = await listAccounts();
let resolveList!: (value: typeof saved) => void;
listAccounts.mockReturnValueOnce(
new Promise((resolve) => {
resolveList = resolve;
}),
);
syncAccounts.mockResolvedValue({ ...saved, mutationAllowed: false });
render(<UserInfoSection isCollapsed={false} />);
fireEvent.keyDown(screen.getByRole('button', { name: 'sidebar.user.moreLabel' }), {
key: 'Enter',
});
expect(screen.queryByText('sidebar.accountSwitcher.sharedDataRestriction')).toBeNull();
await act(async () => resolveList({ ...saved, mutationAllowed: false }));
expect(await screen.findByText('sidebar.accountSwitcher.sharedDataRestriction')).toBeTruthy();
const other = screen.getByRole('menuitem', { name: /Other user/ });
expect(other.getAttribute('aria-disabled')).toBe('true');
fireEvent.click(other);
expect(switchAccount).not.toHaveBeenCalled();
expect(toast.loading).not.toHaveBeenCalled();
});
it.each([false, true])(
'keeps progress visible after menu dismissal and clears it on completion (failure=%s)',
async (failure) => {
let resolveSwitch!: () => void;
let rejectSwitch!: (reason: Error) => void;
switchAccount.mockReturnValueOnce(
new Promise<void>((resolve, reject) => {
resolveSwitch = resolve;
rejectSwitch = reject;
}),
);
render(<UserInfoSection isCollapsed={false} />);
fireEvent.keyDown(screen.getByRole('button', { name: 'sidebar.user.moreLabel' }), {
key: 'Enter',
});
await userEvent.click(await screen.findByRole('menuitem', { name: /Other user/ }));
await waitFor(() =>
expect(toast.loading).toHaveBeenCalledWith('sidebar.accountSwitcher.switching'),
);
expect(screen.queryByRole('menu')).toBeNull();
expect(toast.dismiss).not.toHaveBeenCalled();
await act(async () => {
if (failure) rejectSwitch(new Error('switch failed'));
else resolveSwitch();
});
expect(toast.dismiss).toHaveBeenCalledWith('progress-toast');
if (failure) expect(toast.error).toHaveBeenCalledWith('sidebar.accountSwitcher.switchFailed');
else expect(toast.error).not.toHaveBeenCalled();
fireEvent.keyDown(screen.getByRole('button', { name: 'sidebar.user.moreLabel' }), {
key: 'Enter',
});
expect(
(await screen.findByRole('menuitem', { name: /Other user/ })).getAttribute('aria-disabled'),
).not.toBe('true');
},
);
it('does not report switching while confirmation is pending or cancelled', async () => {
runningSnapshot.set('running-session', { isRunning: true });
let resolveConfirm!: (confirmed: boolean) => void;
confirm.mockReturnValueOnce(
new Promise<boolean>((resolve) => {
resolveConfirm = resolve;
}),
);
render(<UserInfoSection isCollapsed={false} />);
fireEvent.keyDown(screen.getByRole('button', { name: 'sidebar.user.moreLabel' }), {
key: 'Enter',
});
await userEvent.click(await screen.findByRole('menuitem', { name: /Other user/ }));
await waitFor(() => expect(confirm).toHaveBeenCalledOnce());
expect(toast.loading).not.toHaveBeenCalled();
await act(async () => resolveConfirm(false));
expect(toast.loading).not.toHaveBeenCalled();
expect(switchAccount).not.toHaveBeenCalled();
});
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} />);
fireEvent.pointerDown(screen.getByRole('button', { name: 'sidebar.user.moreLabel' }), {
button: 0,
ctrlKey: false,
});
expect(await screen.findByRole('menuitem', { name: /Other user/ })).toBeTruthy();
authState.dataOwnerId = 'owner-b';
view.rerender(<UserInfoSection isCollapsed={false} />);
expect(screen.queryByRole('menuitem', { name: /Other user/ })).toBeNull();
});
it('does not show an account section when only one account is saved', async () => {
listAccounts.mockResolvedValueOnce({
mutationAllowed: true,
accounts: [
{
accountKey: 'current',
displayName: 'Cindy user',
email: '[email protected]',
avatarUrl: null,
kind: 'personal',
orgName: null,
orgLogoUrl: null,
isCurrent: true,
},
],
});
syncAccounts.mockResolvedValueOnce({
mutationAllowed: true,
accounts: [
{
accountKey: 'current',
displayName: 'Cindy user',
email: '[email protected]',
avatarUrl: null,
kind: 'personal',
orgName: null,
orgLogoUrl: null,
isCurrent: true,
},
],
});
render(<UserInfoSection isCollapsed={false} />);
fireEvent.pointerDown(screen.getByRole('button', { name: 'sidebar.user.moreLabel' }), {
button: 0,
ctrlKey: false,
});
await waitFor(() => expect(syncAccounts).toHaveBeenCalledOnce());
expect(screen.queryByRole('menuitem', { name: /Cindy user/ })).toBeNull();
});
it('keeps the sign-in entry before Settings when the user is not signed in', async () => {
authState.user = null;
authState.mode = 'local';
render(<UserInfoSection isCollapsed={false} />);
fireEvent.pointerDown(screen.getByRole('button', { name: 'sidebar.user.moreLabel' }), {
button: 0,
ctrlKey: false,
});
const signIn = await screen.findByRole('menuitem', {
name: 'login.signIn',
});
const settings = screen.getByRole('menuitem', { name: 'sidebar.user.menuSettings' });
expect(signIn.compareDocumentPosition(settings) & Node.DOCUMENT_POSITION_FOLLOWING).toBe(
Node.DOCUMENT_POSITION_FOLLOWING,
);
expect(listAccounts).not.toHaveBeenCalled();
fireEvent.click(signIn);
await waitFor(() =>
expect(navigate).toHaveBeenCalledWith('/add-account', { state: { returnTo: '/' } }),
);
expect(toast.loading).not.toHaveBeenCalled();
});
it('reports navigation failure and re-enables the sign-in entry', async () => {
authState.user = null;
authState.mode = 'local';
navigate.mockRejectedValueOnce(new Error('navigation failed'));
render(<UserInfoSection isCollapsed={false} />);
fireEvent.keyDown(screen.getByRole('button', { name: 'sidebar.user.moreLabel' }), {
key: 'Enter',
});
await userEvent.click(await screen.findByRole('menuitem', { name: 'login.signIn' }));
await waitFor(() =>
expect(toast.error).toHaveBeenCalledWith('sidebar.accountSwitcher.startFailed'),
);
expect(toast.loading).not.toHaveBeenCalled();
fireEvent.keyDown(screen.getByRole('button', { name: 'sidebar.user.moreLabel' }), {
key: 'Enter',
});
expect(
(await screen.findByRole('menuitem', { name: 'login.signIn' })).getAttribute('aria-disabled'),
).not.toBe('true');
});
});