Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 避免登录页重复初始化误报失败 (#3984)
Dash35905e700c40
diff --git a/apps/desktop/src/renderer/components/login/LoginPage.tsx b/apps/desktop/src/renderer/components/login/LoginPage.tsxindex c5d1281d01..03e4ad2ec8 100644--- a/apps/desktop/src/renderer/components/login/LoginPage.tsx+++ b/apps/desktop/src/renderer/components/login/LoginPage.tsx@@ -128,6 +128,7 @@ export function LoginPage({intent?: 'sign-in' | 'add-account';onClose?: () => void;}) {+ // AddAccountLoginPage owns initialization: a second load would race its flow reset.const {isLoading,errorCode,@@ -140,7 +141,7 @@ export function LoginPage({dispatchWithResult,clearError,enterLocalMode,- } = useLogin();+ } = useLogin({ autoLoad: intent !== 'add-account' });const { t } = useTranslation();const handoff = useLoginHandoff();const isAddAccount = intent === 'add-account';diff --git a/apps/desktop/src/renderer/components/login/__tests__/LoginPage.initialization.test.tsx b/apps/desktop/src/renderer/components/login/__tests__/LoginPage.initialization.test.tsxnew file mode 100644index 0000000000..cabf2168fb--- /dev/null+++ b/apps/desktop/src/renderer/components/login/__tests__/LoginPage.initialization.test.tsx@@ -0,0 +1,129 @@+// @vitest-environment jsdom++import { useState } from 'react';+import { cleanup, render, screen, waitFor } from '@testing-library/react';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { MemoryRouter } from 'react-router-dom';+import { CindyAuthClient, reduceAuthFlow, type AuthFlowState } from '@cindy/auth-client';+import { createScenarioFetch } from '@cindy/auth-client/fixtures';+import type { DesktopLoginActionResult } from '../../../../shared/authIpc';++const auth = vi.hoisted(() => ({+ value: {} as Record<string, unknown>,+ loadLoginState: vi.fn<() => Promise<DesktopLoginActionResult>>(),+ beginAddAccount: vi.fn<() => Promise<DesktopLoginActionResult>>(),+ cancelAddAccount: vi.fn(async () => undefined),+}));++vi.mock('@/contexts/AuthContext', () => ({ useAuth: () => auth.value }));+vi.mock('react-i18next', () => ({+ useTranslation: () => ({ t: (key: string) => key }),+}));+vi.mock('@/components/title-bar/WindowControls', () => ({ WindowControls: () => null }));+vi.mock('@/components/sidebar/AccountSwitcherDialog', () => ({+ AccountSwitcherDialog: () => null,+}));++import { AppShellCoverProvider } from '@/contexts/AppShellCoverContext';+import { AddAccountLoginPage } from '../AddAccountLoginPage';+import { LoginPage } from '../LoginPage';++let identifierState: AuthFlowState;+let publishState: (state: AuthFlowState | null) => void;+let epoch: number;++function Harness({ addAccount = false }: { addAccount?: boolean }) {+ const [loginState, setLoginState] = useState<AuthFlowState | null>(null);+ publishState = setLoginState;+ auth.value = {+ loginState,+ loadLoginState: auth.loadLoginState,+ beginAddAccount: auth.beginAddAccount,+ cancelAddAccount: auth.cancelAddAccount,+ };+ return (+ <AppShellCoverProvider>+ <MemoryRouter>{addAccount ? <AddAccountLoginPage /> : <LoginPage />}</MemoryRouter>+ </AppShellCoverProvider>+ );+}++beforeEach(async () => {+ epoch = 0;+ Object.defineProperty(window, 'electronAPI', {+ configurable: true,+ value: { platform: 'darwin' },+ });+ const providers = await new CindyAuthClient({+ baseUrl: 'https://auth.scenario.invalid',+ region: 'global',+ deviceId: 'login-initialization-test',+ clientType: 'desktop',+ fetch: createScenarioFetch('providers:both', { region: 'global' })!,+ }).getProviders();+ identifierState = reduceAuthFlow(null, { type: 'providers-loaded', providers });++ // Model main's epoch contract: beginAddAccount invalidates any earlier provider load.+ auth.loadLoginState.mockImplementation(async () => {+ const loadEpoch = epoch;+ await Promise.resolve();+ publishState(identifierState);+ return loadEpoch === epoch+ ? { success: true, state: identifierState }+ : { success: false, code: 'AUTH_FLOW_SUPERSEDED', state: identifierState };+ });+ auth.beginAddAccount.mockImplementation(async () => {+ epoch += 1;+ await Promise.resolve();+ publishState(identifierState);+ return { success: true, state: identifierState };+ });+});++afterEach(() => {+ cleanup();+ vi.clearAllMocks();+});++describe('login initialization ownership', () => {+ it('opens add-account login without an invalidated load or a false failure', async () => {+ render(<Harness addAccount />);++ await screen.findByTestId('login-panel-identifier');+ await waitFor(() => expect(auth.beginAddAccount).toHaveBeenCalledOnce());+ expect(screen.queryByTestId('login-error-text')).toBeNull();+ expect(auth.loadLoginState).not.toHaveBeenCalled();+ expect(screen.getByTestId('login-input').getAttribute('style')).not.toContain(+ 'var(--login-error-fg)',+ );+ });++ it('still initializes ordinary sign-in automatically', async () => {+ render(<Harness />);++ await screen.findByTestId('login-panel-identifier');+ expect(auth.loadLoginState).toHaveBeenCalledOnce();+ expect(auth.beginAddAccount).not.toHaveBeenCalled();+ expect(screen.queryByTestId('login-error-text')).toBeNull();+ });++ it('preserves the retry screen when add-account initialization actually fails', async () => {+ auth.beginAddAccount.mockImplementation(async () => {+ const state: AuthFlowState = {+ step: 'error',+ code: 'AUTH_SERVICE_UNAVAILABLE',+ recoverTo: 'identifier',+ };+ publishState(state);+ return { success: false, code: state.code, state };+ });+ render(<Harness addAccount />);++ await screen.findByTestId('login-panel-error');+ expect(screen.getByTestId('login-error-retry')).toBeTruthy();+ expect(screen.getByTestId('login-error-text').textContent).toBe(+ 'login.errors.AUTH_SERVICE_UNAVAILABLE',+ );+ expect(auth.loadLoginState).not.toHaveBeenCalled();+ });+});diff --git a/apps/desktop/src/renderer/hooks/useLogin.ts b/apps/desktop/src/renderer/hooks/useLogin.tsindex 1d7808d4f1..bd09fe799c 100644--- a/apps/desktop/src/renderer/hooks/useLogin.ts+++ b/apps/desktop/src/renderer/hooks/useLogin.ts@@ -31,7 +31,7 @@ interface UseLoginReturn {}/** Coordinates presentation state while all credentials and tickets stay in main. */-export function useLogin(): UseLoginReturn {+export function useLogin({ autoLoad = true }: { autoLoad?: boolean } = {}): UseLoginReturn {const {loginState,loadLoginState,@@ -47,7 +47,7 @@ export function useLogin(): UseLoginReturn {const loadingRef = useRef(false);useEffect(() => {- if (loginState || loadingRef.current) return;+ if (!autoLoad || loginState || loadingRef.current) return;loadingRef.current = true;setIsLoading(true);void loadLoginState()@@ -59,7 +59,7 @@ export function useLogin(): UseLoginReturn {loadingRef.current = false;setIsLoading(false);});- }, [loadLoginState, loginState]);+ }, [autoLoad, loadLoginState, loginState]);const dispatchWithResult = useCallback(async (action: DesktopLoginAction): Promise<{ success: boolean; code: string | null }> => {