mirror of
https://github.com/makecindy/cindy.git
synced 2026-12-02 15:31:12 +08:00
fix(desktop): 供应商向导 OpenAI 检测建议直达不再静默自关 (#268)
codexAuth 反映的是整机 ChatGPT 凭证,不含「凭证归哪个账号」的 native binding。 换账号后本机已有他人绑定的登录时,检测建议直达打开的向导会挂载即自关: 不弹任何 UI、也不给当前账号补绑定,「去授权」永远点不出效果。 现在只在本向导内发起过登录后,codexAuth 状态翻 connected 才收口自动完成; 直达打开时向导保持可见,点「授权」走完整登录并把凭证绑定到当前账号。 Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
/**
|
||||
* AddProviderWizard —— OpenAI 检测建议直达的关键不变量:
|
||||
*
|
||||
* 1. 本机已有 ChatGPT 凭证(codexAuth 已是 oauth connected)但当前账号未绑定时,
|
||||
* 向导**不得**挂载即自关:codexAuth 是整机凭证态,不含按账号的 native binding,
|
||||
* 自关会既不弹 UI 也不补绑定,「去授权」从此点不出任何效果(回归:换账号死循环)。
|
||||
* 2. 本向导内点「授权」发起登录并成功 → 正常收口 onDone('openai')。
|
||||
* 3. anthropic entry 直达 → 授权步正常渲染(对照组,证明弹窗结构本身可用)。
|
||||
*/
|
||||
|
||||
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
||||
import React from 'react';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import type { ProviderView } from '@cindy/model-providers';
|
||||
|
||||
const { codexState, triggerLoginMock } = vi.hoisted(() => ({
|
||||
codexState: { kind: 'unauthenticated' } as { kind: string; authSource?: string },
|
||||
triggerLoginMock: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('react-i18next', () => ({
|
||||
useTranslation: () => ({ t: (key: string) => key, i18n: { language: 'zh-CN' } }),
|
||||
}));
|
||||
|
||||
vi.mock('@/hooks/useCodexAuth', () => ({
|
||||
// 与真实实现同判定:仅 authenticated + oauth 视为 ChatGPT 已连接。
|
||||
isChatGptConnectionConnected: (state: { kind: string; authSource?: string }) =>
|
||||
state.kind === 'authenticated' && state.authSource === 'oauth',
|
||||
useCodexAuth: () => ({
|
||||
state: codexState,
|
||||
triggerLogin: triggerLoginMock,
|
||||
cancelLogin: vi.fn(),
|
||||
logout: vi.fn(),
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/toast', () => ({
|
||||
toast: { error: vi.fn(), success: vi.fn() },
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/customProviders', () => ({
|
||||
createCustomProvider: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/customProviderId', () => ({
|
||||
uniqueCustomProviderId: (name: string) => name,
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/providerModels', () => ({
|
||||
providerMonogram: () => 'X',
|
||||
}));
|
||||
|
||||
vi.mock('@/components/icons/ProviderLogoMark', () => ({
|
||||
hasProviderLogo: () => false,
|
||||
ProviderLogoMark: () => null,
|
||||
}));
|
||||
|
||||
import { AddProviderWizard } from '@/components/settings/AddProviderWizard';
|
||||
|
||||
const anthropicProvider = {
|
||||
id: 'anthropic',
|
||||
name: 'Anthropic',
|
||||
source: 'builtin',
|
||||
agents: ['claude-code'],
|
||||
auth: { method: 'oauth' },
|
||||
routing: {},
|
||||
models: { 'claude-code': [] },
|
||||
connected: false,
|
||||
} as unknown as ProviderView;
|
||||
|
||||
const openaiProvider = {
|
||||
...anthropicProvider,
|
||||
id: 'openai',
|
||||
name: 'OpenAI',
|
||||
agents: ['claude-code', 'codex'],
|
||||
} as unknown as ProviderView;
|
||||
|
||||
function renderWizard(providerId: string, onDone: (id?: string) => void) {
|
||||
return render(
|
||||
React.createElement(AddProviderWizard, {
|
||||
providers: [anthropicProvider, openaiProvider],
|
||||
entry: { kind: 'builtin' as const, providerId },
|
||||
onOpenCustomForm: vi.fn(),
|
||||
onClose: vi.fn(),
|
||||
onDone,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
codexState.kind = 'unauthenticated';
|
||||
delete codexState.authSource;
|
||||
(window as unknown as { electronAPI: unknown }).electronAPI = {
|
||||
maker: {
|
||||
listProviderPresets: vi.fn(async () => ({ presets: [] })),
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('AddProviderWizard — OpenAI 检测建议直达', () => {
|
||||
it('本机已有 ChatGPT 凭证但账号未绑定 → 向导保持打开,不静默自关', () => {
|
||||
codexState.kind = 'authenticated';
|
||||
codexState.authSource = 'oauth';
|
||||
const onDone = vi.fn();
|
||||
renderWizard('openai', onDone);
|
||||
|
||||
// 授权步可见、可交互;onDone 不得被立即触发。
|
||||
expect(screen.getByText('settings.providers.button.authorize')).not.toBeNull();
|
||||
expect(onDone).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('本向导内点「授权」登录成功 → onDone(openai) 收口', async () => {
|
||||
triggerLoginMock.mockResolvedValue('authenticated');
|
||||
const onDone = vi.fn();
|
||||
renderWizard('openai', onDone);
|
||||
|
||||
fireEvent.click(screen.getByText('settings.providers.button.authorize'));
|
||||
await waitFor(() => expect(onDone).toHaveBeenCalledWith('openai'));
|
||||
expect(triggerLoginMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('anthropic entry 直达 → 授权步正常渲染(对照组)', () => {
|
||||
const onDone = vi.fn();
|
||||
renderWizard('anthropic', onDone);
|
||||
|
||||
expect(screen.getByText('settings.providers.wizard.titleWith')).not.toBeNull();
|
||||
expect(screen.getByText('settings.providers.button.authorize')).not.toBeNull();
|
||||
expect(onDone).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -25,7 +25,7 @@ import { Tip } from '@/components/ui/tooltip';
|
||||
import { createCustomProvider, type RuntimeKeys } from '@/lib/customProviders';
|
||||
import { uniqueCustomProviderId } from '@/lib/customProviderId';
|
||||
import { providerMonogram } from '@/lib/providerModels';
|
||||
import { useCodexAuth } from '@/hooks/useCodexAuth';
|
||||
import { isChatGptConnectionConnected, useCodexAuth } from '@/hooks/useCodexAuth';
|
||||
import { hasProviderLogo, ProviderLogoMark } from '@/components/icons/ProviderLogoMark';
|
||||
|
||||
import { sortPresetsForLocale } from '@cindy/model-providers';
|
||||
@@ -261,6 +261,14 @@ export function AddProviderWizard({
|
||||
setStep(2);
|
||||
}, []);
|
||||
|
||||
/**
|
||||
* 本向导内是否发起过 OpenAI 登录。codexAuth 反映的是整机 ChatGPT 凭证,不含
|
||||
* 「凭证归哪个账号」的 native binding —— 换账号后本机可能已有别人绑定的登录,
|
||||
* 此时 codexAuth 一挂载就是 connected;若不加此限定,检测建议直达打开的向导会
|
||||
* 挂载即自关,既不弹任何 UI 也不给当前账号补绑定,「去授权」永远点不出效果。
|
||||
*/
|
||||
const openaiLoginStartedRef = useRef(false);
|
||||
|
||||
// ── OAuth 授权(复用既有鉴权流;成功即完成,无第 3 步)────────────────────
|
||||
const handleAuthorize = useCallback(async () => {
|
||||
if (!sel || sel.kind !== 'oauth') return;
|
||||
@@ -277,6 +285,7 @@ export function AddProviderWizard({
|
||||
}
|
||||
if (!r.ok && r.reason === 'login_cancelled') return;
|
||||
} else if (id === 'openai') {
|
||||
openaiLoginStartedRef.current = true;
|
||||
const outcome = await codexAuth.triggerLogin();
|
||||
ok = outcome === 'authenticated';
|
||||
if (outcome === 'cancelled') return;
|
||||
@@ -322,6 +331,22 @@ export function AddProviderWizard({
|
||||
onClose();
|
||||
}, [loggingIn, cancelAuthorize, onClose]);
|
||||
|
||||
// OpenAI 走 useCodexAuth:hook 状态翻 connected 时视为完成(triggerLogin 也会返回,
|
||||
// oneshot ref 保证只收口一次)。只收口本向导内发起的登录(openaiLoginStartedRef),
|
||||
// 不把「本机已有凭证」误当成完成 —— 见 openaiLoginStartedRef 的注释。
|
||||
const openaiDoneRef = useRef(false);
|
||||
useEffect(() => {
|
||||
if (openaiDoneRef.current || !openaiLoginStartedRef.current) return;
|
||||
if (
|
||||
sel?.kind === 'oauth' &&
|
||||
sel.provider.id === 'openai' &&
|
||||
isChatGptConnectionConnected(codexAuth.state, false)
|
||||
) {
|
||||
openaiDoneRef.current = true;
|
||||
onDone('openai');
|
||||
}
|
||||
}, [codexAuth.state, sel, onDone]);
|
||||
|
||||
// ── 预设:进入 Step 3 时自动拉取模型 ─────────────────────────────────────
|
||||
const startFetch = useCallback(async () => {
|
||||
if (!sel || sel.kind !== 'preset') return;
|
||||
|
||||
Reference in New Issue
Block a user