phase 5: fixed expense logic simplified and compacted
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import request from "supertest";
|
||||
import { describe, it, expect, beforeAll, beforeEach, afterAll } from "vitest";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import appFactory from "./appFactory";
|
||||
import { prisma, resetUser, ensureUser, U, cid, pid, closePrisma } from "./helpers";
|
||||
import { prisma, resetUser, ensureUser, U, closePrisma } from "./helpers";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
let app: FastifyInstance;
|
||||
@@ -11,7 +12,9 @@ beforeAll(async () => {
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
if (app) {
|
||||
await app.close();
|
||||
}
|
||||
await closePrisma();
|
||||
});
|
||||
|
||||
@@ -23,16 +26,22 @@ describe("GET /transactions", () => {
|
||||
await resetUser(U);
|
||||
await ensureUser(U);
|
||||
|
||||
catId = cid("c");
|
||||
planId = pid("p");
|
||||
|
||||
await prisma.variableCategory.create({
|
||||
data: { id: catId, userId: U, name: "Groceries", percent: 100, priority: 1, isSavings: false, balanceCents: 5000n },
|
||||
});
|
||||
|
||||
await prisma.fixedPlan.create({
|
||||
const category = await prisma.variableCategory.create({
|
||||
data: {
|
||||
id: planId,
|
||||
id: randomUUID(),
|
||||
userId: U,
|
||||
name: "Groceries",
|
||||
percent: 100,
|
||||
priority: 1,
|
||||
isSavings: false,
|
||||
balanceCents: 5000n,
|
||||
},
|
||||
});
|
||||
catId = category.id;
|
||||
|
||||
const plan = await prisma.fixedPlan.create({
|
||||
data: {
|
||||
id: randomUUID(),
|
||||
userId: U,
|
||||
name: "Rent",
|
||||
totalCents: 10000n,
|
||||
@@ -43,6 +52,7 @@ describe("GET /transactions", () => {
|
||||
fundingMode: "auto-on-deposit",
|
||||
},
|
||||
});
|
||||
planId = plan.id;
|
||||
|
||||
await prisma.transaction.createMany({
|
||||
data: [
|
||||
@@ -100,16 +110,31 @@ describe("POST /transactions", () => {
|
||||
let catId: string;
|
||||
let planId: string;
|
||||
|
||||
function postTransactionsWithCsrf() {
|
||||
const csrf = randomUUID().replace(/-/g, "");
|
||||
return request(app.server)
|
||||
.post("/transactions")
|
||||
.set("x-user-id", U)
|
||||
.set("x-csrf-token", csrf)
|
||||
.set("Cookie", `csrf=${csrf}`);
|
||||
}
|
||||
|
||||
function patchWithCsrf(path: string) {
|
||||
const csrf = randomUUID().replace(/-/g, "");
|
||||
return request(app.server)
|
||||
.patch(path)
|
||||
.set("x-user-id", U)
|
||||
.set("x-csrf-token", csrf)
|
||||
.set("Cookie", `csrf=${csrf}`);
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
await resetUser(U);
|
||||
await ensureUser(U);
|
||||
|
||||
catId = cid("cat");
|
||||
planId = pid("plan");
|
||||
|
||||
await prisma.variableCategory.create({
|
||||
const category = await prisma.variableCategory.create({
|
||||
data: {
|
||||
id: catId,
|
||||
id: randomUUID(),
|
||||
userId: U,
|
||||
name: "Dining",
|
||||
percent: 100,
|
||||
@@ -118,10 +143,11 @@ describe("POST /transactions", () => {
|
||||
balanceCents: 5000n,
|
||||
},
|
||||
});
|
||||
catId = category.id;
|
||||
|
||||
await prisma.fixedPlan.create({
|
||||
const plan = await prisma.fixedPlan.create({
|
||||
data: {
|
||||
id: planId,
|
||||
id: randomUUID(),
|
||||
userId: U,
|
||||
name: "Loan",
|
||||
totalCents: 10000n,
|
||||
@@ -132,12 +158,11 @@ describe("POST /transactions", () => {
|
||||
fundingMode: "auto-on-deposit",
|
||||
},
|
||||
});
|
||||
planId = plan.id;
|
||||
});
|
||||
|
||||
it("spends from a variable category and updates balance", async () => {
|
||||
const res = await request(app.server)
|
||||
.post("/transactions")
|
||||
.set("x-user-id", U)
|
||||
const res = await postTransactionsWithCsrf()
|
||||
.send({
|
||||
kind: "variable_spend",
|
||||
amountCents: 2000,
|
||||
@@ -157,10 +182,8 @@ describe("POST /transactions", () => {
|
||||
expect(tx.isReconciled).toBe(true);
|
||||
});
|
||||
|
||||
it("prevents overdrawing fixed plans", async () => {
|
||||
const res = await request(app.server)
|
||||
.post("/transactions")
|
||||
.set("x-user-id", U)
|
||||
it("returns insufficient budget when fixed payment requires unavailable top-up", async () => {
|
||||
const res = await postTransactionsWithCsrf()
|
||||
.send({
|
||||
kind: "fixed_payment",
|
||||
amountCents: 400000, // exceeds funded
|
||||
@@ -169,13 +192,11 @@ describe("POST /transactions", () => {
|
||||
});
|
||||
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.code).toBe("OVERDRAFT_PLAN");
|
||||
expect(res.body.code).toBe("INSUFFICIENT_AVAILABLE_BUDGET");
|
||||
});
|
||||
|
||||
it("updates note/receipt and reconciliation via patch", async () => {
|
||||
const created = await request(app.server)
|
||||
.post("/transactions")
|
||||
.set("x-user-id", U)
|
||||
const created = await postTransactionsWithCsrf()
|
||||
.send({
|
||||
kind: "variable_spend",
|
||||
amountCents: 1000,
|
||||
@@ -185,9 +206,7 @@ describe("POST /transactions", () => {
|
||||
expect(created.status).toBe(200);
|
||||
const txId = created.body.id;
|
||||
|
||||
const res = await request(app.server)
|
||||
.patch(`/transactions/${txId}`)
|
||||
.set("x-user-id", U)
|
||||
const res = await patchWithCsrf(`/transactions/${txId}`)
|
||||
.send({
|
||||
note: "Cleared",
|
||||
isReconciled: true,
|
||||
|
||||
Reference in New Issue
Block a user