- 新增 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
107 lines
3.5 KiB
TypeScript
107 lines
3.5 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
|
|
import {
|
|
createProvider,
|
|
deleteProvider,
|
|
disableProvider,
|
|
enableProvider,
|
|
fetchProvider,
|
|
fetchProviderList,
|
|
testProviderConnection,
|
|
updateProvider,
|
|
} from "../../../src/web/hooks/use-providers";
|
|
import { installFetchMock, jsonResponse } from "../test-utils";
|
|
|
|
const 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" as const,
|
|
updatedAt: "2024-01-01T00:00:00.000Z",
|
|
};
|
|
|
|
async function expectRejectsWithMessage(action: () => Promise<unknown>, message: string) {
|
|
try {
|
|
await action();
|
|
throw new Error("expected rejection");
|
|
} catch (error) {
|
|
expect(error).toBeInstanceOf(Error);
|
|
expect((error as Error).message).toBe(message);
|
|
}
|
|
}
|
|
|
|
function jsonBody(body: BodyInit | null | undefined): unknown {
|
|
return JSON.parse(typeof body === "string" ? body : "{}");
|
|
}
|
|
|
|
describe("use-providers request helpers", () => {
|
|
test("fetchProviderList builds correct query params", async () => {
|
|
const calls = installFetchMock(() => jsonResponse({ items: [PROVIDER], page: 1, pageSize: 20, total: 1 }));
|
|
|
|
const result = await fetchProviderList({ keyword: "OpenAI", page: 1, pageSize: 20 });
|
|
|
|
expect(result.items).toHaveLength(1);
|
|
expect(calls[0]?.method).toBe("GET");
|
|
expect(calls[0]?.url).toBe("/api/providers?page=1&pageSize=20&keyword=OpenAI");
|
|
});
|
|
|
|
test("CRUD and enable/disable use correct method, URL and body", async () => {
|
|
const calls = installFetchMock((call) => {
|
|
if (call.method === "DELETE") return new Response(null, { status: 204 });
|
|
return jsonResponse(
|
|
{ provider: PROVIDER },
|
|
{ status: call.method === "POST" && call.url === "/api/providers" ? 201 : 200 },
|
|
);
|
|
});
|
|
|
|
await createProvider({ apiKey: "sk-test", baseUrl: "https://api.openai.com/v1", name: "OpenAI", type: "openai" });
|
|
await updateProvider("pv1", { name: "New OpenAI" });
|
|
await enableProvider("pv1");
|
|
await disableProvider("pv1");
|
|
await deleteProvider("pv1");
|
|
await fetchProvider("pv1");
|
|
|
|
expect(calls.map((c) => c.method + " " + c.url)).toEqual([
|
|
"POST /api/providers",
|
|
"PATCH /api/providers/pv1",
|
|
"POST /api/providers/pv1/enable",
|
|
"POST /api/providers/pv1/disable",
|
|
"DELETE /api/providers/pv1",
|
|
"GET /api/providers/pv1",
|
|
]);
|
|
expect(jsonBody(calls[0]?.body)).toEqual({
|
|
apiKey: "sk-test",
|
|
baseUrl: "https://api.openai.com/v1",
|
|
name: "OpenAI",
|
|
type: "openai",
|
|
});
|
|
expect(jsonBody(calls[1]?.body)).toEqual({ name: "New OpenAI" });
|
|
});
|
|
|
|
test("testProviderConnection uses correct URL and parses response", async () => {
|
|
installFetchMock(() => jsonResponse({ providerTestResponse: { message: "ok", ok: true } }));
|
|
|
|
const result = await testProviderConnection("pv1");
|
|
|
|
expect(result).toEqual({ message: "ok", ok: true });
|
|
});
|
|
|
|
test("error response uses backend error field", async () => {
|
|
installFetchMock(() => jsonResponse({ error: "dup" }, { status: 409 }));
|
|
|
|
await expectRejectsWithMessage(
|
|
() => createProvider({ apiKey: "sk", baseUrl: "https://x.com", name: "dup", type: "openai-compatible" }),
|
|
"dup",
|
|
);
|
|
});
|
|
|
|
test("non-JSON error falls back to HTTP status", async () => {
|
|
installFetchMock(() => new Response("broken", { status: 500 }));
|
|
|
|
await expectRejectsWithMessage(() => fetchProvider("missing"), "HTTP 500");
|
|
});
|
|
});
|