import { beforeEach, describe, expect, it, vi } from "vitest"; vi.mock("./client.server.js", () => ({ createDbClient: vi.fn() })); import { createDbClient } from "./client.server.js"; import { databaseTargetLabel, isDatabaseReachable } from "./database-reachable.js"; type ProbeClient = { $queryRaw: ReturnType; $disconnect: ReturnType; }; function installProbe(probe: ProbeClient): void { vi.mocked(createDbClient).mockReturnValue(probe as never); } describe("databaseTargetLabel", () => { it("renders host:port for an explicit port", () => { expect(databaseTargetLabel("postgres://user:pw@db.example.com:6543/app")).toBe("db.example.com:6543"); }); it("defaults the port to 5432 when the URL omits it", () => { expect(databaseTargetLabel("postgres://user:pw@db.example.com/app")).toBe("db.example.com:5432"); }); it("never leaks credentials embedded in the URL", () => { const label = databaseTargetLabel("postgres://admin:s3cr3t@db.example.com:5432/app"); expect(label).not.toContain("s3cr3t"); expect(label).not.toContain("admin"); }); it("returns a generic label for an unparseable URL", () => { expect(databaseTargetLabel("not a url")).toBe("configured database"); }); }); describe("isDatabaseReachable", () => { beforeEach(() => { vi.clearAllMocks(); }); it("resolves true when the probe query succeeds and disconnects afterwards", async () => { const probe: ProbeClient = { $queryRaw: vi.fn().mockResolvedValue([{ "?column?": 1 }]), $disconnect: vi.fn().mockResolvedValue(undefined), }; installProbe(probe); await expect(isDatabaseReachable("postgres://localhost:5432/app")).resolves.toBe(true); expect(createDbClient).toHaveBeenCalledWith(expect.objectContaining({ connectionString: "postgres://localhost:5432/app" })); expect(probe.$disconnect).toHaveBeenCalledTimes(1); }); it("resolves false when the probe query rejects (unreachable / auth failure)", async () => { const probe: ProbeClient = { // Build the rejected promise at call time so the source's `.then` rejection // handler attaches in the same tick (no spurious unhandled-rejection warning). $queryRaw: vi.fn().mockImplementation(() => Promise.reject(new Error("P1001 connection refused"))), $disconnect: vi.fn().mockResolvedValue(undefined), }; installProbe(probe); await expect(isDatabaseReachable("postgres://localhost:5432/app")).resolves.toBe(false); expect(probe.$disconnect).toHaveBeenCalledTimes(1); }); it("resolves false when the query outlives the timeout budget", async () => { const probe: ProbeClient = { $queryRaw: vi.fn().mockImplementation(() => new Promise(() => undefined)), $disconnect: vi.fn().mockResolvedValue(undefined), }; installProbe(probe); await expect(isDatabaseReachable("postgres://localhost:5432/app", 5)).resolves.toBe(false); expect(probe.$disconnect).toHaveBeenCalledTimes(1); }); it("swallows a disconnect failure on the cleanup path", async () => { const probe: ProbeClient = { $queryRaw: vi.fn().mockResolvedValue([{ "?column?": 1 }]), $disconnect: vi.fn().mockRejectedValue(new Error("disconnect boom")), }; installProbe(probe); await expect(isDatabaseReachable("postgres://localhost:5432/app")).resolves.toBe(true); expect(probe.$disconnect).toHaveBeenCalledTimes(1); }); });