import { afterEach, expect, test } from "bun:test"; import grabMariaDBClient from "./grab-mariadb-client"; import setMariaDBClient from "./set-mariadb-client"; // Clients connect lazily, so none of these open a connection const config = { db_name: "bmt_client_cache", ssl_reject_unauthorized: false }; const originalConfig = global.CONFIG; afterEach(async () => { await global.MARIADB_CLIENT?.close(); global.CONFIG = originalConfig; }); test("reuses the client for the same config", () => { expect(grabMariaDBClient({ config })).toBe(grabMariaDBClient({ config: { ...config } })); }); test("a closed client is dropped from the cache and the global slot", async () => { setMariaDBClient({ config }); const first = global.MARIADB_CLIENT!; await first.close(); expect(global.MARIADB_CLIENT).toBeUndefined(); const second = grabMariaDBClient({ config }); expect(second).toBeDefined(); expect(second).not.toBe(first); }); test("the global client is not reused for a different config", () => { setMariaDBClient({ config }); global.CONFIG = { db_name: "bmt_other" }; const other = grabMariaDBClient({ config: global.CONFIG }); expect(other).not.toBe(global.MARIADB_CLIENT); other?.close(); });