- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
103 lines
3.7 KiB
TypeScript
103 lines
3.7 KiB
TypeScript
import { PrismaService } from "./prisma.service";
|
|
|
|
// Replace the client class before importing the service: no native engine or DB.
|
|
jest.mock("@prisma/client", () => ({
|
|
PrismaClient: class {
|
|
$connect = jest.fn();
|
|
$disconnect = jest.fn();
|
|
$executeRawUnsafe = jest.fn();
|
|
},
|
|
}));
|
|
|
|
describe("PrismaService lifecycle", () => {
|
|
let service: PrismaService;
|
|
beforeEach(() => {
|
|
service = new PrismaService();
|
|
});
|
|
afterEach(() => jest.restoreAllMocks());
|
|
|
|
it("waits for connection before running idempotent DDL sequentially", async () => {
|
|
let connected = false;
|
|
let active = false;
|
|
jest.spyOn(service, "$connect").mockImplementation(async () => {
|
|
await Promise.resolve();
|
|
connected = true;
|
|
});
|
|
const execute = jest
|
|
.spyOn(
|
|
service as unknown as {
|
|
$executeRawUnsafe: (sql: string) => Promise<number>;
|
|
},
|
|
"$executeRawUnsafe",
|
|
)
|
|
.mockImplementation(async () => {
|
|
expect(connected).toBe(true);
|
|
expect(active).toBe(false);
|
|
active = true;
|
|
await Promise.resolve();
|
|
active = false;
|
|
return 0;
|
|
});
|
|
await service.onModuleInit();
|
|
expect(service.$connect).toHaveBeenCalledTimes(1);
|
|
const sql = execute.mock.calls.map(([statement]) => statement);
|
|
expect(sql).toHaveLength(16);
|
|
expect(
|
|
sql.every((statement) =>
|
|
/^CREATE (TABLE|INDEX) IF NOT EXISTS /.test(statement),
|
|
),
|
|
).toBe(true);
|
|
expect(
|
|
sql
|
|
.filter((statement) => statement.startsWith("CREATE TABLE"))
|
|
.map((statement) => statement.match(/"([^"]+)"/)?.[1]),
|
|
).toEqual(["tickets", "ticket_messages", "sms_logs", "product_reviews"]);
|
|
expect(sql[0]).toContain('REFERENCES "users"("id") ON DELETE CASCADE');
|
|
expect(sql[5]).toContain('REFERENCES "tickets"("id") ON DELETE CASCADE');
|
|
expect(sql[12]).toContain('REFERENCES "products"("id") ON DELETE CASCADE');
|
|
expect(service.$disconnect).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("propagates connection failure without attempting any schema writes", async () => {
|
|
const error = new Error("database unavailable");
|
|
jest.spyOn(service, "$connect").mockRejectedValue(error);
|
|
await expect(service.onModuleInit()).rejects.toBe(error);
|
|
expect(service.$executeRawUnsafe).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it.each([new Error("permission denied"), "raw driver failure", null])(
|
|
"logs DDL failure and still attempts the remaining statements (%p)",
|
|
async (error) => {
|
|
const warn = jest
|
|
.spyOn(console, "warn")
|
|
.mockImplementation(() => undefined);
|
|
const execute = jest
|
|
.spyOn(service, "$executeRawUnsafe")
|
|
.mockRejectedValueOnce(error)
|
|
.mockResolvedValue(0);
|
|
await expect(service.onModuleInit()).resolves.toBeUndefined();
|
|
expect(execute).toHaveBeenCalledTimes(16);
|
|
expect(warn).toHaveBeenCalledTimes(1);
|
|
expect(warn).toHaveBeenCalledWith(
|
|
expect.stringContaining(
|
|
'[PrismaService] DDL execution note for (CREATE TABLE IF NOT EXISTS "tickets"',
|
|
),
|
|
error instanceof Error ? error.message : error,
|
|
);
|
|
},
|
|
);
|
|
|
|
it("disconnects once during destruction without schema operations", async () => {
|
|
await expect(service.onModuleDestroy()).resolves.toBeUndefined();
|
|
expect(service.$disconnect).toHaveBeenCalledTimes(1);
|
|
expect(service.$connect).not.toHaveBeenCalled();
|
|
expect(service.$executeRawUnsafe).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("propagates disconnect failures so shutdown cannot silently succeed", async () => {
|
|
const error = new Error("disconnect failed");
|
|
jest.spyOn(service, "$disconnect").mockRejectedValue(error);
|
|
await expect(service.onModuleDestroy()).rejects.toBe(error);
|
|
});
|
|
});
|