- 移除供应商/模型启用禁用能力,清理DB schema/migration/API/前端 - 供应商测试改为Base URL连通性+/models探测 - 新增POST /api/models/test模型连接测试 - 新增GET /api/providers/options专用供应商选项接口 - 统一工具栏为ModelsToolbar,参考项目管理布局 - 模型弹窗优化:默认能力、响应式3列标签、并排数值 - 前后端正整数校验、供应商下拉loading/error/empty状态 - 表格列宽统一,操作列/名称列固定宽度
178 lines
6.5 KiB
TypeScript
178 lines
6.5 KiB
TypeScript
import type Database from "bun:sqlite";
|
|
|
|
import { describe, expect, test } from "bun:test";
|
|
|
|
import {
|
|
createProvider,
|
|
deleteProvider,
|
|
getProvider,
|
|
listProviderOptions,
|
|
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("迁移后的供应商和模型表不包含 enabled 字段", () => {
|
|
withDb((db) => {
|
|
const providerColumns = db.query("PRAGMA table_info(providers)").all() as Array<{ name: string }>;
|
|
const modelColumns = db.query("PRAGMA table_info(models)").all() as Array<{ name: string }>;
|
|
|
|
expect(providerColumns.map((column) => column.name)).not.toContain("enabled");
|
|
expect(modelColumns.map((column) => column.name)).not.toContain("enabled");
|
|
});
|
|
});
|
|
|
|
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; 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");
|
|
});
|
|
});
|
|
|
|
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 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");
|
|
});
|
|
});
|
|
|
|
test("供应商 options 返回最小字段", () => {
|
|
withDb((db) => {
|
|
createProvider(db, { apiKey: "sk", baseUrl: "https://a.com", name: "选项", type: "openai" });
|
|
|
|
const options = listProviderOptions(db);
|
|
expect(options.length).toBe(1);
|
|
expect(typeof options[0]?.id).toBe("string");
|
|
expect(options[0]).toMatchObject({ name: "选项", type: "openai" });
|
|
expect(options[0]).not.toHaveProperty("apiKey");
|
|
expect(options[0]).not.toHaveProperty("enabled");
|
|
});
|
|
});
|
|
});
|