- 修复 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
90 lines
3.1 KiB
TypeScript
90 lines
3.1 KiB
TypeScript
import { describe, expect, mock, test } from "bun:test";
|
|
|
|
import { createMigratedTestDatabase } from "../../helpers";
|
|
|
|
let generateTextImpl: (_opts: unknown) => unknown = () => ({});
|
|
|
|
void mock.module("ai", () => ({
|
|
createProviderRegistry: (providers: Record<string, { languageModel: (modelId: string) => unknown }>) => ({
|
|
languageModel: (id: string) => {
|
|
const [providerId, modelId] = id.split(":");
|
|
const provider = providers[providerId ?? ""];
|
|
if (!provider || !modelId) throw new Error(`No such provider: ${id}`);
|
|
return provider.languageModel(modelId);
|
|
},
|
|
}),
|
|
generateText: mock((opts: unknown) => generateTextImpl(opts)),
|
|
}));
|
|
|
|
describe("AI registry", () => {
|
|
test("testProviderConnection rejects invalid config", async () => {
|
|
generateTextImpl = () => {
|
|
throw new Error("Connection failed");
|
|
};
|
|
|
|
const { testProviderConnection } = await import("../../../src/server/ai/registry");
|
|
|
|
const result = await testProviderConnection({
|
|
apiKey: "bad-key",
|
|
baseUrl: "https://0.0.0.0:1",
|
|
name: "Bad",
|
|
type: "openai-compatible",
|
|
});
|
|
|
|
expect(result.ok).toBe(false);
|
|
expect(result.message).toContain("连接失败");
|
|
expect(typeof result.message).toBe("string");
|
|
});
|
|
|
|
test("testProviderConnection return shape is correct", async () => {
|
|
generateTextImpl = () => ({});
|
|
|
|
const { testProviderConnection } = await import("../../../src/server/ai/registry");
|
|
|
|
const result = await testProviderConnection({
|
|
apiKey: "sk-test",
|
|
baseUrl: "https://api.openai.com/v1",
|
|
name: "Test",
|
|
type: "openai",
|
|
});
|
|
|
|
expect(result.ok).toBe(true);
|
|
expect(result.message).toBe("连接成功");
|
|
});
|
|
|
|
test("buildProviderRegistry 从 DB 构建包含启用供应商的注册表", async () => {
|
|
const handle = createMigratedTestDatabase("registry-build-test");
|
|
const now = new Date().toISOString();
|
|
|
|
handle.db
|
|
.prepare(
|
|
"INSERT INTO providers (id, name, type, base_url, api_key, enabled, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
|
)
|
|
.run("pv1", "OpenAI", "openai", "https://api.openai.com/v1", "sk-test", 1, now, now);
|
|
handle.db
|
|
.prepare(
|
|
"INSERT INTO providers (id, name, type, base_url, api_key, enabled, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
|
)
|
|
.run("pv2", "Disabled", "anthropic", "https://api.anthropic.com", "sk-off", 0, now, now);
|
|
|
|
const { buildProviderRegistry } = await import("../../../src/server/ai/registry");
|
|
const registry = buildProviderRegistry(handle.db);
|
|
|
|
expect(() => registry.languageModel("pv1:gpt-4o")).not.toThrow();
|
|
expect(() => registry.languageModel("pv2:claude-3")).toThrow();
|
|
|
|
handle.cleanup();
|
|
});
|
|
|
|
test("buildProviderRegistry 无启用供应商时返回空注册表", async () => {
|
|
const handle = createMigratedTestDatabase("registry-empty-test");
|
|
|
|
const { buildProviderRegistry } = await import("../../../src/server/ai/registry");
|
|
const registry = buildProviderRegistry(handle.db);
|
|
|
|
expect(typeof registry.languageModel).toBe("function");
|
|
|
|
handle.cleanup();
|
|
});
|
|
});
|