added api logic, vitest, minimal testing ui

This commit is contained in:
2025-11-15 23:26:57 -06:00
parent f4160b91db
commit 4eae966f96
95 changed files with 14155 additions and 469 deletions

View File

@@ -0,0 +1,65 @@
// tests/allocator.test.ts
import { describe, it, expect, beforeEach, afterAll } from "vitest";
import { prisma, resetUser, ensureUser, U, cid, pid, closePrisma } from "./helpers";
import { allocateIncome } from "../src/allocator";
describe("allocator — core behaviors", () => {
beforeEach(async () => {
await resetUser(U);
await ensureUser(U);
});
afterAll(async () => {
await closePrisma();
});
it("distributes remainder to variables by largest remainder with savings-first tie", async () => {
const c1 = cid("c1");
const c2 = cid("c2"); // make this savings to test the tiebreaker
await prisma.variableCategory.createMany({
data: [
{ id: c1, userId: U, name: "Groceries", percent: 60, priority: 2, isSavings: false, balanceCents: 0n },
{ id: c2, userId: U, name: "Saver", percent: 40, priority: 1, isSavings: true, balanceCents: 0n },
],
});
const p1 = pid("rent");
await prisma.fixedPlan.create({
data: {
id: p1,
userId: U,
name: "Rent",
cycleStart: new Date().toISOString(),
dueOn: new Date(Date.now() + 7 * 864e5).toISOString(),
totalCents: 10000n,
fundedCents: 0n,
priority: 1,
fundingMode: "auto-on-deposit",
},
});
// $100 income
const result = await allocateIncome(prisma as any, U, 10000, new Date().toISOString(), "inc1");
expect(result).toBeDefined();
// rent should be funded first up to need
const fixed = result.fixedAllocations ?? [];
const variable = result.variableAllocations ?? [];
// sanity
expect(Array.isArray(fixed)).toBe(true);
expect(Array.isArray(variable)).toBe(true);
});
it("handles zeros and single bucket", async () => {
const cOnly = cid("only");
await prisma.variableCategory.create({
data: { id: cOnly, userId: U, name: "Only", percent: 100, priority: 1, isSavings: false, balanceCents: 0n },
});
const result = await allocateIncome(prisma as any, U, 0, new Date().toISOString(), "inc2");
expect(result).toBeDefined();
const variable = result.variableAllocations ?? [];
const sum = variable.reduce((s, a: any) => s + (a.amountCents ?? 0), 0);
expect(sum).toBe(0);
});
});

8
api/tests/appFactory.ts Normal file
View File

@@ -0,0 +1,8 @@
import type { FastifyInstance } from "fastify";
export default async function appFactory(): Promise<FastifyInstance> {
// env is already set in tests/setup.ts, so now we can import
const { default: app } = await import("../src/server"); // ESM + TLA safe
await app.ready(); // ensure all plugins registered
return app;
}

42
api/tests/helpers.ts Normal file
View File

@@ -0,0 +1,42 @@
// tests/helpers.ts
import { PrismaClient } from "@prisma/client";
export const prisma = new PrismaClient();
// Handy test user id
export const U = "demo-user-1";
// Monotonic id helpers so we never collide with existing rows
let cseq = 0;
let pseq = 0;
export const cid = (base = "c") => `${base}_${Date.now()}_${cseq++}`;
export const pid = (base = "p") => `${base}_${Date.now()}_${pseq++}`;
/**
* Hard-reset all data for a given user in dependency-safe order.
* Also deletes the user row so tests can re-create/upsert cleanly.
*/
export async function resetUser(userId: string) {
await prisma.$transaction([
prisma.allocation.deleteMany({ where: { userId } }),
prisma.transaction.deleteMany({ where: { userId } }),
prisma.incomeEvent.deleteMany({ where: { userId } }),
prisma.fixedPlan.deleteMany({ where: { userId } }),
prisma.variableCategory.deleteMany({ where: { userId } }),
]);
await prisma.user.deleteMany({ where: { id: userId } });
}
/** Ensure the user exists (id stable) */
export async function ensureUser(userId: string) {
await prisma.user.upsert({
where: { id: userId },
update: {},
create: { id: userId, email: `${userId}@demo.local` },
});
}
/** Close Prisma after all tests */
export async function closePrisma() {
await prisma.$disconnect();
}

