fix: fix bug in rebalanace, stale session values being read
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user