import { describe, it, expect, mock, beforeEach } from "bun:test"; // Mock DrizzleClient mock.module("./DrizzleClient", () => ({ DrizzleClient: { transaction: async (cb: any) => cb("MOCK_TX") } })); import { withTransaction } from "./db"; import { setShuttingDown, getActiveTransactions, decrementTransactions } from "./shutdown"; describe("db withTransaction", () => { beforeEach(() => { setShuttingDown(false); // Reset transaction count while (getActiveTransactions() > 0) { decrementTransactions(); } }); it("should allow transactions when not shutting down", async () => { const result = await withTransaction(async (tx) => { return "success"; }); expect(result).toBe("success"); expect(getActiveTransactions()).toBe(0); }); it("should throw error when shutting down", async () => { setShuttingDown(true); expect(withTransaction(async (tx) => { return "success"; })).rejects.toThrow("System is shutting down, no new transactions allowed."); expect(getActiveTransactions()).toBe(0); }); it("should increment and decrement transaction count", async () => { let countDuring = 0; await withTransaction(async (tx) => { countDuring = getActiveTransactions(); return "ok"; }); expect(countDuring).toBe(1); expect(getActiveTransactions()).toBe(0); }); });