View File

@@ -0,0 +1,71 @@
import { beforeAll, afterAll, describe, it, expect } from "vitest";
import request from "supertest";
import { PrismaClient } from "@prisma/client";
import type { FastifyInstance } from "fastify";
import { resetUser } from "./helpers";
// Ensure env BEFORE importing the server
process.env.NODE_ENV = process.env.NODE_ENV || "test";
process.env.PORT = process.env.PORT || "0";
process.env.DATABASE_URL =
process.env.DATABASE_URL || "postgres://app:app@localhost:5432/skymoney";
const prisma = new PrismaClient();
let app: FastifyInstance;
beforeAll(async () => {
// dynamic import AFTER env is set
const { default: srv } = await import("../src/server"); // <-- needs `export default app` in server.ts
app = srv;
await app.ready();
const U = "demo-user-1";
await resetUser(U);
await prisma.user.upsert({
where: { id: U },
update: {},
create: { id: U, email: `${U}@demo.local` },
});
await prisma.variableCategory.createMany({
data: [
{ id: "c1", userId: U, name: "Groceries", percent: 60, priority: 1, isSavings: false, balanceCents: 0n },
{ id: "c2", userId: U, name: "Saver", percent: 40, priority: 2, isSavings: true, balanceCents: 0n },
],
skipDuplicates: true,
});
await prisma.fixedPlan.create({
data: {
id: "p1",
userId: U,
name: "Rent",
cycleStart: new Date().toISOString(),
dueOn: new Date(Date.now() + 7 * 864e5).toISOString(),
totalCents: 10000n,
fundedCents: 0n,
priority: 1,
fundingMode: "auto-on-deposit",
},
});
});
afterAll(async () => {
await app.close();
await prisma.$disconnect();
});
describe("POST /income integration", () => {
it("allocates funds and returns audit", async () => {
const res = await request(app.server)
.post("/income")
.set("x-user-id", "demo-user-1")
.send({ amountCents: 5000 });
expect(res.status).toBe(200);
expect(res.body).toHaveProperty("fixedAllocations");
expect(res.body).toHaveProperty("variableAllocations");
expect(typeof res.body.remainingUnallocatedCents).toBe("number");
});
});

61
api/tests/income.test.ts Normal file
View File

@@ -0,0 +1,61 @@
// tests/income.test.ts
import request from "supertest";
import { describe, it, expect, beforeEach, afterAll, beforeAll } from "vitest";
import appFactory from "./appFactory";
import { prisma, resetUser, ensureUser, U, cid, pid, closePrisma } from "./helpers";
import type { FastifyInstance } from "fastify";
let app: FastifyInstance;
beforeAll(async () => {
app = await appFactory(); // <-- await the app
});
afterAll(async () => {
await app.close(); // <-- close server
await closePrisma();
});
describe("POST /income", () => {
beforeEach(async () => {
await resetUser(U);
await ensureUser(U);
await prisma.variableCategory.createMany({
data: [
{ id: cid("c1"), userId: U, name: "Groceries", percent: 60, priority: 2, isSavings: false, balanceCents: 0n },
{ id: cid("c2"), userId: U, name: "Saver", percent: 40, priority: 1, isSavings: true, balanceCents: 0n },
],
});
await prisma.fixedPlan.create({
data: {
id: pid("rent"),
userId: U,
name: "Rent",
cycleStart: new Date().toISOString(),
dueOn: new Date(Date.now() + 7 * 864e5).toISOString(),
totalCents: 10000n,
fundedCents: 0n,
priority: 1,
fundingMode: "auto-on-deposit",
},
});
});
afterAll(async () => {
await closePrisma();
});
it("allocates fixed first then variables; updates balances; returns audit", async () => {
const res = await request(app.server)
.post("/income")
.set("x-user-id", U)
.send({ amountCents: 15000 });
expect(res.statusCode).toBe(200);
expect(res.body).toHaveProperty("fixedAllocations");
expect(res.body).toHaveProperty("variableAllocations");
expect(typeof res.body.remainingUnallocatedCents).toBe("number");
});
});

