forked from syntaxbullet/AuroraBot-discord
57 lines
1.7 KiB
TypeScript
57 lines
1.7 KiB
TypeScript
import { describe, it, expect, mock, beforeEach } from "bun:test";
|
|
|
|
// Mock shutdown module before importing db
|
|
mock.module("./shutdown", () => {
|
|
let shuttingDown = false;
|
|
let transactions = 0;
|
|
return {
|
|
isShuttingDown: () => shuttingDown,
|
|
setShuttingDown: (v: boolean) => { shuttingDown = v; },
|
|
incrementTransactions: () => { transactions++; },
|
|
decrementTransactions: () => { transactions--; },
|
|
getActiveTransactions: () => transactions,
|
|
};
|
|
});
|
|
|
|
// Mock DrizzleClient
|
|
mock.module("./DrizzleClient", () => ({
|
|
DrizzleClient: {
|
|
transaction: async (cb: any) => cb("MOCK_TX")
|
|
}
|
|
}));
|
|
|
|
import { withTransaction } from "./db";
|
|
import { setShuttingDown, getActiveTransactions } from "./shutdown";
|
|
|
|
describe("db withTransaction", () => {
|
|
beforeEach(() => {
|
|
setShuttingDown(false);
|
|
});
|
|
|
|
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);
|
|
});
|
|
});
|