Files
SkyMoney/api/tests/helpers.ts
Ricearoni1245 9c7f4d5139
All checks were successful
Deploy / deploy (push) Successful in 1m29s
Security Tests / security-non-db (push) Successful in 20s
Security Tests / security-db (push) Successful in 24s
removed unneccesary files
2026-03-21 17:30:11 -05:00

44 lines
1.4 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 } }),
prisma.budgetSession.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: { timezone: "UTC" },
create: { id: userId, email: `${userId}@demo.local`, timezone: "UTC" },
});
}
/** Close Prisma after all tests */
export async function closePrisma() {
await prisma.$disconnect();
}