import { readFile } from "node:fs/promises"; import { describe, expect, it, vi } from "vitest"; vi.mock("server-only", () => ({})); import { buildPlaintextPoolConfig } from "../lib/server/database"; describe("PostgreSQL readiness contract", () => { it("checks every runtime table and both atomic functions", async () => { const source = await readFile(new URL("../lib/server/database.ts", import.meta.url), "utf8"); for (const table of [ "assets", "generation_jobs", "usage_events", "projects", "image_templates", "platform_organizations", "platform_users", "platform_account_migrations", "platform_runtime_settings", "billing_price_rules", "billing_wallets", "billing_ledger" ]) { expect(source).toContain(`('${table}',`); } expect(source).toContain("claim_generation_jobs(text,integer,integer)"); expect(source).toContain("billing_post_wallet_entry(text,text,text,text,text,bigint,text,text,text,jsonb)"); }); it("forces the legacy server adapter to use plaintext without reading a CA", async () => { const source = await readFile(new URL("../lib/server/database.ts", import.meta.url), "utf8"); expect(source).not.toContain("readFileSync"); expect(source).not.toContain("rejectUnauthorized"); expect(source).not.toContain('|| "verify-full"'); expect(source).toContain('["sslmode", "sslrootcert"].includes(key.toLowerCase())'); expect(source).toContain('parsed.searchParams.set("sslmode", "disable")'); expect(source).toContain("ssl: false"); }); it("normalizes a legacy TLS URL into an executable plaintext pool config", () => { const config = buildPlaintextPoolConfig( "postgresql://app:secret@rds.example:5432/app?connect_timeout=5&sslmode=verify-full&sslrootcert=/missing/ca.pem" ); expect(config.ssl).toBe(false); expect(config.connectionString).toContain("sslmode=disable"); expect(config.connectionString).not.toContain("verify-full"); expect(config.connectionString).not.toContain("sslrootcert"); expect(config.connectionString).toContain("connect_timeout=5"); }); });