removed unneccesary files
This commit is contained in:
@@ -32,8 +32,8 @@ export async function resetUser(userId: string) {
|
||||
export async function ensureUser(userId: string) {
|
||||
await prisma.user.upsert({
|
||||
where: { id: userId },
|
||||
update: {},
|
||||
create: { id: userId, email: `${userId}@demo.local` },
|
||||
update: { timezone: "UTC" },
|
||||
create: { id: userId, email: `${userId}@demo.local`, timezone: "UTC" },
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -40,27 +40,28 @@ function calculateNextDueDateLikeServer(
|
||||
|
||||
switch (frequency) {
|
||||
case "weekly":
|
||||
zoned.setUTCDate(zoned.getUTCDate() + 7);
|
||||
zoned.setDate(zoned.getDate() + 7);
|
||||
break;
|
||||
case "biweekly":
|
||||
zoned.setUTCDate(zoned.getUTCDate() + 14);
|
||||
zoned.setDate(zoned.getDate() + 14);
|
||||
break;
|
||||
case "monthly": {
|
||||
const targetDay = zoned.getUTCDate();
|
||||
const nextMonth = zoned.getUTCMonth() + 1;
|
||||
const nextYear = zoned.getUTCFullYear() + Math.floor(nextMonth / 12);
|
||||
const nextMonthIndex = nextMonth % 12;
|
||||
const targetDay = zoned.getDate();
|
||||
zoned.setDate(1);
|
||||
zoned.setMonth(zoned.getMonth() + 1);
|
||||
const lastDay = new Date(
|
||||
Date.UTC(nextYear, nextMonthIndex + 1, 0)
|
||||
).getUTCDate();
|
||||
zoned.setUTCFullYear(nextYear, nextMonthIndex, Math.min(targetDay, lastDay));
|
||||
zoned.getFullYear(),
|
||||
zoned.getMonth() + 1,
|
||||
0
|
||||
).getDate();
|
||||
zoned.setDate(Math.min(targetDay, lastDay));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return base;
|
||||
}
|
||||
|
||||
zoned.setUTCHours(0, 0, 0, 0);
|
||||
zoned.setHours(0, 0, 0, 0);
|
||||
return fromZonedTime(zoned, timezone);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,8 @@ describe("rolloverFixedPlans", () => {
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
const results = await rolloverFixedPlans(prisma, "2025-01-10T00:00:00Z");
|
||||
// Rollover job only processes after 6 AM in the user's timezone.
|
||||
const results = await rolloverFixedPlans(prisma, "2025-01-10T10:00:00Z");
|
||||
const match = results.find((r) => r.planId === plan.id);
|
||||
expect(match?.cyclesAdvanced).toBe(1);
|
||||
expect(match?.deficitCents).toBe(4000);
|
||||
@@ -48,7 +49,8 @@ describe("rolloverFixedPlans", () => {
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
const results = await rolloverFixedPlans(prisma, "2025-02-05T00:00:00Z");
|
||||
// Rollover job only processes after 6 AM in the user's timezone.
|
||||
const results = await rolloverFixedPlans(prisma, "2025-02-05T10:00:00Z");
|
||||
const match = results.find((r) => r.planId === plan.id);
|
||||
expect(match?.cyclesAdvanced).toBe(2);
|
||||
expect(match?.carryForwardCents).toBe(2000);
|
||||
|
||||
@@ -4,6 +4,7 @@ import { describe, it, expect, beforeEach, beforeAll, afterAll } from "vitest";
|
||||
import appFactory from "./appFactory";
|
||||
import { prisma, resetUser, ensureUser, U, cid, closePrisma } from "./helpers";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { randomUUID } from "node:crypto";
|
||||
|
||||
let app: FastifyInstance;
|
||||
|
||||
@@ -34,9 +35,12 @@ describe("Variable Categories guard (sum=100)", () => {
|
||||
});
|
||||
|
||||
it("rejects create that would push sum away from 100", async () => {
|
||||
const csrf = randomUUID().replace(/-/g, "");
|
||||
const res = await request(app.server)
|
||||
.post("/variable-categories")
|
||||
.set("x-user-id", U)
|
||||
.set("x-csrf-token", csrf)
|
||||
.set("Cookie", `csrf=${csrf}`)
|
||||
.send({ name: "Oops", percent: 10, isSavings: false, priority: 99 });
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
@@ -45,9 +49,12 @@ describe("Variable Categories guard (sum=100)", () => {
|
||||
|
||||
it("rejects update that breaks the sum", async () => {
|
||||
const existing = await prisma.variableCategory.findFirst({ where: { userId: U } });
|
||||
const csrf = randomUUID().replace(/-/g, "");
|
||||
const res = await request(app.server)
|
||||
.patch(`/variable-categories/${existing!.id}`)
|
||||
.set("x-user-id", U)
|
||||
.set("x-csrf-token", csrf)
|
||||
.set("Cookie", `csrf=${csrf}`)
|
||||
.send({ percent: 90 });
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
|
||||
@@ -3,6 +3,7 @@ import { describe, it, expect, beforeAll, afterAll, beforeEach } from "vitest";
|
||||
import appFactory from "./appFactory";
|
||||
import { prisma, resetUser, ensureUser, U, cid, closePrisma } from "./helpers";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { randomUUID } from "node:crypto";
|
||||
|
||||
let app: FastifyInstance;
|
||||
|
||||
@@ -59,10 +60,13 @@ describe("manual rebalance", () => {
|
||||
|
||||
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 csrf = randomUUID().replace(/-/g, "");
|
||||
const postRes = await request(app.server)
|
||||
.post("/variable-categories/manual-rebalance")
|
||||
.set("x-user-id", U)
|
||||
.send({ targets });
|
||||
.set("x-csrf-token", csrf)
|
||||
.set("Cookie", `csrf=${csrf}`)
|
||||
.send({ targets, forceLowerSavings: true });
|
||||
|
||||
expect(postRes.statusCode).toBe(200);
|
||||
expect(postRes.body?.availableCents).toBe(10_000);
|
||||
@@ -78,11 +82,14 @@ describe("manual rebalance", () => {
|
||||
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
|
||||
const csrf = randomUUID().replace(/-/g, "");
|
||||
|
||||
const res = await request(app.server)
|
||||
.post("/variable-categories/manual-rebalance")
|
||||
.set("x-user-id", U)
|
||||
.send({ targets });
|
||||
.set("x-csrf-token", csrf)
|
||||
.set("Cookie", `csrf=${csrf}`)
|
||||
.send({ targets, forceLowerSavings: true });
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.body?.ok).toBe(true);
|
||||
@@ -93,10 +100,13 @@ describe("manual rebalance", () => {
|
||||
it("rejects sum mismatch", async () => {
|
||||
const cats = await prisma.variableCategory.findMany({ where: { userId: U }, orderBy: { priority: "asc" } });
|
||||
const targets = cats.map((c, i) => ({ id: c.id, targetCents: i === 0 ? 5000 : 1000 })); // 5000 + 3*1000 = 8000
|
||||
const csrf = randomUUID().replace(/-/g, "");
|
||||
|
||||
const res = await request(app.server)
|
||||
.post("/variable-categories/manual-rebalance")
|
||||
.set("x-user-id", U)
|
||||
.set("x-csrf-token", csrf)
|
||||
.set("Cookie", `csrf=${csrf}`)
|
||||
.send({ targets });
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
@@ -108,10 +118,13 @@ describe("manual rebalance", () => {
|
||||
// savings to 500 (below 20% of 10000 = 2000)
|
||||
const targets = cats.map((c) => ({ id: c.id, targetCents: c.isSavings ? 500 : 3166 })); // 500 + 3*3166 = 9998 -> adjust
|
||||
targets[1].targetCents += 2; // total 10000
|
||||
const csrf = randomUUID().replace(/-/g, "");
|
||||
|
||||
const res = await request(app.server)
|
||||
.post("/variable-categories/manual-rebalance")
|
||||
.set("x-user-id", U)
|
||||
.set("x-csrf-token", csrf)
|
||||
.set("Cookie", `csrf=${csrf}`)
|
||||
.send({ targets });
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
@@ -120,6 +133,8 @@ describe("manual rebalance", () => {
|
||||
const resOk = await request(app.server)
|
||||
.post("/variable-categories/manual-rebalance")
|
||||
.set("x-user-id", U)
|
||||
.set("x-csrf-token", csrf)
|
||||
.set("Cookie", `csrf=${csrf}`)
|
||||
.send({ targets, forceLowerSavings: true });
|
||||
|
||||
expect(resOk.statusCode).toBe(200);
|
||||
@@ -129,13 +144,16 @@ describe("manual rebalance", () => {
|
||||
const cats = await prisma.variableCategory.findMany({ where: { userId: U }, orderBy: { priority: "asc" } });
|
||||
const targets = cats.map((c, i) => ({ id: c.id, targetCents: i === 0 ? 9000 : 333 }));
|
||||
targets[1].targetCents += 1; // sum 10000
|
||||
const csrf = randomUUID().replace(/-/g, "");
|
||||
|
||||
const res = await request(app.server)
|
||||
.post("/variable-categories/manual-rebalance")
|
||||
.set("x-user-id", U)
|
||||
.set("x-csrf-token", csrf)
|
||||
.set("Cookie", `csrf=${csrf}`)
|
||||
.send({ targets });
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
expect(res.body?.code).toBe("OVER_80_PERCENT");
|
||||
expect(res.body?.code).toBe("OVER_80_CONFIRM_REQUIRED");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user