import { fireEvent, render, screen, waitFor } from "@testing-library/react"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { AddProviderDialog } from "@/components/providers/AddProviderDialog"; import type { ProviderFormValues } from "@/components/providers/forms/ProviderForm"; vi.mock("@/components/ui/dialog", () => ({ Dialog: ({ children }: { children: React.ReactNode }) =>
{children}
, DialogContent: ({ children }: { children: React.ReactNode }) => (
{children}
), DialogHeader: ({ children }: { children: React.ReactNode }) => (
{children}
), DialogTitle: ({ children }: { children: React.ReactNode }) => (

{children}

), DialogDescription: ({ children }: { children: React.ReactNode }) => (

{children}

), DialogFooter: ({ children }: { children: React.ReactNode }) => (
{children}
), })); let mockFormValues: ProviderFormValues; vi.mock("@/components/providers/forms/ProviderForm", () => ({ ProviderForm: ({ onSubmit }: { onSubmit: (values: ProviderFormValues) => void }) => (
{ event.preventDefault(); onSubmit(mockFormValues); }} /> ), })); describe("AddProviderDialog", () => { beforeEach(() => { mockFormValues = { name: "Test Provider", websiteUrl: "https://provider.example.com", settingsConfig: JSON.stringify({ env: {}, config: {} }), meta: { custom_endpoints: { "https://api.new-endpoint.com": { url: "https://api.new-endpoint.com", addedAt: 1, }, }, }, }; }); it("使用 ProviderForm 返回的自定义端点", async () => { const handleSubmit = vi.fn().mockResolvedValue(undefined); const handleOpenChange = vi.fn(); render( , ); fireEvent.click( screen.getByRole("button", { name: "common.add", }), ); await waitFor(() => expect(handleSubmit).toHaveBeenCalledTimes(1)); const submitted = handleSubmit.mock.calls[0][0]; expect(submitted.meta?.custom_endpoints).toEqual( mockFormValues.meta?.custom_endpoints, ); expect(handleOpenChange).toHaveBeenCalledWith(false); }); it("在缺少自定义端点时回退到配置中的 baseUrl", async () => { const handleSubmit = vi.fn().mockResolvedValue(undefined); mockFormValues = { name: "Base URL Provider", websiteUrl: "", settingsConfig: JSON.stringify({ env: { ANTHROPIC_BASE_URL: "https://claude.base" }, config: {}, }), }; render( , ); fireEvent.click( screen.getByRole("button", { name: "common.add", }), ); await waitFor(() => expect(handleSubmit).toHaveBeenCalledTimes(1)); const submitted = handleSubmit.mock.calls[0][0]; expect(submitted.meta?.custom_endpoints).toEqual({ "https://claude.base": { url: "https://claude.base", addedAt: expect.any(Number), lastUsed: undefined, }, }); }); });