From 2abcaa6ee0054765a88efdfef0874e29c482839f Mon Sep 17 00:00:00 2001 From: Allen Xu Date: Wed, 11 Nov 2026 07:42:58 -0800 Subject: [PATCH] fix: show account identity for Codex OAuth provider cards (#9300) Co-authored-by: Jason Co-authored-by: Claude Opus 5.5 --- src/components/providers/ProviderCard.tsx | 20 ++-- .../ProviderCard.codexAccount.test.tsx | 91 +++++++++++++++++-- 2 files changed, 94 insertions(+), 17 deletions(-) diff --git a/src/components/providers/ProviderCard.tsx b/src/components/providers/ProviderCard.tsx index 9d5c58752..90a3ad709 100644 --- a/src/components/providers/ProviderCard.tsx +++ b/src/components/providers/ProviderCard.tsx @@ -231,6 +231,11 @@ export function ProviderCard({ }: ProviderCardProps) { const { t } = useTranslation(); const codexOfficialIdentity = resolveCodexOfficialIdentity(appId, provider); + const isBoundCodexOfficial = codexOfficialIdentity === "managed_account"; + const isCodexOauth = + appId === "codex" + ? isBoundCodexOfficial + : provider.meta?.providerType === PROVIDER_TYPES.CODEX_OAUTH; const managedCodexAccountId = resolveManagedAccountId( provider.meta, "codex_oauth", @@ -242,13 +247,13 @@ export function ProviderCard({ } = useQuery({ queryKey: ["managed-auth-status", "codex_oauth"], queryFn: () => authApi.authGetStatus("codex_oauth"), - enabled: - codexOfficialIdentity === "managed_account" && - Boolean(managedCodexAccountId), + enabled: isCodexOauth, staleTime: 30_000, }); const managedCodexAccount = codexAuthStatus?.accounts.find( - (account) => account.id === managedCodexAccountId, + (account) => + account.id === + (managedCodexAccountId ?? codexAuthStatus?.default_account_id), ); const manualNote = provider.notes?.trim() || undefined; const providerNameIncludesAccountLogin = Boolean( @@ -292,7 +297,6 @@ export function ProviderCard({ return true; }, [provider.notes, displayUrl, fallbackUrlText]); - const isBoundCodexOfficial = codexOfficialIdentity === "managed_account"; const isOfficial = isOfficialProvider(provider, appId); // Claude 官方卡绑了 Claude 官方账号:副标题写账号,额度按这个号查 const claudeBoundAccountId = @@ -340,10 +344,6 @@ export function ProviderCard({ appId !== "copilot" && (provider.meta?.providerType === PROVIDER_TYPES.GITHUB_COPILOT || provider.meta?.usage_script?.templateType === "github_copilot"); - const isCodexOauth = - appId === "codex" - ? isBoundCodexOfficial - : provider.meta?.providerType === PROVIDER_TYPES.CODEX_OAUTH; // xAI OAuth (SuperGrok 反代):额度经自管 OAuth token 自动显示,与 codex_oauth 同构 const isXaiOauth = provider.meta?.providerType === PROVIDER_TYPES.XAI_OAUTH; // Antigravity 官方卡绑了授权中心的账号:额度查绑定的号(只有 macOS 能切号,其他平台忽略绑定) @@ -496,7 +496,7 @@ export function ProviderCard({ )} - {codexOfficialIdentity && codexOfficialIdentity !== "api_key" ? ( + {codexOfficialIdentity === "native_login" || isCodexOauth ? (
{codexOfficialIdentity === "native_login" ? ( <> diff --git a/tests/components/ProviderCard.codexAccount.test.tsx b/tests/components/ProviderCard.codexAccount.test.tsx index 62caefa86..4b35b3ce8 100644 --- a/tests/components/ProviderCard.codexAccount.test.tsx +++ b/tests/components/ProviderCard.codexAccount.test.tsx @@ -3,7 +3,9 @@ import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { describe, expect, it, vi } from "vitest"; import { ProviderCard } from "@/components/providers/ProviderCard"; -import type { ManagedAuthStatus } from "@/lib/api"; +import { http, HttpResponse } from "msw"; +import type { AppId, ManagedAuthStatus } from "@/lib/api"; +import { server } from "../msw/server"; import type { Provider } from "@/types"; import { createTestQueryClient } from "../utils/testQueryClient"; @@ -90,6 +92,7 @@ const authStatus = (login: string): ManagedAuthStatus => ({ function renderCard( provider: Provider, options: { + appId?: AppId; status?: ManagedAuthStatus; isCurrent?: boolean; onEdit?: (provider: Provider) => void; @@ -99,11 +102,14 @@ function renderCard( ) { const queryClient = createTestQueryClient(); if (options.planName) { - queryClient.setQueryData(["codex_oauth", "quota", "account-long"], { - success: true, - tiers: [], - planName: options.planName, - }); + queryClient.setQueryData( + [ + "codex_oauth", + "quota", + provider.meta?.authBinding?.accountId ?? "default", + ], + { success: true, tiers: [], planName: options.planName }, + ); } if (options.status) { queryClient.setQueryData( @@ -116,7 +122,7 @@ function renderCard( { + const proxyProvider = (): Provider => ({ + ...managedProvider("Codex"), + category: "third_party", + websiteUrl: "https://openai.com/chatgpt/pricing", + settingsConfig: { + env: { ANTHROPIC_BASE_URL: "https://chatgpt.com/backend-api/codex" }, + }, + }); + + it.each(["claude", "claude-desktop"] as const)( + "shows the bound Codex account and plan instead of the website in %s", + async (appId) => { + server.use( + http.post("http://tauri.local/auth_get_status", () => + HttpResponse.json(authStatus("me@example.com")), + ), + ); + renderCard(proxyProvider(), { appId, planName: "Plus" }); + + expect(await screen.findByText("me@example.com")).toHaveClass("truncate"); + expect(screen.getByText("· Plus")).toBeInTheDocument(); + expect( + screen.queryByText("https://openai.com/chatgpt/pricing"), + ).not.toBeInTheDocument(); + }, + ); + + it("shows the default Codex account when the proxy has no explicit binding", () => { + const provider = proxyProvider(); + delete provider.meta!.authBinding; + const status = authStatus("default@example.com"); + status.accounts.unshift({ + ...status.accounts[0], + id: "another-account", + login: "another@example.com", + is_default: false, + }); + renderCard(provider, { appId: "claude", status, planName: "Plus" }); + + expect(screen.getByText("default@example.com")).toBeInTheDocument(); + expect(screen.getByText("· Plus")).toBeInTheDocument(); + expect(screen.queryByText("another@example.com")).not.toBeInTheDocument(); + }); + + it("preserves manual notes on Codex proxy cards", () => { + renderCard( + { ...proxyProvider(), notes: "Work account" }, + { + appId: "claude", + status: authStatus("me@example.com"), + planName: "Plus", + }, + ); + + expect(screen.getByText("Work account")).toBeInTheDocument(); + expect(screen.getByText("· Plus")).toBeInTheDocument(); + expect(screen.queryByText("me@example.com")).not.toBeInTheDocument(); + }); + + it("does not replace a missing bound proxy account with the default account", () => { + const provider = proxyProvider(); + provider.meta!.authBinding!.accountId = "deleted-account"; + renderCard(provider, { + appId: "claude", + status: authStatus("default@example.com"), + }); + + expect(screen.getByText("绑定的账号不可用")).toBeInTheDocument(); + expect(screen.queryByText("default@example.com")).not.toBeInTheDocument(); + }); + it("puts the bound account's plan after the login once its quota is in", () => { renderCard(managedProvider("OpenAI Official"), { status: authStatus("me@example.com"),