SPB Git forge

spb/polyllm

Public
15commits 1branches 0releases
2.2 MBsize
maindefault branch
13 days agolast push
TypeScript 97.4% SQL 1% JavaScript 0.9% CSS 0.6%
2.7 KB · 62 lines typescript
Raw Blame History
1import { describe, it, expect, afterAll } from "vitest";2import { execSync } from "node:child_process";34// Loads .env (DATABASE_URL, provider keys) without printing anything.5import "../../scripts/load-env";67const hasDb = (() => {8  try {9    execSync(`psql "${process.env.DATABASE_URL ?? "postgres://localhost:5432/polyllm"}" -Atc "select 1"`, { stdio: "ignore" });10    return true;11  } catch {12    return false;13  }14})();1516describe.skipIf(!hasDb)("integration: database + registry + key vault", () => {17  it("syncs a provider into the registry with a real key and reads it back", async () => {18    const key = process.env.ANTHROPIC_API_KEY;19    if (!key) return; // no key → nothing to sync20    const { syncProvider, listRegistryModels, getModel } = await import("@/lib/ai/registry");21    const res = await syncProvider("anthropic", key, "test");22    expect(res.ok).toBe(true);23    expect(res.found).toBeGreaterThan(3);24    const models = await listRegistryModels({ provider: "anthropic" });25    expect(models.some((m) => m.id.startsWith("claude-"))).toBe(true);26    const m = await getModel(models[0].key);27    expect(m?.capabilities.text).toBe(true);28    expect(m?.pricing?.inputPerMillion).toBeGreaterThan(0);29  });3031  it("stores provider keys encrypted and never returns them to callers", async () => {32    process.env.API_KEY_ENCRYPTION_SECRET ??= "integration-secret-0123456789abcdef0123456789abcdef";33    const { getDb, users, providerConnections } = await import("@/db");34    const { upsertConnection, listConnections, getDecryptedKey, deleteConnection } = await import("@/lib/providers/keys");35    const { eq } = await import("drizzle-orm");36    const db = getDb();37    const id = `it_${Date.now()}`;38    await db.insert(users).values({ id, name: "Integration", email: `${id}@polyllm.test`, emailVerified: true });39    try {40      const fake = "xai-integration-test-key-0000000000000000";41      const res = await upsertConnection(id, "xai", fake, { validate: false });42      expect(res.ok).toBe(true);43      const [row] = await db.select().from(providerConnections).where(eq(providerConnections.userId, id));44      expect(row.encryptedKey.startsWith("v1.")).toBe(true);45      expect(row.encryptedKey).not.toContain("integration-test");46      expect(row.keyHint).toMatch(/••••/);47      const pub = await listConnections(id);48      expect(JSON.stringify(pub)).not.toContain("integration-test");49      expect(await getDecryptedKey(id, "xai")).toBe(fake);50      await deleteConnection(id, "xai");51      expect(await getDecryptedKey(id, "xai")).toBeNull();52    } finally {53      await db.delete(users).where(eq(users.id, id));54    }55  });5657  afterAll(async () => {58    const { closeDb } = await import("@/db");59    await closeDb();60  });61});62