fix: 模型管理审查修复与归档
- 修复 registry 测试 ai mock 缺失 createProviderRegistry 导出 - 新增 POST /api/providers/test 支持未保存供应商配置连通性测试 - 供应商表单新增测试连接按钮,新建默认 openai-compatible - 连通性测试按 ok 展示成功/失败,不再统一 success 样式 - 模型表单新建时也可测试供应商连接 - 模型页使用独立 provider 列表避免分页/搜索影响 - 移除模型管理组件内联 style - 新增 ProviderTestResultResponse 共享响应类型 - 新增 bun run format:check 脚本 - 补充关键测试覆盖(删除关联、连通性、默认类型、表单测试) - 更新 docs/user/usage.md、docs/development/*、design.md、tasks.md - 归档 change 至 openspec/changes/archive/2026-05-29-add-model-management
This commit is contained in:
@@ -1,13 +1,24 @@
|
||||
import type Database from "bun:sqlite";
|
||||
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { describe, expect, mock, test } from "bun:test";
|
||||
|
||||
import type { Provider, RuntimeMode } from "../../../src/shared/api";
|
||||
|
||||
import { createModel } from "../../../src/server/db/models";
|
||||
import { createProvider } from "../../../src/server/db/providers";
|
||||
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> {
|
||||
const { handleCreateProvider: h } = await import("../../../src/server/routes/providers/create");
|
||||
return h(req, db, MODE);
|
||||
@@ -49,7 +60,15 @@ async function listProvidersViaHandler(req: Request, db: Database): Promise<Resp
|
||||
return h(req, db, MODE);
|
||||
}
|
||||
|
||||
import { createProvider } from "../../../src/server/db/providers";
|
||||
async function testProviderConfigViaHandler(req: Request, db: Database): Promise<Response> {
|
||||
const { handleTestProviderConfig: h } = await import("../../../src/server/routes/providers/test");
|
||||
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");
|
||||
@@ -167,6 +186,62 @@ describe("供应商 API 路由", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("DELETE /api/providers/:id 存在关联模型时返回 409", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
const provider = createTestProvider(db, "有关联模型");
|
||||
const modelResult = createModel(db, {
|
||||
capabilities: ["text"],
|
||||
modelId: "gpt-4o",
|
||||
name: "GPT-4o",
|
||||
providerId: provider.id,
|
||||
});
|
||||
if ("error" in modelResult) throw new Error(modelResult.error);
|
||||
|
||||
const req = new Request(`http://localhost/api/providers/${provider.id}`, { method: "DELETE" });
|
||||
const res = await deleteProviderViaHandler(req, db);
|
||||
expect(res.status).toBe(409);
|
||||
const body = (await res.json()) as { error: string };
|
||||
expect(body.error).toContain("存在模型");
|
||||
});
|
||||
});
|
||||
|
||||
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",
|
||||
});
|
||||
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 });
|
||||
});
|
||||
});
|
||||
|
||||
test("创建同名供应商返回 409", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
const req1 = new Request("http://localhost/api/providers", {
|
||||
|
||||
Reference in New Issue
Block a user