Files
SkyMoney/api/tests/variable-categories.guard.test.ts

57 lines
1.7 KiB
TypeScript

// tests/variable-categories.guard.test.ts
import request from "supertest";
import { describe, it, expect, beforeEach, beforeAll, afterAll } from "vitest";
import appFactory from "./appFactory";
import { prisma, resetUser, ensureUser, U, cid, closePrisma } from "./helpers";
import type { FastifyInstance } from "fastify";
let app: FastifyInstance;
beforeAll(async () => {
app = await appFactory();
});
afterAll(async () => {
await app.close();
await closePrisma();
});
describe("Variable Categories guard (sum=100)", () => {
beforeEach(async () => {
await resetUser(U);
await ensureUser(U);
await prisma.variableCategory.createMany({
data: [
{ id: cid("a"), userId: U, name: "A", percent: 50, priority: 1, isSavings: false, balanceCents: 0n },
{ id: cid("b"), userId: U, name: "B", percent: 50, priority: 2, isSavings: false, balanceCents: 0n },
],
});
});
afterAll(async () => {
await closePrisma();
});
it("rejects create that would push sum away from 100", async () => {
const res = await request(app.server)
.post("/variable-categories")
.set("x-user-id", U)
.send({ name: "Oops", percent: 10, isSavings: false, priority: 99 });
expect(res.statusCode).toBe(400);
expect(res.body?.message).toMatch(/Percents must sum to 100/i);
});
it("rejects update that breaks the sum", async () => {
const existing = await prisma.variableCategory.findFirst({ where: { userId: U } });
const res = await request(app.server)
.patch(`/variable-categories/${existing!.id}`)
.set("x-user-id", U)
.send({ percent: 90 });
expect(res.statusCode).toBe(400);
expect(res.body?.message).toMatch(/Percents must sum to 100/i);
});
});