added api logic, vitest, minimal testing ui

This commit is contained in:
2025-11-15 23:26:57 -06:00
parent f4160b91db
commit 4eae966f96
95 changed files with 14155 additions and 469 deletions

42
api/tests/helpers.ts Normal file
View File

@@ -0,0 +1,42 @@
// 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();
}