refactor: 简化模型管理,移除启用/禁用,优化测试和布局

- 移除供应商/模型启用禁用能力,清理DB schema/migration/API/前端
- 供应商测试改为Base URL连通性+/models探测
- 新增POST /api/models/test模型连接测试
- 新增GET /api/providers/options专用供应商选项接口
- 统一工具栏为ModelsToolbar,参考项目管理布局
- 模型弹窗优化:默认能力、响应式3列标签、并排数值
- 前后端正整数校验、供应商下拉loading/error/empty状态
- 表格列宽统一,操作列/名称列固定宽度
This commit is contained in:
2026-05-29 18:03:33 +08:00
parent 9241c782e6
commit 34e915ccf4
39 changed files with 895 additions and 961 deletions

View File

@@ -12,7 +12,6 @@ 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",
@@ -23,7 +22,6 @@ 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",
@@ -34,7 +32,6 @@ 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",
@@ -165,8 +162,9 @@ describe("ModelFormModal", () => {
},
open: true,
providers: [ENABLED_PROVIDER, DISABLED_PROVIDER],
providersError: null,
providersLoading: false,
submitting: false,
testConnection: () => Promise.resolve({ message: "连接成功", ok: true }),
}),
);
@@ -190,8 +188,9 @@ describe("ModelFormModal", () => {
onUpdate: () => Promise.resolve(),
open: true,
providers: [ENABLED_PROVIDER],
providersError: null,
providersLoading: false,
submitting: false,
testConnection: () => Promise.resolve({ message: "连接成功", ok: true }),
}),
);
@@ -200,9 +199,7 @@ describe("ModelFormModal", () => {
expect(onCreate).not.toHaveBeenCalled();
});
test("新建模型时可测试所选供应商连接", async () => {
const testConnection = mock(() => Promise.resolve({ message: "连接成功", ok: true }));
test("新建模型默认选中文本和推理能力", async () => {
renderWithProviders(
createElement(ModelFormModal, {
editingModel: null,
@@ -212,16 +209,111 @@ describe("ModelFormModal", () => {
onUpdate: () => Promise.resolve(),
open: true,
providers: [ENABLED_PROVIDER],
providersError: null,
providersLoading: false,
submitting: false,
testConnection,
}),
);
await waitFor(() => expect(screen.getByText("测试连接")).not.toBeNull());
await waitFor(() => expect(screen.getByLabelText("文本")).not.toBeNull());
const textCheckbox = screen.getByLabelText("文本");
const reasoningCheckbox = screen.getByLabelText("推理");
expect((textCheckbox as { checked?: boolean }).checked).toBe(true);
expect((reasoningCheckbox as { checked?: boolean }).checked).toBe(true);
});
test("新建模型展示供应商 options 列表", async () => {
renderWithProviders(
createElement(ModelFormModal, {
editingModel: null,
onCancel: () => undefined,
onCreate: () => Promise.resolve(),
onOpenChange: () => undefined,
onUpdate: () => Promise.resolve(),
open: true,
providers: [ENABLED_PROVIDER, DISABLED_PROVIDER],
providersError: null,
providersLoading: false,
submitting: false,
}),
);
await waitFor(() => expect(screen.getByPlaceholderText("请输入模型名称")).not.toBeNull());
fireEvent.mouseDown(screen.getByRole("combobox"));
fireEvent.click(await screen.findByText("OpenAI"));
expect(await screen.findByText("OpenAI")).not.toBeNull();
expect(await screen.findByText("DeepSeek")).not.toBeNull();
});
test("供应商下拉展示加载错误提示", async () => {
renderWithProviders(
createElement(ModelFormModal, {
editingModel: null,
onCancel: () => undefined,
onCreate: () => Promise.resolve(),
onOpenChange: () => undefined,
onUpdate: () => Promise.resolve(),
open: true,
providers: [],
providersError: new Error("options failed"),
providersLoading: false,
submitting: false,
}),
);
await waitFor(() => expect(screen.getByPlaceholderText("请输入模型名称")).not.toBeNull());
fireEvent.mouseDown(screen.getByRole("combobox"));
expect(await screen.findByText("供应商加载失败options failed")).not.toBeNull();
});
test("编辑模型时可测试模型连接", async () => {
const testModelConnection = mock(() => Promise.resolve({ message: "模型连接成功", ok: true }));
renderWithProviders(
createElement(ModelFormModal, {
editingModel: ENABLED_MODEL,
onCancel: () => undefined,
onCreate: () => Promise.resolve(),
onOpenChange: () => undefined,
onUpdate: () => Promise.resolve(),
open: true,
providers: [ENABLED_PROVIDER],
providersError: null,
providersLoading: false,
submitting: false,
testModelConnection,
}),
);
await waitFor(() => expect(screen.getByRole("button", { name: "测试连接" })).not.toBeNull());
fireEvent.click(screen.getByRole("button", { name: "测试连接" }));
await waitFor(() => expect(testConnection).toHaveBeenCalledWith("pv1"));
await waitFor(() =>
expect(testModelConnection).toHaveBeenCalledWith({
modelId: "gpt-4o",
providerId: "pv1",
}),
);
});
test("新建模型也显示测试连接按钮", async () => {
renderWithProviders(
createElement(ModelFormModal, {
editingModel: null,
onCancel: () => undefined,
onCreate: () => Promise.resolve(),
onOpenChange: () => undefined,
onUpdate: () => Promise.resolve(),
open: true,
providers: [ENABLED_PROVIDER],
providersError: null,
providersLoading: false,
submitting: false,
testModelConnection: () => Promise.resolve({ message: "ok", ok: true }),
}),
);
await waitFor(() => expect(screen.getByRole("button", { name: "测试连接" })).not.toBeNull());
});
});