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(auth): surface desktop logout failures

xd-bobo7a284545a609
diff --git a/apps/desktop/src/renderer/hooks/__tests__/useLogout.test.ts b/apps/desktop/src/renderer/hooks/__tests__/useLogout.test.ts
new file mode 100644
index 0000000000..281b76c754
--- /dev/null
+++ b/apps/desktop/src/renderer/hooks/__tests__/useLogout.test.ts
@@ -0,0 +1,75 @@
+// @vitest-environment jsdom
+
+import { act, renderHook } from '@testing-library/react';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { useLogout } from '../useLogout';
+
+const mocks = vi.hoisted(() => ({
+ confirm: vi.fn(),
+ logout: vi.fn(),
+ toastError: vi.fn(),
+}));
+
+vi.mock('react-i18next', () => ({
+ useTranslation: () => ({
+ t: (key: string, options?: { defaultValue?: string }) => options?.defaultValue ?? key,
+ }),
+}));
+
+vi.mock('@/contexts/AuthContext', () => ({
+ useAuth: () => ({ logout: mocks.logout }),
+}));
+
+vi.mock('@/components/ui/confirm-dialog-provider', () => ({
+ useConfirmDialog: () => ({ confirm: mocks.confirm }),
+}));
+
+vi.mock('@/lib/toast', () => ({
+ toast: { error: mocks.toastError },
+}));
+
+describe('useLogout', () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it('does not log out when confirmation is cancelled', async () => {
+ mocks.confirm.mockResolvedValue(false);
+ const { result } = renderHook(() => useLogout());
+
+ await act(async () => {
+ await result.current.handleLogout();
+ });
+
+ expect(mocks.logout).not.toHaveBeenCalled();
+ expect(mocks.toastError).not.toHaveBeenCalled();
+ });
+
+ it('logs out after confirmation without showing an error', async () => {
+ mocks.confirm.mockResolvedValue(true);
+ mocks.logout.mockResolvedValue(undefined);
+ const { result } = renderHook(() => useLogout());
+
+ await act(async () => {
+ await result.current.handleLogout();
+ });
+
+ expect(mocks.logout).toHaveBeenCalledOnce();
+ expect(mocks.toastError).not.toHaveBeenCalled();
+ });
+
+ it('contains credential-store failures and shows a retryable fallback', async () => {
+ mocks.confirm.mockResolvedValue(true);
+ mocks.logout.mockRejectedValue(
+ new Error('Error invoking remote method: Error: [CREDENTIAL_STORE_UNAVAILABLE] vault locked'),
+ );
+ const { result } = renderHook(() => useLogout());
+
+ await act(async () => {
+ await expect(result.current.handleLogout()).resolves.toBeUndefined();
+ });
+
+ expect(mocks.toastError).toHaveBeenCalledWith('ipcError.INTERNAL');
+ });
+});
diff --git a/apps/desktop/src/renderer/hooks/useLogout.ts b/apps/desktop/src/renderer/hooks/useLogout.ts
index e93bce8e2a..09770d140f 100644
--- a/apps/desktop/src/renderer/hooks/useLogout.ts
+++ b/apps/desktop/src/renderer/hooks/useLogout.ts
@@ -3,6 +3,8 @@ import { useTranslation } from 'react-i18next';
import { useAuth } from '@/contexts/AuthContext';
import { useConfirmDialog } from '@/components/ui/confirm-dialog-provider';
+import { toast } from '@/lib/toast';
+import { mapIpcErrorToI18nKey } from '@/utils/ipcError';
export function useLogout(): { handleLogout: () => Promise<void> } {
const { logout } = useAuth();
@@ -17,8 +19,16 @@ export function useLogout(): { handleLogout: () => Promise<void> } {
cancelText: t('logic.confirm.cancel'),
confirmVariant: 'destructive',
});
- if (confirmed) {
+ if (!confirmed) return;
+
+ try {
await logout();
+ } catch (error) {
+ toast.error(
+ t(mapIpcErrorToI18nKey(error, { fallback: 'ipcError.INTERNAL' }), {
+ defaultValue: t('ipcError.INTERNAL'),
+ }),
+ );
}
}, [logout, confirm, t]);