Files
SkyMoney/api/tests/income.test.ts

62 lines
1.8 KiB
TypeScript

// tests/income.test.ts
import request from "supertest";
import { describe, it, expect, beforeEach, afterAll, beforeAll } from "vitest";
import appFactory from "./appFactory";
import { prisma, resetUser, ensureUser, U, cid, pid, closePrisma } from "./helpers";
import type { FastifyInstance } from "fastify";
let app: FastifyInstance;
beforeAll(async () => {
app = await appFactory(); // <-- await the app
});
afterAll(async () => {
await app.close(); // <-- close server
await closePrisma();
});
describe("POST /income", () => {
beforeEach(async () => {
await resetUser(U);
await ensureUser(U);
await prisma.variableCategory.createMany({
data: [
{ id: cid("c1"), userId: U, name: "Groceries", percent: 60, priority: 2, isSavings: false, balanceCents: 0n },
{ id: cid("c2"), userId: U, name: "Saver", percent: 40, priority: 1, isSavings: true, balanceCents: 0n },
],
});
await prisma.fixedPlan.create({
data: {
id: pid("rent"),
userId: U,
name: "Rent",
cycleStart: new Date().toISOString(),
dueOn: new Date(Date.now() + 7 * 864e5).toISOString(),
totalCents: 10000n,
fundedCents: 0n,
priority: 1,
fundingMode: "auto-on-deposit",
},
});
});
afterAll(async () => {
await closePrisma();
});
it("allocates fixed first then variables; updates balances; returns audit", async () => {
const res = await request(app.server)
.post("/income")
.set("x-user-id", U)
.send({ amountCents: 15000 });
expect(res.statusCode).toBe(200);
expect(res.body).toHaveProperty("fixedAllocations");
expect(res.body).toHaveProperty("variableAllocations");
expect(typeof res.body.remainingUnallocatedCents).toBe("number");
});
});