fiddy/apps/web/__tests__/recurring-entries.test.ts
2026-02-11 23:45:15 -08:00

78 lines
2.7 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import path from "node:path";
import { fileURLToPath } from "node:url";
import dotenv from "dotenv";
import getPool from "../lib/server/db";
import { createRecurringEntry, deleteRecurringEntry, listRecurringEntries } from "../lib/server/recurring-entries";
import { ensureTagsForGroup } from "../lib/server/tags";
import { cleanupTestData, uniqueInviteCode } from "./test-helpers";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const envLoaded = dotenv.config({ path: path.resolve(__dirname, "../../../.env") });
const hasDb = Boolean(process.env.DATABASE_URL);
test("recurring entries list", async t => {
if (!hasDb) {
t.skip("DATABASE_URL not set");
return;
}
if (envLoaded.error) t.diagnostic(String(envLoaded.error));
const pool = getPool();
const client = await pool.connect();
let userId: number | null = null;
let groupId: number | null = null;
try {
const userRes = await client.query(
"insert into users(email, password_hash) values($1,$2) returning id",
[`recurring_${Date.now()}@example.com`, "hash"]
);
userId = userRes.rows[0].id as number;
const groupRes = await client.query(
"insert into groups(name, invite_code, created_by) values($1,$2,$3) returning id",
["Recurring Test", uniqueInviteCode("R"), userId]
);
groupId = groupRes.rows[0].id as number;
await client.query(
"insert into group_members(group_id, user_id, role) values($1,$2,'GROUP_ADMIN')",
[groupId, userId]
);
await ensureTagsForGroup({ userId, groupId, tags: ["rent"] });
await createRecurringEntry({
groupId,
userId,
entryType: "SPENDING",
amountDollars: 900,
occurredAt: "2026-02-01",
necessity: "NECESSARY",
purchaseType: "Rent",
notes: "Monthly rent",
tags: ["rent"],
isRecurring: true,
frequency: "MONTHLY",
intervalCount: 1,
endCondition: "NEVER",
nextRunAt: "2026-02-01"
});
const list = await listRecurringEntries(groupId);
assert.equal(list.length, 1);
assert.equal(list[0].isRecurring, true);
} finally {
if (groupId) {
const list = await listRecurringEntries(groupId);
for (const entry of list) await deleteRecurringEntry({ id: entry.id, groupId });
}
await cleanupTestData(client, { userIds: [userId], groupId });
client.release();
}
});