diff --git a/cli/src/__tests__/database-check.test.ts b/cli/src/__tests__/database-check.test.ts index 51c42cf7717..ae09869c7b3 100644 --- a/cli/src/__tests__/database-check.test.ts +++ b/cli/src/__tests__/database-check.test.ts @@ -1,10 +1,19 @@ import fs from "node:fs"; import os from "node:os"; import path from "node:path"; -import { afterEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { databaseCheck } from "../checks/database-check.js"; import type { PaperclipConfig } from "../config/schema.js"; +const database = vi.hoisted(() => ({ createDb: vi.fn(), execute: vi.fn() })); +vi.mock("@paperclipai/db", () => ({ createDb: database.createDb })); + +beforeEach(() => { + vi.stubEnv("DATABASE_URL", undefined); + database.createDb.mockReset().mockReturnValue({ execute: database.execute }); + database.execute.mockReset().mockResolvedValue([]); +}); + const created: string[] = []; const ORIGINAL_IN_WORKTREE = process.env.PAPERCLIP_IN_WORKTREE; @@ -26,6 +35,7 @@ function embeddedConfig(dataDir: string): PaperclipConfig { afterEach(() => { vi.restoreAllMocks(); + vi.unstubAllEnvs(); if (ORIGINAL_IN_WORKTREE === undefined) delete process.env.PAPERCLIP_IN_WORKTREE; else process.env.PAPERCLIP_IN_WORKTREE = ORIGINAL_IN_WORKTREE; while (created.length > 0) { @@ -77,3 +87,54 @@ describe("databaseCheck — embedded postgres temp-dir guard", () => { expect(result.status).toBe("pass"); }); }); + +describe("databaseCheck effective PostgreSQL target", () => { + const savedUrl = "postgres://saved:synthetic@db.example/saved"; + const environmentUrl = "postgres://environment:synthetic@db.example/environment"; + const postgres = (connectionString?: string) => ({ database: { mode: "postgres", connectionString } }) as PaperclipConfig; + + it("checks an environment-only PostgreSQL URL", async () => { + vi.stubEnv("DATABASE_URL", environmentUrl); + expect(await databaseCheck(postgres())).toMatchObject({ status: "pass", message: "PostgreSQL connection successful" }); + expect(database.createDb).toHaveBeenCalledExactlyOnceWith(environmentUrl); + expect(database.execute).toHaveBeenCalledExactlyOnceWith("SELECT 1"); + }); + it("uses the environment target before the saved target", async () => { + vi.stubEnv("DATABASE_URL", environmentUrl); + expect((await databaseCheck(postgres(savedUrl))).status).toBe("pass"); + expect(database.createDb).toHaveBeenCalledExactlyOnceWith(environmentUrl); + }); + it("checks external PostgreSQL even when the saved mode is embedded", async () => { + vi.stubEnv("DATABASE_URL", environmentUrl); + const dataDir = path.join(makeBase(), "not-created"); + expect((await databaseCheck(embeddedConfig(dataDir))).message).toBe("PostgreSQL connection successful"); + expect(database.createDb).toHaveBeenCalledExactlyOnceWith(environmentUrl); + expect(fs.existsSync(dataDir)).toBe(false); + }); + it("uses the saved PostgreSQL target when DATABASE_URL is unset", async () => { + expect((await databaseCheck(postgres(savedUrl))).status).toBe("pass"); + expect(database.createDb).toHaveBeenCalledExactlyOnceWith(savedUrl); + }); + it("does not fall back to a saved URL when DATABASE_URL is explicitly empty", async () => { + vi.stubEnv("DATABASE_URL", ""); + expect(await databaseCheck(postgres(savedUrl))).toMatchObject({ status: "fail", message: "PostgreSQL mode selected but no connection string configured" }); + expect(database.createDb).not.toHaveBeenCalled(); + }); + it("keeps embedded storage when DATABASE_URL is empty", async () => { + vi.stubEnv("DATABASE_URL", ""); + vi.stubEnv("PAPERCLIP_IN_WORKTREE", "false"); + expect((await databaseCheck(embeddedConfig(path.join(makeBase(), "db")))).status).toBe("pass"); + expect(database.createDb).not.toHaveBeenCalled(); + }); + it("passes the environment value unchanged, as the server does", async () => { + vi.stubEnv("DATABASE_URL", ` ${environmentUrl} `); + await databaseCheck(postgres(savedUrl)); + expect(database.createDb).toHaveBeenCalledExactlyOnceWith(` ${environmentUrl} `); + }); + it("reports a failed probe of the effective target instead of checking the saved target", async () => { + vi.stubEnv("DATABASE_URL", environmentUrl); + database.execute.mockRejectedValue(new Error("Synthetic connection refused")); + expect(await databaseCheck(postgres(savedUrl))).toMatchObject({ status: "fail", message: "Cannot connect to PostgreSQL: Synthetic connection refused" }); + expect(database.createDb).toHaveBeenCalledExactlyOnceWith(environmentUrl); + }); +}); diff --git a/cli/src/checks/database-check.ts b/cli/src/checks/database-check.ts index 8e15cad14a3..8195edcade2 100644 --- a/cli/src/checks/database-check.ts +++ b/cli/src/checks/database-check.ts @@ -12,8 +12,10 @@ function isInsideOsTmpDir(targetPath: string): boolean { } export async function databaseCheck(config: PaperclipConfig, configPath?: string): Promise { - if (config.database.mode === "postgres") { - if (!config.database.connectionString) { + const connectionString = process.env.DATABASE_URL + ?? (config.database.mode === "postgres" ? config.database.connectionString : undefined); + if (connectionString || config.database.mode === "postgres") { + if (!connectionString) { return { name: "Database", status: "fail", @@ -25,7 +27,7 @@ export async function databaseCheck(config: PaperclipConfig, configPath?: string try { const { createDb } = await import("@paperclipai/db"); - const db = createDb(config.database.connectionString); + const db = createDb(connectionString); await db.execute("SELECT 1"); return { name: "Database",