32
api/tests/setup.ts Normal file
View File

@@ -0,0 +1,32 @@
import { execSync } from "node:child_process";
import { beforeAll, afterAll } from "vitest";
import { PrismaClient } from "@prisma/client";
process.env.NODE_ENV = process.env.NODE_ENV || "test";
process.env.DATABASE_URL =
process.env.DATABASE_URL ||
"postgres://app:app@localhost:5432/skymoney";
process.env.PORT = process.env.PORT || "0"; // fastify can bind an ephemeral port
process.env.HOST ??= "127.0.0.1";
process.env.CORS_ORIGIN = process.env.CORS_ORIGIN || "";
export const prisma = new PrismaClient();
// hard reset for a single user
export async function resetUser(userId: string) {
await prisma.allocation.deleteMany({ where: { userId } });
await prisma.transaction.deleteMany({ where: { userId } });
await prisma.incomeEvent.deleteMany({ where: { userId } });
await prisma.fixedPlan.deleteMany({ where: { userId } });
await prisma.variableCategory.deleteMany({ where: { userId } });
await prisma.user.deleteMany({ where: { id: userId } });
}
beforeAll(async () => {
// make sure the schema is applied before running tests
execSync("npx prisma migrate deploy", { stdio: "inherit" });
});
afterAll(async () => {
await prisma.$disconnect();
});

View File

@@ -0,0 +1,66 @@
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 type { FastifyInstance } from "fastify";
let app: FastifyInstance;
beforeAll(async () => {
app = await appFactory();
});
afterAll(async () => {
await app.close();
await closePrisma();
});
describe("GET /transactions", () => {
beforeEach(async () => {
await resetUser(U);
await ensureUser(U);
const c = cid("c");
await prisma.variableCategory.create({
data: { id: c, userId: U, name: "Groceries", percent: 100, priority: 1, isSavings: false, balanceCents: 5000n },
});
// seed some transactions of different kinds/dates
await prisma.transaction.createMany({
data: [
{
id: `t_${Date.now()}_1`,
userId: U,
occurredAt: new Date("2025-01-03T12:00:00.000Z"),
kind: "variable_spend",
categoryId: c,
amountCents: 1000n,
},
{
id: `t_${Date.now()}_2`,
userId: U,
occurredAt: new Date("2025-01-10T12:00:00.000Z"),
kind: "fixed_payment",
planId: null,
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")
.set("x-user-id", U);
expect(res.statusCode).toBe(200);
const body = res.body;
expect(Array.isArray(body.items)).toBe(true);
expect(body.items.length).toBe(1);
expect(body.items[0].kind).toBe("variable_spend");
});
});

View File

@@ -0,0 +1,56 @@
// tests/variable-categories.guard.test.ts
import request from "supertest";
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";
let app: FastifyInstance;
beforeAll(async () => {
app = await appFactory();
});
afterAll(async () => {
await app.close();
await closePrisma();
});
describe("Variable Categories guard (sum=100)", () => {
beforeEach(async () => {
await resetUser(U);
await ensureUser(U);
await prisma.variableCategory.createMany({
data: [
{ id: cid("a"), userId: U, name: "A", percent: 50, priority: 1, isSavings: false, balanceCents: 0n },
{ id: cid("b"), userId: U, name: "B", percent: 50, priority: 2, isSavings: false, balanceCents: 0n },
],
});
});
afterAll(async () => {
await closePrisma();
});
it("rejects create that would push sum away from 100", async () => {
const res = await request(app.server)
.post("/variable-categories")
.set("x-user-id", U)
.send({ name: "Oops", percent: 10, isSavings: false, priority: 99 });
expect(res.statusCode).toBe(400);
expect(res.body?.message).toMatch(/Percents must sum to 100/i);
});
it("rejects update that breaks the sum", async () => {
const existing = await prisma.variableCategory.findFirst({ where: { userId: U } });
const res = await request(app.server)
.patch(`/variable-categories/${existing!.id}`)
.set("x-user-id", U)
.send({ percent: 90 });
expect(res.statusCode).toBe(400);
expect(res.body?.message).toMatch(/Percents must sum to 100/i);
});
});