43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
// tests/helpers.ts
|
|
import { PrismaClient } from "@prisma/client";
|
|
|
|
export const prisma = new PrismaClient();
|
|
|
|
// Handy test user id
|
|
export const U = "demo-user-1";
|
|
|
|
// Monotonic id helpers so we never collide with existing rows
|
|
let cseq = 0;
|
|
let pseq = 0;
|
|
export const cid = (base = "c") => `${base}_${Date.now()}_${cseq++}`;
|
|
export const pid = (base = "p") => `${base}_${Date.now()}_${pseq++}`;
|
|
|
|
/**
|
|
* Hard-reset all data for a given user in dependency-safe order.
|
|
* Also deletes the user row so tests can re-create/upsert cleanly.
|
|
*/
|
|
export async function resetUser(userId: string) {
|
|
await prisma.$transaction([
|
|
prisma.allocation.deleteMany({ where: { userId } }),
|
|
prisma.transaction.deleteMany({ where: { userId } }),
|
|
prisma.incomeEvent.deleteMany({ where: { userId } }),
|
|
prisma.fixedPlan.deleteMany({ where: { userId } }),
|
|
prisma.variableCategory.deleteMany({ where: { userId } }),
|
|
]);
|
|
await prisma.user.deleteMany({ where: { id: userId } });
|
|
}
|
|
|
|
/** Ensure the user exists (id stable) */
|
|
export async function ensureUser(userId: string) {
|
|
await prisma.user.upsert({
|
|
where: { id: userId },
|
|
update: {},
|
|
create: { id: userId, email: `${userId}@demo.local` },
|
|
});
|
|
}
|
|
|
|
/** Close Prisma after all tests */
|
|
export async function closePrisma() {
|
|
await prisma.$disconnect();
|
|
}
|