import { fireEvent, render, screen, waitFor } from "@testing-library/react"; import { QueryClientProvider } from "@tanstack/react-query"; import { describe, expect, it, vi } from "vitest"; import { ProviderForm, type ProviderFormValues, } from "@/components/providers/forms/ProviderForm"; import { createTestQueryClient } from "../utils/testQueryClient"; vi.mock("@/components/providers/forms/CodexOAuthSection", () => ({ CodexOAuthSection: ({ onAccountSelect, }: { onAccountSelect?: (accountId: string | null) => void; }) => (
), })); vi.mock("@/components/providers/forms/CodexConfigEditor", () => ({ default: () =>
, })); vi.mock("@/components/providers/forms/ProviderAdvancedConfig", () => ({ ProviderAdvancedConfig: () =>
, })); vi.mock("@/components/providers/forms/hooks", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, useCopilotAuth: () => ({ isAuthenticated: false, isStatusSuccess: true, isStatusError: false, }), useCodexOauth: () => ({ isAuthenticated: true, isStatusSuccess: true, isStatusError: false, }), useCodexCommonConfig: () => ({ useCommonConfig: false, commonConfigSnippet: "", commonConfigError: null, handleCommonConfigToggle: vi.fn(), handleCommonConfigSnippetChange: vi.fn(), isExtracting: false, handleExtract: vi.fn(), clearCommonConfigError: vi.fn(), }), useGeminiCommonConfig: () => ({ useCommonConfig: false, commonConfigSnippet: "", commonConfigError: null, handleCommonConfigToggle: vi.fn(), handleCommonConfigSnippetChange: vi.fn(), isExtracting: false, handleExtract: vi.fn(), clearCommonConfigError: vi.fn(), }), }; }); vi.mock("@/lib/query", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, useSettingsQuery: () => ({ data: { commonConfigConfirmed: true }, }), }; }); function renderCodexForm(onSubmit: (values: ProviderFormValues) => void) { const queryClient = createTestQueryClient(); return render( , ); } describe("ProviderForm Codex Official managed account", () => { it("persists the selected managed account while stripping OAuth secrets", async () => { const onSubmit = vi.fn(); renderCodexForm(onSubmit); fireEvent.click(screen.getByRole("button", { name: /OpenAI Official/ })); fireEvent.click( await screen.findByRole("button", { name: "select-managed-account" }), ); fireEvent.click(screen.getByRole("button", { name: "save-provider" })); await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1)); const submitted = onSubmit.mock.calls[0][0] as ProviderFormValues; expect(submitted).toEqual( expect.objectContaining({ presetId: "codex-0", presetCategory: "official", meta: expect.objectContaining({ providerType: "codex_oauth", authBinding: { source: "managed_account", authProvider: "codex_oauth", accountId: "acct-managed", }, }), }), ); expect(JSON.parse(submitted.settingsConfig)).toEqual({ auth: {}, config: "", }); }); it("keeps Official on native browser login when no managed account is selected", async () => { const onSubmit = vi.fn(); renderCodexForm(onSubmit); fireEvent.click(screen.getByRole("button", { name: /OpenAI Official/ })); fireEvent.click( await screen.findByRole("button", { name: "select-managed-account" }), ); fireEvent.click( screen.getByRole("button", { name: "select-native-login" }), ); fireEvent.click(screen.getByRole("button", { name: "save-provider" })); await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1)); const submitted = onSubmit.mock.calls[0][0] as ProviderFormValues; expect(submitted.presetId).toBe("codex-0"); expect(submitted.presetCategory).toBe("official"); expect(submitted.meta?.providerType).toBeUndefined(); expect(submitted.meta?.authBinding).toBeUndefined(); }); });