fix: fix bug in rebalanace, stale session values being read
All checks were successful
Deploy / deploy (push) Successful in 1m30s
Security Tests / security-non-db (push) Successful in 20s
Security Tests / security-db (push) Successful in 24s

This commit is contained in:
2026-03-12 13:51:46 -05:00
parent a03fbea5e7
commit 58545b2da7
3 changed files with 52 additions and 7 deletions

View File

@@ -44,6 +44,37 @@ describe("manual rebalance", () => {
await seedBasics();
});
it("uses live category balances when session availableCents is stale", async () => {
await prisma.budgetSession.updateMany({
where: { userId: U },
data: { availableCents: 1234n },
});
const getRes = await request(app.server)
.get("/variable-categories/manual-rebalance")
.set("x-user-id", U);
expect(getRes.statusCode).toBe(200);
expect(getRes.body?.availableCents).toBe(10_000);
const cats = await prisma.variableCategory.findMany({ where: { userId: U }, orderBy: { priority: "asc" } });
const targets = cats.map((c) => ({ id: c.id, targetCents: 2500 })); // 4 * 2500 = 10000
const postRes = await request(app.server)
.post("/variable-categories/manual-rebalance")
.set("x-user-id", U)
.send({ targets });
expect(postRes.statusCode).toBe(200);
expect(postRes.body?.availableCents).toBe(10_000);
const session = await prisma.budgetSession.findFirst({
where: { userId: U },
orderBy: { periodStart: "desc" },
select: { availableCents: true },
});
expect(Number(session?.availableCents ?? 0n)).toBe(10_000);
});
it("rebalances when sums match available", async () => {
const cats = await prisma.variableCategory.findMany({ where: { userId: U }, orderBy: { priority: "asc" } });
const targets = cats.map((c) => ({ id: c.id, targetCents: 2500 })); // 4 * 2500 = 10000