- 新增 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
196 lines
7.1 KiB
TypeScript
196 lines
7.1 KiB
TypeScript
import type Database from "bun:sqlite";
|
|
|
|
import { describe, expect, test } from "bun:test";
|
|
|
|
import {
|
|
createProvider,
|
|
deleteProvider,
|
|
disableProvider,
|
|
enableProvider,
|
|
getProvider,
|
|
listProviders,
|
|
updateProvider,
|
|
} from "../../../src/server/db/providers";
|
|
import { createMigratedTestDatabase } from "../../helpers";
|
|
|
|
function withDb(callback: (db: Database) => void): void {
|
|
const handle = createMigratedTestDatabase("providers-test");
|
|
try {
|
|
callback(handle.db);
|
|
handle.close();
|
|
} finally {
|
|
handle.cleanup();
|
|
}
|
|
}
|
|
|
|
describe("供应商数据访问层", () => {
|
|
test("创建供应商", () => {
|
|
withDb((db) => {
|
|
const result = createProvider(db, {
|
|
apiKey: "sk-test",
|
|
baseUrl: "https://api.openai.com/v1",
|
|
name: "OpenAI",
|
|
type: "openai",
|
|
});
|
|
expect("error" in result).toBe(false);
|
|
const provider = (
|
|
result as { provider: { apiKey: string; baseUrl: string; enabled: boolean; name: string; type: string } }
|
|
).provider;
|
|
expect(provider.name).toBe("OpenAI");
|
|
expect(provider.type).toBe("openai");
|
|
expect(provider.baseUrl).toBe("https://api.openai.com/v1");
|
|
expect(provider.apiKey).toBe("sk-test");
|
|
expect(provider.enabled).toBe(true);
|
|
});
|
|
});
|
|
|
|
test("供应商名称唯一", () => {
|
|
withDb((db) => {
|
|
createProvider(db, { apiKey: "sk-1", baseUrl: "https://a.com", name: "唯一", type: "openai" });
|
|
const result = createProvider(db, { apiKey: "sk-2", baseUrl: "https://b.com", name: "唯一", type: "openai" });
|
|
expect("error" in result).toBe(true);
|
|
expect((result as unknown as { error: string }).error).toContain("已存在");
|
|
});
|
|
});
|
|
|
|
test("名称为空时创建失败", () => {
|
|
withDb((db) => {
|
|
const result = createProvider(db, { apiKey: "sk", baseUrl: "https://a.com", name: " ", type: "openai" });
|
|
expect("error" in result).toBe(true);
|
|
expect((result as unknown as { error: string }).error).toContain("不能为空");
|
|
});
|
|
});
|
|
|
|
test("列表查询(分页和关键字)", () => {
|
|
withDb((db) => {
|
|
createProvider(db, { apiKey: "sk-1", baseUrl: "https://a.com", name: "Alpha", type: "openai" });
|
|
createProvider(db, { apiKey: "sk-2", baseUrl: "https://b.com", name: "Beta", type: "anthropic" });
|
|
createProvider(db, { apiKey: "sk-3", baseUrl: "https://c.com", name: "Gamma", type: "openai-compatible" });
|
|
|
|
const result1 = listProviders(db, { page: 1, pageSize: 20 });
|
|
expect(result1.total).toBe(3);
|
|
|
|
const result2 = listProviders(db, { keyword: "Alpha", page: 1, pageSize: 20 });
|
|
expect(result2.total).toBe(1);
|
|
expect(result2.items[0]!.name).toBe("Alpha");
|
|
|
|
const result3 = listProviders(db, { page: 1, pageSize: 2 });
|
|
expect(result3.items.length).toBe(2);
|
|
});
|
|
});
|
|
|
|
test("获取供应商详情", () => {
|
|
withDb((db) => {
|
|
const created = createProvider(db, { apiKey: "sk", baseUrl: "https://a.com", name: "详情", type: "openai" });
|
|
const id = (created as { provider: { id: string } }).provider.id;
|
|
|
|
const result = getProvider(db, id);
|
|
expect("error" in result).toBe(false);
|
|
expect((result as { provider: { name: string } }).provider.name).toBe("详情");
|
|
});
|
|
});
|
|
|
|
test("获取不存在的供应商返回 404", () => {
|
|
withDb((db) => {
|
|
const result = getProvider(db, "nonexistent");
|
|
expect("error" in result).toBe(true);
|
|
expect((result as unknown as { status: number }).status).toBe(404);
|
|
});
|
|
});
|
|
|
|
test("更新供应商", () => {
|
|
withDb((db) => {
|
|
const created = createProvider(db, { apiKey: "sk", baseUrl: "https://a.com", name: "原名", type: "openai" });
|
|
const id = (created as { provider: { id: string } }).provider.id;
|
|
|
|
const result = updateProvider(db, id, { name: "新名" });
|
|
expect("error" in result).toBe(false);
|
|
expect((result as { provider: { name: string } }).provider.name).toBe("新名");
|
|
});
|
|
});
|
|
|
|
test("更新供应商名称重复失败", () => {
|
|
withDb((db) => {
|
|
createProvider(db, { apiKey: "sk-1", baseUrl: "https://a.com", name: "已存在", type: "openai" });
|
|
const created = createProvider(db, { apiKey: "sk-2", baseUrl: "https://b.com", name: "原名", type: "openai" });
|
|
const id = (created as { provider: { id: string } }).provider.id;
|
|
|
|
const result = updateProvider(db, id, { name: "已存在" });
|
|
expect("error" in result).toBe(true);
|
|
expect((result as unknown as { error: string }).error).toContain("已存在");
|
|
});
|
|
});
|
|
|
|
test("启用/禁用供应商", () => {
|
|
withDb((db) => {
|
|
const created = createProvider(db, { apiKey: "sk", baseUrl: "https://a.com", name: "测试", type: "openai" });
|
|
const id = (created as { provider: { id: string } }).provider.id;
|
|
|
|
const disabled = disableProvider(db, id);
|
|
expect("error" in disabled).toBe(false);
|
|
expect((disabled as { provider: { enabled: boolean } }).provider.enabled).toBe(false);
|
|
|
|
const enabled = enableProvider(db, id);
|
|
expect("error" in enabled).toBe(false);
|
|
expect((enabled as { provider: { enabled: boolean } }).provider.enabled).toBe(true);
|
|
});
|
|
});
|
|
|
|
test("重复禁用失败", () => {
|
|
withDb((db) => {
|
|
const created = createProvider(db, { apiKey: "sk", baseUrl: "https://a.com", name: "测试", type: "openai" });
|
|
const id = (created as { provider: { id: string } }).provider.id;
|
|
disableProvider(db, id);
|
|
|
|
const result = disableProvider(db, id);
|
|
expect("error" in result).toBe(true);
|
|
expect((result as unknown as { status: number }).status).toBe(409);
|
|
});
|
|
});
|
|
|
|
test("重复启用失败", () => {
|
|
withDb((db) => {
|
|
const created = createProvider(db, { apiKey: "sk", baseUrl: "https://a.com", name: "测试", type: "openai" });
|
|
const id = (created as { provider: { id: string } }).provider.id;
|
|
|
|
const result = enableProvider(db, id);
|
|
expect("error" in result).toBe(true);
|
|
expect((result as unknown as { status: number }).status).toBe(409);
|
|
});
|
|
});
|
|
|
|
test("删除供应商", () => {
|
|
withDb((db) => {
|
|
const created = createProvider(db, { apiKey: "sk", baseUrl: "https://a.com", name: "删除测试", type: "openai" });
|
|
const id = (created as { provider: { id: string } }).provider.id;
|
|
|
|
const result = deleteProvider(db, id);
|
|
expect("error" in result).toBe(false);
|
|
|
|
const after = getProvider(db, id);
|
|
expect("error" in after).toBe(true);
|
|
});
|
|
});
|
|
|
|
test("删除不存在的供应商返回 404", () => {
|
|
withDb((db) => {
|
|
const result = deleteProvider(db, "nonexistent");
|
|
expect("error" in result).toBe(true);
|
|
expect((result as unknown as { status: number }).status).toBe(404);
|
|
});
|
|
});
|
|
|
|
test("默认类型为 openai-compatible", () => {
|
|
withDb((db) => {
|
|
createProvider(db, { apiKey: "sk", baseUrl: "https://a.com", name: "默认类型", type: "openai-compatible" });
|
|
const result = createProvider(db, {
|
|
apiKey: "sk2",
|
|
baseUrl: "https://b.com",
|
|
name: "显式默认",
|
|
type: "openai-compatible",
|
|
});
|
|
expect((result as { provider: { type: string } }).provider.type).toBe("openai-compatible");
|
|
});
|
|
});
|
|
});
|