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:
@@ -1,6 +1,6 @@
|
||||
import type Database from "bun:sqlite";
|
||||
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { describe, expect, mock, test } from "bun:test";
|
||||
|
||||
import type { Model, RuntimeMode } from "../../../src/shared/api";
|
||||
|
||||
@@ -30,16 +30,6 @@ async function deleteModelViaHandler(req: Request, db: Database): Promise<Respon
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
async function disableModelViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
const { handleDisableModel: h } = await import("../../../src/server/routes/models/disable");
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
async function enableModelViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
const { handleEnableModel: h } = await import("../../../src/server/routes/models/enable");
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
async function getModelViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
const { handleGetModel: h } = await import("../../../src/server/routes/models/get");
|
||||
return h(req, db, MODE);
|
||||
@@ -53,6 +43,13 @@ async function listModelsViaHandler(req: Request, db: Database): Promise<Respons
|
||||
import { createModel } from "../../../src/server/db/models";
|
||||
import { createProvider } from "../../../src/server/db/providers";
|
||||
|
||||
void mock.module("ai", () => ({
|
||||
createProviderRegistry: () => ({
|
||||
languageModel: () => ({}),
|
||||
}),
|
||||
generateText: () => Promise.resolve({ text: "Hi" }),
|
||||
}));
|
||||
|
||||
function seedProvider(db: Database, name?: string): string {
|
||||
const result = createProvider(db, {
|
||||
apiKey: "sk-test",
|
||||
@@ -64,6 +61,11 @@ function seedProvider(db: Database, name?: string): string {
|
||||
return result.provider.id;
|
||||
}
|
||||
|
||||
async function testModelViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
const { handleTestModelConfig: h } = await import("../../../src/server/routes/models/test");
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
async function updateModelViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
const { handleUpdateModel: h } = await import("../../../src/server/routes/models/update");
|
||||
return h(req, db, MODE);
|
||||
@@ -163,34 +165,6 @@ describe("models API routes", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("POST /api/models/:id/enable", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
const model = createTestModel(db, "EnableTest");
|
||||
await disableModelViaHandler(
|
||||
new Request("http://localhost/api/models/" + model.id + "/disable", { method: "POST" }),
|
||||
db,
|
||||
);
|
||||
|
||||
const req = new Request("http://localhost/api/models/" + model.id + "/enable", { method: "POST" });
|
||||
const res = await enableModelViaHandler(req, db);
|
||||
expect(res.status).toBe(200);
|
||||
const body = (await res.json()) as { model: Model };
|
||||
expect(body.model.enabled).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
test("POST /api/models/:id/disable", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
const model = createTestModel(db, "DisableTest");
|
||||
|
||||
const req = new Request("http://localhost/api/models/" + model.id + "/disable", { method: "POST" });
|
||||
const res = await disableModelViaHandler(req, db);
|
||||
expect(res.status).toBe(200);
|
||||
const body = (await res.json()) as { model: Model };
|
||||
expect(body.model.enabled).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
test("DELETE /api/models/:id", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
const model = createTestModel(db, "DeleteTest");
|
||||
@@ -219,4 +193,74 @@ describe("models API routes", () => {
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
test("invalid numeric fields return 400", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
const providerId = seedProvider(db);
|
||||
|
||||
const createReq = new Request("http://localhost/api/models", {
|
||||
body: JSON.stringify({
|
||||
capabilities: ["text"],
|
||||
contextLength: 0,
|
||||
modelId: "test",
|
||||
name: "Test",
|
||||
providerId,
|
||||
}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
});
|
||||
const createRes = await createModelViaHandler(createReq, db);
|
||||
expect(createRes.status).toBe(400);
|
||||
|
||||
const model = createTestModel(db, "NumericTest", providerId);
|
||||
const updateReq = new Request("http://localhost/api/models/" + model.id, {
|
||||
body: JSON.stringify({ maxOutputTokens: 1.5 }),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "PATCH",
|
||||
});
|
||||
const updateRes = await updateModelViaHandler(updateReq, db);
|
||||
expect(updateRes.status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
test("POST /api/models/test 成功测试模型连接", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
const providerId = seedProvider(db);
|
||||
|
||||
const req = new Request("http://localhost/api/models/test", {
|
||||
body: JSON.stringify({ modelId: "gpt-4o", providerId }),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
});
|
||||
const res = await testModelViaHandler(req, db);
|
||||
expect(res.status).toBe(200);
|
||||
const body = (await res.json()) as { modelTestResponse: { message: string; ok: boolean } };
|
||||
expect(body.modelTestResponse.ok).toBe(true);
|
||||
expect(body.modelTestResponse.message).toContain("模型连接成功");
|
||||
});
|
||||
});
|
||||
|
||||
test("POST /api/models/test 缺少 providerId 返回 400", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
const req = new Request("http://localhost/api/models/test", {
|
||||
body: JSON.stringify({ modelId: "gpt-4o" }),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
});
|
||||
const res = await testModelViaHandler(req, db);
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
test("POST /api/models/test 不存在的供应商返回 404", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
const req = new Request("http://localhost/api/models/test", {
|
||||
body: JSON.stringify({ modelId: "gpt-4o", providerId: "nonexistent" }),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
});
|
||||
const res = await testModelViaHandler(req, db);
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,7 +2,7 @@ import type Database from "bun:sqlite";
|
||||
|
||||
import { describe, expect, mock, test } from "bun:test";
|
||||
|
||||
import type { Provider, RuntimeMode } from "../../../src/shared/api";
|
||||
import type { Provider, ProviderOption, RuntimeMode } from "../../../src/shared/api";
|
||||
|
||||
import { createModel } from "../../../src/server/db/models";
|
||||
import { createProvider } from "../../../src/server/db/providers";
|
||||
@@ -10,13 +10,10 @@ import { createMigratedMemoryTestDatabase } from "../../helpers";
|
||||
|
||||
const MODE: RuntimeMode = "test";
|
||||
|
||||
let generateTextImpl: (_opts: unknown) => unknown = () => ({});
|
||||
|
||||
void mock.module("ai", () => ({
|
||||
createProviderRegistry: () => ({
|
||||
languageModel: () => ({}),
|
||||
}),
|
||||
generateText: mock((opts: unknown) => generateTextImpl(opts)),
|
||||
}));
|
||||
|
||||
async function createProviderViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
@@ -24,10 +21,10 @@ async function createProviderViaHandler(req: Request, db: Database): Promise<Res
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
function createTestProvider(db: Database, name = "测试供应商"): Provider {
|
||||
function createTestProvider(db: Database, name = "测试供应商", baseUrl = "https://api.test.com/v1"): Provider {
|
||||
const result = createProvider(db, {
|
||||
apiKey: "sk-test",
|
||||
baseUrl: "https://api.test.com/v1",
|
||||
baseUrl,
|
||||
name,
|
||||
type: "openai",
|
||||
});
|
||||
@@ -40,21 +37,16 @@ async function deleteProviderViaHandler(req: Request, db: Database): Promise<Res
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
async function disableProviderViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
const { handleDisableProvider: h } = await import("../../../src/server/routes/providers/disable");
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
async function enableProviderViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
const { handleEnableProvider: h } = await import("../../../src/server/routes/providers/enable");
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
async function getProviderViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
const { handleGetProvider: h } = await import("../../../src/server/routes/providers/get");
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
async function listProviderOptionsViaHandler(_req: Request, db: Database): Promise<Response> {
|
||||
const { handleListProviderOptions: h } = await import("../../../src/server/routes/providers/options");
|
||||
return h(db, MODE);
|
||||
}
|
||||
|
||||
async function listProvidersViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
const { handleListProviders: h } = await import("../../../src/server/routes/providers/list");
|
||||
return h(req, db, MODE);
|
||||
@@ -65,16 +57,29 @@ async function testProviderConfigViaHandler(req: Request, db: Database): Promise
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
async function testProviderViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
const { handleTestProvider: h } = await import("../../../src/server/routes/providers/test");
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
async function updateProviderViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
const { handleUpdateProvider: h } = await import("../../../src/server/routes/providers/update");
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
async function withProviderServer(
|
||||
modelsResponse: Response,
|
||||
callback: (baseUrl: string) => Promise<void>,
|
||||
): Promise<void> {
|
||||
const server = Bun.serve({
|
||||
fetch(request) {
|
||||
if (request.method === "HEAD") return new Response(null, { status: 200 });
|
||||
return modelsResponse;
|
||||
},
|
||||
port: 0,
|
||||
});
|
||||
try {
|
||||
await callback(`http://127.0.0.1:${server.port}/v1`);
|
||||
} finally {
|
||||
await server.stop(true);
|
||||
}
|
||||
}
|
||||
|
||||
async function withRouteDb(callback: (db: Database) => Promise<void>): Promise<void> {
|
||||
const handle = createMigratedMemoryTestDatabase("route-provider-test");
|
||||
try {
|
||||
@@ -120,6 +125,22 @@ describe("供应商 API 路由", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("GET /api/providers/options 返回最小字段", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
createTestProvider(db, "选项供应商");
|
||||
|
||||
const req = new Request("http://localhost/api/providers/options");
|
||||
const res = await listProviderOptionsViaHandler(req, db);
|
||||
expect(res.status).toBe(200);
|
||||
const body = (await res.json()) as { items: ProviderOption[] };
|
||||
expect(body.items).toHaveLength(1);
|
||||
expect(typeof body.items[0]?.id).toBe("string");
|
||||
expect(body.items[0]).toMatchObject({ name: "选项供应商", type: "openai" });
|
||||
expect(body.items[0]).not.toHaveProperty("apiKey");
|
||||
expect(body.items[0]).not.toHaveProperty("enabled");
|
||||
});
|
||||
});
|
||||
|
||||
test("GET /api/providers/:id 获取详情", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
const provider = createTestProvider(db, "详情路由");
|
||||
@@ -148,34 +169,6 @@ describe("供应商 API 路由", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("POST /api/providers/:id/enable 启用", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
const provider = createTestProvider(db, "启用测试");
|
||||
await disableProviderViaHandler(
|
||||
new Request(`http://localhost/api/providers/${provider.id}/disable`, { method: "POST" }),
|
||||
db,
|
||||
);
|
||||
|
||||
const req = new Request(`http://localhost/api/providers/${provider.id}/enable`, { method: "POST" });
|
||||
const res = await enableProviderViaHandler(req, db);
|
||||
expect(res.status).toBe(200);
|
||||
const body = (await res.json()) as { provider: Provider };
|
||||
expect(body.provider.enabled).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
test("POST /api/providers/:id/disable 禁用", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
const provider = createTestProvider(db, "禁用测试");
|
||||
|
||||
const req = new Request(`http://localhost/api/providers/${provider.id}/disable`, { method: "POST" });
|
||||
const res = await disableProviderViaHandler(req, db);
|
||||
expect(res.status).toBe(200);
|
||||
const body = (await res.json()) as { provider: Provider };
|
||||
expect(body.provider.enabled).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
test("DELETE /api/providers/:id 删除供应商", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
const provider = createTestProvider(db, "删除路由");
|
||||
@@ -205,40 +198,25 @@ describe("供应商 API 路由", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("POST /api/providers/:id/test 返回连通性失败结果", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
generateTextImpl = () => {
|
||||
throw new Error("bad key");
|
||||
};
|
||||
const provider = createTestProvider(db, "测试失败供应商");
|
||||
|
||||
const req = new Request(`http://localhost/api/providers/${provider.id}/test`, { method: "POST" });
|
||||
const res = await testProviderViaHandler(req, db);
|
||||
expect(res.status).toBe(200);
|
||||
const body = (await res.json()) as { providerTestResponse: { message: string; ok: boolean } };
|
||||
expect(body.providerTestResponse.ok).toBe(false);
|
||||
expect(body.providerTestResponse.message).toContain("连接失败");
|
||||
generateTextImpl = () => ({});
|
||||
});
|
||||
});
|
||||
|
||||
test("POST /api/providers/test 使用表单配置测试连通性", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
generateTextImpl = () => ({});
|
||||
const req = new Request("http://localhost/api/providers/test", {
|
||||
body: JSON.stringify({
|
||||
apiKey: "sk-test",
|
||||
baseUrl: "https://api.openai.com/v1",
|
||||
name: "OpenAI",
|
||||
type: "openai",
|
||||
}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
await withProviderServer(Response.json({ data: [{ id: "gpt-4o" }] }), async (baseUrl) => {
|
||||
const req = new Request("http://localhost/api/providers/test", {
|
||||
body: JSON.stringify({
|
||||
apiKey: "sk-test",
|
||||
baseUrl,
|
||||
name: "OpenAI",
|
||||
type: "openai",
|
||||
}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
});
|
||||
const res = await testProviderConfigViaHandler(req, db);
|
||||
expect(res.status).toBe(200);
|
||||
const body = (await res.json()) as { providerTestResponse: { message: string; ok: boolean } };
|
||||
expect(body.providerTestResponse.ok).toBe(true);
|
||||
expect(body.providerTestResponse.message).toContain("/models 返回 1 个模型");
|
||||
});
|
||||
const res = await testProviderConfigViaHandler(req, db);
|
||||
expect(res.status).toBe(200);
|
||||
const body = (await res.json()) as { providerTestResponse: { message: string; ok: boolean } };
|
||||
expect(body.providerTestResponse).toEqual({ message: "连接成功", ok: true });
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user