final touches for beta skymoney (at least i think)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import request from "supertest";
|
||||
import { describe, it, expect, beforeAll, beforeEach, afterAll } from "vitest";
|
||||
import appFactory from "./appFactory";
|
||||
import { prisma, resetUser, ensureUser, U, cid, closePrisma } from "./helpers";
|
||||
import { prisma, resetUser, ensureUser, U, cid, pid, closePrisma } from "./helpers";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
let app: FastifyInstance;
|
||||
@@ -16,16 +16,34 @@ afterAll(async () => {
|
||||
});
|
||||
|
||||
describe("GET /transactions", () => {
|
||||
let catId: string;
|
||||
let planId: string;
|
||||
|
||||
beforeEach(async () => {
|
||||
await resetUser(U);
|
||||
await ensureUser(U);
|
||||
|
||||
const c = cid("c");
|
||||
catId = cid("c");
|
||||
planId = pid("p");
|
||||
|
||||
await prisma.variableCategory.create({
|
||||
data: { id: c, userId: U, name: "Groceries", percent: 100, priority: 1, isSavings: false, balanceCents: 5000n },
|
||||
data: { id: catId, userId: U, name: "Groceries", percent: 100, priority: 1, isSavings: false, balanceCents: 5000n },
|
||||
});
|
||||
|
||||
await prisma.fixedPlan.create({
|
||||
data: {
|
||||
id: planId,
|
||||
userId: U,
|
||||
name: "Rent",
|
||||
totalCents: 10000n,
|
||||
fundedCents: 2000n,
|
||||
priority: 1,
|
||||
cycleStart: new Date().toISOString(),
|
||||
dueOn: new Date(Date.now() + 864e5).toISOString(),
|
||||
fundingMode: "auto-on-deposit",
|
||||
},
|
||||
});
|
||||
|
||||
// seed some transactions of different kinds/dates
|
||||
await prisma.transaction.createMany({
|
||||
data: [
|
||||
{
|
||||
@@ -33,7 +51,7 @@ describe("GET /transactions", () => {
|
||||
userId: U,
|
||||
occurredAt: new Date("2025-01-03T12:00:00.000Z"),
|
||||
kind: "variable_spend",
|
||||
categoryId: c,
|
||||
categoryId: catId,
|
||||
amountCents: 1000n,
|
||||
},
|
||||
{
|
||||
@@ -41,17 +59,13 @@ describe("GET /transactions", () => {
|
||||
userId: U,
|
||||
occurredAt: new Date("2025-01-10T12:00:00.000Z"),
|
||||
kind: "fixed_payment",
|
||||
planId: null,
|
||||
planId,
|
||||
amountCents: 2000n,
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await closePrisma();
|
||||
});
|
||||
|
||||
it("paginates + filters by kind/date", async () => {
|
||||
const res = await request(app.server)
|
||||
.get("/transactions?from=2025-01-02&to=2025-01-06&kind=variable_spend&page=1&limit=10")
|
||||
@@ -63,4 +77,129 @@ describe("GET /transactions", () => {
|
||||
expect(body.items.length).toBe(1);
|
||||
expect(body.items[0].kind).toBe("variable_spend");
|
||||
});
|
||||
|
||||
it("filters by bucket id for either category or plan", async () => {
|
||||
const byCategory = await request(app.server)
|
||||
.get(`/transactions?bucketId=${catId}`)
|
||||
.set("x-user-id", U);
|
||||
|
||||
expect(byCategory.status).toBe(200);
|
||||
expect(byCategory.body.items.every((t: any) => t.categoryId === catId)).toBe(true);
|
||||
|
||||
const byPlan = await request(app.server)
|
||||
.get(`/transactions?bucketId=${planId}`)
|
||||
.set("x-user-id", U);
|
||||
|
||||
expect(byPlan.status).toBe(200);
|
||||
expect(byPlan.body.items.every((t: any) => t.planId === planId)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /transactions", () => {
|
||||
const dateISO = new Date().toISOString();
|
||||
let catId: string;
|
||||
let planId: string;
|
||||
|
||||
beforeEach(async () => {
|
||||
await resetUser(U);
|
||||
await ensureUser(U);
|
||||
|
||||
catId = cid("cat");
|
||||
planId = pid("plan");
|
||||
|
||||
await prisma.variableCategory.create({
|
||||
data: {
|
||||
id: catId,
|
||||
userId: U,
|
||||
name: "Dining",
|
||||
percent: 100,
|
||||
priority: 1,
|
||||
isSavings: false,
|
||||
balanceCents: 5000n,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.fixedPlan.create({
|
||||
data: {
|
||||
id: planId,
|
||||
userId: U,
|
||||
name: "Loan",
|
||||
totalCents: 10000n,
|
||||
fundedCents: 3000n,
|
||||
priority: 1,
|
||||
cycleStart: new Date().toISOString(),
|
||||
dueOn: new Date(Date.now() + 864e5).toISOString(),
|
||||
fundingMode: "auto-on-deposit",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("spends from a variable category and updates balance", async () => {
|
||||
const res = await request(app.server)
|
||||
.post("/transactions")
|
||||
.set("x-user-id", U)
|
||||
.send({
|
||||
kind: "variable_spend",
|
||||
amountCents: 2000,
|
||||
occurredAtISO: dateISO,
|
||||
categoryId: catId,
|
||||
note: "Groceries run",
|
||||
receiptUrl: "https://example.com/receipt",
|
||||
isReconciled: true,
|
||||
});
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
const category = await prisma.variableCategory.findUniqueOrThrow({ where: { id: catId } });
|
||||
expect(Number(category.balanceCents)).toBe(3000);
|
||||
const tx = await prisma.transaction.findFirstOrThrow({ where: { userId: U, categoryId: catId } });
|
||||
expect(tx.note).toBe("Groceries run");
|
||||
expect(tx.receiptUrl).toBe("https://example.com/receipt");
|
||||
expect(tx.isReconciled).toBe(true);
|
||||
});
|
||||
|
||||
it("prevents overdrawing fixed plans", async () => {
|
||||
const res = await request(app.server)
|
||||
.post("/transactions")
|
||||
.set("x-user-id", U)
|
||||
.send({
|
||||
kind: "fixed_payment",
|
||||
amountCents: 400000, // exceeds funded
|
||||
occurredAtISO: dateISO,
|
||||
planId,
|
||||
});
|
||||
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.code).toBe("OVERDRAFT_PLAN");
|
||||
});
|
||||
|
||||
it("updates note/receipt and reconciliation via patch", async () => {
|
||||
const created = await request(app.server)
|
||||
.post("/transactions")
|
||||
.set("x-user-id", U)
|
||||
.send({
|
||||
kind: "variable_spend",
|
||||
amountCents: 1000,
|
||||
occurredAtISO: dateISO,
|
||||
categoryId: catId,
|
||||
});
|
||||
expect(created.status).toBe(200);
|
||||
const txId = created.body.id;
|
||||
|
||||
const res = await request(app.server)
|
||||
.patch(`/transactions/${txId}`)
|
||||
.set("x-user-id", U)
|
||||
.send({
|
||||
note: "Cleared",
|
||||
isReconciled: true,
|
||||
receiptUrl: "https://example.com/r.pdf",
|
||||
});
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.isReconciled).toBe(true);
|
||||
expect(res.body.note).toBe("Cleared");
|
||||
expect(res.body.receiptUrl).toBe("https://example.com/r.pdf");
|
||||
|
||||
const tx = await prisma.transaction.findUniqueOrThrow({ where: { id: txId } });
|
||||
expect(tx.isReconciled).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user