🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
import { describe, expect, it, vi } from "vitest";
import { GatewayModelCatalogService } from "@/src/features/ai-gateway/server/provider/models/gatewayModelCatalogService";
import { GatewayProviderService } from "@/src/features/ai-gateway/server/provider/connection/gatewayProviderService";
import {
getGatewayProviderDefinition,
providerSupportsApiFormat,
} from "@/src/features/ai-gateway/server/provider/registry";
import type { PrismaClient } from "@langfuse/shared/src/db";
import { encrypt } from "@langfuse/shared/encryption";
import type { Redis } from "ioredis";
import type { OrgAuthedContext } from "@/src/server/api/trpc";
const session = {
user: { id: "user-1" },
orgId: "org-1",
orgRole: "OWNER",
} as OrgAuthedContext["session"];
describe("AI gateway provider registry", () => {
it("exposes only controlled provider URLs and explicit capabilities", () => {
expect(getGatewayProviderDefinition("OPENAI")).toMatchObject({
baseUrl: "https://api.openai.com/v1",
authType: "bearer",
});
expect(getGatewayProviderDefinition("ANTHROPIC")).toMatchObject({
baseUrl: "https://api.anthropic.com/v1",
authType: "x-api-key",
validationModel: "claude-haiku-4-5-20251001",
});
expect(providerSupportsApiFormat("OPENAI", "openai.responses")).toBe(true);
expect(providerSupportsApiFormat("ANTHROPIC", "anthropic.messages")).toBe(
true,
);
});
it("validates credentials before persisting a connection", async () => {
const validator = vi
.fn()
.mockRejectedValue(new Error("invalid provider credential"));
const prisma = {
gatewayAiConnection: { findMany: vi.fn().mockResolvedValue([]) },
} as unknown as PrismaClient;
const service = new GatewayProviderService(prisma, validator);
await expect(
service.create({
organizationId: "org-1",
name: "Primary",
provider: "OPENAI",
credential: "invalid",
session,
}),
).rejects.toThrow("invalid provider credential");
expect(validator).toHaveBeenCalledWith({
provider: "OPENAI",
credential: "invalid",
});
});
it("reads model lists from an organization and connection scoped cache", async () => {
const fetcher = vi.fn<typeof fetch>();
const redis = {
get: vi.fn().mockResolvedValue(
JSON.stringify([
{
id: "gpt-5-mini",
provider: "OPENAI",
canonicalSlug: "gpt-5-mini",
displayName: "gpt-5-mini",
createdAt: "2026-01-01T00:00:00.000Z",
},
]),
),
ttl: vi.fn().mockResolvedValue(300),
setex: vi.fn(),
} as unknown as Redis;
const prisma = {
gatewayAiConnection: {
findFirst: vi.fn().mockResolvedValue({
id: "connection-1",
organizationId: "org-1",
provider: "OPENAI",
encryptedCredential: "unused",
allowedModels: null,
}),
},
} as unknown as PrismaClient;
const service = new GatewayModelCatalogService(prisma, fetcher, redis);
await expect(
service.refreshModels({
organizationId: "org-1",
connectionId: "connection-1",
}),
).resolves.toEqual({
connectionId: "connection-1",
success: true,
models: ["gpt-5-mini"],
});
expect(redis.get).toHaveBeenCalledWith(
"ai-gateway:models:org-1:connection-1",
);
expect(fetcher).not.toHaveBeenCalled();
expect(prisma.gatewayAiConnection.findFirst).not.toHaveBeenCalled();
});
it("does not cache a background refresh that finished after its connection changed", async () => {
const fetcher = vi
.fn<typeof fetch>()
.mockResolvedValue(
Response.json({ data: [{ id: "old-credential-model", created: 10 }] }),
);
const redis = {
get: vi.fn().mockResolvedValue(
JSON.stringify([
{
id: "gpt-5-mini",
provider: "OPENAI",
canonicalSlug: "gpt-5-mini",
displayName: "gpt-5-mini",
createdAt: "2026-01-01T00:00:00.000Z",
},
]),
),
ttl: vi.fn().mockResolvedValue(10),
set: vi.fn().mockResolvedValue("OK"),
setex: vi.fn().mockResolvedValue("OK"),
} as unknown as Redis;
const findFirst = vi
.fn()
.mockResolvedValueOnce({
id: "connection-1",
organizationId: "org-1",
provider: "OPENAI",
encryptedCredential: encrypt("sk-old"),
status: "ENABLED",
allowedModels: null,
updatedAt: new Date(0),
})
.mockResolvedValueOnce(null);
const prisma = {
gatewayAiConnection: { findFirst },
} as unknown as PrismaClient;
const service = new GatewayModelCatalogService(prisma, fetcher, redis);
await service.refreshModels({
organizationId: "org-1",
connectionId: "connection-1",
});
await vi.waitFor(() => expect(findFirst).toHaveBeenCalledTimes(2));
expect(findFirst).toHaveBeenLastCalledWith({
where: {
organizationId: "org-1",
id: "connection-1",
updatedAt: new Date(0),
},
select: { id: true },
});
await new Promise((resolve) => setTimeout(resolve, 0));
expect(redis.setex).not.toHaveBeenCalled();
});
it("serves a nearly expired model list while refreshing it once in the background", async () => {
const fetcher = vi
.fn<typeof fetch>()
.mockResolvedValue(
Response.json({ data: [{ id: "gpt-5", created: 10 }] }),
);
const redis = {
get: vi.fn().mockResolvedValue(
JSON.stringify([
{
id: "gpt-5-mini",
provider: "OPENAI",
canonicalSlug: "gpt-5-mini",
displayName: "gpt-5-mini",
createdAt: "2026-01-01T00:00:00.000Z",
},
]),
),
ttl: vi.fn().mockResolvedValue(10),
set: vi.fn().mockResolvedValueOnce("OK").mockResolvedValue(null),
setex: vi.fn().mockResolvedValue("OK"),
} as unknown as Redis;
const prisma = {
gatewayAiConnection: {
findFirst: vi.fn().mockResolvedValue({
id: "connection-1",
organizationId: "org-1",
provider: "OPENAI",
encryptedCredential: encrypt("sk-test"),
status: "ENABLED",
allowedModels: null,
}),
},
} as unknown as PrismaClient;
const service = new GatewayModelCatalogService(prisma, fetcher, redis);
const request = {
organizationId: "org-1",
connectionId: "connection-1",
};
await expect(
Promise.all([
service.refreshModels(request),
service.refreshModels(request),
]),
).resolves.toEqual([
{ connectionId: "connection-1", success: true, models: ["gpt-5-mini"] },
{ connectionId: "connection-1", success: true, models: ["gpt-5-mini"] },
]);
await vi.waitFor(() =>
expect(redis.setex).toHaveBeenCalledWith(
"ai-gateway:models:org-1:connection-1",
300,
expect.stringContaining('"id":"gpt-5"'),
),
);
expect(fetcher).toHaveBeenCalledTimes(1);
});
it("collects every Anthropic model page as canonical metadata", async () => {
const fetcher = vi
.fn<typeof fetch>()
.mockResolvedValueOnce(
Response.json({
data: [
{
id: "claude-a",
display_name: "Claude A",
created_at: "2026-01-01T00:00:00.000Z",
},
],
has_more: true,
last_id: "claude-a",
}),
)
.mockResolvedValueOnce(
Response.json({
data: [
{
id: "claude-b",
display_name: "Claude B",
created_at: "2026-02-01T00:00:00.000Z",
},
],
has_more: false,
}),
);
const prisma = {
gatewayAiConnection: {
findFirst: vi.fn().mockResolvedValue({
id: "connection-1",
organizationId: "org-1",
provider: "ANTHROPIC",
encryptedCredential: encrypt("sk-test"),
status: "ENABLED",
allowedModels: null,
}),
},
} as unknown as PrismaClient;
const service = new GatewayModelCatalogService(prisma, fetcher, null);
await expect(
service.getModelCatalog({
organizationId: "org-1",
connectionId: "connection-1",
}),
).resolves.toMatchObject({
success: true,
models: [
{
id: "claude-a",
provider: "ANTHROPIC",
canonicalSlug: "claude-a",
displayName: "Claude A",
createdAt: "2026-01-01T00:00:00.000Z",
},
{
id: "claude-b",
provider: "ANTHROPIC",
canonicalSlug: "claude-b",
displayName: "Claude B",
createdAt: "2026-02-01T00:00:00.000Z",
},
],
});
expect(String(fetcher.mock.calls[1]?.[0])).toContain("after_id=claude-a");
});
it("treats malformed provider model responses as failures", async () => {
const fetcher = vi
.fn<typeof fetch>()
.mockResolvedValue(Response.json({ data: [{}] }));
const redis = {
get: vi.fn().mockResolvedValue(null),
setex: vi.fn(),
} as unknown as Redis;
const prisma = {
gatewayAiConnection: {
findFirst: vi.fn().mockResolvedValue({
id: "connection-1",
organizationId: "org-1",
provider: "OPENAI",
encryptedCredential: encrypt("sk-test"),
status: "ENABLED",
allowedModels: null,
}),
},
} as unknown as PrismaClient;
const service = new GatewayModelCatalogService(prisma, fetcher, redis);
await expect(
service.refreshModels({
organizationId: "org-1",
connectionId: "connection-1",
}),
).resolves.toEqual({
connectionId: "connection-1",
success: false,
error: "provider_error",
});
expect(redis.setex).not.toHaveBeenCalled();
});
it("bypasses and refreshes the cache during an explicit sync", async () => {
const fetcher = vi.fn<typeof fetch>().mockResolvedValue(
new Response(JSON.stringify({ data: [{ id: "gpt-5", created: 10 }] }), {
status: 200,
}),
);
const redis = {
get: vi
.fn()
.mockResolvedValue(
JSON.stringify([{ id: "stale-model", provider: "OPENAI" }]),
),
setex: vi.fn().mockResolvedValue("OK"),
del: vi.fn().mockResolvedValue(1),
} as unknown as Redis;
const prisma = {
gatewayAiConnection: {
findFirst: vi.fn().mockResolvedValue({
id: "connection-1",
organizationId: "org-1",
provider: "OPENAI",
encryptedCredential: encrypt("sk-test"),
status: "ENABLED",
allowedModels: null,
}),
},
} as unknown as PrismaClient;
const service = new GatewayModelCatalogService(prisma, fetcher, redis);
await expect(
service.refreshModels({
organizationId: "org-1",
connectionId: "connection-1",
forceRefresh: true,
}),
).resolves.toEqual({
connectionId: "connection-1",
success: true,
models: ["gpt-5", "codex-auto-review"],
});
expect(redis.get).not.toHaveBeenCalled();
expect(redis.del).toHaveBeenCalledWith(
"ai-gateway:models:org-1:connection-1",
);
expect(redis.setex).toHaveBeenCalledWith(
"ai-gateway:models:org-1:connection-1",
300,
JSON.stringify([
{
id: "gpt-5",
provider: "OPENAI",
canonicalSlug: "gpt-5",
displayName: "gpt-5",
createdAt: "1970-01-01T00:00:10.000Z",
},
{
id: "codex-auto-review",
provider: "OPENAI",
canonicalSlug: "codex-auto-review",
displayName: "codex-auto-review",
createdAt: "1970-01-01T00:00:00.000Z",
},
]),
);
});
it("does not duplicate a custom model the provider already lists", async () => {
const fetcher = vi.fn<typeof fetch>().mockResolvedValue(
Response.json({
data: [
{ id: "codex-auto-review", created: 10 },
{ id: "gpt-5", created: 10 },
],
}),
);
const prisma = {
gatewayAiConnection: {
findFirst: vi.fn().mockResolvedValue({
id: "connection-1",
organizationId: "org-1",
provider: "OPENAI",
encryptedCredential: encrypt("sk-test"),
status: "ENABLED",
allowedModels: null,
}),
},
} as unknown as PrismaClient;
const service = new GatewayModelCatalogService(prisma, fetcher, null);
await expect(
service.refreshModels({
organizationId: "org-1",
connectionId: "connection-1",
}),
).resolves.toMatchObject({ models: ["codex-auto-review", "gpt-5"] });
});
});