- 新增 providers/models 数据库表、迁移和数据访问层 - 新增 15 个后端 API 路由(供应商/模型 CRUD + 连通性测试) - 新增 AI 服务层(registry.ts: buildProviderRegistry + testProviderConnection) - 新增前端模型管理页面(Tabs: 供应商/模型,含表格、表单、工具栏) - 新增前端 hooks(use-providers, use-models) - 新增共享类型和 MODEL_CAPABILITIES 常量 - 新增 10 个测试文件(66 个测试用例,4 个因 bun test ESM 兼容问题待修复) - 更新开发文档(architecture, backend, frontend) - 附带 apply-review 修复:统一错误响应、提取共享常量、清理重复测试 注意:registry.test.ts 中 4 个测试因 bun test 无法解析 createProviderRegistry ESM 导出而失败,详情见 context.md
128 lines
3.9 KiB
TypeScript
128 lines
3.9 KiB
TypeScript
import { fireEvent, screen, waitFor } from "@testing-library/react";
|
|
import { describe, expect, mock, test } from "bun:test";
|
|
import { createElement } from "react";
|
|
|
|
import type { Model, Provider } from "../../../src/shared/api";
|
|
|
|
import { ModelFormModal } from "../../../src/web/pages/models/components/ModelFormModal";
|
|
import { ProviderFormModal } from "../../../src/web/pages/models/components/ProviderFormModal";
|
|
import { renderWithProviders } from "../test-utils";
|
|
|
|
const ENABLED_PROVIDER: Provider = {
|
|
apiKey: "sk-test",
|
|
baseUrl: "https://api.openai.com/v1",
|
|
createdAt: "2024-01-01T00:00:00.000Z",
|
|
enabled: true,
|
|
id: "pv1",
|
|
name: "OpenAI",
|
|
type: "openai",
|
|
updatedAt: "2024-01-01T00:00:00.000Z",
|
|
};
|
|
|
|
const DISABLED_PROVIDER: Provider = {
|
|
apiKey: "sk-off",
|
|
baseUrl: "https://api.deepseek.com/v1",
|
|
createdAt: "2024-01-01T00:00:00.000Z",
|
|
enabled: false,
|
|
id: "pv2",
|
|
name: "DeepSeek",
|
|
type: "openai-compatible",
|
|
updatedAt: "2024-01-01T00:00:00.000Z",
|
|
};
|
|
|
|
const ENABLED_MODEL: Model = {
|
|
capabilities: ["text", "reasoning"],
|
|
contextLength: 128000,
|
|
createdAt: "2024-01-01T00:00:00.000Z",
|
|
enabled: true,
|
|
id: "m1",
|
|
maxOutputTokens: 4096,
|
|
modelId: "gpt-4o",
|
|
name: "GPT-4o",
|
|
providerId: "pv1",
|
|
updatedAt: "2024-01-01T00:00:00.000Z",
|
|
};
|
|
|
|
function clickLatestConfirmButton() {
|
|
const buttons = screen.getAllByRole("button", { name: /OK|确\s*定/ });
|
|
fireEvent.click(buttons[buttons.length - 1]!);
|
|
}
|
|
|
|
describe("ProviderFormModal", () => {
|
|
test("编辑供应商表单只提交变更字段", async () => {
|
|
const updateCalls: unknown[] = [];
|
|
|
|
renderWithProviders(
|
|
createElement(ProviderFormModal, {
|
|
editingProvider: ENABLED_PROVIDER,
|
|
onCancel: () => undefined,
|
|
onCreate: () => Promise.resolve(),
|
|
onOpenChange: () => undefined,
|
|
onUpdate: (args: unknown) => {
|
|
updateCalls.push(args);
|
|
return Promise.resolve();
|
|
},
|
|
open: true,
|
|
submitting: false,
|
|
}),
|
|
);
|
|
|
|
await waitFor(() => expect(screen.getByPlaceholderText("请输入供应商名称")).not.toBeNull());
|
|
fireEvent.change(screen.getByPlaceholderText("请输入供应商名称"), { target: { value: "New OpenAI" } });
|
|
clickLatestConfirmButton();
|
|
|
|
await waitFor(() => expect(updateCalls.length).toBe(1));
|
|
expect(updateCalls[0]).toEqual({ data: { name: "New OpenAI" }, id: "pv1" });
|
|
});
|
|
});
|
|
|
|
describe("ModelFormModal", () => {
|
|
test("编辑模型表单只提交变更字段", async () => {
|
|
const updateCalls: unknown[] = [];
|
|
|
|
renderWithProviders(
|
|
createElement(ModelFormModal, {
|
|
editingModel: ENABLED_MODEL,
|
|
onCancel: () => undefined,
|
|
onCreate: () => Promise.resolve(),
|
|
onOpenChange: () => undefined,
|
|
onUpdate: (args: unknown) => {
|
|
updateCalls.push(args);
|
|
return Promise.resolve();
|
|
},
|
|
open: true,
|
|
providers: [ENABLED_PROVIDER, DISABLED_PROVIDER],
|
|
submitting: false,
|
|
}),
|
|
);
|
|
|
|
await waitFor(() => expect(screen.getByPlaceholderText("请输入模型名称")).not.toBeNull());
|
|
fireEvent.change(screen.getByPlaceholderText("请输入模型名称"), { target: { value: "GPT-4o Mini" } });
|
|
clickLatestConfirmButton();
|
|
|
|
await waitFor(() => expect(updateCalls.length).toBe(1));
|
|
expect(updateCalls[0]).toEqual({ data: { name: "GPT-4o Mini" }, id: "m1" });
|
|
});
|
|
|
|
test("模型表单校验失败不会提交", async () => {
|
|
const onCreate = mock(() => Promise.resolve());
|
|
|
|
renderWithProviders(
|
|
createElement(ModelFormModal, {
|
|
editingModel: null,
|
|
onCancel: () => undefined,
|
|
onCreate,
|
|
onOpenChange: () => undefined,
|
|
onUpdate: () => Promise.resolve(),
|
|
open: true,
|
|
providers: [ENABLED_PROVIDER],
|
|
submitting: false,
|
|
}),
|
|
);
|
|
|
|
await waitFor(() => expect(screen.getByPlaceholderText("请输入模型名称")).not.toBeNull());
|
|
clickLatestConfirmButton();
|
|
expect(onCreate).not.toHaveBeenCalled();
|
|
});
|
|
});
|