import assert from "node:assert/strict"; import test from "node:test"; import { closeLocalPostgresDataPool, closeLocalPostgresDataPools, createLocalPostgresDataClient, } from "../src/lib/db/local-postgres-client-core.ts"; /** * Non-Docker unit tests for the per-key pool close helper. pg Pool is lazy: * constructing a client only registers the pool in the per-URL cache and * never opens a socket, so these tests run without PostgreSQL and never * produce 57P01 teardown noise. */ test("closeLocalPostgresDataPool is a safe no-op for an unknown key", async () => { await assert.doesNotReject( closeLocalPostgresDataPool("postgresql://never-registered@127.0.0.1:1/nope"), ); }); test("closeLocalPostgresDataPool closes only the requested per-key pool", async () => { const urlA = "postgresql://unit-a@127.0.0.1:1/jyotisha-unit-a"; const urlB = "postgresql://unit-b@127.0.0.1:1/jyotisha-unit-b"; createLocalPostgresDataClient(urlA, null, "service_role"); createLocalPostgresDataClient(urlB, null, "service_role"); // Closing one key must never reject or affect the other key's close. await assert.doesNotReject(closeLocalPostgresDataPool(urlA)); await assert.doesNotReject(closeLocalPostgresDataPool(urlB)); // Re-closing the same key is idempotent. await assert.doesNotReject(closeLocalPostgresDataPool(urlA)); }); test("the per-key cache accepts a fresh pool for a closed key", async () => { const url = "postgresql://unit-c@127.0.0.1:1/jyotisha-unit-c"; createLocalPostgresDataClient(url, null, "service_role"); await closeLocalPostgresDataPool(url); // A later client for the same URL registers a new pool (the close deleted // the entry before ending the old pool); close again must resolve. createLocalPostgresDataClient(url, null, "service_role"); await assert.doesNotReject(closeLocalPostgresDataPool(url)); }); test("per-key close after a global close is a safe no-op", async () => { const url = "postgresql://unit-d@127.0.0.1:1/jyotisha-unit-d"; createLocalPostgresDataClient(url, null, "service_role"); await closeLocalPostgresDataPools(); await assert.doesNotReject(closeLocalPostgresDataPool(url)); });